1 | scope oshi_calcProcessCPUUsage. |
2 | |
3 | sbool #debug; |
4 | |
5 | sclass #Data { |
6 | S characteristics; // for identifying the process |
7 | long cpuTime, timestamp; |
8 | |
9 | toString { ret "cpuTime=" + cpuTime + ", timestamp=" + timestamp + ", characteristics=" + characteristics; } |
10 | } |
11 | |
12 | // key: pid; TODO: delete dead processes from map |
13 | static Map<Int, Data> #map = synchroMap(); |
14 | |
15 | // call this in proper interval (1 to 3 seconds) to work properly |
16 | static double oshi_calcProcessCPUUsage(OSProcess p) { |
17 | if (p == null) ret 0; |
18 | int pid = p.getProcessID(); |
19 | Data data = map.get(pid); |
20 | S characteristics = oshi_immutableProcessCharacteristics(p); |
21 | if (data == null || neq(data.characteristics, characteristics)) { |
22 | if (debug && data != null) print("Dropping data for " + pid + " (" + data.characteristics + " vs " + characteristics + ")"); |
23 | map.put(pid, data = new Data); |
24 | } |
25 | if (data.characteristics == null) data.characteristics = characteristics; |
26 | |
27 | long cpuTime = p.getKernelTime() + p.getUserTime(); |
28 | |
29 | double result = 0; |
30 | long now = sysNow(); |
31 | if (debug) |
32 | print("oshi cpu: " + pid + " / " + cpuTime + " / " + data); |
33 | if (data.timestamp != 0) |
34 | result = percentRatio(cpuTime-data.cpuTime, now-data.timestamp); |
35 | data.timestamp = now; |
36 | data.cpuTime = cpuTime; |
37 | ret result; |
38 | } |
39 | |
40 | end scope |
download show line numbers debug dex old transpilations
Travelled to 12 computer(s): bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1019497 |
Snippet name: | oshi_calcProcessCPUUsage - use v2 instead |
Eternal ID of this version: | #1019497/14 |
Text MD5: | 67e142d2806a486a47c8d78917127399 |
Author: | stefan |
Category: | javax / os |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2018-11-10 22:56:32 |
Source code size: | 1316 bytes / 40 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 245 / 347 |
Version history: | 13 change(s) |
Referenced in: | [show references] |