~teejee2008/timeshift/trunk

« back to all changes in this revision

Viewing changes to src/Utility/ProcStats.vala

  • Committer: Tony George
  • Date: 2016-08-13 04:16:47 UTC
  • Revision ID: tony.george.kol@gmail.com-20160813041647-ivf2g6rszt00xco5
Updated project structure

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/*
 
3
 * ProcStats.vala
 
4
 *
 
5
 * Copyright 2016 Tony George <teejee2008@gmail.com>
 
6
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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,
 
20
 * MA 02110-1301, USA.
 
21
 *
 
22
 *
 
23
 */
 
24
 
 
25
using TeeJee.Logging;
 
26
using TeeJee.FileSystem;
 
27
using TeeJee.ProcessHelper;
 
28
 
 
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;
 
35
 
 
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;
 
41
 
 
42
                public double usage_percent = 0;
 
43
 
 
44
                public static ProcStats stat_prev = null;
 
45
 
 
46
                public ProcStats(string line){
 
47
                        string[] arr = line.split(" ");
 
48
                        int col = 0;
 
49
                        if (arr[col++] == "cpu"){
 
50
                                if (arr[col].length == 0){ col++; };
 
51
 
 
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++]);
 
57
 
 
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;
 
64
 
 
65
                                        usage_percent = (user_delta + nice_delta + system_delta) * 100 / (user_delta + nice_delta + system_delta + idle_delta);
 
66
                                }
 
67
                                else{
 
68
                                        usage_percent = 0;
 
69
 
 
70
                                }
 
71
 
 
72
                                ProcStats.stat_prev = this;
 
73
                        }
 
74
                }
 
75
                
 
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(" ");
 
81
                                if (arr[0] == "cpu"){
 
82
                                        ProcStats stat = new ProcStats(line);
 
83
                                        return stat.usage_percent;
 
84
                                }
 
85
                        }
 
86
                        return 0;
 
87
                }
 
88
        }
 
89