Skip to content

Instantly share code, notes, and snippets.

@BrotherJing
Last active January 30, 2021 11:35
Show Gist options
  • Save BrotherJing/5508e82765125134b6e1a43e0359a529 to your computer and use it in GitHub Desktop.
Save BrotherJing/5508e82765125134b6e1a43e0359a529 to your computer and use it in GitHub Desktop.
add a param to task_struct to record process schedule
// include/linux/sched.h
struct task_struct {
int ctx;
volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
void *stack;
atomic_t usage;
unsigned int flags; /* per process flags, defined below */
unsigned int ptrace;
//...
}
// include/linux/init_task.h
/*
* INIT_TASK is used to set up the first task table, touch at
* your own risk!. Base=0, limit=0x1fffff (=2MB)
*/
#define INIT_TASK(tsk) \
{ \
.ctx = 0, \
.state = 0, \
.stack = &init_thread_info,
//...
}
// kernel/sched/core.c
static void __sched notrace __schedule(bool preempt)
{
struct task_struct *prev, *next;
unsigned long *switch_count;
struct rq *rq;
int cpu;
cpu = smp_processor_id();
rq = cpu_rq(cpu);
rcu_note_context_switch();
prev = rq->curr;
prev->ctx++;
//...
}
// fs/proc/base.c
static const struct pid_entry tgid_base_stuff[] = {
DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
//...
ONE("ctx", S_IRUSR, proc_pid_my_ctx),
//...
}
static int proc_pid_my_ctx(struct seq_file *m, struct pid_namespace *ns,
struct pid *pid, struct task_struct *task)
{
seq_printf(m,"%d\n",task->ctx);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment