summaryrefslogtreecommitdiffstats
path: root/src/detection/loadavg/loadavg_linux.c
blob: a1693123f3122a84266f5058701e824034cf2456 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "detection/loadavg/loadavg.h"
#include "common/io.h"

#include <sys/sysinfo.h>

const char* ffDetectLoadavg(double result[3]) {
#ifndef __ANDROID__ // cat: /proc/loadavg: Permission denied

    // Don't use syscall for container compatibility. #620
    char buf[64];
    ssize_t nRead = ffReadFileData("/proc/loadavg", sizeof(buf) - 1, buf);
    if (nRead > 0) {
        buf[nRead] = '\0';

        if (sscanf(buf, "%lf%lf%lf", &result[0], &result[1], &result[2]) == 3) {
            return NULL;
        }
    }

#endif
#ifndef __GNU__
    // getloadavg requires higher ANDROID_API version
    struct sysinfo si;
    if (sysinfo(&si) < 0) {
        return "sysinfo() failed";
    }

    for (int i = 0; i < 3; i++) {
        result[i] = (double) si.loads[i] / (1 << SI_LOAD_SHIFT);
    }
#endif
    return NULL;
}