File: E-Load.c

package info (click to toggle)
epplets 0.8.cvs.2005032801-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,452 kB
  • ctags: 2,121
  • sloc: ansic: 21,627; sh: 8,360; makefile: 310
file content (164 lines) | stat: -rw-r--r-- 2,894 bytes parent folder | download | duplicates (3)
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
#include "config.h"
#include "epplet.h"

#ifdef HAVE_LIBGTOP
#include <glibtop.h>
#include <glibtop/loadavg.h>
#include <glibtop/cpu.h>
#include "proc.h"
#endif

int cpus = 0;
double *prev_val = NULL;
int                *load_val = NULL;
Epplet_gadget      *load     = NULL;

static void cb_timer(void *data);
static void cb_close(void *data);
int count_cpus(void);

static void
cb_timer(void *data)
{
#ifdef HAVE_LIBGTOP

/* libgtop only handles total load, not per-CPU load */

    glibtop_loadavg loadavg;
    double val, val2;
    int i;
    glibtop_get_loadavg (&loadavg);
    val2=loadavg.loadavg[0];
    val2 *= 20;

   /* printf ("Load: %f\n", val2); */

    if (val2 > 100)
      val2 = 100;
    load_val[0] = val2;
    Epplet_gadget_data_changed(load[0]);

#else

   static FILE *f;
   int i;

   f = fopen("/proc/stat", "r");
   if (f)
     {
	char s[256];
	
	if (cpus > 1)
	   fgets(s, 255, f);
	for (i = 0; i < cpus; i++)
	  {
	     char ss[64];
	     double val, val2;
	     
	     fgets(s, 255, f);
	     sscanf(s, "%*s %s %*s %*s %*s", ss);
	     val = atof(ss);
	     val2 = val - prev_val[i];
	     prev_val[i] = val;
	     val2 *= 3;
	     if (val2 > 100)
		val2 = 100;
	     load_val[i] = val2;
	     Epplet_gadget_data_changed(load[i]);
	  }
	fclose(f);
     }

#endif

   Esync();
   Epplet_timer(cb_timer, NULL, 0.333, "TIMER");   
   data = NULL;
}

static void
cb_close(void *data)
{
   Epplet_unremember();
   Esync();
   data = NULL;
   exit(0);
}


int
count_cpus(void)
{
#ifdef HAVE_LIBGTOP
  int i,c = 0;
  int bits;
  glibtop_cpu cpu;

    glibtop_get_cpu (&cpu);
    bits= (int)cpu.xcpu_flags;
    for (i=0; i<GLIBTOP_NCPU; i++) {
      c += bits&1;
      /*      printf ("%d: %o - %d\n",i,bits,c ); */
      bits>>=1;
    }
    /* printf ("CPUs: %d\n", c); */
 
  return c;
#else
   FILE *f;
   char s[256];
   
   f = fopen("/proc/stat", "r");
   if (f)
     {
	int count = 0;
	char ok = 1;
	
	while (ok)
	  {
	     if (!fgets(s, 255, f))
		ok = 0;
	     else
	       {
		  if (strncmp(s, "cpu", 3))
		     ok = 0;
		  else		  
		     count++;
	       }
	  }
	if (count > 1)
	   count--;
	fclose (f);
	return count;
     }
   exit(1);
#endif
}

int
main(int argc, char **argv)
{
   int i;
   
   atexit(Epplet_cleanup);
   cpus = count_cpus();
   load_val = malloc(sizeof(int) * cpus);
   prev_val = malloc(sizeof(double) * cpus);
   load     = malloc(sizeof(Epplet_gadget) * cpus);
   
   Epplet_Init("E-Load", "0.1", "Enlightenment Load Epplet",
	       5, cpus, argc, argv, 0);
   Epplet_timer(cb_timer, NULL, 0.333, "TIMER");
   Epplet_gadget_show(Epplet_create_button(NULL, NULL,
					   2, 2, 0, 0, "CLOSE", 0, NULL,
					   cb_close, NULL));
   for (i = 0; i < cpus; i++)
     {
	load[i] = Epplet_create_hbar(16, 3 + (i * 16), 62, 12, 
				     0, &(load_val[i]));
	Epplet_gadget_show(load[i]);
     }
   Epplet_show();
   Epplet_Loop();
   return 0;
}