5
* Copyright 2016 Tony George <teejee2008@gmail.com>
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26
using TeeJee.FileSystem;
27
using TeeJee.ProcessHelper;
29
public class ProcStats : GLib.Object {
30
public double user = 0;
31
public double nice = 0;
32
public double system = 0;
33
public double idle = 0;
34
public double iowait = 0;
36
public double user_delta = 0;
37
public double nice_delta = 0;
38
public double system_delta = 0;
39
public double idle_delta = 0;
40
public double iowait_delta = 0;
42
public double usage_percent = 0;
44
public static ProcStats stat_prev = null;
46
public ProcStats(string line){
47
string[] arr = line.split(" ");
49
if (arr[col++] == "cpu"){
50
if (arr[col].length == 0){ col++; };
52
user = double.parse(arr[col++]);
53
nice = double.parse(arr[col++]);
54
system = double.parse(arr[col++]);
55
idle = double.parse(arr[col++]);
56
iowait = double.parse(arr[col++]);
58
if (ProcStats.stat_prev != null){
59
user_delta = user - ProcStats.stat_prev.user;
60
nice_delta = nice - ProcStats.stat_prev.nice;
61
system_delta = system - ProcStats.stat_prev.system;
62
idle_delta = idle - ProcStats.stat_prev.idle;
63
iowait_delta = iowait - ProcStats.stat_prev.iowait;
65
usage_percent = (user_delta + nice_delta + system_delta) * 100 / (user_delta + nice_delta + system_delta + idle_delta);
72
ProcStats.stat_prev = this;
76
//returns 0 when it is called first time
77
public static double get_cpu_usage(){
78
string txt = file_read("/proc/stat");
79
foreach(string line in txt.split("\n")){
80
string[] arr = line.split(" ");
82
ProcStats stat = new ProcStats(line);
83
return stat.usage_percent;