summaryrefslogtreecommitdiffstats
path: root/src/collectors/proc.plugin/proc_pressure.c
blob: c4d4bc2b1e67e0facd9d0f3262a3b28fcd8de5a3 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// SPDX-License-Identifier: GPL-3.0-or-later

#include "plugin_proc.h"

#define PLUGIN_PROC_MODULE_PRESSURE_NAME "/proc/pressure"
#define CONFIG_SECTION_PLUGIN_PROC_PRESSURE "plugin:" PLUGIN_PROC_CONFIG_NAME ":" PLUGIN_PROC_MODULE_PRESSURE_NAME

// linux calculates this every 2 seconds, see kernel/sched/psi.c PSI_FREQ
#define MIN_PRESSURE_UPDATE_EVERY 2

static int pressure_update_every = 0;

static struct pressure resources[PRESSURE_NUM_RESOURCES] = {
    {
        .some = {
                .available = true,
                .share_time = {.id = "cpu_some_pressure", .title = "CPU some pressure"},
                .total_time = {.id = "cpu_some_pressure_stall_time", .title = "CPU some pressure stall time"}
                },
        .full = {
                // Disable CPU full pressure.
                // See https://github.com/torvalds/linux/commit/890d550d7dbac7a31ecaa78732aa22be282bb6b8
                .available = false,
                .share_time = {.id = "cpu_full_pressure", .title = "CPU full pressure"},
                .total_time = {.id = "cpu_full_pressure_stall_time", .title = "CPU full pressure stall time"}
                },
    },
    {
        .some = {
                .available = true,
                .share_time = {.id = "memory_some_pressure", .title = "Memory some pressure"},
                .total_time = {.id = "memory_some_pressure_stall_time", .title = "Memory some pressure stall time"}
                },
        .full = {
                .available = true,
                .share_time = {.id = "memory_full_pressure", .title = "Memory full pressure"},
                .total_time = {.id = "memory_full_pressure_stall_time", .title = "Memory full pressure stall time"}
                },
    },
    {
        .some = {
                .available = true,
                .share_time = {.id = "io_some_pressure", .title = "I/O some pressure"},
                .total_time = {.id = "io_some_pressure_stall_time", .title = "I/O some pressure stall time"}
                },
        .full = {
                .available = true,
                .share_time = {.id = "io_full_pressure", .title = "I/O full pressure"},
                .total_time = {.id = "io_full_pressure_stall_time", .title = "I/O full pressure stall time"}
                },
    },
    {
        .some = {
                // this is not available
                .available = false,
                .share_time = {.id = "irq_some_pressure", .title = "IRQ some pressure"},
                .total_time = {.id = "irq_some_pressure_stall_time", .title = "IRQ some pressure stall time"}
                },
        .full = {
                .available = true,
                .share_time = {.id = "irq_full_pressure", .title = "IRQ full pressure"},
                .total_time = {.id = "irq_full_pressure_stall_time", .title = "IRQ full pressure stall time"}
                },
    },
};

static struct resource_info {
    procfile *pf;
    const char *name;       // metric file name
    const char *family;     // webui section name
    int section_priority;
} resource_info[PRESSURE_NUM_RESOURCES] = {
        { .name = "cpu",    .family = "cpu",    .section_priority = NETDATA_CHART_PRIO_SYSTEM_CPU },
        { .name = "memory", .family = "ram",    .section_priority = NETDATA_CHART_PRIO_SYSTEM_RAM },
        { .name = "io",     .family = "disk",   .section_priority = NETDATA_CHART_PRIO_SYSTEM_IO },
        { .name = "irq",    .family = "interrupts",  .section_priority = NETDATA_CHART_PRIO_SYSTEM_INTERRUPTS },
};

void update_pressure_charts(struct pressure_charts *pcs) {
    if (pcs->share_time.st) {
        rrddim_set_by_pointer(
            pcs->share_time.st, pcs->share_time.rd10, (collected_number)(pcs->share_time.value10 * 100));
        rrddim_set_by_pointer(
            pcs->share_time.st, pcs->share_time.rd60, (collected_number)(pcs->share_time.value60 * 100));
        rrddim_set_by_pointer(
            pcs->share_time.st, pcs->share_time.rd300, (collected_number)(pcs->share_time.value300 * 100));
        rrdset_done(pcs->share_time.st);
    }
    if (pcs->total_time.st) {
        rrddim_set_by_pointer(
            pcs->total_time.st, pcs->total_time.rdtotal, (collected_number)(pcs->total_time.value_total));
        rrdset_done(pcs->total_time.st);
    }
}

