深入理解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狀態



至此,本文結束
在下才疏學淺,有撒湯漏水的,請各位不吝賜教...


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM