summaryrefslogtreecommitdiffstats
path: root/collectors/proc.plugin/sys_devices_pci_aer.c
blob: 2961951826139a3527222d82421818ea862d195e (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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// SPDX-License-Identifier: GPL-3.0-or-later

#include "plugin_proc.h"

static char *pci_aer_dirname = NULL;

typedef enum __attribute__((packed)) {
    AER_DEV_NONFATAL                    = (1 << 0),
    AER_DEV_CORRECTABLE                 = (1 << 1),
    AER_DEV_FATAL                       = (1 << 2),
    AER_ROOTPORT_TOTAL_ERR_COR          = (1 << 3),
    AER_ROOTPORT_TOTAL_ERR_FATAL        = (1 << 4),
} AER_TYPE;

struct aer_value {
    kernel_uint_t count;
    RRDDIM *rd;
};

struct aer_entry {
    bool updated;

    STRING *name;
    AER_TYPE type;

    procfile *ff;
    DICTIONARY *values;

    RRDSET *st;
};

DICTIONARY *aer_root = NULL;

static bool aer_value_conflict_callback(const DICTIONARY_ITEM *item __maybe_unused, void *old_value, void *new_value, void *data __maybe_unused) {
	struct aer_value *v = old_value;
    struct aer_value *nv = new_value;

    v->count = nv->count;

    return false;
}

static void aer_insert_callback(const DICTIONARY_ITEM *item __maybe_unused, void *value, void *data __maybe_unused) {
    struct aer_entry *a = value;
    a->values = dictionary_create(DICT_OPTION_SINGLE_THREADED|DICT_OPTION_DONT_OVERWRITE_VALUE);
    dictionary_register_conflict_callback(a->values, aer_value_conflict_callback, NULL);
}

static void add_pci_aer(const char *base_dir, const char *d_name, AER_TYPE type) {
    char buffer[FILENAME_MAX + 1];
    snprintfz(buffer, FILENAME_MAX, "%s/%s", base_dir, d_name);
    struct aer_entry *a = dictionary_set(aer_root, buffer, NULL, sizeof(struct aer_entry));

    if(!a->name)
        a->name = string_strdupz(d_name);

    a->type = type;
}

static bool recursively_find_pci_aer(AER_TYPE types, const char *base_dir, const char *d_name, int depth) {
    if(depth > 100)
        return false;

    char buffer[FILENAME_MAX + 1];
    snprintfz(buffer, FILENAME_MAX, "%s/%s", base_dir, d_name);
    DIR *dir = opendir(buffer);
    if(unlikely(!dir)) {
        collector_error("Cannot read PCI_AER directory '%s'", buffer);
        return true;
    }

    struct dirent *de = NULL;
    while((de = readdir(dir))) {
        if(de->d_type == DT_DIR) {
            if(de->d_name[0] == '.')
                continue;

            recursively_find_pci_aer(types, buffer, de->d_name, depth + 1);
        }
        else if(de->d_type == DT_REG) {
            if((types & AER_DEV_NONFATAL) && strcmp(de->d_name, "aer_dev_nonfatal") == 0) {
                add_pci_aer(buffer, de->d_name, AER_DEV_NONFATAL);
            }
            else if((types & AER_DEV_CORRECTABLE) && strcmp(de->d_name, "aer_dev_correctable") == 0) {
                add_pci_aer(buffer, de->d_name, AER_DEV_CORRECTABLE);
            }
            else if((types & AER_DEV_FATAL) && strcmp(de->d_name, "aer_dev_fatal") == 0) {
                add_pci_aer(buffer, de->d_name, AER_DEV_FATAL);
            }
            else if((types & AER_ROOTPORT_TOTAL_ERR_COR) && strcmp(de->d_name, "aer_rootport_total_err_cor") == 0) {
                add_pci_aer(buffer, de->d_name, AER_ROOTPORT_TOTAL_ERR_COR);
            }
            else if((types & AER_ROOTPORT_TOTAL_ERR_FATAL) && strcmp(de->d_name, "aer_rootport_total_err_fatal") == 0) {
                add_pci_aer(buffer, de->d_name, AER_ROOTPORT_TOTAL_ERR_FATAL);
            }
        }
    }
    closedir(dir);
    return true;
}

static void find_all_pci_aer(AER_TYPE types) {
    char name[FILENAME_MAX + 1];
    snprintfz(name, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/devices");
    pci_aer_dirname = config_get("plugin:proc:/sys/devices/pci/aer", "directory to monitor", name);

    DIR *dir = opendir(pci_aer_dirname);
    if(unlikely(!dir)) {
        collector_error("Cannot read PCI_AER directory '%s'", pci_aer_dirname);
        return;
    }

    struct dirent *de = NULL;
    while((de = readdir(dir))) {
        if(de->d_type == DT_DIR && de->d_name[0] == 'p' && de->d_name[1] == 'c' && de->d_name[2] == 'i' && isdigit(de->d_name[3]))
            recursively_find_pci_aer(types, pci_aer_dirname, de->d_name, 1);
    }
    closedir(dir);
}

static void read_pci_aer_values(const char *filename, struct aer_entry *t) {
    t->updated = false;

    if(unlikely(!t->ff)) {
        t->ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
        if(unlikely(!t->ff))
            return;
    }

    t->ff = procfile_readall(t->ff);
    if(unlikely(!t->ff || procfile_lines(t->ff) < 1 || procfile_linewords(t->ff, 0) < 1))
        return;

    size_t lines = procfile_lines(t->ff);
    for(size_t l = 0; l < lines ; l++) {
        if(procfile_linewords(t->ff, l) != 2)
            continue;

        struct aer_value v = {
                .count = str2ull(procfile_lineword(t->ff, l, 1), NULL)
        };

        char *key = procfile_lineword(t->ff, l, 0);
        if(!key || !*key || (key[0] == 'T' && key[1] == 'O' && key[2] == 'T' && key[3] == 'A' && key[4] == 'L' && key[5] == '_'))
            continue;

        dictionary_set(t->values, key, &v, sizeof(v));
    }

    t->updated = true;
}

static void read_pci_aer_count(const char *filename, struct aer_entry *t) {
    t->updated = false;

    if(unlikely(!t->ff)) {
        t->ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
        if(unlikely(!t->ff))
            return;
    }

    t->ff = procfile_readall(t->ff);
    if(unlikely(!t->ff || procfile_lines(t->ff) < 1 || procfile_linewords(t->ff, 0) < 1))
        return;

    struct aer_value v = {
            .count = str2ull(procfile_lineword(t->ff, 0, 0), NULL)
    };
    dictionary_set(t->values, "count", &v, sizeof(v));
    t->updated = true;
}

static void add_label_from_link(struct aer_entry *a, const char *path, const char *link) {
    char name[FILENAME_MAX + 1];
    strncpyz(name, path, FILENAME_MAX);
    char *slash = strrchr(name, '/');
    if(slash)
        *slash = '\0';

    char name2[FILENAME_MAX + 1];
    snprintfz(name2, FILENAME_MAX, "%s/%s", name, link);

    ssize_t len = readlink(name2, name, FILENAME_MAX);
    if(len != -1) {
        name[len] = '\0';  // Null-terminate the string
        slash = strrchr(name, '/');
        if(slash) slash++;
        else slash = name;
        rrdlabels_add(a->st->rrdlabels, link, slash, RRDLABEL_SRC_AUTO);
    }
}

int do_proc_sys_devices_pci_aer(int update_every, usec_t dt __maybe_unused) {
    if(unlikely(!aer_root)) {
        int do_root_ports = CONFIG_BOOLEAN_AUTO;
        int do_pci_slots = CONFIG_BOOLEAN_NO;

        char buffer[100 + 1] = "";
        rrdlabels_get_value_strcpyz(localhost->rrdlabels, buffer, 100, "_virtualization");
        if(strcmp(buffer, "none") != 0) {
            // no need to run on virtualized environments
            do_root_ports = CONFIG_BOOLEAN_NO;
            do_pci_slots = CONFIG_BOOLEAN_NO;
        }

        do_root_ports = config_get_boolean("plugin:proc:/sys/class/pci/aer", "enable root ports", do_root_ports);
        do_pci_slots = config_get_boolean("plugin:proc:/sys/class/pci/aer", "enable pci slots", do_pci_slots);

        if(!do_root_ports && !do_pci_slots)
            return 1;

        aer_root = dictionary_create(DICT_OPTION_SINGLE_THREADED | DICT_OPTION_DONT_OVERWRITE_VALUE);
        dictionary_register_insert_callback(aer_root, aer_insert_callback, NULL);

        AER_TYPE types = ((do_root_ports) ? (AER_ROOTPORT_TOTAL_ERR_COR|AER_ROOTPORT_TOTAL_ERR_FATAL) : 0) |
                ((do_pci_slots) ? (AER_DEV_FATAL|AER_DEV_NONFATAL|AER_DEV_CORRECTABLE) : 0);

        find_all_pci_aer(types);

        if(!dictionary_entries(aer_root))
            return 1;
    }

	struct aer_entry *a;
    dfe_start_read(aer_root, a) {
        switch(a->type) {
            case AER_DEV_NONFATAL:
            case AER_DEV_FATAL:
            case AER_DEV_CORRECTABLE:
                read_pci_aer_values(a_dfe.name, a);
                break;

            case AER_ROOTPORT_TOTAL_ERR_COR:
            case AER_ROOTPORT_TOTAL_ERR_FATAL:
                read_pci_aer_count(a_dfe.name, a);
                break;
        }

        if(!a->updated)
            continue;

        if(!a->st) {
            const char *title;
            const char *context;

            switch(a->type) {
                case AER_DEV_NONFATAL:
                    title = "PCI Advanced Error Reporting (AER) Non-Fatal Errors";
                    context = "pci.aer_nonfatal";
                    break;

                case AER_DEV_FATAL:
                    title = "PCI Advanced Error Reporting (AER) Fatal Errors";
                    context = "pci.aer_fatal";
                    break;

                case AER_DEV_CORRECTABLE:
                    title = "PCI Advanced Error Reporting (AER) Correctable Errors";
                    context = "pci.aer_correctable";
                    break;

                case AER_ROOTPORT_TOTAL_ERR_COR:
                    title = "PCI Root-Port Advanced Error Reporting (AER) Correctable Errors";
                    context = "pci.rootport_aer_correctable";
                    break;

                case AER_ROOTPORT_TOTAL_ERR_FATAL:
                    title = "PCI Root-Port Advanced Error Reporting (AER) Fatal Errors";
                    context = "pci.rootport_aer_fatal";
                    break;

                default:
                    title = "Unknown PCI Advanced Error Reporting";
                    context = "pci.unknown_aer";
                    break;
            }

            char id[RRD_ID_LENGTH_MAX + 1];
            char nm[RRD_ID_LENGTH_MAX + 1];
            size_t len = strlen(pci_aer_dirname);

            const char *fname = a_dfe.name;
            if(strncmp(a_dfe.name, pci_aer_dirname, len) == 0)
                fname = &a_dfe.name[len];

            if(*fname == '/')
                fname++;

            snprintfz(id, RRD_ID_LENGTH_MAX, "%s_%s", &context[4], fname);
            char *slash = strrchr(id, '/');
            if(slash)
                *slash = '\0';

            netdata_fix_chart_id(id);

            snprintfz(nm, RRD_ID_LENGTH_MAX, "%s", fname);
            slash = strrchr(nm, '/');
            if(slash)
                *slash = '\0';

            a->st = rrdset_create_localhost(
                    "pci"
            		, id
            		, NULL
            		, "aer"
            		, context
            		, title
            		, "errors/s"
            		, PLUGIN_PROC_NAME
            		, "/sys/devices/pci/aer"
            		, NETDATA_CHART_PRIO_PCI_AER
            		, update_every
            		, RRDSET_TYPE_LINE
            );

            rrdlabels_add(a->st->rrdlabels, "device", nm, RRDLABEL_SRC_AUTO);
            add_label_from_link(a, a_dfe.name, "driver");

            struct aer_value *v;
            dfe_start_read(a->values, v) {
                v->rd = rrddim_add(a->st, v_dfe.name, NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
            }
            dfe_done(v);
        }

        struct aer_value *v;
        dfe_start_read(a->values, v) {
            if(unlikely(!v->rd))
                v->rd = rrddim_add(a->st, v_dfe.name, NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);

            rrddim_set_by_pointer(a->st, v->rd, (collected_number)v->count);
        }
        dfe_done(v);

        rrdset_done(a->st);
    }
    dfe_done(a);

    return 0;
}