scope oshi_calcProcessCPUUsage. sbool #debug; sclass #Data { S characteristics; // for identifying the process long cpuTime, timestamp; toString { ret "cpuTime=" + cpuTime + ", timestamp=" + timestamp + ", characteristics=" + characteristics; } } // key: pid; TODO: delete dead processes from map static Map #map = synchroMap(); // call this in proper interval (1 to 3 seconds) to work properly static double oshi_calcProcessCPUUsage(OSProcess p) { if (p == null) ret 0; int pid = p.getProcessID(); Data data = map.get(pid); S characteristics = oshi_immutableProcessCharacteristics(p); if (data == null || neq(data.characteristics, characteristics)) { if (debug && data != null) print("Dropping data for " + pid + " (" + data.characteristics + " vs " + characteristics + ")"); map.put(pid, data = new Data); } if (data.characteristics == null) data.characteristics = characteristics; long cpuTime = p.getKernelTime() + p.getUserTime(); double result = 0; long now = sysNow(); if (debug) print("oshi cpu: " + pid + " / " + cpuTime + " / " + data); if (data.timestamp != 0) result = percentRatio(cpuTime-data.cpuTime, now-data.timestamp); data.timestamp = now; data.cpuTime = cpuTime; ret result; } end scope