scope oshi_calcProcessCPUUsage_v2.
sclass #Data {
long cpuTime, timestamp;
OSProcess process;
}
// key: pid
static Map<Int, Data> #map = synchroMap();
svoid _onLoad_oshi_calcProcessCPUUsage_v2 {
ownResource(vmBus_onMessage('osProcesses, voidfunc(L processes) {
Set<Int> keep = asHashSet(collectMethod(processes, 'getProcessID));
for (Iterator<Int> it = map.keySet().iterator(); it.hasNext(); ) {
Int pid = it.next();
if (!keep.contains(pid)) {
Data data = map.get(pid);
print("Lost process: [" + pid + "] " + data.process.getName());
it.remove();
}
}
}));
}
// call this in proper interval (1 to 3 seconds) to work properly
static double oshi_calcProcessCPUUsage_v2(OSProcess p) {
if (p == null) ret 0;
int pid = p.getProcessID();
Data data = map.get(pid);
if (data == null)
map.put(pid, data = nu(Data, process := p));
long cpuTime = p.getKernelTime() + p.getUserTime();
double result = 0;
long now = sysNow();
if (data.timestamp != 0)
result = percentRatio(cpuTime-data.cpuTime, now-data.timestamp);
data.timestamp = now;
data.cpuTime = cpuTime;
ret result;
}
end scopeBegan life as a copy of #1019497
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: | #1019502 |
| Snippet name: | oshi_calcProcessCPUUsage_v2 - better handling of deleted processes |
| Eternal ID of this version: | #1019502/5 |
| Text MD5: | 2e22331ef7f7e5b0f68314c4aa4d2801 |
| 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:52:41 |
| Source code size: | 1209 bytes / 44 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 733 / 758 |
| Version history: | 4 change(s) |
| Referenced in: | #1006654 - Standard functions list 2 (LIVE, continuation of #761) |