2 wmcore by bitman <bitman@bitmania.de>
6 based on wmlm by ben jarvis <bjarvis@bresnanlink.net>
8 This is a dockapp that shows the usage of each core in the system.
9 The dockapp splits into two displays, the upper one showing the common usage of the system and the
10 lower display showing one graph per each core.
12 It detects the number of cores and computes the usage to be represented as a bar graph.
13 wmcore works with a variable number of cores, I have tested the display with 1 up to 16 (simulated) cores.
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
37 #include <sys/param.h>
38 #include <sys/types.h>
41 #include <X11/extensions/shape.h>
42 #include <libdockapp/wmgeneral.h>
43 #include <libdockapp/misc.h>
45 #include "wmcore_master.xpm"
46 #include "wmcore_mask.xbm"
51 #define DEBUG 0 // 0 disable, 1 enable
54 printf("Usage: wmcore [options]\n\n");
55 printf(" -d n Number of microseconds of delay between each screen update.\n");
56 printf(" (The default is 1000000 = 1 sec)\n");
64 int main(int argc
, char **argv
) {
69 unsigned int cpu
; // current number of cpu when looping over all cpus
70 unsigned int cpu_max
=0; // number of detected cpus in the system
72 unsigned int curr_user
[MAX_CPU
];
73 unsigned int curr_nice
[MAX_CPU
];
74 unsigned int curr_syst
[MAX_CPU
];
75 unsigned int curr_idle
[MAX_CPU
];
76 unsigned int curr_total
[MAX_CPU
];
78 unsigned int prev_idle
[MAX_CPU
];
79 unsigned int prev_total
[MAX_CPU
];
81 unsigned int diff_idle
[MAX_CPU
];
82 unsigned int diff_total
[MAX_CPU
];
83 unsigned int diff_usage
[MAX_CPU
];
94 fputs(PACKAGE_STRING
" by bitman@bitmania.de\n", stdout
);
95 for (c
=1;c
<=argc
;c
++) {
96 if (argv
[c
] == NULL
) {}
97 else if (!strcmp(argv
[c
],"-d")) {
99 delay
= atoi(argv
[c
]);
100 printf("delaying %d microseconds between updates\n", delay
);
102 else if (!strcmp(argv
[c
],"-h")) {
111 openXwindow(argc
,argv
,wmcore_master_xpm
,wmcore_mask_bits
,wmcore_mask_width
,wmcore_mask_height
);
115 // Check how many cpu cores are present
116 if((in
= fopen("/proc/stat","r")) == NULL
)
118 printf("\nUnable to open file /proc/stat\n");
122 while( strstr(tmp
, "cpu") ) {
129 printf("CPU-Cores detected: %i\n",cpu_max
);
132 // Calculate the height per bar, to fit in the screen
137 for(cpu
=0; cpu
<cpu_max
; cpu
++) {
145 while(XPending(display
)) {
146 XNextEvent(display
,&Event
);
147 switch (Event
.type
) {
152 XCloseDisplay(display
);
159 // get the cpu core usage
160 if((in
= fopen("/proc/stat","r")) == NULL
)
162 printf("\nUnable to open file /proc/stat\n");
166 for(cpu
=0; cpu
< cpu_max
; cpu
++) {
167 fgets(tmp
,200,in
); // read next line
168 token
= strtok(tmp
," ");
169 curr_user
[cpu
] = atoi(strtok(NULL
," "));
170 curr_nice
[cpu
] = atoi(strtok(NULL
," "));
171 curr_syst
[cpu
] = atoi(strtok(NULL
," "));
172 curr_idle
[cpu
] = atoi(strtok(NULL
," "));
173 curr_total
[cpu
] = curr_user
[cpu
] + curr_nice
[cpu
] + curr_syst
[cpu
] + curr_idle
[cpu
];
174 diff_idle
[cpu
] = curr_idle
[cpu
]-prev_idle
[cpu
];
175 diff_total
[cpu
] = curr_total
[cpu
]-prev_total
[cpu
];
176 if(diff_total
[cpu
]>0){
177 diff_usage
[cpu
] = (unsigned int) (1000*(diff_total
[cpu
]-diff_idle
[cpu
])/diff_total
[cpu
]+5)/10 ;
185 // Display total usage over all cores in upper display field
187 for(cpu
=0; cpu
<cpu_max
; cpu
++) {
192 copyXPMArea(129,0, 61,8, 1, 2 ); // blank preveous bar
193 copyXPMArea(65,0, t
,8, 1, 2 ); // draw new bar
197 // Display the bar graphs for each core in lower display field
198 for(cpu
=0; cpu
<cpu_max
; cpu
++) {
199 // erase preveous bar
200 copyXPMArea(129,0, 61,h
, 1, 18+(cpu
*h
));
202 // calculate the length of the bar and display
203 t
=((diff_usage
[cpu
]*60)/100);
204 copyXPMArea(65,0, t
,h
, 1, 18+(cpu
*h
));
206 // Remember the total and idle CPU times for the next run.
207 prev_total
[cpu
]=curr_total
[cpu
];
208 prev_idle
[cpu
]=curr_idle
[cpu
];