scope oshi_calcProcessCPUUsage. sclass #Data { long startTime; // for identifying the process long cpuTime, timestamp; } // pair(pid, start time); TODO: delete dead processes from map static Map #data = 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 = data.get(pid); if (data == null || data.startTime != p.getStartTime()) data.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 (data.timestamp != 0) result = percentRatio(cpuTime-data.cpuTime, now-data.timestamp); data.timestamp = now; data.cpuTime = cpuTime; ret result; } end scope