summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2009-04-26 16:47:24 +0000
committerDimitri Sokolyuk <demon@dim13.org>2009-04-26 16:47:24 +0000
commit2da07f7213925b29ce84c41fecc431c2426fac50 (patch)
tree5a043837c8ef98b73ac32bc99e1cd70278796e79
parentf89dd0aedc6a7cb546c109508e703450a9ac1b56 (diff)
add ncpu
-rw-r--r--lavg/lavg.c40
1 files changed, 29 insertions, 11 deletions
diff --git a/lavg/lavg.c b/lavg/lavg.c
index 57a0e00..5901f0f 100644
--- a/lavg/lavg.c
+++ b/lavg/lavg.c
@@ -19,11 +19,12 @@
static const char rcsid[] = "$Id$";
#endif /* not lint */
+#include <sys/param.h>
+#include <sys/sysctl.h>
+#include <err.h>
#include <stdio.h>
#include <stdlib.h>
-#define THRESHOLD 1
-
static const struct {
char *name;
char *color;
@@ -34,28 +35,45 @@ static const struct {
{"red", "#FF0000"}
};
-#define NELEM 3
+int
+ncpu(void)
+{
+ int mib[2], n;
+ size_t len;
+
+ mib[0] = CTL_HW;
+ mib[1] = HW_NCPU;
+ len = sizeof(n);
+
+ if (sysctl(mib, 2, &n, &len, NULL, 0) == -1)
+ err(1, "sysctl");
+
+ return n;
+}
int
main(void)
{
- double avg[NELEM];
- int i, n;
+ double avg[3];
+ int theshold, i, n;
+
+ threshold = ncpu();
printf("Content-Type: text/html\n\n<html>\n");
- if (getloadavg(avg, NELEM) == -1)
+ if (getloadavg(avg, 3) == -1)
printf("no load average information available\n");
else {
n = 0;
- for (i = 0; i < NELEM; i++) {
- if (avg[i] > THRESHOLD)
+ for (i = 0; i < 3; i++) {
+ if (avg[i] > threshold)
n++;
}
- printf("<head>\n<title>load %.2f %s</title>\n</head>\n<body>\n"
- "load averages:", avg[0], status[n].name);
- for (i = 0; i < NELEM; i++) {
+ printf("<head>\n<title>load %.2f %s</title>\n</head>\n",
+ avg[0], status[n].name);
+ printf("<body>\ncpu number: %d<br>\nload averages:", threshold);
+ for (i = 0; i < 3; i++) {
if (i > 0)
printf(",");
printf(" %.2f", avg[i]);