Article Title: Who is switching our processes in Linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
When running a Linux server, we often need to know who is performing process switching and what causes process switching. Because process switching costs a lot, I will give a number from the LMbench test:
Context switching? Times in microseconds? Smaller is better
-------------------------
Host OS 2 p/0 K 2 p/16 K 2 p/64 K 8 p/16 K 8 p/64 K 16 p/16 K 16 p/64 K
Ctxsw
------------------------
My174.cm4 Linux 2.6.18-6.1100 7.0200 6.1100 8.7400 7.7200 8.96000
On my high-end server, the overhead of process switching is about 8 us, which is unacceptable to high-performance servers. Therefore, we need to do as much as possible in a time slice, instead of wasting time on unnecessary switching.
Let's find out who is switching our process:
View sourceprint? [Root @ my174 admin] # dstat 1
---- Total-cpu-usage -----dsk/total--net/total---- paging -- --- system --
Usr sys idl wai hiq siq | read writ | recv send | in out | int csw
0 0 100 0 0 | 0 0 | 796B 1488B | 0 0 | 1004 128
0 0 100 0 0 | 0 0 | 280B 728B | 0 0 | 1005 114
0 0 100 0 0 | 0 0 | 280B 728B | 0 0 | 1005 128
0 0 100 0 0 | 0 0 | 280B 728B | 0 0 | 1005 114
0 0 100 0 0 | 0 320k | 280B 728B | 0 0 | 1008 143
...
We can see that the number of CSWs is 120/S, but tools similar to dstat or vmstat do not tell us who is doing bad things. Okay! Let's do it ourselves.
Offering our lovely systemtap!
View sourceprint? [Root @ my174 admin] # cat> cswmon. stp
#! /Usr/bin/env stap
#
#
Global csw_count
Global idle_count
Probe scheduler. cpu_off {
Csw_count [task_prev, task_next] ++
Idle_count + = idle
}
Function fmt_task (task_prev, task_next)
{
Return sprintf ("% s (% d)-> % s (% d )",
Task_execname (task_prev ),
Task_pid (task_prev ),
Task_execname (task_next ),
Task_pid (task_next ))
}
Function print_cswtop (){
Printf ("% 45 s % 10s \ n", "Context switch", "COUNT ")
Foreach ([task_prev, task_next] in csw_count-limit 20 ){
Printf ("% 45 s % 10d \ n", fmt_task (task_prev, task_next), csw_count [task_prev, task_next])
}
Printf ("% 45 s % 10d \ n", "idle", idle_count)
Delete csw_count
Delete idle_count
}
Probe timer. s ($1 ){
Print_cswtop ()
Printf ("-------------------------------------------------------------- \ n ")
}
CTRL + D
This script will print the TOP 20 most switched processes and their pid at the specified time. Let's take a look at the result:
View sourceprint? [Root @ my174 admin] # stap cswmon. stp 5
Context switch COUNT
Swapper (0)-& gt; systemtap/11 (908) 500
Systemtap/11 (908)-> swapper (0) 498
Swapper (0)-> fct1-worker (2492) 50
Fct1-worker (2492)-> swapper (0) 50
Swapper (0)-> fct0-worker (2191) 50
Fct0-worker (2191)-> swapper (0) 50
Swapper (0)-& gt; bond0 (3432) 50
Bond0 (3432)-> swapper (0) 50
Stapio (879)-> swapper (0) 26
Swapper (0)-> stapi/O (879) 25
Stapio (879)-> swapper (0) 19
Swapper (0)-> stapi/O (879) 17
Swapper (0)-> watchdog/9 (31) 5
Watchdog/9 (31)-> swapper (0) 5
Swapper (0)-> mysqld (18346) 5
Mysqld (18346)-> swapper (0) 5
Swapper (0)-> watchdog/13 (43) 5
Watchdog/13 (43)-> swapper (0) 5
Swapper (0)-> watchdog/14 (46) 5
Watchdog/14 (46)-> swapper (0) 5
Idle 859
--------------------------------------------------------------
...
We can see where the process is switched and how many times it happened. In the last line, I printed the number of idle times. That is to say, when the system has nothing to do, It switches to idle (0) this process goes to rest.
Through the above investigation, we will clearly understand the overhead of our system, so that we can locate the problem easily.