Wie überprüfe ich, ob und wie oft mein Prozess vom Kernel unterbrochen wird?

Wie überprüfe ich, ob und wie oft mein Prozess vom Kernel unterbrochen wird?

For the purpose of profiling a program I would like to run it uninterrupted on one CPU. To do this I use a combination of taskset and chrt:

# taskset -c 1 chrt -f 99 ./my_program

Now my question is if there is a tool that lets me check if/how often the process is nevertheless interrupted by context switches to the kernel.

Antwort1

You can use perf; for example,

perf stat -e context-switches,cpl_cycles.ring0,cpl_cycles.ring123 your_command

will produce a summary similar to

 Performance counter stats for 'your_command':

                 1      context-switches                                            
        11,890,096      cpl_cycles.ring0                                            
         9,980,265      cpl_cycles.ring123                                          

       0.011218937 seconds time elapsed

       0.007533000 seconds user
       0.003766000 seconds sys

which shows that there was one context switch (to another process, not the kernel) during your_command’s execution, and the CPU spent 54% of its time running kernel code.

Ensuring that a given process gets as much of the CPU’s attention as possible can get quite complicated. Victor Stinner’s benchmark setup documentationbietet einen guten Überblick über die Probleme und Techniken zu ihrer Eindämmung. Sein Bericht konzentriert sich auf Benchmarking, aber vieles davon ist auch auf andere Situationen anwendbar.

verwandte Informationen