2014-02-13 68 views
0

所以我正在嘗試編寫一個Java函數來收集進程的CPU時間,並將它們與以前的讀數進行比較,以確定自上次採樣以來進程要求的CPU時間。我發現如何從該站點獲取進程的CPU時間http://codeseekah.com/2012/10/21/android-shell-tricks-ps/什麼是「CPU時間」(使用Android的ps -x)?

基本上,您可以執行「ps -x」並添加您在最後看到的值;他們看起來像這樣(u:15,s:854)。問題是我似乎獲得了比預期更高的價值。我在這裏理解這個頁面的方式是http://en.wikipedia.org/wiki/CPU_time#Total_CPU_time,在給定的掛牆時間間隔內最大的CPU時間是(內核數量)*(掛牆時間間隔)。我的測試設備有4個內核,我每3秒抽樣一次。我從當前值中減去前一個值,並經常看到12秒以上的最終值。那可能嗎?我誤解了數據?我會在

以下發布一些代碼片段
if (currentNameAndTime.get(i).processName.equals(oldNameAndTime.get(j).processName)) { 
    // If they match, subtract the CPU times, and store the 
    // result in the time field 
    obj.time = (currentNameAndTime.get(i).time - oldNameAndTime.get(j).time); 
    // Add the object to the array that will be returned 
    finalNameAndTime.add(obj); 
    // Break the chain, as after a match is found, all other 
    // name comparisons will fail 
    break; 
} 
if (oldNameAndTime.size() == 0) { 
     FgBgCPUInfo obj = new FgBgCPUInfo(); 
     obj.processName = "FIRST RUN"; 
     obj.time = 0; 
     finalNameAndTime.add(obj); 
    } 
oldNameAndTime = new ArrayList<FgBgCPUInfo>(currentNameAndTime); 

謝謝!

回答

1

這些值是時鐘滴答中的CPU和系統時間。定義可以從桌面系統的​​文本中找到:

  utime %lu Amount of time that this process has been scheduled 
         in user mode, measured in clock ticks (divide by 
         sysconf(_SC_CLK_TCK). This includes guest time, 
         guest_time (time spent running a virtual CPU, see 
         below), so that applications that are not aware of 
         the guest time field do not lose that time from 
         their calculations. 

      stime %lu Amount of time that this process has been scheduled 
         in kernel mode, measured in clock ticks (divide by 
         sysconf(_SC_CLK_TCK). 

的值是每個線程的跟蹤,所以你應該使用ps -t -x看到每個進程中的所有線程。如果沒有-t,您只需查看主線程的統計信息,這可能就是爲什麼您的數字沒有加起來。

+0

這不是桌面系統嗎?我認爲這可能是毫秒,而不是時鐘滴答 – Kmanc

+1

桌面與手機並不重要。我嘗試了一個簡單的測試,用一個程序調用'System.nanoTime()'直到兩秒鐘過去,然後進入睡眠狀態。 'ps -x -t'中的結果是'dalvikvm(u:3,s:204)',這大概是你期望的每秒100個滴答聲。 – fadden

相關問題