5 #include <sys/utsname.h>
10 #include <libdockapp/wmgeneral.h>
12 #include "asmon-master.xpm"
13 #include "asmon-mask.xbm"
19 #define EXEC_ON_CLICK 1
20 #define ASMON_VERSION "0.72"
25 #define BCHAR_HEIGHT 9
41 /* Evil globals I haven't removed yet */
42 long last_pageins
= 0, last_pageouts
= 0;
43 long last_swapins
= 0, last_swapouts
= 0;
46 char Command
[256] = "";
50 void DrawUptime(void);
52 void printversion(void);
53 void asmon_routine(int Xpid
, int allmem
);
54 void DrawLite(int state
, int dx
, int dy
);
58 float DrawMemSwap(void);
59 extern int getLoad(float *);
60 extern int getSwap(unsigned long *, unsigned long *);
61 extern int getMem(unsigned long *, unsigned long *);
62 extern int getCPU(unsigned long *, unsigned long *,
63 unsigned long *, unsigned long *,
64 unsigned long *, unsigned long *,
65 unsigned long *, unsigned long *);
67 void DrawXmem(int Xpid
, float total
);
68 float DrawMemSwap(float total
, int allmem
);
71 int main(int argc
, char *argv
[])
80 if (strlen(ProgName
) >= 5)
81 ProgName
+= (strlen(ProgName
) - 5);
83 for (i
= 1; i
< argc
; i
++) {
88 if (strcmp(arg
+ 1, "display")) {
96 strncpy(Command
, argv
[i
], 253);
97 strcat(Command
, " &");
111 "X Server memory stats unavailable for Solaris.\n");
125 if ((fp
= fopen("/var/run/server.0.pid", "r")) != NULL
) {
126 fscanf(fp
, " %d", &Xpid
);
129 if ((fp
= fopen("/tmp/.X0-lock", "r")) != NULL
) {
130 fscanf(fp
, " %d", &Xpid
);
138 /* Open 64x64 window */
139 openXwindow(argc
, argv
, asmon_master_xpm
, asmon_mask_bits
,
140 asmon_mask_width
, asmon_mask_height
);
142 asmon_routine(Xpid
, allmem
);
146 /**************************************************************************/
151 "\nasmon %s - by Brad Hall (bkh@rio.vg)\n\t\toriginally based on Timecop's wmcpu\n\n",
154 "The top bar: left is the CPU usage, right is the load average\n");
156 "The middle bar: left memory usage devided by ticks into shared, buffers, and\n\t\t cached, respectively, and the number of megs used\n");
158 "The lower bar: the left swap usage and the number of megs swappedd avg\n");
160 "The bottom: the left is a set of LED's marking page's and swap's, the right is\n\t\t a bar representing the amount of memory that the X server \n\t\t is taking up, and the exact megs\n\n usage:\n");
161 fprintf(stderr
, "\t-display <display name>\n");
162 fprintf(stderr
, "\t-h\tthis screen\n");
163 fprintf(stderr
, "\t-v\tprint the version number\n");
166 "\t-u\tforce asmon to show uptime, rather than X mem use\n");
169 fprintf(stderr
, "\t-e cmd\texecute 'cmd' on mouse click\n");
171 fprintf(stderr
, "\n");
174 /**************************************************************************/
176 void printversion(void)
178 fprintf(stderr
, "asmon %s\n", ASMON_VERSION
);
181 /**************************************************************************/
183 void asmon_routine(int Xpid
, int allmem
)
185 int xpm_X
= 0, xpm_Y
= 0, count
= 0;
193 /* Only run every 15 iterations */
194 if (count
== 0 || count
== 15)
196 total
= DrawMemSwap();
198 total
= DrawMemSwap(total
, allmem
);
204 /* X mem or Uptime? */
209 DrawXmem(Xpid
, total
);
214 RedrawWindowXY(xpm_X
, xpm_Y
);
215 while (XPending(display
)) {
216 XNextEvent(display
, &Event
);
217 switch (Event
.type
) {
221 fprintf(stderr
, "system(%s)\n", Command
);
228 RedrawWindowXY(xpm_X
, xpm_Y
);
231 XCloseDisplay(display
);
243 /**************************************************************************/
247 /* CPU Usage Meter */
250 unsigned long cpuIdle
, cpuUser
, cpuKern
, cpuWait
;
251 unsigned long pageIn
, pageOut
, swapIn
, swapOut
;
253 /* remember the statistics read last time */
254 static float cpustat
[4] = { 0.0, 0.0, 0.0, 0.0 };
255 float fields
[4] = { 0.0, 0.0, 0.0, 0.0 };
256 float cputotal
= 0.0;
258 getCPU(&cpuIdle
, &cpuUser
, &cpuKern
, &cpuWait
,
259 &pageIn
, &pageOut
, &swapIn
, &swapOut
);
261 // Calculate CPU stuff
262 fields
[0] = ((float)cpuIdle
- cpustat
[0]);
263 cpustat
[0] = (float)cpuIdle
;
264 cputotal
+= fields
[0];
266 fields
[1] = ((float)cpuUser
- cpustat
[1]);
267 cpustat
[1] = (float)cpuUser
;
268 cputotal
+= fields
[1];
270 fields
[2] = ((float)cpuKern
- cpustat
[2]);
271 cpustat
[2] = (float)cpuKern
;
272 cputotal
+= fields
[2];
274 fields
[3] = ((float)cpuWait
- cpustat
[3]);
275 cpustat
[3] = (float)cpuWait
;
276 cputotal
+= fields
[3];
280 cputotal
= ((cputotal
- (fields
[0] + fields
[2])) * 1.55);
283 copyXPMArea(3, 84, cputotal
, 9, 5, 5);
284 copyXPMArea(15, 105, (27 - cputotal
), 9, (5 + cputotal
), 5);
285 copyXPMArea(16, 46, 2, 14, 32, 2);
288 if (pageIn
> last_pageins
)
289 DrawLite(B_RED
, 5, 48);
291 DrawLite(B_OFF
, 5, 48);
293 if (pageOut
> last_pageouts
)
294 DrawLite(B_RED
, 10, 48);
296 DrawLite(B_OFF
, 10, 48);
298 last_pageins
= pageIn
;
299 last_pageouts
= pageOut
;
302 if (swapIn
> last_swapins
)
303 DrawLite(B_RED
, 5, 53);
305 DrawLite(B_OFF
, 5, 53);
307 if (swapOut
> last_swapouts
)
308 DrawLite(B_RED
, 10, 53);
310 DrawLite(B_OFF
, 10, 53);
312 last_swapins
= swapIn
;
313 last_swapouts
= swapOut
;
318 /* CPU Usage Meter */
322 static double cpustat
[7]; /* remember the statistics read last time */
323 //double fields[7], info[7], cputotal=0.0,idlee=0.0;
324 double fields
[7], info
[7], cputotal
= 0.0;
325 long pageins
= 0, pageouts
= 0, swapins
= 0, swapouts
= 0;
329 if ((fp
= fopen("/proc/stat", "r")) != NULL
) {
331 fscanf(fp
, "cpu %lf %lf %lf %lf %lf %lf %lf", info
,
332 info
+ 1, info
+ 2, info
+ 3, info
+ 4, info
+ 5,
337 if ((fp
= fopen("/proc/vmstat", "r")) != NULL
) {
338 // gather data for LED's
339 while (fgets(buf
, 127, fp
)) {
340 if (strstr(buf
, "pgpgin"))
341 sscanf(buf
, "pgpgin %ld",
344 if (strstr(buf
, "pgpgout"))
345 sscanf(buf
, "pgpgout %ld",
348 if (strstr(buf
, "pswpin"))
349 sscanf(buf
, "pswpin %ld",
352 if (strstr(buf
, "pswpout"))
353 sscanf(buf
, "pswpout %ld",
360 // Calculate CPU stuff
361 for (i
= 0; i
< 7; i
++) {
362 fields
[i
] = info
[i
] - cpustat
[i
];
363 cputotal
+= fields
[i
];
364 cpustat
[i
] = info
[i
];
370 //cputotal = 100 * l1 ;
371 //cputotal=(100-(idlee*100/16))*26/100;
373 cputotal
= (cputotal
- (fields
[3] + fields
[4])) * 1.55;
376 copyXPMArea(3, 84, cputotal
, 9, 5, 5);
377 copyXPMArea(15, 105, (27 - cputotal
), 9, (5 + cputotal
),
379 copyXPMArea(16, 46, 2, 14, 32, 2);
382 if (pageins
> last_pageins
) {
383 DrawLite(B_RED
, 5, 48);
385 DrawLite(B_OFF
, 5, 48);
388 if (pageouts
> last_pageouts
) {
389 DrawLite(B_RED
, 10, 48);
391 DrawLite(B_OFF
, 10, 48);
393 last_pageins
= pageins
;
394 last_pageouts
= pageouts
;
397 if (swapins
> last_swapins
) {
398 DrawLite(B_RED
, 5, 53);
400 DrawLite(B_OFF
, 5, 53);
403 if (swapouts
> last_swapouts
) {
404 DrawLite(B_RED
, 10, 53);
406 DrawLite(B_OFF
, 10, 53);
408 last_swapins
= swapins
;
409 last_swapouts
= swapouts
;
415 /**************************************************************************/
421 static float oldv
= -1.0;
425 if (getLoad(&ftmp
) != -1) {
428 if ((fp
= fopen("/proc/loadavg", "r")) != NULL
) {
429 fscanf(fp
, "%f", &ftmp
);
434 tempa
= (ftmp
+ 0.005) * 100;
436 copyXPMArea(3 + (tempy
* 6), 66, 6, 9, 50, 5);
439 copyXPMArea(3 + (tempy
* 6), 66, 6, 9, 44, 5);
440 copyXPMArea(65, 66, 3, 9, 41, 5);
443 tempy
= (tempy
- 10);
444 copyXPMArea(3 + (tempy
* 6), 95, 6, 9, 34, 5);
446 copyXPMArea(3 + (tempy
* 6), 66, 6, 9, 34, 5);
452 /**************************************************************************/
457 float DrawMemSwap(void
459 unsigned long memMax
, memFree
, swapMax
, swapFree
;
460 unsigned long MEMmem
, MEMswap
;
461 float memUsed
, swapUsed
;
464 getMem(&memMax
, &memFree
);
465 memUsed
= (float)(memMax
- memFree
);
467 getSwap(&swapMax
, &swapFree
);
468 swapUsed
= (float)(swapMax
- swapFree
);
474 if (((float)memMax
/ 1048576) >= 1)
475 MEMmem
= ((memUsed
* 31) / (float)memMax
);
477 MEMmem
= ((memUsed
* 36) / (float)memMax
);
481 copyXPMArea(4, 115, 55, 11, 4, 18);
484 copyXPMArea(3, 75, MEMmem
, 9, 5, 19);
485 copyXPMArea(15, 105, (36 - MEMmem
), 9, (5 + MEMmem
), 19);
487 tempa
= (memUsed
/ 1048576);
488 tempy
= (tempa
% 10);
489 copyXPMArea((3 + (tempy
* 6)), 66, 6, 9, 50, 19);
490 tempy
= ((tempa
/ 10) % 10);
491 copyXPMArea((3 + (tempy
* 6)), 66, 6, 9, 44, 19);
492 tempy
= ((tempa
/ 100) % 10);
494 copyXPMArea((3 + (tempy
* 6)), 66, 6, 9, 38, 19);
495 copyXPMArea(16, 46, 2, 14, 35, 16);
497 copyXPMArea(16, 46, 2, 14, 41, 16);
501 copyXPMArea(4, 115, 55, 11, 4, 32);
507 if (((float)swapMax
/ 1048576) >= 1)
508 MEMswap
= ((swapUsed
* 31) / (float)swapMax
);
510 MEMswap
= ((swapUsed
* 36) / (float)swapMax
);
513 copyXPMArea(3, 75, MEMswap
, 9, 5, 33);
514 copyXPMArea(15, 105, (36 - MEMswap
), 9, (5 + MEMswap
), 33);
516 tempa
= (swapUsed
/ 1048576);
517 tempy
= (tempa
% 10);
518 copyXPMArea((3 + (tempy
* 6)), 66, 6, 9, 50, 33);
519 tempy
= ((tempa
/ 10) % 10);
520 copyXPMArea((3 + (tempy
* 6)), 66, 6, 9, 44, 33);
521 tempy
= ((tempa
/ 100) % 10);
523 copyXPMArea((3 + (tempy
* 6)), 66, 6, 9, 38, 33);
524 copyXPMArea(16, 46, 2, 14, 42, 16);
526 copyXPMArea(16, 46, 2, 14, 41, 30);
529 return (float)memMax
;
535 float DrawMemSwap(float total
, int allmem
)
538 if ((fp
= fopen("/proc/meminfo", "r")) != NULL
) {
540 float used
, freeM
, buffers
, cached
, swaptotal
,
542 unsigned long MEMshar
, MEMbuff
, MEMswap
;
547 fgets(junk
, 120, fp
);
548 if (strstr(junk
, "MemTotal")) {
549 sscanf(junk
, "MemTotal: %f kB",
551 total
= scratch
* 1024;
553 if (strstr(junk
, "MemFree")) {
554 sscanf(junk
, "MemFree: %f kB",
556 freeM
= scratch
* 1024;
557 used
= total
- freeM
;
559 if (strstr(junk
, "Buffers")) {
560 sscanf(junk
, "Buffers: %f kB",
562 buffers
= scratch
* 1024;
564 if (strstr(junk
, "Cached")) {
565 sscanf(junk
, "Cached: %f kB", &scratch
);
566 cached
= scratch
* 1024;
568 if (strstr(junk
, "SwapTotal")) {
569 sscanf(junk
, "SwapTotal: %f kB",
571 swaptotal
= scratch
* 1024;
573 if (strstr(junk
, "SwapFree")) {
574 sscanf(junk
, "SwapFree: %f kB",
576 swapfreeM
= scratch
* 1024;
577 swapused
= swaptotal
- swapfreeM
;
584 if ((total
/ 101048576) >= 1) {
586 ((used
- buffers
- cached
) / total
) * 27;
587 MEMbuff
= (buffers
/ total
) * 27;
590 ((used
- buffers
- cached
) / total
) * 33;
591 MEMbuff
= (buffers
/ total
) * 33;
594 copyXPMArea(4, 115, 55, 11, 4, 18);
596 if ((total
/ 101048576) >= 1) {
597 copyXPMArea(3, 75, ((used
/ total
) * 28), 9, 5,
600 copyXPMArea(3, 75, ((used
/ total
) * 34), 9, 5,
604 copyXPMArea(15, 105, 1, 9, 5 + MEMshar
, 19);
605 copyXPMArea(15, 105, 1, 9, 7 + MEMshar
+ MEMbuff
, 19);
606 copyXPMArea(15, 105, (36 - (used
/ total
) * 34), 9,
607 (5 + (used
/ total
) * 34), 19);
609 tempa
= used
/ 1048576;
611 copyXPMArea(3 + (tempy
* 6), 66, 6, 9, 50, 19);
612 tempy
= (tempa
/ 10) % 10;
613 copyXPMArea(3 + (tempy
* 6), 66, 6, 9, 44, 19);
614 tempy
= (tempa
/ 100) % 10;
615 if ((total
/ 101048576) >= 1) {
616 copyXPMArea(3 + (tempy
* 6), 66, 6, 9, 38, 19);
617 copyXPMArea(16, 46, 2, 14, 35, 16);
619 copyXPMArea(16, 46, 2, 14, 41, 16);
626 if ((total
/ 101048576) >= 1)
627 MEMswap
= (swapused
* 31) / swaptotal
;
629 MEMswap
= (swapused
* 36) / swaptotal
;
632 copyXPMArea(4, 115, 55, 11, 4, 32);
634 copyXPMArea(3, 75, MEMswap
, 9, 5, 33);
635 copyXPMArea(15, 105, (36 - (MEMswap
)), 9, 5 + MEMswap
, 33);
637 tempa
= swapused
/ 1048576;
639 copyXPMArea(3 + (tempy
* 6), 66, 6, 9, 50, 33);
640 tempy
= (tempa
/ 10) % 10;
641 copyXPMArea(3 + (tempy
* 6), 66, 6, 9, 44, 33);
644 copyXPMArea(3 + (tempy
* 6), 66, 6, 9, 38, 33);
645 copyXPMArea(16, 46, 2, 14, 35, 30);
647 copyXPMArea(16, 46, 2, 14, 41, 30);
656 /**************************************************************************/
661 void DrawXmem(int Xpid
, float total
)
664 char buf
[128], XFileName
[256];
666 long old_Xsize
= -1, Xsize
= 0;
668 sprintf(XFileName
, "/proc/%d/status", Xpid
);
670 if ((fp
= fopen(XFileName
, "r")) != NULL
) {
671 while (fgets(buf
, 127, fp
)) {
672 if (strstr(buf
, "VmSize"))
673 sscanf(buf
, "VmSize: %ld", &Xsize
);
675 if (old_Xsize
!= Xsize
) {
676 int tempy
, tempa
, tempb
;
678 ratio
= Xsize
/ (total
/ 1024);
679 if (Xsize
> (total
/ 1024))
680 Xsize
= total
/ 1024;
681 Xsize
= Xsize
/ 1024;
683 copyXPMArea(3 + (tempy
* 6), 66, 6, 9, 50, 48);
688 copyXPMArea(3, 84, ((ratio
) * 17), 11, 18, 47);
689 copyXPMArea(15, 105, (23 - ((ratio
) * 17)), 11,
690 (18 + (ratio
* 22)), 47);
691 copyXPMArea(3 + (tempy
* 6), 66, 6, 9, 44, 48);
692 copyXPMArea(3 + (tempb
* 6), 66, 6, 9, 38, 48);
693 copyXPMArea(16, 46, 2, 14, 36, 46);
695 copyXPMArea(3, 84, ((ratio
) * 22), 11, 18, 47);
696 copyXPMArea(15, 105, (23 - ((ratio
) * 22)), 11,
697 (18 + (ratio
* 22)), 47);
698 copyXPMArea(3 + (tempy
* 6), 66, 6, 9, 44, 48);
699 copyXPMArea(16, 46, 2, 14, 41, 46);
708 /**************************************************************************/
711 void DrawUptime(void)
713 int upt
, days
= 0, hours
= 0, mins
= 0, old_mins
= -1, old_hours
= -1;
718 idUtmp
.ut_type
= BOOT_TIME
;
720 pUtmp
= getutid(&idUtmp
);
721 upt
= (time(0) - pUtmp
->ut_time
);
724 if ((fp
= fopen("/proc/uptime", "r")) != NULL
)
725 fscanf(fp
, "%d", &upt
);
728 mins
= (upt
/ 60) % 60;
729 hours
= (upt
/ 3600) % 24;
730 days
= (upt
/ 86400);
731 if (old_hours
!= hours
)
733 if (old_mins
!= mins
) {
737 copyXPMArea(20, 105, 36, 9, 18, 48);
739 copyXPMArea(3 + (tempy
* 6), 66, 6, 9, 50, 48);
741 copyXPMArea(3 + (tempy
* 6), 66, 6, 9, 44, 48);
742 copyXPMArea(63, 66, 3, 9, 41, 48);
744 copyXPMArea(3 + (tempy
* 6), 66, 6, 9, 34, 48);
745 tempy
= (days
/ 10) % 10;
746 copyXPMArea(3 + (tempy
* 6), 66, 6, 9, 28, 48);
748 copyXPMArea(3 + (tempy
* 6), 66, 6, 9, 22, 48);
751 copyXPMArea(3 + (tempy
* 6), 66, 6, 9, 50, 48);
753 copyXPMArea(3 + (tempy
* 6), 66, 6, 9, 44, 48);
754 copyXPMArea(63, 66, 3, 9, 41, 48);
756 copyXPMArea(3 + (tempy
* 6), 66, 6, 9, 34, 48);
758 copyXPMArea(3 + (tempy
* 6), 66, 6, 9, 28, 48);
759 copyXPMArea(63, 66, 3, 9, 25, 48);
761 copyXPMArea(3 + (tempy
* 6), 66, 6, 9, 18, 48);
767 /**************************************************************************/
770 void DrawLite(int state
, int dx
, int dy
)
774 copyXPMArea(BREDX
, BREDY
, LITEW
, LITEH
, dx
, dy
);
777 copyXPMArea(BGREENX
, BGREENY
, LITEW
, LITEH
, dx
, dy
);
781 copyXPMArea(BOFFX
, BOFFY
, LITEW
, LITEH
, dx
, dy
);