8 #include <sys/utsname.h>
11 #include <sys/param.h>
12 #include <sys/types.h>
16 #include <X11/extensions/shape.h>
18 #include <libdockapp/wmgeneral.h>
19 #include <libdockapp/misc.h>
21 #include "asmon-master.xpm"
22 #include "asmon-mask.xbm"
28 #define EXEC_ON_CLICK 1
29 #define ASMON_VERSION "0.71"
34 #define BCHAR_HEIGHT 9
51 /* Evil globals I haven't removed yet */
52 long last_pageins
=0, last_pageouts
=0;
53 long last_swapins
=0, last_swapouts
=0;
55 static int has_kern26
= 0;
62 void DrawUptime(void);
64 void printversion(void);
65 void asmon_routine(int Xpid
, int allmem
);
66 void DrawLite(int state
, int dx
, int dy
);
70 float DrawMemSwap(void);
71 extern int getLoad(float *);
72 extern int getSwap(unsigned long *, unsigned long *);
73 extern int getMem(unsigned long *, unsigned long *);
74 extern int getCPU(unsigned long *, unsigned long *,
75 unsigned long *, unsigned long *,
76 unsigned long *, unsigned long *,
77 unsigned long *, unsigned long *);
79 void DrawXmem(int Xpid
, float total
);
80 float DrawMemSwap(float total
, int allmem
);
83 int main(int argc
, char *argv
[])
91 int kernMajor
, kernMinor
, kernRev
;
95 if (strlen(ProgName
) >= 5)
96 ProgName
+= (strlen(ProgName
) - 5);
98 for (i
=1; i
<argc
; i
++) {
103 if (strcmp(arg
+1, "display")) {
111 strncpy( Command
, argv
[i
], 253);
112 strcat( Command
, " &");
125 fprintf(stderr
, "X Server memory stats unavailable for Solaris.\n");
139 if ( (fp
= fopen("/var/run/server.0.pid", "r")) != NULL
)
141 fscanf(fp
, " %d", &Xpid
);
144 if ( (fp
= fopen("/tmp/.X0-lock", "r")) != NULL
)
146 fscanf(fp
, " %d", &Xpid
);
154 /* Open 64x64 window */
155 openXwindow(argc
, argv
, asmon_master_xpm
, asmon_mask_bits
, asmon_mask_width
, asmon_mask_height
);
157 if( uname( &name
) != -1 )
159 if( strcmp( name
.sysname
, "Linux" ) == 0 )
161 sscanf(name
.release
, "%d.%d.%d", &kernMajor
, &kernMinor
,
163 if(( kernMajor
== 2 ) && ( kernMinor
== 6 ))
169 fprintf(stderr
, "Can't find system name\n");
173 asmon_routine(Xpid
, allmem
);
177 /**************************************************************************/
180 fprintf(stderr
, "\nasmon %s - by Brad Hall (bkh@rio.vg)\n\t\toriginally based on Timecop's wmcpu\n\n", ASMON_VERSION
);
181 fprintf(stderr
, "The top bar: left is the CPU usage, right is the load average\n");
182 fprintf(stderr
, "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");
183 fprintf(stderr
, "The lower bar: the left swap usage and the number of megs swappedd avg\n");
184 fprintf(stderr
, "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");
185 fprintf(stderr
, "\t-display <display name>\n");
186 fprintf(stderr
, "\t-h\tthis screen\n");
187 fprintf(stderr
, "\t-v\tprint the version number\n");
189 fprintf(stderr
, "\t-u\tforce asmon to show uptime, rather than X mem use\n");
192 fprintf(stderr
, "\t-e cmd\texecute 'cmd' on mouse click\n");
194 fprintf(stderr
, "\n");
197 /**************************************************************************/
199 void printversion(void)
201 fprintf(stderr
, "asmon %s\n", ASMON_VERSION
);
204 /**************************************************************************/
206 void asmon_routine(int Xpid
, int allmem
)
208 int xpm_X
=0, xpm_Y
=0, count
=0;
217 /* Only run every 15 iterations */
218 if ( count
== 0 || count
== 15)
222 total
=DrawMemSwap(total
, allmem
);
228 /* X mem or Uptime? */
234 DrawXmem(Xpid
, total
);
239 RedrawWindowXY(xpm_X
, xpm_Y
);
240 while (XPending(display
))
242 XNextEvent(display
, &Event
);
248 fprintf(stderr
,"system(%s)\n",Command
);
250 if (Command
[ 0 ]) system(Command
);
254 RedrawWindowXY(xpm_X
, xpm_Y
);
257 XCloseDisplay(display
);
270 /**************************************************************************/
274 /* CPU Usage Meter */
280 unsigned long cpuIdle
, cpuUser
, cpuKern
, cpuWait
;
281 unsigned long pageIn
, pageOut
, swapIn
, swapOut
;
283 /* remember the statistics read last time */
284 static float cpustat
[4] = {0.0, 0.0, 0.0, 0.0};
285 float fields
[4] = {0.0, 0.0, 0.0, 0.0};
286 float cputotal
= 0.0;
288 getCPU(&cpuIdle
, &cpuUser
, &cpuKern
, &cpuWait
,
289 &pageIn
, &pageOut
, &swapIn
, &swapOut
);
291 // Calculate CPU stuff
292 fields
[0] = ((float)cpuIdle
- cpustat
[0]);
293 cpustat
[0] = (float)cpuIdle
;
294 cputotal
+= fields
[0];
296 fields
[1] = ((float)cpuUser
- cpustat
[1]);
297 cpustat
[1] = (float)cpuUser
;
298 cputotal
+= fields
[1];
300 fields
[2] = ((float)cpuKern
- cpustat
[2]);
301 cpustat
[2] = (float)cpuKern
;
302 cputotal
+= fields
[2];
304 fields
[3] = ((float)cpuWait
- cpustat
[3]);
305 cpustat
[3] = (float)cpuWait
;
306 cputotal
+= fields
[3];
311 cputotal
= ((cputotal
- (fields
[0] + fields
[2])) * 1.55);
312 if (cputotal
> 26) cputotal
= 26;
313 copyXPMArea(3, 84, cputotal
, 9, 5, 5);
314 copyXPMArea(15, 105, (27 - cputotal
), 9, (5 + cputotal
), 5);
315 copyXPMArea(16, 46, 2, 14, 32, 2);
319 if (pageIn
> last_pageins
) DrawLite(B_RED
, 5, 48);
320 else DrawLite(B_OFF
, 5, 48);
322 if (pageOut
> last_pageouts
) DrawLite(B_RED
, 10, 48);
323 else DrawLite(B_OFF
, 10, 48);
325 last_pageins
= pageIn
;
326 last_pageouts
= pageOut
;
329 if (swapIn
> last_swapins
) DrawLite(B_RED
, 5, 53);
330 else DrawLite(B_OFF
, 5, 53);
332 if (swapOut
> last_swapouts
) DrawLite(B_RED
, 10, 53);
333 else DrawLite(B_OFF
, 10, 53);
335 last_swapins
= swapIn
;
336 last_swapouts
= swapOut
;
341 /* CPU Usage Meter */
345 static double cpustat
[7]; /* remember the statistics read last time */
346 //double fields[7], info[7], cputotal=0.0,idlee=0.0;
347 double fields
[7], info
[7], cputotal
=0.0;
348 long pageins
=0, pageouts
=0, swapins
=0, swapouts
=0 ;
352 if( (fp
= fopen("/proc/stat", "r")) != NULL
)
357 fscanf(fp
, "cpu %lf %lf %lf %lf %lf %lf %lf", info
, info
+1,
358 info
+2, info
+3, info
+4, info
+5, info
+6);
362 if( (fp
= fopen("/proc/vmstat", "r")) != NULL
)
364 // gather data for LED's
365 while( fgets(buf
, 127, fp
) )
367 if (strstr(buf
, "pgpgin"))
368 sscanf(buf
, "pgpgin %ld", &pageins
);
370 if (strstr(buf
, "pgpgout"))
371 sscanf(buf
, "pgpgout %ld", &pageouts
);
373 if (strstr(buf
, "pswpin"))
374 sscanf(buf
, "pswpin %ld", &swapins
);
376 if (strstr(buf
, "pswpout"))
377 sscanf(buf
, "pswpout %ld", &swapouts
);
383 fscanf(fp
, "cpu %lf %lf %lf %lf", info
, info
+1, info
+2,
386 // gather data for LED's
387 while( fgets(buf
, 127, fp
) )
389 if (strstr(buf
, "page"))
390 sscanf(buf
, "page %ld %ld", &pageins
, &pageouts
);
392 if (strstr(buf
, "swap"))
393 sscanf(buf
, "swap %ld %ld", &swapins
, &swapouts
);
398 // Calculate CPU stuff
401 for(i
= 0; i
< 7; i
++)
403 fields
[i
] = info
[i
] - cpustat
[i
];
404 cputotal
+= fields
[i
];
405 cpustat
[i
] = info
[i
];
408 for(i
= 0; i
< 4; i
++)
410 fields
[i
] = info
[i
] - cpustat
[i
];
411 cputotal
+= fields
[i
];
412 cpustat
[i
] = info
[i
];
421 //cputotal = 100 * l1 ;
422 //cputotal=(100-(idlee*100/16))*26/100;
425 cputotal
= (cputotal
-(fields
[3]+fields
[4]))*1.55;
426 if ( cputotal
> 26 ) cputotal
= 26;
427 copyXPMArea(3,84,cputotal
,9,5,5);
428 copyXPMArea(15,105,(27-cputotal
),9,(5+cputotal
),5);
429 copyXPMArea(16,46,2,14,32,2);
433 if (pageins
> last_pageins
)
435 DrawLite(B_RED
, 5, 48);
437 DrawLite(B_OFF
, 5, 48);
440 if (pageouts
> last_pageouts
)
442 DrawLite(B_RED
, 10, 48);
444 DrawLite(B_OFF
, 10, 48);
446 last_pageins
= pageins
;
447 last_pageouts
= pageouts
;
450 if (swapins
> last_swapins
)
452 DrawLite(B_RED
, 5, 53);
454 DrawLite(B_OFF
, 5, 53);
457 if (swapouts
> last_swapouts
)
459 DrawLite(B_RED
, 10, 53);
461 DrawLite(B_OFF
, 10, 53);
463 last_swapins
= swapins
;
464 last_swapouts
= swapouts
;
470 /**************************************************************************/
476 static float oldv
= -1.0;
480 if (getLoad(&ftmp
) != -1)
484 if( (fp
= fopen("/proc/loadavg", "r")) != NULL
)
486 fscanf(fp
, "%f", &ftmp
);
492 tempa
=(ftmp
+0.005)*100;
494 copyXPMArea(3+(tempy
*6),66,6,9,50,5);
497 copyXPMArea(3+(tempy
*6),66,6,9,44,5);
498 copyXPMArea(65,66,3,9,41,5);
503 copyXPMArea(3+(tempy
*6),95,6,9,34,5);
505 copyXPMArea(3+(tempy
*6),66,6,9,34,5);
511 /**************************************************************************/
521 unsigned long memMax
, memFree
, swapMax
, swapFree
;
522 unsigned long MEMmem
, MEMswap
;
523 float memUsed
, swapUsed
;
526 getMem(&memMax
, &memFree
);
527 memUsed
= (float)(memMax
- memFree
);
529 getSwap(&swapMax
, &swapFree
);
530 swapUsed
= (float)(swapMax
- swapFree
);
537 if (((float)memMax
/ 1048576) >= 1)
538 MEMmem
= ((memUsed
* 31) / (float)memMax
);
540 MEMmem
= ((memUsed
* 36) / (float)memMax
);
544 copyXPMArea(4, 115, 55, 11, 4, 18);
547 copyXPMArea(3, 75, MEMmem
, 9, 5, 19);
548 copyXPMArea(15, 105, (36 - MEMmem
), 9, (5 + MEMmem
), 19);
550 tempa
= (memUsed
/ 1048576);
551 tempy
= (tempa
% 10);
552 copyXPMArea((3 + (tempy
* 6)), 66, 6, 9, 50, 19);
553 tempy
= ((tempa
/ 10) % 10);
554 copyXPMArea((3 + (tempy
* 6)), 66, 6, 9, 44, 19);
555 tempy
= ((tempa
/ 100) % 10);
558 copyXPMArea((3 + (tempy
* 6)), 66, 6, 9, 38, 19);
559 copyXPMArea(16, 46, 2, 14, 35, 16);
563 copyXPMArea(16, 46, 2, 14, 41, 16);
567 copyXPMArea(4, 115, 55, 11, 4, 32);
574 if (((float)swapMax
/ 1048576) >= 1)
575 MEMswap
= ((swapUsed
* 31) / (float)swapMax
);
577 MEMswap
= ((swapUsed
* 36) / (float)swapMax
);
580 copyXPMArea(3, 75, MEMswap
, 9, 5, 33);
581 copyXPMArea(15, 105, (36 - MEMswap
), 9, (5 + MEMswap
), 33);
583 tempa
= (swapUsed
/ 1048576);
584 tempy
= (tempa
% 10);
585 copyXPMArea((3 + (tempy
* 6)), 66, 6, 9, 50, 33);
586 tempy
= ((tempa
/ 10) % 10);
587 copyXPMArea((3 + (tempy
* 6)), 66, 6, 9, 44, 33);
588 tempy
= ((tempa
/ 100) % 10);
591 copyXPMArea((3 + (tempy
* 6)), 66, 6, 9, 38, 33);
592 copyXPMArea(16, 46, 2, 14, 42, 16);
596 copyXPMArea(16, 46, 2, 14, 41, 30);
599 return (float)memMax
;
605 float DrawMemSwap(float total
, int allmem
)
608 if( (fp
= fopen("/proc/meminfo", "r")) != NULL
)
610 static float stotal
=0.0, sshared
=0.0, sbuffers
=0.0, scached
=0.0;
612 float used
, freeM
, shared
, buffers
, cached
, swaptotal
,
614 unsigned long MEMshar
,MEMbuff
, MEMswap
;
623 fgets(junk
, 120, fp
);
624 if (strstr(junk
, "MemTotal"))
626 sscanf(junk
, "MemTotal: %f kB", &scratch
);
627 total
= scratch
* 1024;
629 if (strstr(junk
, "MemFree"))
631 sscanf(junk
, "MemFree: %f kB", &scratch
);
632 freeM
= scratch
* 1024;
633 used
= total
- freeM
;
635 if (strstr(junk
, "Buffers"))
637 sscanf(junk
, "Buffers: %f kB", &scratch
);
638 buffers
= scratch
* 1024;
640 if (strstr(junk
, "Cached"))
642 sscanf(junk
, "Cached: %f kB", &scratch
);
643 cached
= scratch
* 1024;
645 if (strstr(junk
, "SwapTotal"))
647 sscanf(junk
, "SwapTotal: %f kB", &scratch
);
648 swaptotal
= scratch
* 1024;
650 if (strstr(junk
, "SwapFree"))
652 sscanf(junk
, "SwapFree: %f kB", &scratch
);
653 swapfreeM
= scratch
* 1024;
654 swapused
= swaptotal
- swapfreeM
;
659 fscanf(fp
, "Mem: %f %f %f %f %f %f\nSwap: %f %f %f", &total
,
660 &used
, &freeM
, &shared
, &buffers
, &cached
,
661 &swaptotal
, &swapused
, &swapfreeM
);
666 if(stotal
!= total
|| sshared
!= shared
|| sbuffers
!= buffers
|| scached
!= cached
)
668 stotal
= total
; sshared
= shared
; sbuffers
= buffers
; scached
= cached
;
669 if ( (total
/101048576) >= 1)
671 MEMshar
=((used
-buffers
-cached
)/total
)*27;
672 MEMbuff
=(buffers
/total
)*27;
674 MEMshar
=((used
-buffers
-cached
)/total
)*33;
675 MEMbuff
=(buffers
/total
)*33;
678 copyXPMArea(4, 115, 55, 11, 4, 18);
680 if ( (total
/101048576) >= 1)
682 copyXPMArea(3,75,((used
/total
)*28),9,5,19);
684 copyXPMArea(3,75,((used
/total
)*34),9,5,19);
687 copyXPMArea(15,105,1,9,5+MEMshar
,19);
688 copyXPMArea(15,105,1,9,7+MEMshar
+MEMbuff
,19);
689 copyXPMArea(15,105,(36-(used
/total
)*34),9,(5+(used
/total
)*34),19);
693 copyXPMArea(3+(tempy
*6),66,6,9,50,19);
695 copyXPMArea(3+(tempy
*6),66,6,9,44,19);
696 tempy
=(tempa
/100)%10;
697 if ( (total
/101048576) >= 1) {
698 copyXPMArea(3+(tempy
*6),66,6,9,38,19);
699 copyXPMArea(16,46,2,14,35,16);
701 copyXPMArea(16,46,2,14,41,16);
705 if ( swaptotal
== 0 ) MEMswap
= 0;
707 if ( (total
/101048576) >= 1)
708 MEMswap
=(swapused
*31)/swaptotal
;
709 else MEMswap
=(swapused
*36)/swaptotal
;
712 copyXPMArea(4, 115, 55, 11, 4, 32);
714 copyXPMArea(3,75,MEMswap
,9,5,33);
715 copyXPMArea(15,105,(36-(MEMswap
)),9,5+MEMswap
,33);
717 tempa
=swapused
/1048576;
719 copyXPMArea(3+(tempy
*6),66,6,9,50,33);
721 copyXPMArea(3+(tempy
*6),66,6,9,44,33);
724 copyXPMArea(3+(tempy
*6),66,6,9,38,33);
725 copyXPMArea(16,46,2,14,35,30);
727 copyXPMArea(16,46,2,14,41,30);
735 /**************************************************************************/
740 void DrawXmem(int Xpid
, float total
)
743 char buf
[128], XFileName
[256];
745 long old_Xsize
=-1, Xsize
=0;
747 sprintf(XFileName
, "/proc/%d/status", Xpid
);
749 if ((fp
= fopen(XFileName
, "r")) != NULL
)
751 while( fgets(buf
, 127, fp
) )
753 if (strstr(buf
, "VmSize"))
754 sscanf(buf
, "VmSize: %ld", &Xsize
);
758 int tempy
, tempa
, tempb
;
760 ratio
=Xsize
/(total
/1024);
761 if ( Xsize
> (total
/1024) ) Xsize
=total
/1024;
764 copyXPMArea(3+(tempy
*6),66,6,9,50,48);
770 copyXPMArea(3,84,((ratio
)*17),11,18,47);
771 copyXPMArea(15,105,(23-((ratio
)*17)),11,(18+(ratio
*22)),47);
772 copyXPMArea(3+(tempy
*6),66,6,9,44,48);
773 copyXPMArea(3+(tempb
*6),66,6,9,38,48);
774 copyXPMArea(16,46,2,14,36,46);
776 copyXPMArea(3,84,((ratio
)*22),11,18,47);
777 copyXPMArea(15,105,(23-((ratio
)*22)),11,(18+(ratio
*22)),47);
778 copyXPMArea(3+(tempy
*6),66,6,9,44,48);
779 copyXPMArea(16,46,2,14,41,46);
788 /**************************************************************************/
791 void DrawUptime(void)
793 int upt
, days
=0,hours
=0,mins
=0, old_mins
=-1,old_hours
=-1;
798 idUtmp
.ut_type
= BOOT_TIME
;
800 pUtmp
= getutid(&idUtmp
);
801 upt
= (time(0) - pUtmp
->ut_time
);
804 if( (fp
= fopen("/proc/uptime", "r")) != NULL
)
805 fscanf(fp
, "%d",&upt
);
819 copyXPMArea(20,105,36,9,18,48);
821 copyXPMArea(3+(tempy
*6),66,6,9,50,48);
823 copyXPMArea(3+(tempy
*6),66,6,9,44,48);
824 copyXPMArea(63,66,3,9,41,48);
826 copyXPMArea(3+(tempy
*6),66,6,9,34,48);
828 copyXPMArea(3+(tempy
*6),66,6,9,28,48);
830 copyXPMArea(3+(tempy
*6),66,6,9,22,48);
833 copyXPMArea(3+(tempy
*6),66,6,9,50,48);
835 copyXPMArea(3+(tempy
*6),66,6,9,44,48);
836 copyXPMArea(63,66,3,9,41,48);
838 copyXPMArea(3+(tempy
*6),66,6,9,34,48);
840 copyXPMArea(3+(tempy
*6),66,6,9,28,48);
841 copyXPMArea(63,66,3,9,25,48);
843 copyXPMArea(3+(tempy
*6),66,6,9,18,48);
849 /**************************************************************************/
852 void DrawLite(int state
, int dx
, int dy
)
857 copyXPMArea(BREDX
, BREDY
, LITEW
, LITEH
, dx
, dy
);
860 copyXPMArea(BGREENX
, BGREENY
, LITEW
, LITEH
, dx
, dy
);
864 copyXPMArea(BOFFX
, BOFFY
, LITEW
, LITEH
, dx
, dy
);