/* * Cputnik - a simple cpu and memory monitor * * Copyright (C) 2002-2005 pasp and sill * * 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 Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "cputnik.h" FILE *fp_stat, *fp_loadavg, *fp_memory; int cmd_lmb, cmd_rmb, update_period; int show_memory, free_memory; char command_lmb[PATH_MAX], command_rmb[PATH_MAX], *ProgName; stat_dev cpu_device; /*----------------------------------------------------------------------*/ void get_mem_statistics(int *free) { long long int m_total, m_free, m_cached; char temp[BUFFER_SIZE]; fp_memory = freopen("/proc/meminfo", "r", fp_memory); while(fscanf(fp_memory, "%s", temp)!=EOF) { if(!strncmp(temp,"MemTotal:", 9)) fscanf(fp_memory, "%Ld", &m_total); if(!strncmp(temp,"MemFree:", 8)) fscanf(fp_memory, "%Ld", &m_free); if(!strncmp(temp,"Cached:", 7)) fscanf(fp_memory, "%Ld", &m_cached); } *free = (int)(((float)(m_total - m_free - m_cached) / m_total) * 100.0); } /*----------------------------------------------------------------------*/ void draw_memory_meter(void) { free_memory = free_memory * (METER_WIDTH / 100.0); if (free_memory > METER_WIDTH) free_memory = METER_WIDTH; dcl_copy_xpm_area(0, 64, METER_WIDTH, 5, 26, 12); dcl_copy_xpm_area(32, 64, free_memory, 5, 26, 12); } /*----------------------------------------------------------------------*/ void get_cpu_statistics(char *devname, long *is, long *ds, long *idle) { char temp[BUFFER_SIZE], *p, *tokens = " \t\n"; int i; float f; *is = *ds = *idle = 0; if (!strncmp(devname, "cpu", 3)) { fseek(fp_stat, 0, SEEK_SET); while (fgets(temp, 128, fp_stat)) { if (strstr(temp, "cpu")) { p = strtok(temp, tokens); /* 1..3, 4 == idle, we don't want idle! */ for (i=0; i<3; i++) { p = strtok(NULL, tokens); *ds += atol(p); } p = strtok(NULL, tokens); *idle = atol(p); } } fp_loadavg = freopen("/proc/loadavg", "r", fp_loadavg); fscanf(fp_loadavg, "%f", &f); *is = (long) (100 * f); } } /*----------------------------------------------------------------------*/ void draw_stats(int *his, int num, int size, int x_left, int y_bottom) { int pixels_per_byte, j, k, *p, d; pixels_per_byte = 100; p = his; for (j=0; j pixels_per_byte) pixels_per_byte += 100; p++; } p = his; for (k=0; k 0; j-=100) { for (k=0; kname, &k, &istat, &idle); st->rt_idle = idle - st->idlelast; st->idlelast = idle; st->rt_stat = istat - st->statlast; st->statlast = istat; st->his[V_WIDTH] += k; st->hisaddcnt++; } /*----------------------------------------------------------------------*/ void cputnik_routine(int argc, char **argv) { XEvent Event; int but_stat = -1, xfd = 0; long start_time, current_time, next_time, istat, idle, k; unsigned long i, j; fd_set inputs; struct timeval timeout; fp_memory = fopen("/proc/meminfo", "r"); fp_loadavg = fopen("/proc/loadavg", "r"); fp_stat = fopen("/proc/stat", "r"); for (j=0; j METER_WIDTH) j = METER_WIDTH; dcl_copy_xpm_area(32, 64, j, 7-(show_memory*2), 26, 5); if (current_time >= next_time) { next_time += update_period; if (cpu_device.his[V_WIDTH]) cpu_device.his[V_WIDTH] /= cpu_device.hisaddcnt; for (j=1; j