~cairo-dock-team/cairo-dock-plug-ins/experimental

« back to all changes in this revision

Viewing changes to Doncky/src/applet-cpusage.c

  • Committer: nochka85
  • Date: 2010-01-31 14:38:19 UTC
  • mto: This revision was merged to the branch mainline in revision 1490.
  • Revision ID: nochka85@nochka85-desktop-20100131143819-j6nno0cl7yxssexq
Doncky - 1st release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
* This file is a part of the Cairo-Dock project
 
3
*
 
4
* Copyright : (C) see the 'copyright' file.
 
5
* E-mail    : see the 'copyright' file.
 
6
*
 
7
* This program is free software; you can redistribute it and/or
 
8
* modify it under the terms of the GNU General Public License
 
9
* as published by the Free Software Foundation; either version 3
 
10
* of the License, or (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
* You should have received a copy of the GNU General Public License
 
17
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
*/
 
19
 
 
20
 
 
21
#include <stdlib.h>
 
22
#include <stdio.h>
 
23
#include <string.h>
 
24
#include <math.h>
 
25
 
 
26
#include <fcntl.h>
 
27
#include <unistd.h>
 
28
 
 
29
#include "applet-struct.h"
 
30
#include "applet-cpusage.h"
 
31
 
 
32
#define CPUSAGE_DATA_PIPE               CD_SYSMONITOR_PROC_FS"/stat"
 
33
#define CPUSAGE_UPTIME_PIPE             CD_SYSMONITOR_PROC_FS"/uptime"
 
34
#define CPUSAGE_LOADAVG_PIPE    CD_SYSMONITOR_PROC_FS"/loadavg"
 
35
#define CPUSAGE_PROC_INFO_PIPE  CD_SYSMONITOR_PROC_FS"/cpuinfo"
 
36
 
 
37
 
 
38
void cd_sysmonitor_get_uptime (gchar **cUpTime, gchar **cActivityTime)
 
39
{
 
40
        FILE *fd = fopen (CPUSAGE_UPTIME_PIPE, "r");
 
41
        if (fd == NULL)
 
42
        {
 
43
                cd_warning ("can't open %s", CPUSAGE_UPTIME_PIPE);
 
44
                return ;
 
45
        }
 
46
        
 
47
        double fUpTime = 0, fIdleTime = 0;
 
48
        int r = fscanf (fd, "%lf %lf\n", &fUpTime, &fIdleTime);
 
49
        fclose (fd);
 
50
        
 
51
        const int minute = 60;
 
52
        const int hour = minute * 60;
 
53
        const int day = hour * 24;
 
54
        int iUpTime = (int) fUpTime, iActivityTime = (int) (fUpTime - fIdleTime);
 
55
        //~ *cUpTime = g_strdup_printf ("%d %s, %d:%02d:%02d",
 
56
                //~ iUpTime / day, D_("day(s)"),
 
57
                //~ (iUpTime % day) / hour,
 
58
                //~ (iUpTime % hour) / minute,
 
59
                //~ iUpTime % minute);
 
60
        *cUpTime = g_strdup_printf ("%d:%02d:%02d",
 
61
                iUpTime / hour,
 
62
                (iUpTime % hour) / minute,
 
63
                iUpTime % minute);
 
64
        *cActivityTime = g_strdup_printf ("%d %s, %d:%02d:%02d",
 
65
                iActivityTime / day, D_("day(s)"),
 
66
                (iActivityTime % day) / hour,
 
67
                (iActivityTime % hour) / minute,
 
68
                iActivityTime % minute);
 
69
}
 
70
 
 
71
 
 
72
void cd_sysmonitor_get_cpu_info (CairoDockModuleInstance *myApplet)
 
73
{
 
74
        gchar *cContent = NULL;
 
75
        gsize length=0;
 
76
        g_file_get_contents (CPUSAGE_PROC_INFO_PIPE, &cContent, &length, NULL);
 
77
        if (cContent == NULL)
 
78
        {
 
79
                cd_warning ("sysmonitor : can't open %s, assuming their is only 1 CPU with 1 core", CPUSAGE_PROC_INFO_PIPE);
 
80
                myData.iNbCPU = 1;
 
81
        }
 
82
        else
 
83
        {
 
84
                gchar *line = cContent;
 
85
                gchar *str;
 
86
                do
 
87
                {
 
88
                        str = NULL;
 
89
                        if (myData.cModelName == NULL && strncmp (line, "model name", 10) == 0)
 
90
                        {
 
91
                                str = strchr (line+10, ':');
 
92
                                if (str != NULL)
 
93
                                {
 
94
                                        gchar *str2 = strchr (str+2, '\n');
 
95
                                        if (str2 != NULL)
 
96
                                        {
 
97
                                                *str2 = '\0';
 
98
                                                myData.cModelName = g_strdup (str + 2);  // on saute l'espace apres le ':'.
 
99
                                                *str2 = '\n';
 
100
                                        }
 
101
                                }
 
102
                        }
 
103
                        else if (myData.iFrequency == 0 && strncmp (line, "cpu MHz", 7) == 0)
 
104
                        {
 
105
                                str = strchr (line+7, ':');
 
106
                                if (str != NULL)
 
107
                                {
 
108
                                        myData.iFrequency = atoi (str + 2);  // on saute l'espace apres le ':'.
 
109
                                }
 
110
                        }
 
111
                        else if (strncmp (line, "processor", 9) == 0)  // processeur virtuel.
 
112
                        {
 
113
                                cd_debug ("  found 1 virtual processor");
 
114
                                myData.iNbCPU ++;
 
115
                        }
 
116
                        /*else if (strncmp (line, "physical id", 11) == 0)  // processeur physique.
 
117
                        {
 
118
                                
 
119
                        }
 
120
                        else if (strncmp (line, "cpu cores", 9) == 0)  // nbre de coeurs de ce processeur physique.
 
121
                        {
 
122
                                
 
123
                        }*/
 
124
                        
 
125
                        if (str != NULL)
 
126
                                line = str;  // optimisation : on se place apres ce qu'on a trouve.
 
127
                        
 
128
                        str = strchr (line, '\n');
 
129
                        if (str == NULL)
 
130
                                break ;  // on cherche tous les processeurs.
 
131
                        line = str + 1;
 
132
                }
 
133
                while (TRUE);
 
134
        }
 
135
        myData.iNbCPU = MAX (myData.iNbCPU, 1);
 
136
        cd_debug ("sysmonitor : %d CPU/core(s) found", myData.iNbCPU);
 
137
        g_free (cContent);
 
138
}
 
139
 
 
140
 
 
141
#define go_to_next_value(tmp) \
 
142
        tmp ++; \
 
143
        while (g_ascii_isdigit (*tmp)) \
 
144
                tmp ++; \
 
145
        while (*tmp == ' ') \
 
146
                tmp ++; \
 
147
        if (*tmp == '\0') { \
 
148
                cd_warning ("sysmonitor : problem when reading pipe"); \
 
149
                myData.bAcquisitionOK = FALSE; \
 
150
                return ; }
 
151
 
 
152
void cd_sysmonitor_get_cpu_data (CairoDockModuleInstance *myApplet)
 
153
{
 
154
        static char cContent[512+1];
 
155
        
 
156
        FILE *fd = fopen (CPUSAGE_DATA_PIPE, "r");
 
157
        if (fd == NULL)
 
158
        {
 
159
                cd_warning ("sysmonitor : can't open %s", CPUSAGE_DATA_PIPE);
 
160
                myData.bAcquisitionOK = FALSE;
 
161
                return ;
 
162
        }
 
163
        
 
164
        gchar *tmp = fgets (cContent, 512, fd);  // on ne prend que la 1ere ligne, somme de tous les processeurs.
 
165
        fclose (fd);
 
166
        if (tmp == NULL)
 
167
        {
 
168
                cd_warning ("sysmonitor : can't read %s", CPUSAGE_DATA_PIPE);
 
169
                myData.bAcquisitionOK = FALSE;
 
170
                return ;
 
171
        }
 
172
        
 
173
        g_timer_stop (myData.pClock);
 
174
        double fTimeElapsed = g_timer_elapsed (myData.pClock, NULL);
 
175
        g_timer_start (myData.pClock);
 
176
        g_return_if_fail (fTimeElapsed > 0.1);  // en conf, c'est 1s minimum.
 
177
        
 
178
        long long int new_cpu_user = 0, new_cpu_user_nice = 0, new_cpu_system = 0, new_cpu_idle = 0;
 
179
        tmp += 3;  // on saute 'cpu'.
 
180
        while (*tmp == ' ')  // on saute les espaces.
 
181
                tmp ++;
 
182
        new_cpu_user = atoll (tmp);
 
183
        
 
184
        go_to_next_value(tmp)
 
185
        new_cpu_user_nice = atoll (tmp);
 
186
        //g_print ("%lld --> %lld\n", myData.cpu_user, new_cpu_user);
 
187
        
 
188
        go_to_next_value(tmp)
 
189
        new_cpu_system = atoll (tmp);
 
190
        
 
191
        go_to_next_value(tmp)
 
192
        new_cpu_idle = atoll (tmp);
 
193
        
 
194
        if (myData.bInitialized)  // la 1ere iteration on ne peut pas calculer la frequence.
 
195
        {
 
196
                myData.fCpuPercent = 100. * (1. - (new_cpu_idle - myData.cpu_idle) / myConfig.fUserHZ / myData.iNbCPU / fTimeElapsed);
 
197
                if (myData.fCpuPercent < 0)  // peut arriver car le fichier pipe est pas mis a jour tous les dt, donc il y'a potentiellement un ecart de dt avec la vraie valeur. Ca plus le temps d'execution.  
 
198
                        myData.fCpuPercent = 0;
 
199
                if (fabs (myData.fCpuPercent - myData.fPrevCpuPercent) > 1)
 
200
                {
 
201
                        myData.fPrevCpuPercent = myData.fCpuPercent;
 
202
                        myData.bNeedsUpdate = TRUE;
 
203
                }
 
204
                /*cd_debug ("CPU(%d) user : %d -> %d / nice : %d -> %d / sys : %d -> %d / idle : %d -> %d",
 
205
                        myData.iNbCPU,
 
206
                        myData.cpu_user, new_cpu_user,
 
207
                        myData.cpu_user_nice, new_cpu_user_nice,
 
208
                        myData.cpu_system, new_cpu_system,
 
209
                        myData.cpu_idle, new_cpu_idle);
 
210
                cd_debug ("=> CPU user : %.3f / nice : %.3f / sys : %.3f / idle : %.3f",
 
211
                        (new_cpu_user - myData.cpu_user) / myConfig.fUserHZ / myData.iNbCPU / fTimeElapsed,
 
212
                        (new_cpu_user_nice - myData.cpu_user_nice) / myConfig.fUserHZ / myData.iNbCPU / fTimeElapsed,
 
213
                        (new_cpu_system - myData.cpu_system) / myConfig.fUserHZ / myData.iNbCPU / fTimeElapsed,
 
214
                        (new_cpu_idle - myData.cpu_idle) / myConfig.fUserHZ / myData.iNbCPU / fTimeElapsed);*/
 
215
        }
 
216
        myData.cpu_user = new_cpu_user;
 
217
        myData.cpu_user_nice = new_cpu_user_nice;
 
218
        myData.cpu_system = new_cpu_system;
 
219
        myData.cpu_idle = new_cpu_idle;
 
220
}