1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
|
/*
* mb-applet-system-monitor - tiny sys monitor
*
* cpu reading code based on wmbubblemon
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <libmb/mb.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef ENABLE_NLS
# include <libintl.h>
# define _(text) gettext(text)
#else
# define _(text) (text)
#endif
#ifdef MB_HAVE_PNG
#define IMG_EXT "png"
#else
#define IMG_EXT "xpm"
#endif
#define MINISYS_IMG "minisys." IMG_EXT
struct {
/* cpu data */
int loadIndex;
int samples;
u_int64_t *load, *total;
/* memory data */
u_int64_t mem_used;
u_int64_t mem_max;
unsigned int mem_percent; /* memory used, in percent */
} msd;
MBPixbuf *pb = NULL;
MBPixbufImage *ImgIcon = NULL, *ImgIconScaled = NULL, *ImgGraph = NULL;
int GraphHeight = 0, GraphWidth = 0;
char *ThemeName;
int IsKernel26 = 0;
int
check_if_kernel_2_6(void)
{
float v_nr=0;
FILE *version;
if ((version = fopen("/proc/version", "r")) == NULL)
{
fprintf(stderr, "mb-applet-system-monitor: failed to open /proc/version. Exiting\n");
exit(1);
}
fscanf(version, "%*s %*s %f", &v_nr);
fclose(version);
return (v_nr > 2.5);
}
/* returns current CPU load in percent, 0 to 100 */
int system_cpu(void)
{
unsigned int cpuload;
u_int64_t load, total, oload, ototal;
u_int64_t ab, ac, ad, ae;
int i;
FILE *stat;
if ((stat = fopen("/proc/stat", "r")) == NULL)
{
fprintf(stderr, "mb-applet-system-monitor: failed to open /proc/stat. Exiting\n");
exit(1);
}
fscanf(stat, "%*s %Ld %Ld %Ld %Ld", &ab, &ac, &ad, &ae);
fclose(stat);
/* Find out the CPU load */
/* user + sys = load
* total = total */
load = ab + ac + ad; /* cpu.user + cpu.sys; */
total = ab + ac + ad + ae; /* cpu.total; */
/* "i" is an index into a load history */
i = msd.loadIndex;
oload = msd.load[i];
ototal = msd.total[i];
msd.load[i] = load;
msd.total[i] = total;
msd.loadIndex = (i + 1) % msd.samples;
if (ototal == 0)
cpuload = 0;
else
cpuload = (100 * (load - oload)) / (total - ototal);
return cpuload;
}
int system_memory(void)
{
u_int64_t total, mfree, buffers, cached, used, shared,
cache_total, cache_free, cache_used, uneeded = 0;
u_int64_t my_mem_used, my_mem_max;
static int mem_delay = 0;
FILE *mem;
/* put this in permanent storage instead of stack */
static char not_needed[2048];
if (mem_delay-- <= 0)
{
if ((mem = fopen("/proc/meminfo", "r")) == NULL)
{
fprintf(stderr, "mb-applet-system-monitor: failed to open /proc/meminfo. Exiting.\n");
exit(1);
}
fgets(not_needed, 2048, mem);
if (IsKernel26)
{
rewind (mem);
fscanf (mem, "%*s %Ld %*s", &total);
fscanf (mem, "%*s %Ld %*s", &mfree);
fscanf (mem, "%*s %Ld %*s", &buffers);
fscanf (mem, "%*s %Ld %*s", &cached);
fscanf (mem, "%*s %Ld %*s", &uneeded);
fscanf (mem, "%*s %Ld %*s", &uneeded);
fscanf (mem, "%*s %Ld %*s", &uneeded);
fscanf (mem, "%*s %Ld %*s", &uneeded);
fscanf (mem, "%*s %Ld %*s", &uneeded);
fscanf (mem, "%*s %Ld %*s", &uneeded);
fscanf (mem, "%*s %Ld %*s", &uneeded);
fscanf (mem, "%*s %Ld %*s", &uneeded);
fscanf (mem, "%*s %Ld %*s", &uneeded);
fscanf (mem, "%*s %Ld %*s", &cache_total);
fscanf (mem, "%*s %Ld %*s", &cache_free);
total = total * 1024;
mfree = mfree * 1024;
used = total - mfree;
buffers = buffers * 1024;
cached = cached * 1024;
cache_total = cache_total * 1024;
cache_used = cache_total - (cache_free * 1024);
}
else
{ /* Assume 2.4 */
/*
total: used: free: shared: buffers: cached:
*/
fscanf(mem, "%*s %Ld %Ld %Ld %Ld %Ld %Ld", &total, &used, &mfree,
&shared, &buffers, &cached);
fscanf(mem, "%*s %Ld %Ld", &cache_total, &cache_used);
}
fclose(mem);
mem_delay = 25;
/* calculate it */
my_mem_max = cache_total + total;
my_mem_used = cache_used + used - cached - buffers;
msd.mem_used = my_mem_used;
msd.mem_max = my_mem_max;
msd.mem_percent = (100 * msd.mem_used) / msd.mem_max;
/* memory info changed - update things */
return 1;
}
/* nothing new */
return 0;
}
void
paint_callback (MBTrayApp *app, Drawable drw )
{
static int prev_cpu_pixels = -1, prev_mem_pixels = -1;
int cpu_pixels, mem_pixels;
int cpusize, memsize;
int x, y;
int membox_x, membox_y, membox_w, membox_h;
int cpubox_x, cpubox_y, cpubox_w, cpubox_h;
MBPixbufImage *img_backing = NULL;
system_memory(); /* Update reading */
cpusize = system_cpu();
memsize = msd.mem_percent;
cpubox_h = membox_h = (mb_pixbuf_img_get_width(ImgIconScaled)/16) * 10;
cpu_pixels = (cpusize * ( cpubox_h) )/ 100 ;
mem_pixels = (memsize * ( membox_h) )/ 100 ;
if ((cpu_pixels == prev_cpu_pixels && mem_pixels == prev_mem_pixels))
return;
img_backing = mb_tray_app_get_background (app, pb);
mb_pixbuf_img_composite(pb, img_backing, ImgIconScaled, 0, 0);
cpubox_x = (mb_pixbuf_img_get_width(img_backing)/4) - (mb_pixbuf_img_get_width(img_backing)/16);
cpubox_y = cpubox_x;
cpubox_w = (mb_pixbuf_img_get_width(img_backing)/16) * 2;
membox_x = ((mb_pixbuf_img_get_width(img_backing)/4) * 3) - (mb_pixbuf_img_get_width(img_backing)/16);
membox_y = cpubox_y;
membox_w = cpubox_w;
/* clear boxes */
for ( y = membox_y; y < membox_y + membox_h; y++)
for ( x = 0; x < membox_w; x++)
{
mb_pixbuf_img_plot_pixel(pb, img_backing, membox_x + x, y,
0x66, 0x66, 0x66);
mb_pixbuf_img_plot_pixel(pb, img_backing, cpubox_x + x, y,
0x66, 0x66, 0x66);
}
if (cpusize > 0)
{
for ( y = cpubox_h; y > cpubox_h - cpu_pixels; y--)
for ( x = cpubox_x; x < cpubox_x + cpubox_w; x++)
mb_pixbuf_img_plot_pixel(pb, img_backing, x, y + cpubox_y,
0, 0xff, 0);
}
if (memsize > 0)
{
for ( y = membox_h; y > membox_h - mem_pixels; y--)
for ( x = membox_x; x < membox_x + membox_w; x++)
mb_pixbuf_img_plot_pixel(pb, img_backing, x, y + membox_y,
0xff, 0, 0);
}
/* XXX Alert here for low memory */
mb_pixbuf_img_render_to_drawable(pb, img_backing, drw, 0, 0);
mb_pixbuf_img_free( pb, img_backing );
prev_cpu_pixels = cpu_pixels;
prev_mem_pixels = mem_pixels;
}
void
button_callback (MBTrayApp *app, int x, int y, Bool is_released )
{
char tray_msg[256];
int cpu = system_cpu();
if (!is_released)
return;
sprintf(tray_msg, _("CPU: %i %%, MEMORY: %i %%\n"),cpu, msd.mem_percent);
mb_tray_app_tray_send_message(app, tray_msg, 5000);
}
void
resize_callback (MBTrayApp *app, int w, int h )
{
if (ImgIconScaled) mb_pixbuf_img_free(pb, ImgIconScaled);
if (ImgGraph) mb_pixbuf_img_free(pb, ImgGraph);
ImgIconScaled = mb_pixbuf_img_scale(pb, ImgIcon, w, h);
}
void
load_icon(void)
{
char *icon_path = NULL;
if (ImgIcon) mb_pixbuf_img_free(pb, ImgIcon);
icon_path = mb_dot_desktop_icon_get_full_path (ThemeName,
32,
MINISYS_IMG );
if (icon_path == NULL
|| !(ImgIcon = mb_pixbuf_img_new_from_file(pb, icon_path)))
{
fprintf(stderr, "miniapm: failed to load icon %s\n", MINISYS_IMG);
exit(1);
}
free(icon_path);
return;
}
void
theme_callback (MBTrayApp *app, char *theme_name)
{
if (!theme_name) return;
if (ThemeName) free(ThemeName);
ThemeName = strdup(theme_name);
load_icon();
resize_callback (app, mb_tray_app_width(app), mb_tray_app_width(app) );
}
void
timeout_callback ( MBTrayApp *app )
{
mb_tray_app_repaint (app);
}
int
main( int argc, char *argv[])
{
MBTrayApp *app = NULL;
struct timeval tv;
int i;
u_int64_t load = 0, total = 0;
#if ENABLE_NLS
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, DATADIR "/locale");
bind_textdomain_codeset (PACKAGE, "UTF-8");
textdomain (PACKAGE);
#endif
IsKernel26 = check_if_kernel_2_6();
app = mb_tray_app_new ( _("CPU/Mem Monitor"),
resize_callback,
paint_callback,
&argc,
&argv );
msd.samples = 16;
if (msd.load) {
load = msd.load[msd.loadIndex];
free(msd.load);
}
if (msd.total) {
total = msd.total[msd.loadIndex];
free(msd.total);
}
msd.loadIndex = 0;
msd.load = malloc(msd.samples * sizeof(u_int64_t));
msd.total = malloc(msd.samples * sizeof(u_int64_t));
for (i = 0; i < msd.samples; i++) {
msd.load[i] = load;
msd.total[i] = total;
}
pb = mb_pixbuf_new(mb_tray_app_xdisplay(app),
mb_tray_app_xscreen(app));
memset(&tv,0,sizeof(struct timeval));
tv.tv_usec = 400000;
mb_tray_app_set_theme_change_callback (app, theme_callback );
mb_tray_app_set_timeout_callback (app, timeout_callback, &tv);
mb_tray_app_set_button_callback (app, button_callback );
load_icon();
mb_tray_app_set_icon(app, pb, ImgIcon);
mb_tray_app_main (app);
return 1;
}
|