|
@@ -1,11 +1,11 @@
|
|
-
|
|
+
|
|
|
|
|
|
|
|
|
|
*
|
|
*
|
|
* The percentages function was written by William LeFebvre and is part
|
|
* The percentages function was written by William LeFebvre and is part
|
|
* of the 'top' utility. His copyright statement is below.
|
|
* of the 'top' utility. His copyright statement is below.
|
|
*
|
|
*
|
|
- * Copyright (c) 2001-2005 Willem Dijkstra
|
|
+ * Copyright (c) 2001-2007 Willem Dijkstra
|
|
* All rights reserved.
|
|
* All rights reserved.
|
|
*
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* Redistribution and use in source and binary forms, with or without
|
|
@@ -67,11 +67,6 @@
|
|
|
|
|
|
#include "error.h"
|
|
#include "error.h"
|
|
#include "symon.h"
|
|
#include "symon.h"
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-static size_t cp_size;
|
|
|
|
-
|
|
|
|
|
|
|
|
* percentages(cnt, out, new, old, diffs) - calculate percentage change
|
|
* percentages(cnt, out, new, old, diffs) - calculate percentage change
|
|
* between array "old" and "new", putting the percentages i "out".
|
|
* between array "old" and "new", putting the percentages i "out".
|
|
@@ -80,84 +75,83 @@ static size_t cp_size;
|
|
* The routine assumes modulo arithmetic. This function is especially
|
|
* The routine assumes modulo arithmetic. This function is especially
|
|
* useful on BSD mchines for calculating cpu state percentages.
|
|
* useful on BSD mchines for calculating cpu state percentages.
|
|
*/
|
|
*/
|
|
-#ifdef HAS_KERN_CPTIME2
|
|
|
|
int
|
|
int
|
|
percentages(int cnt, int64_t *out, int64_t *new, int64_t *old, int64_t *diffs)
|
|
percentages(int cnt, int64_t *out, int64_t *new, int64_t *old, int64_t *diffs)
|
|
{
|
|
{
|
|
- int64_t change, total_change, *dp, half_total;
|
|
+ int64_t change, total_change, *dp, half_total;
|
|
-#else
|
|
+ int i;
|
|
-static int cp_time_mib[] = {CTL_KERN, KERN_CPTIME};
|
|
+
|
|
-int
|
|
+
|
|
-percentages(int cnt, int *out, register long *new, register long *old, long *diffs)
|
|
+ total_change = 0;
|
|
-{
|
|
+ dp = diffs;
|
|
- long change, total_change, *dp, half_total;
|
|
+
|
|
-#endif
|
|
+
|
|
- int i;
|
|
+ for (i = 0; i < cnt; i++) {
|
|
-
|
|
+ if ((change = *new - *old) < 0) {
|
|
-
|
|
+
|
|
-
|
|
+ change = (QUAD_MAX - *old) + *new;
|
|
- total_change = 0;
|
|
|
|
- dp = diffs;
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- for (i = 0; i < cnt; i++) {
|
|
|
|
- if ((change = *new - *old) < 0) {
|
|
|
|
-
|
|
|
|
- change = (*new - *old);
|
|
|
|
- }
|
|
|
|
- total_change += (*dp++ = change);
|
|
|
|
- *old++ = *new++;
|
|
|
|
}
|
|
}
|
|
|
|
+ total_change += (*dp++ = change);
|
|
|
|
+ *old++ = *new++;
|
|
|
|
+ }
|
|
|
|
|
|
-
|
|
+
|
|
- if (total_change == 0)
|
|
+ if (total_change == 0)
|
|
- total_change = 1;
|
|
+ total_change = 1;
|
|
|
|
|
|
-
|
|
+
|
|
- half_total = total_change / 2l;
|
|
+ half_total = total_change / 2l;
|
|
- for (i = 0; i < cnt; i++)
|
|
+ for (i = 0; i < cnt; i++)
|
|
- *out++ = ((*diffs++ * 1000 + half_total) / total_change);
|
|
+ *out++ = ((*diffs++ * 1000 + half_total) / total_change);
|
|
|
|
|
|
-
|
|
+
|
|
- return (total_change);
|
|
+ return (total_change);
|
|
}
|
|
}
|
|
|
|
|
|
void
|
|
void
|
|
init_cpu(struct stream *st)
|
|
init_cpu(struct stream *st)
|
|
{
|
|
{
|
|
char buf[SYMON_MAX_OBJSIZE];
|
|
char buf[SYMON_MAX_OBJSIZE];
|
|
-
|
|
|
|
#ifdef HAS_KERN_CPTIME2
|
|
#ifdef HAS_KERN_CPTIME2
|
|
const char *errstr;
|
|
const char *errstr;
|
|
int mib[2] = {CTL_HW, HW_NCPU};
|
|
int mib[2] = {CTL_HW, HW_NCPU};
|
|
int ncpu;
|
|
int ncpu;
|
|
long num;
|
|
long num;
|
|
-
|
|
|
|
size_t size = sizeof(ncpu);
|
|
size_t size = sizeof(ncpu);
|
|
|
|
+#endif
|
|
|
|
+
|
|
|
|
+ st->parg.cp.mib[0] = CTL_KERN;
|
|
|
|
+ st->parg.cp.mib[1] = KERN_CPTIME;
|
|
|
|
+ st->parg.cp.miblen = 2;
|
|
|
|
+
|
|
|
|
+#ifdef HAS_KERN_CPTIME2
|
|
if (sysctl(mib, 2, &ncpu, &size, NULL, 0) == -1) {
|
|
if (sysctl(mib, 2, &ncpu, &size, NULL, 0) == -1) {
|
|
warning("could not determine number of cpus: %.200s", strerror(errno));
|
|
warning("could not determine number of cpus: %.200s", strerror(errno));
|
|
ncpu = 1;
|
|
ncpu = 1;
|
|
}
|
|
}
|
|
|
|
|
|
- num = strtonum(st->arg, 0, SYMON_MAXCPUID-1, &errstr);
|
|
+ num = strtonum(st->arg, 0, SYMON_MAXCPUID - 1, &errstr);
|
|
if (errstr != NULL) {
|
|
if (errstr != NULL) {
|
|
fatal("cpu(%.200s) is invalid: %.200s", st->arg, errstr);
|
|
fatal("cpu(%.200s) is invalid: %.200s", st->arg, errstr);
|
|
}
|
|
}
|
|
|
|
|
|
- st->parg.cp.mib[0] = CTL_KERN;
|
|
+ if (ncpu > 1) {
|
|
- st->parg.cp.mib[1] = KERN_CPTIME2;
|
|
+ st->parg.cp.mib[0] = CTL_KERN;
|
|
- st->parg.cp.mib[2] = num;
|
|
+ st->parg.cp.mib[1] = KERN_CPTIME2;
|
|
- if (st->parg.cp.mib[2] >= ncpu) {
|
|
+ st->parg.cp.mib[2] = num;
|
|
- fatal("cpu(%d) is not present", st->parg.cp.mib[2]);
|
|
+ st->parg.cp.miblen = 3;
|
|
|
|
+ if (st->parg.cp.mib[2] >= ncpu) {
|
|
|
|
+ fatal("cpu(%d) is not present", st->parg.cp.mib[2]);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
#endif
|
|
#endif
|
|
|
|
|
|
- cp_size = sizeof(st->parg.cp.time);
|
|
|
|
|
|
|
|
get_cpu(buf, sizeof(buf), st);
|
|
get_cpu(buf, sizeof(buf), st);
|
|
|
|
|
|
info("started module cpu(%.200s)", st->arg);
|
|
info("started module cpu(%.200s)", st->arg);
|
|
}
|
|
}
|
|
|
|
+
|
|
void
|
|
void
|
|
gets_cpu()
|
|
gets_cpu()
|
|
{
|
|
{
|
|
@@ -167,22 +161,26 @@ gets_cpu()
|
|
int
|
|
int
|
|
get_cpu(char *symon_buf, int maxlen, struct stream *st)
|
|
get_cpu(char *symon_buf, int maxlen, struct stream *st)
|
|
{
|
|
{
|
|
- int total;
|
|
+ int i, total;
|
|
|
|
+ size_t len;
|
|
|
|
|
|
-#ifdef HAS_KERN_CPTIME2
|
|
+ len = sizeof(st->parg.cp.time2);
|
|
- if (sysctl(st->parg.cp.mib, 3, &st->parg.cp.time, &cp_size, NULL, 0) < 0) {
|
|
+ if (sysctl(st->parg.cp.mib, st->parg.cp.miblen, &st->parg.cp.time2, &len, NULL, 0) < 0) {
|
|
- warning("%s:%d: sysctl kern.cp_time2 for cpu%d failed", __FILE__, __LINE__, st->parg.cp.mib[2]);
|
|
+ warning("%s:%d: sysctl kern.cp_time* for cpu%d failed", __FILE__, __LINE__,
|
|
|
|
+ st->parg.cp.mib[2]);
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
-#else
|
|
+
|
|
- if (sysctl(cp_time_mib, 2, &st->parg.cp.time, &cp_size, NULL, 0) < 0) {
|
|
+
|
|
- warning("%s:%d: sysctl kern.cp_time failed", __FILE__, __LINE__);
|
|
+ if (st->parg.cp.mib[1] == KERN_CPTIME) {
|
|
- return 0;
|
|
+ bcopy(st->parg.cp.time2, st->parg.cp.time1, sizeof(st->parg.cp.time1));
|
|
|
|
+ for (i = 0; i < CPUSTATES; i++) {
|
|
|
|
+ st->parg.cp.time2[i] = (int64_t) st->parg.cp.time1[i];
|
|
|
|
+ }
|
|
}
|
|
}
|
|
-#endif
|
|
|
|
|
|
|
|
|
|
|
|
- total = percentages(CPUSTATES, st->parg.cp.states, st->parg.cp.time, st->parg.cp.old, st->parg.cp.diff);
|
|
+ total = percentages(CPUSTATES, st->parg.cp.states, st->parg.cp.time2, st->parg.cp.old, st->parg.cp.diff);
|
|
|
|
|
|
return snpack(symon_buf, maxlen, st->arg, MT_CPU,
|
|
return snpack(symon_buf, maxlen, st->arg, MT_CPU,
|
|
(double) (st->parg.cp.states[CP_USER] / 10.0),
|
|
(double) (st->parg.cp.states[CP_USER] / 10.0),
|