Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

23
LINES

< > BotCompany Repo | #1006501 // Linux: Try to get CPU info from /proc/stat [now moved to linux_cpuLoad]

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (1123L/8K/26K).

1  
!7
2  
3  
p-typewriter {
4  
  long PREV_TOTAL = 0, PREV_IDLE = 0;
5  
  
6  
  repeat with sleep 1 {
7  
    S line = first(toLines(loadTextFile("/proc/stat")));
8  
    print(line);
9  
    S[] CPU = line.split("\\s+");
10  
  
11  
    long IDLE = Long.parseLong(CPU[4]); //Get the idle CPU time.
12  
    long TOTAL = Long.parseLong(CPU[1]) + Long.parseLong(CPU[2]) + Long.parseLong(CPU[3]) + Long.parseLong(CPU[4]);
13  
  
14  
    long DIFF_IDLE = IDLE - PREV_IDLE;
15  
    long DIFF_TOTAL = TOTAL - PREV_TOTAL;
16  
    long DIFF_USAGE = DIFF_TOTAL == 0 ? 0 : (1000 * (DIFF_TOTAL - DIFF_IDLE) / DIFF_TOTAL + 5) / 10;
17  
    print("CPU: " + DIFF_USAGE + "%");
18  
  
19  
    PREV_TOTAL = TOTAL;
20  
    PREV_IDLE = IDLE;
21  
    //return (float) DIFF_USAGE;
22  
  }
23  
}

Author comment

Thanks to http://stackoverflow.com/questions/28787946/get-cpu-load-per-core-using-java

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1006501
Snippet name: Linux: Try to get CPU info from /proc/stat [now moved to linux_cpuLoad]
Eternal ID of this version: #1006501/1
Text MD5: da3d6e1d60ce5a5822c0450061f89343
Transpilation MD5: 2746d0fa5888c1759bec326b7c9f7c9c
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-01-11 15:23:00
Source code size: 709 bytes / 23 lines
Pitched / IR pitched: No / No
Views / Downloads: 490 / 529
Referenced in: [show references]