static void proc_pressure_do_resource(procfile *ff, int res_idx, size_t line, bool some) {
    struct pressure_charts *pcs;
    struct resource_info ri;
    pcs = some ? &resources[res_idx].some : &resources[res_idx].full;
    ri = resource_info[res_idx];

    if (unlikely(!pcs->share_time.st)) {
        pcs->share_time.st = rrdset_create_localhost(
            "system",
            pcs->share_time.id,
            NULL,
            ri.family,
            NULL,
            pcs->share_time.title,
            "percentage",
            PLUGIN_PROC_NAME,
            PLUGIN_PROC_MODULE_PRESSURE_NAME,
            ri.section_priority + (some ? 40 : 50),
            pressure_update_every,
            RRDSET_TYPE_LINE);
        pcs->share_time.rd10 =
            rrddim_add(pcs->share_time.st, some ? "some 10" : "full 10", NULL, 1, 100, RRD_ALGORITHM_ABSOLUTE);
        pcs->share_time.rd60 =
            rrddim_add(pcs->share_time.st, some ? "some 60" : "full 60", NULL, 1, 100, RRD_ALGORITHM_ABSOLUTE);
        pcs->share_time.rd300 =
            rrddim_add(pcs->share_time.st, some ? "some 300" : "full 300", NULL, 1, 100, RRD_ALGORITHM_ABSOLUTE);
    }

    pcs->share_time.value10 = strtod(procfile_lineword(ff, line, 2), NULL);
    pcs->share_time.value60 = strtod(procfile_lineword(ff, line, 4), NULL);
    pcs->share_time.value300 = strtod(procfile_lineword(ff, line, 6), NULL);

    if (unlikely(!pcs->total_time.st)) {
        pcs->total_time.st = rrdset_create_localhost(
            "system",
            pcs->total_time.id,
            NULL,
            ri.family,
            NULL,
            pcs->total_time.title,
            "ms",
            PLUGIN_PROC_NAME,
            PLUGIN_PROC_MODULE_PRESSURE_NAME,
            ri.section_priority + (some ? 45 : 55),
            pressure_update_every,
            RRDSET_TYPE_LINE);
        pcs->total_time.rdtotal = rrddim_add(pcs->total_time.st, "time", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
    }

    pcs->total_time.value_total = str2ull(procfile_lineword(ff, line, 8), NULL) / 1000;
}

static void proc_pressure_do_resource_some(procfile *ff, int res_idx, size_t line) {
    proc_pressure_do_resource(ff, res_idx, line, true);
}

static void proc_pressure_do_resource_full(procfile *ff, int res_idx, size_t line) {
    proc_pressure_do_resource(ff, res_idx, line, false);
}

int do_proc_pressure(int update_every, usec_t dt) {
    int ok_count = 0;
    int i;

    static usec_t next_pressure_dt = 0;
    static const char *base_path = NULL;

    update_every = (update_every < MIN_PRESSURE_UPDATE_EVERY) ? MIN_PRESSURE_UPDATE_EVERY : update_every;
    pressure_update_every = update_every;

    if (next_pressure_dt <= dt) {
        next_pressure_dt = update_every * USEC_PER_SEC;
    } else {
        next_pressure_dt -= dt;
        return 0;
    }

    if (unlikely(!base_path))
        base_path = config_get(CONFIG_SECTION_PLUGIN_PROC_PRESSURE, "base path of pressure metrics", "/proc/pressure");

    for (i = 0; i < PRESSURE_NUM_RESOURCES; i++) {
        procfile *ff = resource_info[i].pf;
        int do_some = resources[i].some.enabled, do_full = resources[i].full.enabled;

        if (!resources[i].some.available && !resources[i].full.available)
            continue;

        if (unlikely(!ff)) {
            char filename[FILENAME_MAX + 1];
            char config_key[CONFIG_MAX_NAME + 1];

            snprintfz(filename
                      , FILENAME_MAX
                      , "%s%s/%s"
                      , netdata_configured_host_prefix
                      , base_path
                      , resource_info[i].name);

            do_some = resources[i].some.available ? CONFIG_BOOLEAN_YES : CONFIG_BOOLEAN_NO;
            do_full = resources[i].full.available ? CONFIG_BOOLEAN_YES : CONFIG_BOOLEAN_NO;

            snprintfz(config_key, CONFIG_MAX_NAME, "enable %s some pressure", resource_info[i].name);
            do_some = config_get_boolean(CONFIG_SECTION_PLUGIN_PROC_PRESSURE, config_key, do_some);
            resources[i].some.enabled = do_some;

            snprintfz(config_key, CONFIG_MAX_NAME, "enable %s full pressure", resource_info[i].name);
            do_full = config_get_boolean(CONFIG_SECTION_PLUGIN_PROC_PRESSURE, config_key, do_full);
            resources[i].full.enabled = do_full;

            if (!do_full && !do_some) {
                resources[i].some.available = false;
                resources[i].full.available = false;
                continue;
            }

            ff = procfile_open(filename, " =", PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
            if (unlikely(!ff)) {
                // PSI IRQ was added recently (https://github.com/torvalds/linux/commit/52b1364ba0b105122d6de0e719b36db705011ac1) 
                if (strcmp(resource_info[i].name, "irq") != 0)
                    collector_error("Cannot read pressure information from %s.", filename);
                resources[i].some.available = false;
                resources[i].full.available = false;
                continue;
            }
        }

        ff = procfile_readall(ff);
        resource_info[i].pf = ff;
        if (unlikely(!ff))
            continue;

        size_t lines = procfile_lines(ff);
        if (unlikely(lines < 1)) {
            collector_error("%s has no lines.", procfile_filename(ff));
            continue;
        }

        for(size_t l = 0; l < lines ;l++) {
            const char *key = procfile_lineword(ff, l, 0);
            if(strcmp(key, "some") == 0) {
                if(do_some) {
                    proc_pressure_do_resource_some(ff, i, l);
                    update_pressure_charts(&resources[i].some);
                    ok_count++;
                }
            }
            else if(strcmp(key, "full") == 0) {
                if(do_full) {
                    proc_pressure_do_resource_full(ff, i, l);
                    update_pressure_charts(&resources[i].full);
                    ok_count++;
                }
            }
        }
    }

    if(!ok_count)
        return 1;

    return 0;
}