kernel/trace/trace_stat.c

Source file repositories/reference/linux-study-clean/kernel/trace/trace_stat.c

File Facts

System
Linux kernel
Corpus path
kernel/trace/trace_stat.c
Extension
.c
Size
7587 bytes
Lines
359
Domain
Core OS
Bucket
Scheduler, Processes, Timers, Sync, And Syscalls
Inferred role
Core OS: operation-table or driver-model contract
Status
pattern implementation candidate

Why This File Exists

Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.

Dependency Surface

Detected Declarations

Annotated Snippet

static const struct file_operations tracing_stat_fops = {
	.open		= tracing_stat_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= tracing_stat_release
};

static int tracing_stat_init(void)
{
	int ret;

	ret = tracing_init_dentry();
	if (ret)
		return -ENODEV;

	stat_dir = tracefs_create_dir("trace_stat", NULL);
	if (!stat_dir) {
		pr_warn("Could not create tracefs 'trace_stat' entry\n");
		return -ENOMEM;
	}
	return 0;
}

static int init_stat_file(struct stat_session *session)
{
	int ret;

	if (!stat_dir && (ret = tracing_stat_init()))
		return ret;

	session->file = tracefs_create_file(session->ts->name, TRACE_MODE_WRITE,
					    stat_dir, session,
					    &tracing_stat_fops);
	if (!session->file)
		return -ENOMEM;
	return 0;
}

int register_stat_tracer(struct tracer_stat *trace)
{
	struct stat_session *session, *node;
	int ret;

	if (!trace)
		return -EINVAL;

	if (!trace->stat_start || !trace->stat_next || !trace->stat_show)
		return -EINVAL;

	guard(mutex)(&all_stat_sessions_mutex);

	/* Already registered? */
	list_for_each_entry(node, &all_stat_sessions, session_list) {
		if (node->ts == trace)
			return -EINVAL;
	}

	/* Init the session */
	session = kzalloc_obj(*session);
	if (!session)
		return -ENOMEM;

	session->ts = trace;
	INIT_LIST_HEAD(&session->session_list);
	mutex_init(&session->stat_mutex);

	ret = init_stat_file(session);
	if (ret) {
		destroy_session(session);
		return ret;
	}

	/* Register */
	list_add_tail(&session->session_list, &all_stat_sessions);

	return 0;
}

void unregister_stat_tracer(struct tracer_stat *trace)
{
	struct stat_session *node, *tmp;

	mutex_lock(&all_stat_sessions_mutex);
	list_for_each_entry_safe(node, tmp, &all_stat_sessions, session_list) {
		if (node->ts == trace) {
			list_del(&node->session_list);
			destroy_session(node);
			break;
		}
	}

Annotation

Implementation Notes