谁在切换我们的进程

参考:http://blog.yufeng.info/archives/747

参考:https://www.cnblogs.com/emperor_zark/archive/2012/12/11/context_switch_1.html

在做Linux服务器的时候经常会需要知道谁在做进程切换,什么原因需要做进程切换。 因为进程切换的代价很高。

那么进程 线程 切换一次 多少时钟周期呢?

切换是发生在什么时候呢??

vmstat 1
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
36  0      0 113682376  19188 828872    0    0     1    10    1    0  1  3 96  0  0
39  0      0 113671784  19188 828996    0    0     0     8 1408386 239442 21 58 21  0  0
42  0      0 113675488  19188 829088    0    0     0   132 1396904 228653 21 59 20  0  0
36  0      0 113673592  19196 830044    0    0     0    48 1377641 240742 21 58 21  0  0
>pidstat -w -t 1
erage:        0         -     90123      0.96      0.00  |__wafd
Average:        0         -     90124   1916.83     76.10  |__Worker 0
Average:        0         -     90125   1548.76     75.53  |__Worker 1
Average:        0         -     90126   2011.85     61.57  |__Worker 2
Average:        0         -     90127   1811.66     68.83  |__Worker 3
Average:        0         -     90128   1873.61     69.60  |__Worker 4
Average:        0         -     90129   2007.46     71.32  |__Worker 5
Average:        0         -     90130   1945.12     65.20  |__Worker 6
Average:        0         -     90131   2047.80     63.86  |__Worker 7
sar -w 1  
proc/s  
     Total number of tasks created per second.  
cswch/s  
     Total number of context switches per second.  
11:19:20 AM    proc/s   cswch/s  
11:19:21 AM    110.28  23468.22  
11:19:22 AM    128.85  33910.58  
11:19:23 AM     47.52  40733.66  
11:19:24 AM     35.85  30972.64  
11:19:25 AM     47.62  24951.43  
11:19:26 AM     47.52  42950.50  

 

谁在切换我们的进程

 

#! /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 ("%45s %10s\n", "Context switch", "COUNT")
  foreach ([task_prev, task_next] in csw_count- limit 40) {
    printf("%45s %10d\n", fmt_task(task_prev, task_next), csw_count[task_prev, task_next])
  }
  printf("%45s %10d\n", "idle", idle_count)
 
  delete csw_count
  delete idle_count
}
 
probe timer.s(1) {
  print_cswtop ()
  printf("-----------------\n")
}

 

编译systemtap 然后使用编译好的stap 编译cswcom.stp

对于出现如下的错误:

