summaryrefslogtreecommitdiffstats
path: root/src/detection/cpuusage/cpuusage.c
blob: a78ae7b13cdfd14f54885e8a0a24d39ceaf09fb6 (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "fastfetch.h"
#include "detection/cpuusage/cpuusage.h"
#include "common/time.h"

#include <stdint.h>

static FFlist cpuTimes1;
static uint64_t startTime;

void ffPrepareCPUUsage(void) {
    if (startTime != 0) {
        return; // Already prepared
    }

    ffListInit(&cpuTimes1);
    ffGetCpuUsageInfo(&cpuTimes1);
    startTime = ffTimeGetNow();
}

const char* ffGetCpuUsageResult(FFCPUUsageOptions* options, FFlist* result) {
    const char* error = NULL;
    if (startTime == 0) {
        ffListInit(&cpuTimes1);
        error = ffGetCpuUsageInfo(&cpuTimes1);
        if (error) {
            return error;
        }
        ffTimeSleep(options->waitTime);
    } else {
        uint64_t elapsedTime = ffTimeGetNow() - startTime;
        if (elapsedTime < options->waitTime) {
            ffTimeSleep(options->waitTime - (uint32_t) elapsedTime);
        }
    }

    if (cpuTimes1.length == 0) {
        return "No CPU cores found";
    }

    FF_LIST_AUTO_DESTROY cpuTimes2 = ffListCreate();
    uint32_t retryCount = 0;

retry:
    error = ffGetCpuUsageInfo(&cpuTimes2);
    if (error) {
        return error;
    }
    if (cpuTimes1.length != cpuTimes2.length) {
        return "Unexpected CPU usage result";
    }

    for (uint32_t i = 0; i < cpuTimes1.length; ++i) {
        FFCpuUsageInfo* cpuTime1 = FF_LIST_GET(FFCpuUsageInfo, cpuTimes1, i);
        FFCpuUsageInfo* cpuTime2 = FF_LIST_GET(FFCpuUsageInfo, cpuTimes2, i);
        if (cpuTime2->totalAll <= cpuTime1->totalAll) {
            if (++retryCount <= 3) {
                ffListClear(&cpuTimes2);
                ffTimeSleep(options->waitTime);
                goto retry;
            }
            return "CPU time did not increase. Try increasing wait time.";
        }
    }

    for (uint32_t i = 0; i < cpuTimes1.length; ++i) {
        FFCpuUsageInfo* cpuTime1 = FF_LIST_GET(FFCpuUsageInfo, cpuTimes1, i);
        FFCpuUsageInfo* cpuTime2 = FF_LIST_GET(FFCpuUsageInfo, cpuTimes2, i);
        *FF_LIST_ADD(double, *result) = (double) (cpuTime2->inUseAll - cpuTime1->inUseAll) / (double) (cpuTime2->totalAll - cpuTime1->totalAll) * 100;
        cpuTime1->inUseAll = cpuTime2->inUseAll;
        cpuTime1->totalAll = cpuTime2->totalAll;
    }
    startTime = ffTimeGetNow();
    return NULL;
}