I have an application (a simple C code) running on top of Linux kernel. I am measuring time between two points of the code. I would like to know whether any other process or the scheduler itself has preempted my application between these two points. Is there a way to find this.
Asked
Active
Viewed 1,316 times
1
-
does this help? http://stackoverflow.com/a/21722393/678093 – m.s. Oct 02 '15 at 11:22
-
what is the reason why you need to know this? – Alex Hoppus Oct 02 '15 at 11:27
-
It's not clear whether you want a programmatic method or a command line. And it's not clear whether the application wants to introspect itself or whether you just need to get that data from outside the process. All of these make a difference. For example, /proc/
– kaylum Oct 02 '15 at 11:52 -
@AlexHoppus The requirement for me is to find the time of execution of a certain piece of code. It was observed that when running as baremetal (without OS), the code is much faster compared to when running on OS. So I am investigating the reason begin this. Before going to other reasons, I would first like to make sure that the this is not caused due to preemption. – Ginu Jacob Oct 02 '15 at 12:43
-
@AlanAu: I have the flexibility to edit the code so that I can find any preemption occurred in a critical part of the code – Ginu Jacob Oct 02 '15 at 12:45
-
1@GinuJacob have you heard about clock_gettime function or clock function? Any of this should solve your problem see CLOCK_PROCESS_CPUTIME_ID. Basically the /proc/pid/stime or utime holds time in system ticks, which your application only spent in kernel / userspace respectively. Checking the fact that preemption were done is impossible from userspace (and there is no need) – Alex Hoppus Oct 02 '15 at 12:56
-
1*"It was observed that when running as baremetal (without OS), the code is much faster compared to when running on OS."* -- That's probably true for almost all cases. Issue the `ps -A` command to see all the other processes competing for resources. Execute your application with the `time` command prefix. Try changing the priority using `nice`. Your question is a lot like asking if water is wet. – sawdust Oct 02 '15 at 19:55
-
1@GinuJacob Then do take a look at systemtap. It can allow you to observe the behaviour of any program in great detail. Including when taks switches occur. – kaylum Oct 02 '15 at 22:28
-
3Another idea: put your program into one of the realtime scheduling classes (see sched_setscheduler(2)). That will not completely eliminate interruptions (hardware interrupts and so forth will still trigger), but no other non-realtime processes will be able to preempt your process. And ordinarily there are *no* realtime processes so that will effectively put you on top of the heap. Also bear in mind that there are other reasons why your program may be suspended -- page faults, I/O, etc. – Gil Hamilton Oct 03 '15 at 00:51
1 Answers
1
One way to identify whether there was any context switch between two points in code is to use
getrusage(int who, struct rusage *usage)
call and compare values of ru_nvcsw & ru_nivcsw
struct rusage {
...
long ru_nvcsw; /* voluntary context switches */
long ru_nivcsw; /* involuntary context switches */
};

Nithin
- 191
- 5