深入理解perf报告中的swapper进程

一、前言

1、在perf监控进程的系统调用时,会出现大量swapper进程
2、官方描述该进程是当CPU上没有其他任务运行时,就会执行swapper。换句话说swapper意味着CPU啥事也没干,跑去休息去了
3、本文来观察一下swapper在cpu上的表现


二、环境准备

组件 版本
OS Ubuntu 16.04.4 LTS
systemtap version 4.2/0.165, commit release-4.1-41-g9cde541d4464

三、准备脚本

祭出我们强有力的工具systemtap,这里需要注意的是,systemtap各版本之间有一定的差异,我的版本是在这里下载的:https://sourceware.org/systemtap/getinvolved.html

root@wilson-ubuntu:/opt/stap# stap -V
Systemtap translator/driver (version 4.2/0.165, commit release-4.1-41-g9cde541d4464)
Copyright (C) 2005-2019 Red Hat, Inc. and others
This is free software; see the source for copying conditions.
tested kernel versions: 2.6.18 ... 5.1-rc2
enabled features: PYTHON3 NLS

确定好版本之后,编写一个脚本,主要用到probe::scheduler.cpu_off,https://sourceware.org/systemtap/tapsets/API-scheduler-cpu-off.html

脚本如下:

probe scheduler.cpu_off
{
        printf("%20s (%5d) %5s %20s (%5d)  , is idle:%d \n ", task_execname(task_prev),task_pid(task_prev),"==>",task_execname(task_next),task_pid(task_next),idle)
}

脚本非常简单,scheduler.cpu_off主要描述了进程离开CPU的状态:
task_prev:即将离开CPU的进程
task_next:即将进入CPU的进程
idle:cpu是否处于空闲,这个变量就是我们关注的重点,如果idle为1,那就证明CPU并没有运行任务

四、运行脚本

由于数据量太大,我们筛选一部分:

root@wilson-ubuntu:/opt/stap# stap switch.stp
...
            swapper/0 (    0)   ==>               stapio (29159)  , is idle:1
               stapio (29159)   ==>            swapper/0 (    0)  , is idle:0
            swapper/0 (    0)   ==>            rcu_sched (    7)  , is idle:1
            rcu_sched (    7)   ==>            swapper/0 (    0)  , is idle:0
            swapper/2 (    0)   ==>       irq/31-iwlwifi (  542)  , is idle:1
       irq/31-iwlwifi (  542)   ==>            swapper/2 (    0)  , is idle:0
            swapper/2 (    0)   ==>       irq/31-iwlwifi (  542)  , is idle:1
       irq/31-iwlwifi (  542)   ==>            swapper/2 (    0)  , is idle:0
            swapper/2 (    0)   ==>       irq/31-iwlwifi (  542)  , is idle:1
       irq/31-iwlwifi (  542)   ==>            swapper/2 (    0)  , is idle:0
            swapper/2 (    0)   ==>       irq/31-iwlwifi (  542)  , is idle:1
       irq/31-iwlwifi (  542)   ==>            swapper/2 (    0)  , is idle:0
            swapper/0 (    0)   ==>            rcu_sched (    7)  , is idle:1
            rcu_sched (    7)   ==>            swapper/0 (    0)  , is idle:0
            swapper/2 (    0)   ==>       irq/31-iwlwifi (  542)  , is idle:1
       irq/31-iwlwifi (  542)   ==>            swapper/2 (    0)  , is idle:0
            swapper/2 (    0)   ==>       irq/31-iwlwifi (  542)  , is idle:1
       irq/31-iwlwifi (  542)   ==>            swapper/2 (    0)  , is idle:0
            swapper/0 (    0)   ==>            rcu_sched (    7)  , is idle:1
            swapper/1 (    0)   ==>               stapio (29159)  , is idle:1
...

1、由于是4核的cpu,所以有4个swapper,swapper/n
2、swapper的进程号是0,在系统初始化时创建init进程,之后它就成了一个最低优先级的空闲任务
3、当swapper出现在左边的时候(即将离开cpu的进程),对应最后一个字段idle是1,这时候证明cpu上运行的swapper进程(CPU去闲散去了)
4、由此验证了,当cpu运行swapper进程的时候,实际上cpu是处于闲散的状态,并没有任何真正的任务在上面运行,处于idle状态



至此,本文结束
在下才疏学浅,有撒汤漏水的,请各位不吝赐教...

posted @ 2019-09-29 10:18  wilson排球  阅读(2977)  评论(0编辑  收藏  举报