Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

40
LINES

< > BotCompany Repo | #1019497 // oshi_calcProcessCPUUsage - use v2 instead

JavaX fragment (include)

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<Int, Data> #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

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: 185 / 280
Version history: 13 change(s)
Referenced in: [show references]