Add a feature to the linux kernel, which records the total times a process is scheduled to be executed on CPU.
Basic Idea
Add a counter varible to the struct task_struct, init the counter varible to 0 when a process is created/forked. Every time the process is scheduled to be executed, increase the counter and write the number to a proc file.
Details of how to modify the kernel
0. Environment
VMware Fusion 7
Ubuntu 12.04.5
Kernel Version 3.18.8
1. /include/linux/sched.h
line#1234
1
2
3
4
struct task_struct{
+ unsignedlong ctx;
...
}
2. /kernel/fork.c
line#1622
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
long do_fork(unsignedlong clone_flags,
unsignedlong stack_start,
unsignedlong stack_size,
int __user *parent_tidptr,
int __user *child_tidptr){
...
p = copy_process(clone_flags, stack_start, stack_size,