Skip to content

Instantly share code, notes, and snippets.

@teddichiiwa
Created July 12, 2020 16:16
Show Gist options
  • Save teddichiiwa/d2d9a81e6717bd70be4787f5b2fca90e to your computer and use it in GitHub Desktop.
Save teddichiiwa/d2d9a81e6717bd70be4787f5b2fca90e to your computer and use it in GitHub Desktop.
private Float readUsage() {
try {
RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r");
String load = reader.readLine();
String[] toks = load.split(" ");
long idle1 = Long.parseLong(toks[5]);
long cpu1 = Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[4])
+ Long.parseLong(toks[6]) + Long.parseLong(toks[7]) + Long.parseLong(toks[8]);
try {
Thread.sleep(360);
} catch (Exception e) {}
reader.seek(0);
load = reader.readLine();
reader.close();
toks = load.split(" ");
long idle2 = Long.parseLong(toks[5]);
long cpu2 = Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[4])
+ Long.parseLong(toks[6]) + Long.parseLong(toks[7]) + Long.parseLong(toks[8]);
return (float)(cpu2 - cpu1) / ((cpu2 + idle2) - (cpu1 + idle1));
} catch (IOException ex) {
ex.printStackTrace();
}
return (float) 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment