###1、前言html
一、在perf監控進程的系統調用時,會出現大量swapper進程
二、官方描述該進程是當CPU上沒有其餘任務運行時,就會執行swapper。換句話說swapper意味着CPU啥事也沒幹,跑去休息去了
三、本文來觀察一下swapper在cpu上的表現ubuntu
<br>api
###2、環境準備app
組件 | 版本 |
---|---|
OS | Ubuntu 16.04.4 LTS |
systemtap | version 4.2/0.165, commit release-4.1-41-g9cde541d4464 |
<br>工具
###3、準備腳本code
祭出咱們強有力的工具systemtap,這裏須要注意的是,systemtap各版本之間有必定的差別,個人版本是在這裏下載的:https://sourceware.org/systemtap/getinvolved.htmlhtm
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.htmlblog
腳本以下:進程
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並無運行任務get
###4、運行腳本
因爲數據量太大,咱們篩選一部分:
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 ...
一、因爲是4核的cpu,因此有4個swapper,swapper/n
二、swapper的進程號是0,在系統初始化時建立init進程,以後它就成了一個最低優先級的空閒任務
三、當swapper出如今左邊的時候(即將離開cpu的進程),對應最後一個字段idle是1,這時候證實cpu上運行的swapper進程(CPU去閒散去了)
四、由此驗證了,當cpu運行swapper進程的時候,實際上cpu是處於閒散的狀態,並無任何真正的任務在上面運行,處於idle狀態
<br>
至此,本文結束
在下才疏學淺,有撒湯漏水的,請各位不吝賜教...
原文出處:https://www.cnblogs.com/MrVolleyball/p/11606248.html