summaryrefslogtreecommitdiffstats
path: root/src/test/test-psi-util.c
blob: 0b5a30cf57efe5d33e75b0433b66c29649fd1923 (plain)
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
75
76
77
78
79
80
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include <linux/loadavg.h>

#include "alloc-util.h"
#include "fileio.h"
#include "fs-util.h"
#include "psi-util.h"
#include "tests.h"

static void test_read_mem_pressure(void) {
        _cleanup_(unlink_tempfilep) char path[] = "/tmp/pressurereadtestXXXXXX";
        ResourcePressure rp;

        if (geteuid() != 0)
                return (void) log_tests_skipped("not root");

        assert_se(mkstemp(path));

        assert_se(read_resource_pressure("/verylikelynonexistentpath", PRESSURE_TYPE_SOME, &rp) < 0);
        assert_se(read_resource_pressure(path, PRESSURE_TYPE_SOME, &rp) < 0);

        assert_se(write_string_file(path, "herpdederp\n", WRITE_STRING_FILE_CREATE) == 0);
        assert_se(read_resource_pressure(path, PRESSURE_TYPE_SOME, &rp) < 0);

        /* Pressure file with some invalid values*/
        assert_se(write_string_file(path, "some avg10=0.22=55 avg60=0.17=8 avg300=1.11=00 total=58761459\n"
                                          "full avg10=0.23=55 avg60=0.16=8 avg300=1.08=00 total=58464525", WRITE_STRING_FILE_CREATE) == 0);
        assert_se(read_resource_pressure(path, PRESSURE_TYPE_SOME, &rp) < 0);

        /* Same pressure valid values as below but with duplicate avg60 field */
        assert_se(write_string_file(path, "some avg10=0.22 avg60=0.17 avg60=0.18 avg300=1.11 total=58761459\n"
                                          "full avg10=0.23 avg60=0.16 avg300=1.08 total=58464525", WRITE_STRING_FILE_CREATE) == 0);
        assert_se(read_resource_pressure(path, PRESSURE_TYPE_SOME, &rp) < 0);

        assert_se(write_string_file(path, "some avg10=0.22 avg60=0.17 avg300=1.11 total=58761459\n"
                                          "full avg10=0.23 avg60=0.16 avg300=1.08 total=58464525", WRITE_STRING_FILE_CREATE) == 0);
        assert_se(read_resource_pressure(path, PRESSURE_TYPE_SOME, &rp) == 0);
        assert_se(LOAD_INT(rp.avg10) == 0);
        assert_se(LOAD_FRAC(rp.avg10) == 22);
        assert_se(LOAD_INT(rp.avg60) == 0);
        assert_se(LOAD_FRAC(rp.avg60) == 17);
        assert_se(LOAD_INT(rp.avg300) == 1);
        assert_se(LOAD_FRAC(rp.avg300) == 11);
        assert_se(rp.total == 58761459);
        assert(read_resource_pressure(path, PRESSURE_TYPE_FULL, &rp) == 0);
        assert_se(LOAD_INT(rp.avg10) == 0);
        assert_se(LOAD_FRAC(rp.avg10) == 23);
        assert_se(LOAD_INT(rp.avg60) == 0);
        assert_se(LOAD_FRAC(rp.avg60) == 16);
        assert_se(LOAD_INT(rp.avg300) == 1);
        assert_se(LOAD_FRAC(rp.avg300) == 8);
        assert_se(rp.total == 58464525);

        /* Pressure file with extra unsupported fields */
        assert_se(write_string_file(path, "some avg5=0.55 avg10=0.22 avg60=0.17 avg300=1.11 total=58761459\n"
                                          "full avg10=0.23 avg60=0.16 avg300=1.08 avg600=2.00 total=58464525", WRITE_STRING_FILE_CREATE) == 0);
        assert_se(read_resource_pressure(path, PRESSURE_TYPE_SOME, &rp) == 0);
        assert_se(LOAD_INT(rp.avg10) == 0);
        assert_se(LOAD_FRAC(rp.avg10) == 22);
        assert_se(LOAD_INT(rp.avg60) == 0);
        assert_se(LOAD_FRAC(rp.avg60) == 17);
        assert_se(LOAD_INT(rp.avg300) == 1);
        assert_se(LOAD_FRAC(rp.avg300) == 11);
        assert_se(rp.total == 58761459);
        assert(read_resource_pressure(path, PRESSURE_TYPE_FULL, &rp) == 0);
        assert_se(LOAD_INT(rp.avg10) == 0);
        assert_se(LOAD_FRAC(rp.avg10) == 23);
        assert_se(LOAD_INT(rp.avg60) == 0);
        assert_se(LOAD_FRAC(rp.avg60) == 16);
        assert_se(LOAD_INT(rp.avg300) == 1);
        assert_se(LOAD_FRAC(rp.avg300) == 8);
        assert_se(rp.total == 58464525);
}

int main(void) {
        test_setup_logging(LOG_DEBUG);
        test_read_mem_pressure();
        return 0;
}