|
Lines 289-310
Link Here
|
| 289 |
}; |
289 |
}; |
| 290 |
|
290 |
|
| 291 |
struct cpu_load_struct fresh = { {0, 0, 0, 0, 0} }; |
291 |
struct cpu_load_struct fresh = { {0, 0, 0, 0, 0} }; |
| 292 |
long cpu_used, oldtotal, oldused; |
292 |
long cpu_used;//, oldtotal, oldused; |
|
|
293 |
extern long * g_oldtotal, * g_oldused; |
| 293 |
|
294 |
|
| 294 |
void get_cpu_count(void) |
295 |
void get_cpu_count(void) |
| 295 |
{ |
296 |
{ |
| 296 |
/* int cpu_count = 0; */ |
297 |
int cpu_count = 0; |
|
|
298 |
size_t cpu_count_len = sizeof(cpu_count); |
| 297 |
|
299 |
|
| 298 |
/* XXX: FreeBSD doesn't allow to get per CPU load stats on SMP machines. |
300 |
if (sysctlbyname("hw.ncpu", &cpu_count, &cpu_count_len, NULL, 0) < 0) { |
| 299 |
* It's possible to get a CPU count, but as we fulfill only |
301 |
fprintf(stderr, "Cannot get hw.ncpu"); |
| 300 |
* info.cpu_usage[0], it's better to report there's only one CPU. |
302 |
info.cpu_count = 1; |
| 301 |
* It should fix some bugs (e.g. cpugraph) */ |
303 |
} |
| 302 |
#if 0 |
304 |
else |
| 303 |
if (GETSYSCTL("hw.ncpu", cpu_count) == 0) { |
305 |
{ |
| 304 |
info.cpu_count = cpu_count; |
306 |
info.cpu_count = cpu_count; |
| 305 |
} |
307 |
} |
| 306 |
#endif |
|
|
| 307 |
info.cpu_count = 1; |
| 308 |
|
308 |
|
| 309 |
info.cpu_usage = malloc(info.cpu_count * sizeof(float)); |
309 |
info.cpu_usage = malloc(info.cpu_count * sizeof(float)); |
| 310 |
if (info.cpu_usage == NULL) { |
310 |
if (info.cpu_usage == NULL) { |
|
Lines 315-323
Link Here
|
| 315 |
/* XXX: SMP support */ |
315 |
/* XXX: SMP support */ |
| 316 |
void update_cpu_usage(void) |
316 |
void update_cpu_usage(void) |
| 317 |
{ |
317 |
{ |
| 318 |
long used, total; |
318 |
int i, j = 0; |
| 319 |
long cp_time[CPUSTATES]; |
319 |
long *used, *total; |
| 320 |
size_t cp_len = sizeof(cp_time); |
320 |
static long *cp_time_per_cpu; |
|
|
321 |
size_t cp_len = info.cpu_count * CPUSTATES * sizeof(long); |
| 322 |
size_t len = info.cpu_count * sizeof(long); |
| 321 |
|
323 |
|
| 322 |
/* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */ |
324 |
/* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */ |
| 323 |
if ((cpu_setup == 0) || (!info.cpu_usage)) { |
325 |
if ((cpu_setup == 0) || (!info.cpu_usage)) { |
|
Lines 325-352
Link Here
|
| 325 |
cpu_setup = 1; |
327 |
cpu_setup = 1; |
| 326 |
} |
328 |
} |
| 327 |
|
329 |
|
| 328 |
if (sysctlbyname("kern.cp_time", &cp_time, &cp_len, NULL, 0) < 0) { |
330 |
cp_time_per_cpu = malloc(cp_len); |
| 329 |
fprintf(stderr, "Cannot get kern.cp_time"); |
331 |
used = malloc(len); |
| 330 |
} |
332 |
total = malloc(len); |
| 331 |
|
333 |
|
| 332 |
fresh.load[0] = cp_time[CP_USER]; |
334 |
if (sysctlbyname("kern.cp_times", cp_time_per_cpu, &cp_len, NULL, 0) < 0) { |
| 333 |
fresh.load[1] = cp_time[CP_NICE]; |
335 |
fprintf(stderr, "Cannot get kern.cp_times"); |
| 334 |
fresh.load[2] = cp_time[CP_SYS]; |
336 |
} |
| 335 |
fresh.load[3] = cp_time[CP_IDLE]; |
337 |
|
| 336 |
fresh.load[4] = cp_time[CP_IDLE]; |
338 |
for (i=0; i<info.cpu_count; i++) |
| 337 |
|
339 |
{ |
| 338 |
used = fresh.load[0] + fresh.load[1] + fresh.load[2]; |
340 |
fresh.load[0] = cp_time_per_cpu[CP_USER+j]; |
| 339 |
total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3]; |
341 |
fresh.load[1] = cp_time_per_cpu[CP_NICE+j]; |
| 340 |
|
342 |
fresh.load[2] = cp_time_per_cpu[CP_SYS+j]; |
| 341 |
if ((total - oldtotal) != 0) { |
343 |
fresh.load[3] = cp_time_per_cpu[CP_IDLE+j]; |
| 342 |
info.cpu_usage[0] = ((double) (used - oldused)) / |
344 |
fresh.load[4] = cp_time_per_cpu[CP_IDLE+j]; |
| 343 |
(double) (total - oldtotal); |
345 |
|
| 344 |
} else { |
346 |
j+=CPUSTATES; |
| 345 |
info.cpu_usage[0] = 0; |
347 |
|
| 346 |
} |
348 |
used[i] = fresh.load[0] + fresh.load[1] + fresh.load[2]; |
| 347 |
|
349 |
total[i] = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3]; |
| 348 |
oldused = used; |
350 |
|
| 349 |
oldtotal = total; |
351 |
if ((total[i] - g_oldtotal[i]) != 0) { |
|
|
352 |
info.cpu_usage[i] = ((double) (used[i] - g_oldused[i])) / |
| 353 |
(double) (total[i] - g_oldtotal[i]); |
| 354 |
} else { |
| 355 |
info.cpu_usage[i] = 0; |
| 356 |
} |
| 357 |
|
| 358 |
g_oldused[i] = used[i]; |
| 359 |
g_oldtotal[i] = total[i]; |
| 360 |
} |
| 361 |
|
| 362 |
free(cp_time_per_cpu); |
| 363 |
free(used); |
| 364 |
free(total); |
| 350 |
} |
365 |
} |
| 351 |
|
366 |
|
| 352 |
void update_load_average(void) |
367 |
void update_load_average(void) |