I have to do a system call to count the voluntary & involuntary context switches of a process. I already know the steps to add a new system call to a linux kernel but i have no clue of where i should start for the context-switch function. Any idea?
Share
If your syscall should only report statistics, you can use context switch counting code that is already in the kernel.
wait3 syscall or getrusage syscall already reports context switch count in
struct rusagefields:You can try it by running:
where “
/bin/ls -R” is any program.By searching an “struct rusage” in kernel sources, you can find this
accumulate_thread_rusagein kernel/sys.c, which updates rusage struct. It reads fromstruct task_struct *t; the fieldst->nvcsw;andt->nivcsw;:Then you should search
nvcswandnivcswin kernel folder to find how they are updated by kernel.asmlinkage void __sched schedule(void):
Pointer
switch_countis from line 4091 or line 4111 of the same file.PS: Link from perreal is great: http://oreilly.com/catalog/linuxkernel/chapter/ch10.html (search
context_swtch)