diff --git a/src/util.c b/src/util.c index 2749c538..a072af82 100644 --- a/src/util.c +++ b/src/util.c @@ -96,3 +96,42 @@ pid_t get_pid_max(void) #error "Platform not supported" #endif } + +#if defined(__linux__) && defined(__UCLIBC__) +int getloadavg(double *a, int n) +{ + FILE *fp; + int i; + + if (n <= 0) + { + return n ? -1 : 0; + } + if (n > 3) + { + n = 3; + } + + fp = fopen("/proc/loadavg", "r"); + if (fp == NULL) + { + perror("Failed to open /proc/loadavg file"); + return -1; + } + + for (i = 0; i < n; i++) + { + if (fscanf(fp, "%lf", &a[i]) != 1) + { + break; + } + } + + if (fclose(fp) != 0) + { + perror("Failed to close /proc/loadavg file"); + } + + return i; +} +#endif diff --git a/src/util.h b/src/util.h index 486f3f65..49e2d17d 100644 --- a/src/util.h +++ b/src/util.h @@ -175,4 +175,8 @@ int get_ncpu(void); */ pid_t get_pid_max(void); +#if defined(__linux__) && defined(__UCLIBC__) +int getloadavg(double *a, int n); +#endif + #endif