./install/bin/staprun  ./csw.ko
ERROR: module version mismatch (#1 SMP Thu Nov 3 03:37:37 UTC 2022 vs #1 SMP Sat Sep 24 03:26:50 UTC 2022), release 4.4.131.kylin.x86

处理方式:修改include/generated/compile.h 文件中的时间戳即可

./install/bin/stap  -r /home/xxxx/workspace/v6.0.8.0/src/core/kernel/linux/build/linux-4.4.131/  -m csw  ./cswcom.stp
./install/bin/staprun  ./debugcsw.ko
                               Context switch      COUNT
              Worker 39(50724)->swapper/39(0)       2580
              Worker 43(50724)->swapper/43(0)       2424
              Worker 40(50724)->swapper/40(0)       2397
              Worker 30(50724)->swapper/30(0)       2373
              Worker 26(50724)->swapper/26(0)       2337
              Worker 16(50724)->swapper/16(0)       2335
              swapper/39(0)->Worker 39(50724)       2296
              Worker 29(50724)->swapper/29(0)       2255
              Worker 45(50724)->swapper/45(0)       2253
              Worker 35(50724)->swapper/35(0)       2249
              Worker 33(50724)->swapper/33(0)       2240
              Worker 44(50724)->swapper/44(0)       2234
              Worker 18(50724)->swapper/18(0)       2223
                Worker 4(50724)->swapper/4(0)       2222
              Worker 15(50724)->swapper/15(0)       2188
              swapper/43(0)->Worker 43(50724)       2186
                Worker 5(50724)->swapper/5(0)       2181
              swapper/40(0)->Worker 40(50724)       2157
              Worker 46(50724)->swapper/46(0)       2153
              Worker 14(50724)->swapper/14(0)       2143
              Worker 28(50724)->swapper/28(0)       2135
              Worker 34(50724)->swapper/34(0)       2122
              Worker 32(50724)->swapper/32(0)       2088
              swapper/30(0)->Worker 30(50724)       2084
              Worker 19(50724)->swapper/19(0)       2067
              Worker 38(50724)->swapper/38(0)       2065
              swapper/16(0)->Worker 16(50724)       2064
              swapper/26(0)->Worker 26(50724)       2061
              Worker 11(50724)->swapper/11(0)       2060
              Worker 10(50724)->swapper/10(0)       2053
              Worker 20(50724)->swapper/20(0)       2053
                Worker 0(50724)->swapper/0(0)       2048
                Worker 8(50724)->swapper/8(0)       2036
                Worker 9(50724)->swapper/9(0)       2022
                Worker 1(50724)->swapper/1(0)       2013
              swapper/44(0)->Worker 44(50724)       2011
              swapper/45(0)->Worker 45(50724)       2006
                Worker 7(50724)->swapper/7(0)       2003
              Worker 25(50724)->swapper/25(0)       1994
              swapper/33(0)->Worker 33(50724)       1990
              swapper/29(0)->Worker 29(50724)       1989
              Worker 27(50724)->swapper/27(0)       1980
              swapper/35(0)->Worker 35(50724)       1978
              Worker 17(50724)->swapper/17(0)       1974
              Worker 24(50724)->swapper/24(0)       1969
                swapper/4(0)->Worker 4(50724)       1954
                swapper/5(0)->Worker 5(50724)       1940
              swapper/18(0)->Worker 18(50724)       1935
                Worker 6(50724)->swapper/6(0)       1934
              swapper/15(0)->Worker 15(50724)       1928
                                         idle     100130

可以看到压力测试时:work进程和swapper 之间频繁切换  ,work nice值为-20

vmstat 1
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
35  0      0 114990928  23088 576288    0    0    29     4   12   10  3  6 90  1  0
43  0      0 114988320  23148 576916    0    0     0   300 1328680 220374 21 59 20  0  0
36  0      0 114981168  23148 577100    0    0     0     0 1368671 225874 21 59 20  0  0
36  0      0 114976912  23148 577268    0    0     0   224 1321808 232136 21 58 21  0  0
38  0      0 114979312  23148 577748    0    0     0    16 1327994 228364 21 58 21  0  0
^C

 

 

 

cpu 使用情况为:us 21 sy:59(sys+soft)  id=20

总的cs:22w。

为啥会出现work 和swapper 之间频繁切换!!

参考main purpose of swapper  https://superuser.com/questions/377572/what-is-the-main-purpose-of-the-swapper-process-in-unix

The reason is historical and programatic. The idle task is the task running, if no other task is runnable, like you said it. It has the lowest possible priority, so that's why it's running of no other task is runnable.
Programatic reason: This simplifies process scheduling a lot, because you don't have to care about the special case: "What happens if no task is runnable?", because there always is at least one task runnable, 
the idle task. Also you can count the amount of cpu time used per task. Without the idle task, which task gets the cpu-time accounted no one needs?
Historical reason: Before we had cpus which are able to step-down or go into power saving modes, it HAD to run on full speed at any time. It ran a series of NOP-instructions, if no tasks were runnable.
Today the scheduling of the idle task usually steps down the cpu by using HLT-instructions (halt), so power is saved. So there is a functionality somehow in the idle task in our days.

 

Process 0 is a special process (called swapper or idle process) which runs when the system is idle, i.e. no other process is scheduled. It is the only process which can invoke the idle() system call
So nowadays process #0 on a Unix is the system process, which effectively holds a number of kernel threads doing a number of things, ranging from page-out operations, 
through filesystem cache flushes and buffer zeroing, to idling when there's nothing else to run.

 

posted @ 2022-11-07 14:20  codestacklinuxer  阅读(23)  评论(0)    收藏  举报