Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most efficient way to monitor the number of context switches in linux kernel?

I would like to have a total statistics of the following:

  1. How many softirq occurred ?
  2. How many interrupts occurred ?
  3. How many context switches occurred ?

I know you can use pidstat, cat /proc/interrupts and /cat/proc/softirqs. But using them is too much overhead.

  • How can I get the bottom line values for {1-3} without working with /proc and in the fastest way?

  • can I use ftrace to help me tracking about the events?

I am going to use a high resolution timers for monitoring the system:

  • LWN article
  • IBM developer
  • hrtimer
like image 947
0x90 Avatar asked Jan 26 '13 16:01

0x90


People also ask

How do I monitor context switching in Linux?

Now how to check the total number of context switches on your system. This can be done by running vmstat command with 1 second interval as shown below. The cs column shown in the vmstat output shows you the total number of context switches per second.

How do you find the number of context switches?

If you want to get the number of context switches from within a process, you can look into the GetRUsage syscall.

How can we reduce the time taken for context switching?

The context switch time is dependent on the registers you have to save / restore. One way you can possibly save time is via the AVX extensions on new processors, which allow you to save/restore all of the registers to one block of memory.


2 Answers

Use perf, for example:

# perf stat -B dd if=/dev/zero of=/dev/null count=1000000

1000000+0 records in
1000000+0 records out
512000000 bytes (512 MB) copied, 0.956217 s, 535 MB/s

 Performance counter stats for 'dd if=/dev/zero of=/dev/null count=1000000':

            5,099 cache-misses             #      0.005 M/sec (scaled from 66.58%)
          235,384 cache-references         #      0.246 M/sec (scaled from 66.56%)
        9,281,660 branch-misses            #      3.858 %     (scaled from 33.50%)
      240,609,766 branches                 #    251.559 M/sec (scaled from 33.66%)
    1,403,561,257 instructions             #      0.679 IPC   (scaled from 50.23%)
    2,066,201,729 cycles                   #   2160.227 M/sec (scaled from 66.67%)
              217 page-faults              #      0.000 M/sec
                3 CPU-migrations           #      0.000 M/sec
               83 context-switches         #      0.000 M/sec
       956.474238 task-clock-msecs         #      0.999 CPUs

       0.957617512  seconds time elapsed
like image 81
Cong Wang Avatar answered Sep 23 '22 06:09

Cong Wang


Check Linux's perf subsystem it is the way you need to gain performance counters soft or hard from a Linux system.

like image 25
auselen Avatar answered Sep 19 '22 06:09

auselen