2
* This file is a part of the Cairo-Dock project
4
* Copyright : (C) see the 'copyright' file.
5
* E-mail : see the 'copyright' file.
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.
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/>.
29
#include "applet-struct.h"
30
#include "applet-cpusage.h"
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"
38
void cd_sysmonitor_get_uptime (gchar **cUpTime, gchar **cActivityTime)
40
FILE *fd = fopen (CPUSAGE_UPTIME_PIPE, "r");
43
cd_warning ("can't open %s", CPUSAGE_UPTIME_PIPE);
47
double fUpTime = 0, fIdleTime = 0;
48
int r = fscanf (fd, "%lf %lf\n", &fUpTime, &fIdleTime);
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",
62
(iUpTime % hour) / 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);
72
void cd_sysmonitor_get_cpu_info (CairoDockModuleInstance *myApplet)
74
gchar *cContent = NULL;
76
g_file_get_contents (CPUSAGE_PROC_INFO_PIPE, &cContent, &length, NULL);
79
cd_warning ("sysmonitor : can't open %s, assuming their is only 1 CPU with 1 core", CPUSAGE_PROC_INFO_PIPE);
84
gchar *line = cContent;
89
if (myData.cModelName == NULL && strncmp (line, "model name", 10) == 0)
91
str = strchr (line+10, ':');
94
gchar *str2 = strchr (str+2, '\n');
98
myData.cModelName = g_strdup (str + 2); // on saute l'espace apres le ':'.
103
else if (myData.iFrequency == 0 && strncmp (line, "cpu MHz", 7) == 0)
105
str = strchr (line+7, ':');
108
myData.iFrequency = atoi (str + 2); // on saute l'espace apres le ':'.
111
else if (strncmp (line, "processor", 9) == 0) // processeur virtuel.
113
cd_debug (" found 1 virtual processor");
116
/*else if (strncmp (line, "physical id", 11) == 0) // processeur physique.
120
else if (strncmp (line, "cpu cores", 9) == 0) // nbre de coeurs de ce processeur physique.
126
line = str; // optimisation : on se place apres ce qu'on a trouve.
128
str = strchr (line, '\n');
130
break ; // on cherche tous les processeurs.
135
myData.iNbCPU = MAX (myData.iNbCPU, 1);
136
cd_debug ("sysmonitor : %d CPU/core(s) found", myData.iNbCPU);
141
#define go_to_next_value(tmp) \
143
while (g_ascii_isdigit (*tmp)) \
145
while (*tmp == ' ') \
147
if (*tmp == '\0') { \
148
cd_warning ("sysmonitor : problem when reading pipe"); \
149
myData.bAcquisitionOK = FALSE; \
152
void cd_sysmonitor_get_cpu_data (CairoDockModuleInstance *myApplet)
154
static char cContent[512+1];
156
FILE *fd = fopen (CPUSAGE_DATA_PIPE, "r");
159
cd_warning ("sysmonitor : can't open %s", CPUSAGE_DATA_PIPE);
160
myData.bAcquisitionOK = FALSE;
164
gchar *tmp = fgets (cContent, 512, fd); // on ne prend que la 1ere ligne, somme de tous les processeurs.
168
cd_warning ("sysmonitor : can't read %s", CPUSAGE_DATA_PIPE);
169
myData.bAcquisitionOK = FALSE;
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.
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.
182
new_cpu_user = atoll (tmp);
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);
188
go_to_next_value(tmp)
189
new_cpu_system = atoll (tmp);
191
go_to_next_value(tmp)
192
new_cpu_idle = atoll (tmp);
194
if (myData.bInitialized) // la 1ere iteration on ne peut pas calculer la frequence.
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)
201
myData.fPrevCpuPercent = myData.fCpuPercent;
202
myData.bNeedsUpdate = TRUE;
204
/*cd_debug ("CPU(%d) user : %d -> %d / nice : %d -> %d / sys : %d -> %d / idle : %d -> %d",
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);*/
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;