1. <nobr id="easjo"><address id="easjo"></address></nobr>

      <track id="easjo"><source id="easjo"></source></track>
      1. 
        

      2. <bdo id="easjo"><optgroup id="easjo"></optgroup></bdo>
      3. <track id="easjo"><source id="easjo"><em id="easjo"></em></source></track><option id="easjo"><span id="easjo"><em id="easjo"></em></span></option>
          貴州做網站公司
          貴州做網站公司~專業!靠譜!
          10年網站模板開發經驗,熟悉國內外開源網站程序,包括DEDECMS,WordPress,ZBlog,Discuz! 等網站程序,可為您提供網站建設,網站克隆,仿站,網頁設計,網站制作,網站推廣優化等服務。我們專注高端營銷型網站,企業官網,集團官網,自適應網站,手機網站,網絡營銷,網站優化,網站服務器環境搭建以及托管運維等。為客戶提供一站式網站解決方案?。?!

          linux cpu使用率(Linux中計算特定CPU使用率的方法)

          來源:互聯網轉載 時間:2024-05-03 10:59:01

          Linux中計算特定CPU使用率的方法:首先從【/proc/stat】中獲取 t1時刻系統總體的值;然后從【/proc/stat】中獲取t2時刻系統總的值;最后計算t2與t1之間系統總的CPU使用情況。

          Linux中計算特定CPU使用率的方法:

          1. 背景知識

          在/proc/stat中可以查看每一個CPU的使用情況的,如下圖:

          其中cpu(0/1/2/…)后面的那十個數字含義如下:

          /proc/statkernel/system statistics.  Varies with architecture.  Common entries include:     user nice system idle iowait  irq  softirq steal guest guest_nicecpu  4705 356  584    3699   23    23     0       0     0        0cpu0 1393280 32966 572056 13343292 6130 0 17875 0 23933 0   The amount of time, measured in units of USER_HZ   (1/100ths of a second on most architectures, use   sysconf(_SC_CLK_TCK) to obtain the right value), that   the system ("cpu" line) or the specific CPU ("cpuN"   line) spent in various states:   user   (1) Time spent in user mode.   nice   (2) Time spent in user mode with low priority          (nice).   system (3) Time spent in system mode.   idle   (4) Time spent in the idle task.  This value          should be USER_HZ times the second entry in the          /proc/uptime pseudo-file.   iowait (since Linux 2.5.41)          (5) Time waiting for I/O to complete.  This          value is not reliable, for the following rea‐          sons:          1. The CPU will not wait for I/O to complete;             iowait is the time that a task is waiting for             I/O to complete.  When a CPU goes into idle             state for outstanding task I/O, another task             will be scheduled on this CPU.          2. On a multi-core CPU, the task waiting for I/O             to complete is not running on any CPU, so the             iowait of each CPU is difficult to calculate.          3. The value in this field may decrease in cer‐             tain conditions.   irq (since Linux 2.6.0-test4)          (6) Time servicing interrupts.   softirq (since Linux 2.6.0-test4)          (7) Time servicing softirqs.   steal (since Linux 2.6.11)          (8) Stolen time, which is the time spent in          other operating systems when running in a virtu‐          alized environment   guest (since Linux 2.6.24)          (9) Time spent running a virtual CPU for guest          operating systems under the control of the Linux          kernel.   guest_nice (since Linux 2.6.33)          (10) Time spent running a niced guest (virtual          CPU for guest operating systems under the con‐          trol of the Linux kernel).

          2.計算具體CPU使用率

          有了上面的背景知識,接下來我們就可以計算具體CPU的使用情況了。具體計算方式如下:

          Total CPU time since boot = user+nice+system+idle+iowait+irq+softirq+stealTotal CPU Idle time since boot = idle + iowaitTotal CPU usage time since boot = Total CPU time since boot - Total CPU Idle time since bootTotal CPU percentage = Total CPU usage time since boot/Total CPU time since boot * 100%

          有了上面的計算公式,計算某一CPU使用率或者系統總的CPU占用率也就是不難了。

          示例:計算系統整體CPU占用情況

          首先從/proc/stat中獲取 t1時刻系統總體的user、nice、system、idle、iowait、irq、softirq、steal、guest、guest_nice的值,得到此時Total CPU time since boot(記為total1)和 Total CPU idle time since boot(記為idle1)。

          其次,從/proc/stat中獲取t2時刻系統總的Total CPU time since boot(記為total2)和Total CPU idle time since boot(記為idle2)。(方法同上一步)

          最后,計算t2與t1之間系統總的CPU使用情況。也就是:

          CPU percentage between t1 and t2 = ((total2-total1)-(idle2-idle1))/(total2-total1)* 100%

          其中, ((total2-total1)-(idle2-idle1))實際上就是t1與t2時刻之間系統CPU被占用的時間(總時間 - 空閑時間)。

          下面是一段計算時間段內CPU被占用情況的腳本:

          #!/bin/bash# by Paul Colby (http://colby.id.au), no rights reserved ;)PREV_TOTAL=0PREV_IDLE=0while true; do  # Get the total CPU statistics, discarding the 'cpu ' prefix.  CPU=(`sed -n 's/^cpu\s//p' /proc/stat`)  IDLE=${CPU[3]} # Just the idle CPU time.  # Calculate the total CPU time.  TOTAL=0  for VALUE in "${CPU[@]}"; do    let "TOTAL=$TOTAL+$VALUE"  done  # Calculate the CPU usage since we last checked.  let "DIFF_IDLE=$IDLE-$PREV_IDLE"  let "DIFF_TOTAL=$TOTAL-$PREV_TOTAL"  let "DIFF_USAGE=(1000*($DIFF_TOTAL-$DIFF_IDLE)/$DIFF_TOTAL+5)/10"  echo -en "\rCPU: $DIFF_USAGE%  \b\b"  # Remember the total and idle CPU times for the next check.  PREV_TOTAL="$TOTAL"  PREV_IDLE="$IDLE"  # Wait before checking again.  sleep 1done

          感謝你能夠認真閱讀完這篇文章,希望小編分享Linux中計算特定CPU使用率的方法內容對大家有幫助,同時也希望大家多多支持本站,關注本站行業資訊頻道,遇到問題就找本站,詳細的解決方法等著你來學習!

          標簽:linux cpu使用率-

          c語言中正確的字符常量是用一對單引號將一個字符括起表示合法的字符常量。例如‘a’。數值包括整型、浮點型。整型可用十進制,八進制,十六進制。八進制前面要加0,后面...

          2022年天津專場考試原定于3月19日舉行,受疫情影響確定延期,但目前延期后的考試時間推遲。 符合報名條件的考生,須在規定時間登錄招考資訊網(www.zha...

          :喜歡聽,樂意看。指很受歡迎?!巴卣官Y料”喜聞樂見:[ xǐ wén lè jiàn ]詳細解釋1. 【解釋】:喜歡聽,樂意看。指很受歡迎。2. 【示例】:這是...

          機構信用代碼證就是指承載機構信用代碼的證書。機構信用代碼證由中國人民銀行統一式樣和內容,記載了機構的信用代碼、機構名稱、注冊地址及發證單位等信息。機構在人民銀行和銀行業金融機構辦理業務時,出示機構信用代碼證,可以得到更加方便、快捷的金融服務。各個機構的信用代碼證,在管理和經營的過程中都是非常重要的,不僅是證明自己的重要標識,同時也是,在辦理很多業務的時候所用到的一個經濟識別手段,所以,應該牢牢的記...

          (相關資料圖)哈嘍小伙伴們 ,今天給大家科普一個小知識。在日常生活中我們或多或少的都會接觸到臺式機硬盤如何接筆記本電腦方面的一些說法,有的小伙伴還不是很了解,今天就給大家詳細的介紹一下關于臺式機硬盤如何接筆記本電腦的相關內容。1、筆記本電腦一般硬盤的位置在筆記本的背面,用螺絲刀把硬盤保護蓋打開,這時筆記本硬盤后端有顆螺絲,用螺絲刀打開,取出筆記本硬盤。2、將插在臺式機上硬盤的數據線和電源取下。3、...

          微信錢包銀行儲蓄是什么?微信錢包銀行儲蓄是微信和工商銀行共同,儲蓄理財產品,存入資金相當于存入工商銀行,只和工行一家進行合作,選擇空間較小,但未來可能會有變化。產品的最長期限為3年,存款利率可達3.85%,100元起存,單筆不超過5萬。用戶可在微信開通銀行存款賬戶,銀行派發利息。從安全角度出發,工商銀行作為我國四大行之一,抗風險能力較高,加上該產品是存款類,理財風險是很低的。如何把手機零錢轉入綁定...

          TOP
          国产初高中生视频在线观看|亚洲一区中文|久久亚洲欧美国产精品|黄色网站入口免费进人
          1. <nobr id="easjo"><address id="easjo"></address></nobr>

              <track id="easjo"><source id="easjo"></source></track>
              1. 
                

              2. <bdo id="easjo"><optgroup id="easjo"></optgroup></bdo>
              3. <track id="easjo"><source id="easjo"><em id="easjo"></em></source></track><option id="easjo"><span id="easjo"><em id="easjo"></em></span></option>