Debian Codegraph @

Disks/src/applet-disks.c

raw
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 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <glib/gi18n.h>
24 #include <glib.h>
25 #include <glib/gprintf.h>
26
27 #include <sys/statvfs.h>
28
29 #include "applet-struct.h"
30 #include "applet-notifications.h"
31 #include "applet-disks.h"
32 #include "cairo-dock.h"
33
34 #define DISK_BLOCK_SIZE 512
35 #define DISKS_SPEED_DATA_PIPE "/proc/diskstats"
36
37
38 // Prend un debit en octet par seconde et le transforme en une chaine de la forme : xxx yB/s
39 static void _cd_speed_formatRate (unsigned long long rate, gchar* debit, int iBufferSize, gboolean bLong)
40 {
41 int smallRate;
42 if (rate <= 0)
43 {
44 if (bLong)
45 snprintf (debit, iBufferSize, "0 %s/s", D_("B"));
46 else
47 snprintf (debit, iBufferSize, "0");
48 }
49 else if (rate < 1024)
50 {
51 smallRate = rate;
52 if (bLong)
53 snprintf (debit, iBufferSize, "%i %s/s", smallRate, D_("B"));
54 else
55 snprintf (debit, iBufferSize, "%iB", smallRate);
56 }
57 else if (rate < (1<<20))
58 {
59 smallRate = rate >> 10;
60 if (bLong)
61 snprintf (debit, iBufferSize, "%i %s/s", smallRate, D_("KB"));
62 else
63 snprintf (debit, iBufferSize, "%iK", smallRate);
64 }
65 else if (rate < (1<<30))
66 {
67 smallRate = rate >> 20;
68 if (bLong)
69 snprintf (debit, iBufferSize, "%i %s/s", smallRate, D_("MB"));
70 else
71 snprintf (debit, iBufferSize, "%iM", smallRate);
72 }
73 else if (rate < ((unsigned long long)1<<40))
74 {
75 smallRate = rate >> 30;
76 if (bLong)
77 snprintf (debit, iBufferSize, "%i %s/s", smallRate, D_("GB"));
78 else
79 snprintf (debit, iBufferSize, "%iG", smallRate);
80 }
81 else // c'est vraiment pour dire qu'on est exhaustif :-)
82 {
83 smallRate = rate >> 40;
84 if (bLong)
85 snprintf (debit, iBufferSize, "%i %s/s", smallRate, D_("TB"));
86 else
87 snprintf (debit, iBufferSize, "%iT", smallRate);
88 }
89 }
90
91 void cd_disks_format_value_on_icon (CairoDataRenderer *pRenderer, int iNumValue, gchar *cFormatBuffer, int iBufferLength, GldiModuleInstance *myApplet)
92 {
93 if (iNumValue < (int) myConfig.iNumberParts)
94 {
95 double *pSize;
96 pSize = g_list_nth_data (myData.lParts, iNumValue);
97 snprintf (cFormatBuffer, iBufferLength,
98 "%.f%%",
99 *pSize * 100);
100 }
101 else
102 {
103 static gchar s_upRateFormatted[11];
104 double fValue = cairo_data_renderer_get_normalized_current_value_with_latency (pRenderer, iNumValue);
105 int i = iNumValue / 2;
106 CDDiskSpeedData *pSpeed = g_list_nth_data (myData.lDisks, i);
107 fValue *= (iNumValue == i * 2 ? pSpeed->uMaxReadRate : pSpeed->uMaxWriteRate);
108
109 _cd_speed_formatRate (fValue, s_upRateFormatted, 11, FALSE);
110 snprintf (cFormatBuffer, iBufferLength,
111 "%s%s",
112 cairo_data_renderer_can_write_values (pRenderer) ? (iNumValue == i * 2 ?"↑" : "↓") : "",
113 s_upRateFormatted);
114 }
115 }
116
117
118 gboolean cd_disks_update_from_data (GldiModuleInstance *myApplet)
119 {
120 static gchar s_readRateFormatted[11], s_writeRateFormatted[11];
121 static double s_fValues[CD_DISKS_NB_MAX_VALUES];
122 //~ memset (s_fValues, 0, sizeof (s_fValues));
123 GString *sLabel = g_string_new ("");
124 gsize i;
125
126 CD_APPLET_ENTER;
127 if (myConfig.iNumberParts > 0)
128 {
129 double *pSize;
130 for (i = 0; i < myConfig.iNumberParts; i++)
131 {
132 pSize = g_list_nth_data (myData.lParts, i);
133 s_fValues[i] = *pSize;
134 //cd_warning("Partition %d = %s : %.1f%%", i, myConfig.cParts[i], *pSize * 100);
135
136 if (myConfig.iInfoDisplay == CAIRO_DOCK_INFO_ON_LABEL)
137 {
138 if (i > 0)
139 g_string_append (sLabel, " - ");
140
141 g_string_append_printf (sLabel, "%s : %.1f%%", myConfig.cParts[i], *pSize * 100);
142
143 }
144 }
145 }
146
147 if (myData.iNumberDisks > 0)
148 {
149 CDDiskSpeedData *pSpeed;
150 for (i = 0; i < myData.iNumberDisks; i++)
151 {
152 pSpeed = g_list_nth_data (myData.lDisks, i);
153
154 if(pSpeed->uReadSpeed > pSpeed->uMaxReadRate)
155 pSpeed->uMaxReadRate = pSpeed->uReadSpeed;
156
157 if(pSpeed->uWriteSpeed > pSpeed->uMaxWriteRate)
158 pSpeed->uMaxWriteRate = pSpeed->uWriteSpeed;
159
160 s_fValues[i * 2] = (pSpeed->uMaxReadRate != 0 ? (double) pSpeed->uReadSpeed / pSpeed->uMaxReadRate : 0. );
161 s_fValues[i * 2 + 1] = (pSpeed->uMaxWriteRate != 0 ? (double) pSpeed->uWriteSpeed / pSpeed->uMaxWriteRate : 0. );
162
163 if (myConfig.iInfoDisplay == CAIRO_DOCK_INFO_ON_LABEL)
164 {
165 if (i > 0)
166 g_string_append (sLabel, " - ");
167 if (pSpeed->bAcquisitionOK)
168 {
169 _cd_speed_formatRate (pSpeed->uReadSpeed, s_readRateFormatted, 11, myDesklet != NULL);
170 _cd_speed_formatRate (pSpeed->uWriteSpeed, s_writeRateFormatted, 11, myDesklet != NULL);
171 g_string_append_printf (sLabel, "%s : %s %s / %s %s", pSpeed->cName, D_("r"), s_readRateFormatted, D_("w"), s_writeRateFormatted);
172 }
173 else
174 {
175 g_string_append_printf (sLabel, "%s : %s", pSpeed->cName, D_("N/A"));
176 }
177 }
178
179 }
180 }
181
182 if (myConfig.iNumberParts + myData.iNumberDisks > 0)
183 {
184 /// Display data
185 CD_APPLET_RENDER_NEW_DATA_ON_MY_ICON (s_fValues);
186 if (myConfig.iInfoDisplay == CAIRO_DOCK_INFO_ON_LABEL)
187 CD_APPLET_SET_NAME_FOR_MY_ICON (sLabel->str);
188 }
189
190 g_string_free (sLabel, TRUE);
191 CD_APPLET_LEAVE (TRUE);
192 }
193
194 /* unused atm, will be needed for 'all disks' monitoring option
195 void _cd_disks_set_data (CDDiskSpeedData *pSpeed, long long unsigned uReadBlocks, long long unsigned uWriteBlocks, double fTimeElapsed)
196 {
197 if (pSpeed->bInitialized) // la 1ere iteration on ne peut pas calculer le debit.
198 {
199 pSpeed->uReadSpeed = (uReadBlocks - pSpeed->uReadBlocks) * DISK_BLOCK_SIZE / fTimeElapsed;
200 pSpeed->uWriteSpeed = (uWriteBlocks - pSpeed->uWriteBlocks) * DISK_BLOCK_SIZE / fTimeElapsed;
201 }
202 else
203 pSpeed->bInitialized = TRUE;
204
205 pSpeed->uReadBlocks = uReadBlocks;
206 pSpeed->uWriteBlocks = uWriteBlocks;
207 //~ cd_warning("%s %u %u", pSpeed->cName, pSpeed->uReadSpeed, pSpeed->uWriteSpeed);
208
209 pSpeed->bAcquisitionOK = TRUE;
210 }
211 */
212
213 #define BUFFSIZE (64*1024)
214 static char buff[BUFFSIZE];
215
216 void cd_disks_get_data (GldiModuleInstance *myApplet)
217 {
218 g_timer_stop (myData.pClock);
219 double fTimeElapsed = g_timer_elapsed (myData.pClock, NULL);
220 g_timer_start (myData.pClock);
221
222 if (!(myConfig.iNumberParts + myData.iNumberDisks > 0))
223 {
224 gldi_task_downgrade_frequency (myData.pPeriodicTask);
225 cd_warning("Disks : No disk defined");
226 }
227 g_return_if_fail ((fTimeElapsed > 0.1) || (myConfig.iNumberParts + myData.iNumberDisks > 0));
228
229 if (myConfig.iNumberParts > 0)
230 {
231 gsize i;
232 struct statvfs buffer;
233 int status;
234 double *pSize;
235 for (i = 0; i < myConfig.iNumberParts; i++)
236 {
237 pSize = g_list_nth_data (myData.lParts, i);
238 status = statvfs(myConfig.cParts[i], &buffer);
239 if (status < 0)
240 cd_warning ("Not able to use statvfs");
241 *pSize = 1. - (double) buffer.f_bfree / (double) buffer.f_blocks;
242 }
243 }
244
245 if (myData.iNumberDisks > 0)
246 {
247 char disk_name [16];
248 //~ int disk_major;
249
250 FILE* fd;
251 buff[BUFFSIZE-1] = 0;
252 fd = fopen (DISKS_SPEED_DATA_PIPE, "rb");
253 if (!fd)
254 {
255 gldi_task_downgrade_frequency (myData.pPeriodicTask);
256 cd_warning("Disks : Your kernel doesn't support diskstat. (2.5.70 or above required)");
257 }
258 else
259 {
260 //~ gldi_task_set_normal_frequency (myData.pPeriodicTask);
261 gsize i;
262 CDDiskSpeedData* pSpeed;
263 long long unsigned uReadBlocks, uWriteBlocks;
264 // for (;;)
265 while (TRUE)
266 {
267 if (!fgets(buff,BUFFSIZE-1,fd))
268 {
269 fclose(fd);
270 break;
271 }
272
273 /// Using Linux iostat : http://www.kernel.org/doc/Documentation/iostats.txt
274 /// gathering fields 3, 6 and 10
275 sscanf(buff, " %*d %*d %15s %*u %*u %llu %*u %*u %*u %llu %*u %*u %*u %*u",
276 //&disk_major, // field 1
277 disk_name,
278 &uReadBlocks,
279 &uWriteBlocks
280 );
281
282 if (strlen (disk_name) == 3)
283 for (i = 0; i < myData.iNumberDisks; i++)
284 {
285 pSpeed = g_list_nth_data (myData.lDisks, i);
286 if (strcmp (pSpeed->cName, disk_name) == 0)
287 {
288 if (pSpeed->bInitialized) // la 1ere iteration on ne peut pas calculer le debit.
289 {
290 pSpeed->uReadSpeed = (uReadBlocks - pSpeed->uReadBlocks) * DISK_BLOCK_SIZE / fTimeElapsed;
291 pSpeed->uWriteSpeed = (uWriteBlocks - pSpeed->uWriteBlocks) * DISK_BLOCK_SIZE / fTimeElapsed;
292 }
293 else
294 pSpeed->bInitialized = TRUE;
295
296 pSpeed->uReadBlocks = uReadBlocks;
297 pSpeed->uWriteBlocks = uWriteBlocks;
298 //~ cd_warning("%s %u %u", pSpeed->cName, pSpeed->uReadSpeed, pSpeed->uWriteSpeed);
299
300 pSpeed->bAcquisitionOK = TRUE;
301 break;
302 }
303 }
304
305 // if (myConfig.bListAll && !bFound) // need monitor
306 /// create new
307 //~ cd_warning("creation");
308 //~ pSpeed = g_new0 (CDDiskSpeedData, 1);
309 //~ pSpeed->cName = g_strdup (disk_name);
310 //~ myData.lDisks = g_list_append (myData.lDisks, pSpeed);
311 // speed_set_data (pDisk, new_reads, new_writes);
312 //~ }
313 }
314 }
315 }
316
317 }
318
319 #if (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION < 28)
320 void _reset_parts_list (double *pSize)
321 {
322 g_free (pSize);
323 }
324 #endif
325
326 void cd_disks_reset_parts_list (GldiModuleInstance *myApplet)
327 {
328 if (myConfig.iNumberParts > 0)
329 {
330 #if (GLIB_MAJOR_VERSION > 2) || (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 28)
331 g_list_free_full (myData.lParts, g_free);
332 #else
333 g_list_foreach (myData.lParts, (GFunc) _reset_parts_list, NULL);
334 g_list_free (myData.lParts);
335 #endif
336 myData.lParts = NULL;
337 }
338 }
339
340
341 void _reset_one_disk (CDDiskSpeedData *pSpeed)
342 {
343 if (pSpeed->cName != NULL)
344 g_free (pSpeed->cName);
345 g_free (pSpeed);
346 }
347
348 void cd_disks_reset_disks_list (GldiModuleInstance *myApplet)
349 {
350 if (myData.iNumberDisks > 0)
351 {
352 g_list_foreach (myData.lDisks, (GFunc) _reset_one_disk, NULL);
353 g_list_free (myData.lDisks);
354 myData.lDisks = NULL;
355 }
356 }
357
358