如何测量上下文切换时间
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
LINUX查看指定進(jìn)程的Context?Switch上下文切換:pidstat
Context Switch(CS)上下文切換是cpu性能中的一個(gè)重要指標(biāo),context switch過(guò)高會(huì)導(dǎo)致CPU像個(gè)搬運(yùn)工,頻繁在寄存器和運(yùn)行隊(duì)列之間奔波 ?,更多的時(shí)間花在了線程切換,而不是真正工作的線程上。
linux中一般使用vmstat監(jiān)控系統(tǒng)上下文切換,pidstat監(jiān)控指定進(jìn)程的上下文切換。
pidstat監(jiān)控指定進(jìn)程的Context Switch上下文切換:
首先安裝sysstat:yum install sysstat ?//使用的是fedora,ubuntu/debian使用apg-get
pidstat -w -G testp 1 10 ? ? ? //根據(jù)進(jìn)程名監(jiān)控上下文切換
pidstat -w -p 48863 1 10 ? ? //根據(jù)PID監(jiān)控上下文切換
平均時(shí)間: UID ? ?PID ? ? ? cswch/s ? ? nvcswch/s ? Command
平均時(shí)間: 0 ? ? ? ? 48770 ? ? 2.53 ? ? ? ? ?2.78 ? ? ? ? ? ? ? testp
cswch/s: 每秒任務(wù)主動(dòng)(自愿的)切換上下文的次數(shù),當(dāng)某一任務(wù)處于阻塞等待時(shí),將主動(dòng)讓出自己的CPU資源。
nvcswch/s: 每秒任務(wù)被動(dòng)(不自愿的)切換上下文的次數(shù),CPU分配給某一任務(wù)的時(shí)間片已經(jīng)用完,因此將強(qiáng)迫該進(jìn)程讓出CPU的執(zhí)行權(quán)。
多處理器時(shí)添加-I參數(shù),顯示各個(gè)cpu的使用率,如pidstat -w -I -G testp 1 10
什么是CPU上下文切換?
????????現(xiàn) 在linux是大多基于搶占式,CPU給每個(gè)任務(wù)一定的服務(wù)時(shí)間,當(dāng)時(shí)間片輪轉(zhuǎn)的時(shí)候,需要把當(dāng)前狀態(tài)保存下來(lái),同時(shí)加載下一個(gè)任務(wù),這個(gè)過(guò)程叫做上下文 切換。時(shí)間片輪轉(zhuǎn)的方式,使得多個(gè)任務(wù)利用一個(gè)CPU執(zhí)行成為可能,但是保存現(xiàn)場(chǎng)和加載現(xiàn)場(chǎng),也帶來(lái)了性能消耗。? 那線程上下文切換的次數(shù)和時(shí)間以及性能消耗如何看呢? ?
????????
????????
?
??????????如何獲得上下文切換的次數(shù)?
?????????vmstat直接運(yùn)行即可,在最后幾列,有CPU的context switch次數(shù)。 這個(gè)是系統(tǒng)層面的,加入想看特定進(jìn)程的情況,可以使用pidstat。 ? ??
| 1 2 3 4 5 6 7 | $ vmstat?1?100 procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------ ?r? b?? swpd?? free?? buff? cache?? si?? so??? bi??? bo?? in?? cs us sy id wa st ?0??0?????88?233484?288756?1784744????0????0?????0????23????0????0??4??1?94??0??0 ?4??0?????88?233236?288756?1784752????0????0?????0?????0?6202?7880??4??1?96??0??0 ?2??0?????88?233360?288756?1784800????0????0?????0???112?6277?7612??4??1?95??0??0 ?0??0?????88?232864?288756?1784804????0????0?????0???644?5747?6593??6??0?92??2??0 |
???????????執(zhí)行pidstat,將輸出系統(tǒng)啟動(dòng)后所有活動(dòng)進(jìn)程的cpu統(tǒng)計(jì)信息:? ?? ? ?? ??
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | linux:~ # pidstat Linux?2.6.32.12-0.7-default?(linux)?????????????06/18/12????????_x86_64_ ? 11:37:19??????????PID??? %usr %system? %guest??? %CPU?? CPU? Command …… 11:37:19????????11452????0.00????0.00????0.00????0.00?????2??bash 11:37:19????????11509????0.00????0.00????0.00????0.00?????3??dd 11:37:19:?pidstat獲取信息時(shí)間點(diǎn) PID: 進(jìn)程pid %usr: 進(jìn)程在用戶態(tài)運(yùn)行所占cpu時(shí)間比率 %system: 進(jìn)程在內(nèi)核態(tài)運(yùn)行所占cpu時(shí)間比率 %CPU: 進(jìn)程運(yùn)行所占cpu時(shí)間比率 CPU: 指示進(jìn)程在哪個(gè)核運(yùn)行 Command: 拉起進(jìn)程對(duì)應(yīng)的命令 備注:執(zhí)行pidstat默認(rèn)輸出信息為系統(tǒng)啟動(dòng)后到執(zhí)行時(shí)間點(diǎn)的統(tǒng)計(jì)信息,因而即使當(dāng)前某進(jìn)程的cpu占用率很高,輸出中的值有可能仍為0。 |
??????????
???????????上下文切換的性能消耗在哪里呢?
???? ??????context switch過(guò)高,會(huì)導(dǎo)致CPU像個(gè)搬運(yùn)工,頻繁在寄存器和運(yùn)行隊(duì)列直接奔波? ,更多的時(shí)間花在了線程切換,而不是真正工作的線程上。直接的消耗包括CPU寄存器需要保存和加載,系統(tǒng)調(diào)度器的代碼需要執(zhí)行。間接消耗在于多核 cache之間的共享數(shù)據(jù)。?
?????
??????????引起上下文切換的原因有哪些?
??????????對(duì)于搶占式操作系統(tǒng)而言,?大體有幾種:
??????????1、當(dāng)前任務(wù)的時(shí)間片用完之后,系統(tǒng)CPU正常調(diào)度下一個(gè)任務(wù);
??????????2、當(dāng)前任務(wù)碰到IO阻塞,調(diào)度線程將掛起此任務(wù),繼續(xù)下一個(gè)任務(wù);
??????????3、多個(gè)任務(wù)搶占鎖資源,當(dāng)前任務(wù)沒(méi)有搶到,被調(diào)度器掛起,繼續(xù)下一個(gè)任務(wù);
??????????4、用戶代碼掛起當(dāng)前任務(wù),讓出CPU時(shí)間;
??????????5、硬件中斷;
?????? ??
??????????如何測(cè)試上下文切換的時(shí)間消耗?
????? ? LMbench,知道這個(gè)工具,是在霸爺?shù)牟┛蜕厦?#xff08;http://blog.yufeng.info/archives/753), 然后就開(kāi)始在測(cè)試環(huán)境下搞了一把,一會(huì)就出結(jié)果了。然后就搞了臺(tái)線上機(jī)器安裝這個(gè)工具,然后測(cè)試,后面在測(cè)試Memory的時(shí)候,直接導(dǎo)致Load飆升, 還好沒(méi)人發(fā)現(xiàn),機(jī)器java進(jìn)程重啟就好了。這方面純粹是業(yè)務(wù)選手。霸爺說(shuō)分析的結(jié)果對(duì)于高性能C的開(kāi)發(fā)同學(xué)來(lái)說(shuō),是需要熟記的,沒(méi)辦法,咱是搞java 的,只能每個(gè)指標(biāo)逐個(gè)看一下了。
?????????
??????????LMbench的簡(jiǎn)單介紹?
?????????首先看英文介紹:LMbench -Tools for Performance Analysis,微觀性能分析工具。
?????????官方地址:http://www.bitmover.com/lmbench/
?????????下載地址:http://www.bitmover.com/lmbench/lmbench3.tar.gz?
????? ??
??????????LMbench?主要能干啥?
?????????主要是帶寬(讀取緩存文件、內(nèi)存拷貝、讀寫(xiě)內(nèi)存、管道等)和反應(yīng)時(shí)間(上下文切換、網(wǎng)路、進(jìn)程創(chuàng)建等)的評(píng)測(cè)工具。
? ? ???
???????????LMbench ?安裝?
| 1 2 3 4 | #wget http://www.bitmover.com/lmbench/lmbench3.tar.gz #tar -zxvf lmbench3.tar.gz #cd lmbench3 #make |
?????????中間遇到一個(gè)問(wèn)題,就是報(bào)錯(cuò),在CSDN上面找到了答案,這這里貼一下。
| 1 2 3 4 5 6 7 8 9 10 11 | 此時(shí)會(huì)報(bào)錯(cuò): ???make[2]: *** 沒(méi)有規(guī)則可以創(chuàng)建“bk.ver”需要的目標(biāo)“../SCCS/s.ChangeSet”。 停止。 ???make[2]:正在離開(kāi)目錄 `/home/hero/lmbench3/src' ???make[1]: *** [lmbench] 錯(cuò)誤 2 ???make[1]:正在離開(kāi)目錄 `/home/hero/lmbench3/src' ???make: *** [build] 錯(cuò)誤 2 解決辦法: lmbench3目錄下 ???#mkdir SCCS ???#touch? ./SCCS/s.ChangeSet ???#make |
? ? ? ?
???????????LMbench關(guān)于結(jié)果解釋(這次主要關(guān)注線程切換信息)
????????在網(wǎng)上找了半天,信息很少,只能看doc下面的英文解釋了。 ? ?
????????測(cè)試上下文切換的時(shí)間,一個(gè)上下文切換,包括保存一個(gè)進(jìn)程狀態(tài)的保存和恢復(fù)另外一個(gè)進(jìn)程的時(shí)間。
????????典型的上下文切換性能,僅僅是測(cè)量最小的線程切換時(shí)間。僅僅是做進(jìn)程切換,任何實(shí)質(zhì)的任務(wù)都不做。
| 1 2 3 4 5 6 | Context switching - times in microseconds -?smaller?is better ------------------------------------------------------------------------- Host???????????????? OS??2p/0K?2p/16K?2p/64K?8p/16K?8p/64K?16p/16K?16p/64K ?????????????????????????ctxsw? ctxsw? ctxsw ctxsw? ctxsw?? ctxsw?? ctxsw --------- ------------- ------ ------ ------ ------ ------ ------- ------- commonway Linux?2.6.18-?9.2400?4.0200?9.0300?7.5600?8.3800????11.6?6.28000 時(shí)間的單位是微秒。 |
?????
????????LMbench是如何來(lái)測(cè)量進(jìn)程切換的時(shí)間的???
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | The benchmark is a ring of two to twenty processes that are connected with Unix pipes.? A token is passed from process to process, forcing context switches.? The benchmark measures the time it takes to pass the token two thousand times from process to process.? Each hand off of the token has two costs: (a) the context switch, and (b) the cost of passing the token.? In order to get just the context switching time,the benchmark first measures the cost of passing the token through a ring of pipes in a single process.? This time is defined as the cost of passing the token and is not included in the reported context switch time. .PP ????When the processes are?larger?than the?default?baseline?of ``zero'' (where zero means just big enough to do the benchmark), the cost of the context switch includes the cost of restoring user?level state (cache lines).? This is accomplished by having the process allocate an array of data and sum it as a series of integers after receiving the token but before passing the token to the next process.? Note that the overhead mentioned?above?includes the cost of accessing the data but because it is measured in just one address space, the cost is typically the cost with hot caches.? So the context switch time does not include anything other than the context switch provided that?all?the processes fit in the cache.? If there are cache misses (as is common), the cost of the context switch includes the cost of those cache misses. .PP ????首先是看任務(wù)處理的時(shí)間(通過(guò)一次任務(wù)處理,這個(gè)任務(wù)處理的時(shí)間被定義為token時(shí)間,不包括線程切換的)。 ????然后多次執(zhí)行,排除任務(wù)執(zhí)行的時(shí)間,然后計(jì)算出CS的時(shí)間(如果有cache miss,則CS的時(shí)間也包括cache misses的時(shí)間)。 |
?
參考:http://www.cnblogs.com/EthanCai/p/3705834.html
轉(zhuǎn)載于:https://my.oschina.net/u/2822116/blog/794014
總結(jié)
以上是生活随笔為你收集整理的如何测量上下文切换时间的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 诊断SQLSERVER问题常用的日志
- 下一篇: lnmp搭建的常见错误