summaryrefslogtreecommitdiffstats
path: root/common/osdep/cpuload.c
diff options
context:
space:
mode:
authorwens <wens@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2008-06-17 14:18:53 +0800
committerwens <wens@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2008-06-17 14:18:53 +0800
commit8cb29110950daa71ef6247ed5d0ba1ea9320fa20 (patch)
tree0d1e5083acd77e6593d78ee34dc906de8740edae /common/osdep/cpuload.c
parent2d6dc07feca4e5e3dc8333f514197573dfaefda0 (diff)
downloadpttbbs-8cb29110950daa71ef6247ed5d0ba1ea9320fa20.tar
pttbbs-8cb29110950daa71ef6247ed5d0ba1ea9320fa20.tar.gz
pttbbs-8cb29110950daa71ef6247ed5d0ba1ea9320fa20.tar.bz2
pttbbs-8cb29110950daa71ef6247ed5d0ba1ea9320fa20.tar.lz
pttbbs-8cb29110950daa71ef6247ed5d0ba1ea9320fa20.tar.xz
pttbbs-8cb29110950daa71ef6247ed5d0ba1ea9320fa20.tar.zst
pttbbs-8cb29110950daa71ef6247ed5d0ba1ea9320fa20.zip
Split out os dependent library
git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@4378 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
Diffstat (limited to 'common/osdep/cpuload.c')
-rw-r--r--common/osdep/cpuload.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/common/osdep/cpuload.c b/common/osdep/cpuload.c
new file mode 100644
index 00000000..be71fa72
--- /dev/null
+++ b/common/osdep/cpuload.c
@@ -0,0 +1,50 @@
+/* $Id$ */
+#include <stdio.h>
+#include <string.h>
+#include "osdep.h"
+
+#if defined( __FreeBSD__) || defined(_BSD_SOURCE)
+#include <stdlib.h>
+int
+cpuload(char *str)
+{
+ double l[3] = {-1, -1, -1};
+ if (getloadavg(l, 3) != 3)
+ l[0] = -1;
+
+ if (str) {
+ if (l[0] != -1)
+ sprintf(str, " %.2f %.2f %.2f", l[0], l[1], l[2]);
+ else
+ strcpy(str, " (unknown) ");
+ }
+ return (int)l[0];
+}
+#elif defined(__linux__)
+int
+cpuload(char *str)
+{
+ double l[3] = {-1, -1, -1};
+ FILE *fp;
+
+ if ((fp = fopen("/proc/loadavg", "r"))) {
+ if (fscanf(fp, "%lf %lf %lf", &l[0], &l[1], &l[2]) != 3)
+ l[0] = -1;
+ fclose(fp);
+ }
+ if (str) {
+ if (l[0] != -1)
+ sprintf(str, " %.2f %.2f %.2f", l[0], l[1], l[2]);
+ else
+ strcpy(str, " (unknown) ");
+ }
+ return (int)l[0];
+}
+#else
+int
+cpuload(char *str)
+{
+ strcpy(str, " (unknown) ");
+ return -1;
+}
+#endif