Process context switch

I need to make a system call to count the voluntary and involuntary process context switches. I already know the steps for adding a new system call to the linux kernel, but I do not know where I should start working with the context switch. Any idea?

Thanks.

+8
linux linux-kernel operating-system context-switch
source share
4 answers

If your syscall should only report statistics, you can use a context count code that is already in the kernel.

wait3 syscall or getrusage syscall already reports the context switch count in struct rusage :

 struct rusage { ... long ru_nvcsw; /* voluntary context switches */ long ru_nivcsw; /* involuntary context switches */ }; 

You can try by doing:

 $ /usr/bin/time -v /bin/ls -R .... Voluntary context switches: 1669 Involuntary context switches: 207 

where " /bin/ls -R " is any program.

As a result of searching for "struct rusage" in the kernel sources, you can find this accumulate_thread_rusage in kernel / sys.c, which updates the rusage structure. It is read from struct task_struct *t ; fields t->nvcsw; and t->nivcsw; :

 1477 static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r) 1478 { 1479 r->ru_nvcsw += t->nvcsw; // <<=== here 1480 r->ru_nivcsw += t->nivcsw; 1481 r->ru_minflt += t->min_flt; 1482 r->ru_majflt += t->maj_flt; 

Then you should look for nvcsw and nivcsw in the kernel folder to find out how they are updated by the kernel.

asmlinkage void __sched schedule (void) :

 4124 if (likely(prev != next)) { // <= if we are switching between different tasks 4125 sched_info_switch(prev, next); 4126 perf_event_task_sched_out(prev, next); 4127 4128 rq->nr_switches++; 4129 rq->curr = next; 4130 ++*switch_count; // <= increment nvcsw or nivcsw via pointer 4131 4132 context_switch(rq, prev, next); /* unlocks the rq */ 

The switch_count pointer is line 4091 or line 4111 of the same file.

PS: The link from perreal is great: http://oreilly.com/catalog/linuxkernel/chapter/ch10.html (search context_swtch )

+8
source share

This already exists: the virtual file /proc/NNNN/status (where NNNN is the identifier of the decimal process of the process that you want to learn about) contains, among other things, counting both voluntary and involuntary context switches. Unlike getrusage , this allows you to find out the number of context switches for any process, not just children. See the proc(5) manpage for more details .

+5
source share

The process will make a context switch in case of blocking, expiration of time or for interrupts, etc. The schedule () function is called. Since you want to calculate it for each process separately, you need to save a new variable for each process to count the absence of context switches. And you can update this variable every time in the schedule for the current process. Using a system call, you can read this value. Here is a snippet of the pintos schedule function,

 static void schedule (void) { struct thread *cur = running_thread (); struct thread *next = next_thread_to_run (); struct thread *prev = NULL; ASSERT (intr_get_level () == INTR_OFF); ASSERT (cur->status != THREAD_RUNNING); ASSERT (is_thread (next));<br/> if (cur != next) prev = switch_threads (cur, next); <== here you can update count of "cur" thread_schedule_tail (prev); } 
0
source share

Total number of context switches

cat /proc/PID/sched|grep nr_switches

Voluntary Context Switches

cat /proc/PID/sched | grep nr_voluntary_switches

Inappropriate context switches

cat /proc/PID/sched|grep nr_involuntary_switches

where PID is the process identifier of the process that you want to control.

However, if you want to get these statistics by correcting (by creating a hook) the linux source, the planning-related code is present in

kernel / SCHED /

folders of the source tree. In particular,

kernel / sched / core.c contains the sched () function, which is the linux scheduler code. The CFS (fully fair scheduler) code, which is one of several schedulers present on Linux, is most commonly used in

/kernel/sched/fair.c

scheduler () is executed when the TIF_NEED_RESCHED flag is set, so find out from which all places set this flag (use cscope in the linux source), which will give you an idea of ​​the types of context switches that occur for the process.

0
source share

All Articles