scope oshi_calcProcessCPUUsage. sbool #debug; sclass #Data { long startTime; // for identifying the process long cpuTime, timestamp; toString { ret "startTime=" + startTime + ", cpuTime=" + cpuTime + ", timestamp=" + timestamp; } } // pair(pid, start time); 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); if (data == null || data.startTime != p.getStartTime()) { if (debug && data != null) print("Dropping data for " + pid + " (" + data.startTime + "/" + p.getStartTime() + ")"); map.put(pid, data = new Data); } if (data.startTime == 0) data.startTime = p.getStartTime(); 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