about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAlexander Monakov <amonakov@ispras.ru>2015-02-24 21:44:07 +0300
committerRich Felker <dalias@aerifal.cx>2015-02-25 15:37:55 -0500
commit20cbd607759038dca57f84ef7e7b5d44a3088574 (patch)
tree104f6987b3cb8ea4b741c67a20da11ae140d76d4
parent2de85a985654d2c944931267645d9a0686242dfe (diff)
downloadmusl-20cbd607759038dca57f84ef7e7b5d44a3088574.tar.gz
musl-20cbd607759038dca57f84ef7e7b5d44a3088574.tar.xz
musl-20cbd607759038dca57f84ef7e7b5d44a3088574.zip
getloadavg: use sysinfo() instead of /proc/loadavg
Based on a patch by Szabolcs Nagy.
-rw-r--r--src/legacy/getloadavg.c18
1 files changed, 7 insertions, 11 deletions
diff --git a/src/legacy/getloadavg.c b/src/legacy/getloadavg.c
index 43a8c9e3..ff06de0f 100644
--- a/src/legacy/getloadavg.c
+++ b/src/legacy/getloadavg.c
@@ -1,18 +1,14 @@
 #define _GNU_SOURCE
 #include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
+#include <sys/sysinfo.h>
 
 int getloadavg(double *a, int n)
 {
-	int i;
-	double b[3];
-	FILE *f = fopen("/proc/loadavg", "rbe");
-	if (!f) return -1;
-	i = fscanf(f, "%lf %lf %lf", b, b+1, b+2);
-	fclose(f);
-	if (n > i) n = i;
-	if (n < 0) return -1;
-	memcpy(a, b, n * sizeof *a);
+	struct sysinfo si;
+	if (n <= 0) return n ? -1 : 0;
+	sysinfo(&si);
+	if (n > 3) n = 3;
+	for (int i=0; i<n; i++)
+		a[i] = 1.0/(1<<SI_LOAD_SHIFT) * si.loads[i];
 	return n;
 }