mirror of git://git.musl-libc.org/musl
getloadavg: use sysinfo() instead of /proc/loadavg
Based on a patch by Szabolcs Nagy.
This commit is contained in:
parent
2de85a9856
commit
20cbd60775
|
@ -1,18 +1,14 @@
|
||||||
#define _GNU_SOURCE
|
#define _GNU_SOURCE
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <sys/sysinfo.h>
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
int getloadavg(double *a, int n)
|
int getloadavg(double *a, int n)
|
||||||
{
|
{
|
||||||
int i;
|
struct sysinfo si;
|
||||||
double b[3];
|
if (n <= 0) return n ? -1 : 0;
|
||||||
FILE *f = fopen("/proc/loadavg", "rbe");
|
sysinfo(&si);
|
||||||
if (!f) return -1;
|
if (n > 3) n = 3;
|
||||||
i = fscanf(f, "%lf %lf %lf", b, b+1, b+2);
|
for (int i=0; i<n; i++)
|
||||||
fclose(f);
|
a[i] = 1.0/(1<<SI_LOAD_SHIFT) * si.loads[i];
|
||||||
if (n > i) n = i;
|
|
||||||
if (n < 0) return -1;
|
|
||||||
memcpy(a, b, n * sizeof *a);
|
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue