系統診斷小技巧(12):如何確定執行緒是否因CPU資源波動
引子
執行緒可能因為CPU資源不足或者因為–比如等待網路資料–而波動。這在監控上來看,就是業務波動了。但是確定這一點並不容易。
第一個難點是現場難抓。如果是CPU打滿或者負載很高,現場復現了,但是可能捕捉資料的執行緒沒有機會執行。如何解決這個問題我們在另一個小技巧中討論了,這裡略過。
第二個難點是使用什麼資料來確定執行緒因為CPU資源波動了。下面我們展開討論下。
vruntime
Linux 2.6.33引入了CFS排程器,task_strcut也因之加了sched_entity結構。sched_entity結構有一個欄位是我們感興趣的:vruntime。
struct sched_entity {
/* For load-balancing: */
struct load_weight load;
unsigned long runnable_weight;
struct rb_node run_node;
struct list_head group_node;
unsigned int on_rq;
u64 exec_start;
u64 sum_exec_runtime;
u64 vruntime; // 我們要使用的欄位
u64 prev_sum_exec_runtime;
u64 nr_migrations;
struct sched_statistics statistics;
#ifdef CONFIG_FAIR_GROUP_SCHED
int depth;
struct sched_entity *parent;
/* rq on which this entity is (to be) queued: */
struct cfs_rq *cfs_rq;
/* rq "owned" by this entity/group: */
struct cfs_rq *my_q;
#endif
#ifdef CONFIG_SMP
/*
* Per entity load average tracking.
*
* Put into separate cache line so it does not
* collide with read-mostly values above.
*/
struct sched_avg avg;
#endif
};
vruntime代表的是什麼呢?核心文件是這麼說的
In CFS the virtual runtime is expressed and tracked via the per-task
p->se.vruntime (nanosec-unit) value. This way, it`s possible to accurately
timestamp and measure the “expected CPU time” a task should have gotten.
[ small detail: on “ideal” hardware, at any time all tasks would have the same
p->se.vruntime value — i.e., tasks would execute simultaneously and no task
would ever get “out of balance” from the “ideal” share of CPU time. ]
CFS`s task picking logic is based on this p->se.vruntime value and it is thus
very simple: it always tries to run the task with the smallest p->se.vruntime
value (i.e., the task which executed least so far). CFS always tries to split
up CPU time between runnable tasks as close to “ideal multitasking hardware” as
possible.
Most of the rest of CFS`s design just falls out of this really simple concept,
with a few add-on embellishments like nice levels, multiprocessing and various
algorithm variants to recognize sleepers.
簡單說,vruntime代表了執行緒已經消耗的處理器時間。在理想的硬體上,執行緒應該有相同的vruntime。
這就是我們的依據。
簡單實驗
測試指令碼和壓力工具
我們直接讓測試指令碼列印 vruntime資訊。壓力工具則是使用perf工具。
測試指令碼如下
#!/bin/bash
export LANG=C
for ((i=0;i<10;i++));do
cat /proc/$$/sched
sleep 1
done
壓力工具用法如下
root@pusf:~ perf bench sched messaging -l 10000
綜合起來,我們的測試方法如下
./demo > log/1.log; perf bench sched messaging -l 10000 & sleep 1;./demo > log/2.log
結果分析
我們看下得到的結果
nerd@pusf:/tmp$ egrep vruntime log/{1.log,2.log}
log/1.log:se.vruntime : 22075.635863
log/1.log:se.vruntime : 22076.476482
log/1.log:se.vruntime : 22077.746821
log/1.log:se.vruntime : 22080.537902
log/1.log:se.vruntime : 22084.183713
log/1.log:se.vruntime : 22087.243075
log/1.log:se.vruntime : 22098.180655
log/1.log:se.vruntime : 22099.594014
log/1.log:se.vruntime : 22104.294012
log/1.log:se.vruntime : 22108.701587
log/2.log:se.vruntime : 82731.373434
log/2.log:se.vruntime : 83382.975477
log/2.log:se.vruntime : 78933.644191
log/2.log:se.vruntime : 88235.425663
log/2.log:se.vruntime : 93117.891657
log/2.log:se.vruntime : 101234.834622
log/2.log:se.vruntime : 95899.749367
log/2.log:se.vruntime : 115403.719751
log/2.log:se.vruntime : 124388.997744
log/2.log:se.vruntime : 126752.972070
nerd@pusf:/tmp$
可見,vruntime的區別是顯著的。
相關文章
- Java執行緒診斷Java執行緒
- Oracle系統執行慢及資源消耗多問題診斷 [final]Oracle
- 檢視cpu是否開啟超執行緒執行緒
- 公司某資料子系統定期cpu過高的診斷
- 檢視和診斷JVM執行緒資訊JVM執行緒
- 執行緒數,512是否合理?執行緒
- 我們該如何正確的中斷一個執行緒的執行??執行緒
- windows10系統診斷策略服務未執行如何解決Windows
- 在Linux下判斷系統當前是否開啟了超執行緒Linux執行緒
- Java如何停止執行緒,確定你知道的都是正確的麼?Java執行緒
- 如何診斷RAC系統中的
- 多執行緒-執行緒控制之中斷執行緒執行緒
- disruptor如何實現每CPU執行一個執行緒?執行緒
- Java執行緒:執行緒中斷Java執行緒
- Android 判斷當前執行緒是否是主執行緒的兩種方法Android執行緒
- 多執行緒死鎖除錯小技巧執行緒除錯
- win10 cpu速度上下波動大怎麼辦 win10系統cpu速度波動大如何處理Win10
- cpu、核與執行緒執行緒
- 詳解JAVA執行緒問題診斷工具Thread DumpJava執行緒thread
- 如何正確終止正在執行的子執行緒執行緒
- win10系統如何禁用診斷工具Win10
- Win10系統下網路診斷在哪_win10系統如何使用網路診斷Win10
- 如何正確的關閉 MFC 執行緒執行緒
- Linux檢視系統cpu個數、核心數、執行緒數Linux執行緒
- Java執行緒中斷與終止執行緒執行Java執行緒
- 如何判斷自己的cpu在vmware下是否支援64位的作業系統作業系統
- 在Linux中,如何診斷和解決系統啟動問題?Linux
- 根據CPU核數合理設定執行緒池大小執行緒
- 這樣執行執行緒是否妥當?執行緒
- dbms_addm執行oracle資料庫診斷Oracle資料庫
- 【Java面試】如何中斷一個正在執行的執行緒?Java面試執行緒
- 如何檢視CPU核數和執行緒數?CPU的核心數、執行緒數的關係和區別執行緒
- 聊聊保證執行緒安全的10個小技巧執行緒
- 如何確定Kafka的分割槽數、key和consumer執行緒數Kafka執行緒
- 看執行計劃是否正確
- 執行緒的中斷執行緒
- 中斷JAVA執行緒Java執行緒
- 結合作業系統執行緒 檢視mysql中的sql資源 消耗作業系統執行緒MySql