summaryrefslogtreecommitdiffstats
path: root/collectors/debugfs.plugin/debugfs_zswap.c
blob: a2991b9f148b2c66f80f11474c213d71bae416e3 (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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// SPDX-License-Identifier: GPL-3.0-or-later

#include "debugfs_plugin.h"

static long system_page_size = 4096;

static collected_number pages_to_bytes(collected_number value)
{
    return value * system_page_size;
}

struct netdata_zswap_metric {
    const char *filename;

    const char *chart_id;
    const char *title;
    const char *units;
    RRDSET_TYPE charttype;
    int prio;
    const char *dimension;
    RRD_ALGORITHM algorithm;
    int divisor;

    int enabled;
    int chart_created;

    collected_number value;
    collected_number (*convertv)(collected_number v);
};

static struct netdata_zswap_metric zswap_calculated_metrics[] = {
    {.filename = "",
     .chart_id = "pool_compression_ratio",
     .dimension = "compression_ratio",
     .units = "ratio",
     .title = "Zswap compression ratio",
     .algorithm = RRD_ALGORITHM_ABSOLUTE,
     .charttype = RRDSET_TYPE_LINE,
     .enabled = CONFIG_BOOLEAN_YES,
     .chart_created = CONFIG_BOOLEAN_NO,
     .prio = NETDATA_CHART_PRIO_SYSTEM_ZSWAP_COMPRESS_RATIO,
     .divisor = 100,
     .convertv = NULL,
     .value = -1},
};

enum netdata_zswap_calculated {
    NETDATA_ZSWAP_COMPRESSION_RATIO_CHART,
};

enum netdata_zwap_independent {
    NETDATA_ZSWAP_POOL_TOTAL_SIZE,
    NETDATA_ZSWAP_STORED_PAGES,
    NETDATA_ZSWAP_POOL_LIMIT_HIT,
    NETDATA_ZSWAP_WRITTEN_BACK_PAGES,
    NETDATA_ZSWAP_SAME_FILLED_PAGES,
    NETDATA_ZSWAP_DUPLICATE_ENTRY,

    // Terminator
    NETDATA_ZSWAP_SITE_END
};

static struct netdata_zswap_metric zswap_independent_metrics[] = {
    // https://elixir.bootlin.com/linux/latest/source/mm/zswap.c
    {.filename = "/sys/kernel/debug/zswap/pool_total_size",
     .chart_id = "pool_compressed_size",
     .dimension = "compressed_size",
     .units = "bytes",
     .title = "Zswap compressed bytes currently stored",
     .algorithm = RRD_ALGORITHM_ABSOLUTE,
     .charttype = RRDSET_TYPE_AREA,
     .enabled = CONFIG_BOOLEAN_YES,
     .chart_created = CONFIG_BOOLEAN_NO,
     .prio = NETDATA_CHART_PRIO_SYSTEM_ZSWAP_POOL_TOT_SIZE,
     .divisor = 1,
     .convertv = NULL,
     .value = -1},
    {.filename = "/sys/kernel/debug/zswap/stored_pages",
     .chart_id = "pool_raw_size",
     .dimension = "uncompressed_size",
     .units = "bytes",
     .title = "Zswap uncompressed bytes currently stored",
     .algorithm = RRD_ALGORITHM_ABSOLUTE,
     .charttype = RRDSET_TYPE_AREA,
     .enabled = CONFIG_BOOLEAN_YES,
     .chart_created = CONFIG_BOOLEAN_NO,
     .prio = NETDATA_CHART_PRIO_SYSTEM_ZSWAP_STORED_PAGE,
     .divisor = 1,
     .convertv = pages_to_bytes,
     .value = -1},
    {.filename = "/sys/kernel/debug/zswap/pool_limit_hit",
     .chart_id = "pool_limit_hit",
     .dimension = "limit",
     .units = "events/s",
     .title = "Zswap pool limit was reached",
     .algorithm = RRD_ALGORITHM_INCREMENTAL,
     .charttype = RRDSET_TYPE_LINE,
     .enabled = CONFIG_BOOLEAN_YES,
     .chart_created = CONFIG_BOOLEAN_NO,
     .prio = NETDATA_CHART_PRIO_SYSTEM_ZSWAP_POOL_LIM_HIT,
     .divisor = 1,
     .convertv = NULL,
     .value = -1},
    {.filename = "/sys/kernel/debug/zswap/written_back_pages",
     .chart_id = "written_back_raw_bytes",
     .dimension = "written_back",
     .units = "bytes/s",
     .title = "Zswap uncomressed bytes written back when pool limit was reached",
     .algorithm = RRD_ALGORITHM_INCREMENTAL,
     .charttype = RRDSET_TYPE_AREA,
     .enabled = CONFIG_BOOLEAN_YES,
     .chart_created = CONFIG_BOOLEAN_NO,
     .prio = NETDATA_CHART_PRIO_SYSTEM_ZSWAP_WRT_BACK_PAGES,
     .divisor = 1,
     .convertv = pages_to_bytes,
     .value = -1},
    {.filename = "/sys/kernel/debug/zswap/same_filled_pages",
     .chart_id = "same_filled_raw_size",
     .dimension = "same_filled",
     .units = "bytes",
     .title = "Zswap same-value filled uncompressed bytes currently stored",
     .algorithm = RRD_ALGORITHM_ABSOLUTE,
     .charttype = RRDSET_TYPE_AREA,
     .enabled = CONFIG_BOOLEAN_YES,
     .chart_created = CONFIG_BOOLEAN_NO,
     .prio = NETDATA_CHART_PRIO_SYSTEM_ZSWAP_SAME_FILL_PAGE,
     .divisor = 1,
     .convertv = pages_to_bytes,
     .value = -1},
    {.filename = "/sys/kernel/debug/zswap/duplicate_entry",
     .chart_id = "duplicate_entry",
     .dimension = "duplicate",
     .units = "entries/s",
     .title = "Zswap duplicate store was encountered",
     .algorithm = RRD_ALGORITHM_INCREMENTAL,
     .charttype = RRDSET_TYPE_LINE,
     .enabled = CONFIG_BOOLEAN_YES,
     .chart_created = CONFIG_BOOLEAN_NO,
     .prio = NETDATA_CHART_PRIO_SYSTEM_ZSWAP_DUPP_ENTRY,
     .divisor = 1,
     .convertv = NULL,
     .value = -1},

    // The terminator
    {.filename = NULL,
     .chart_id = NULL,
     .dimension = NULL,
     .units = NULL,
     .title = NULL,
     .algorithm = RRD_ALGORITHM_ABSOLUTE,
     .charttype = RRDSET_TYPE_LINE,
     .enabled = CONFIG_BOOLEAN_NO,
     .chart_created = CONFIG_BOOLEAN_NO,
     .prio = -1,
     .value = -1}};

enum netdata_zswap_rejected {
    NETDATA_ZSWAP_REJECTED_CHART,
    NETDATA_ZSWAP_REJECTED_COMPRESS_POOR,
    NETDATA_ZSWAP_REJECTED_KMEM_FAIL,
    NETDATA_ZSWAP_REJECTED_RALLOC_FAIL,
    NETDATA_ZSWAP_REJECTED_RRECLAIM_FAIL,

    // Terminator
    NETDATA_ZSWAP_REJECTED_END
};

static struct netdata_zswap_metric zswap_rejected_metrics[] = {
    {.filename = "/sys/kernel/debug/zswap/",
     .chart_id = "rejections",
     .dimension = NULL,
     .units = "rejections/s",
     .title = "Zswap rejections",
     .algorithm = RRD_ALGORITHM_INCREMENTAL,
     .charttype = RRDSET_TYPE_STACKED,
     .enabled = CONFIG_BOOLEAN_YES,
     .chart_created = CONFIG_BOOLEAN_NO,
     .prio = NETDATA_CHART_PRIO_SYSTEM_ZSWAP_REJECTS,
     .divisor = 1,
     .convertv = NULL,
     .value = -1},
    {.filename = "/sys/kernel/debug/zswap/reject_compress_poor",
     .chart_id = "reject_compress_poor",
     .dimension = "compress_poor",
     .units = NULL,
     .title = NULL,
     .algorithm = RRD_ALGORITHM_INCREMENTAL,
     .charttype = RRDSET_TYPE_STACKED,
     .enabled = CONFIG_BOOLEAN_YES,
     .chart_created = CONFIG_BOOLEAN_NO,
     .prio = NETDATA_CHART_PRIO_SYSTEM_ZSWAP_REJECTS,
     .divisor = 1,
     .convertv = NULL,
     .value = -1},
    {.filename = "/sys/kernel/debug/zswap/reject_kmemcache_fail",
     .chart_id = "reject_kmemcache_fail",
     .dimension = "kmemcache_fail",
     .units = NULL,
     .title = NULL,
     .algorithm = RRD_ALGORITHM_INCREMENTAL,
     .charttype = RRDSET_TYPE_STACKED,
     .enabled = CONFIG_BOOLEAN_YES,
     .chart_created = CONFIG_BOOLEAN_NO,
     .prio = NETDATA_CHART_PRIO_SYSTEM_ZSWAP_REJECTS,
     .divisor = 1,
     .convertv = NULL,
     .value = -1},
    {.filename = "/sys/kernel/debug/zswap/reject_alloc_fail",
     .chart_id = "reject_alloc_fail",
     .dimension = "alloc_fail",
     .units = NULL,
     .title = NULL,
     .algorithm = RRD_ALGORITHM_INCREMENTAL,
     .charttype = RRDSET_TYPE_STACKED,
     .enabled = CONFIG_BOOLEAN_YES,
     .chart_created = CONFIG_BOOLEAN_NO,
     .prio = NETDATA_CHART_PRIO_SYSTEM_ZSWAP_REJECTS,
     .divisor = 1,
     .convertv = NULL,
     .value = -1},
    {.filename = "/sys/kernel/debug/zswap/reject_reclaim_fail",
     .chart_id = "reject_reclaim_fail",
     .dimension = "reclaim_fail",
     .units = NULL,
     .title = NULL,
     .algorithm = RRD_ALGORITHM_INCREMENTAL,
     .charttype = RRDSET_TYPE_STACKED,
     .enabled = CONFIG_BOOLEAN_YES,
     .chart_created = CONFIG_BOOLEAN_NO,
     .prio = NETDATA_CHART_PRIO_SYSTEM_ZSWAP_REJECTS,
     .divisor = 1,
     .convertv = NULL,
     .value = -1},

    // The terminator
    {.filename = NULL,
     .chart_id = NULL,
     .dimension = NULL,
     .units = NULL,
     .title = NULL,
     .algorithm = RRD_ALGORITHM_ABSOLUTE,
     .charttype = RRDSET_TYPE_STACKED,
     .enabled = CONFIG_BOOLEAN_NO,
     .chart_created = CONFIG_BOOLEAN_NO,
     .prio = -1,
     .value = -1}};

int zswap_collect_data(struct netdata_zswap_metric *metric)
{
    char filename[FILENAME_MAX + 1];
    snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, metric->filename);

    if (read_single_number_file(filename, (unsigned long long *)&metric->value)) {
        error("Cannot read file %s", filename);
        return 1;
    }

    if (metric->convertv)
        metric->value = metric->convertv(metric->value);

    return 0;
}

static void
zswap_send_chart(struct netdata_zswap_metric *metric, int update_every, const char *name, const char *option)
{
    fprintf(
        stdout,
        "CHART system.zswap_%s '' '%s' '%s' 'zswap' '' '%s' %d %d '%s' 'debugfs.plugin' '%s'\n",
        metric->chart_id,
        metric->title,
        metric->units,
        debugfs_rrdset_type_name(metric->charttype),
        metric->prio,
        update_every,
        (!option) ? "" : option,
        name);
}

static void zswap_send_dimension(struct netdata_zswap_metric *metric)
{
    int div = metric->divisor > 0 ? metric->divisor : 1;
    fprintf(
        stdout,
        "DIMENSION '%s' '%s' %s 1 %d ''\n",
        metric->dimension,
        metric->dimension,
        debugfs_rrd_algorithm_name(metric->algorithm),
        div);
}

static void zswap_send_begin(struct netdata_zswap_metric *metric)
{
    fprintf(stdout, "BEGIN system.zswap_%s\n", metric->chart_id);
}

static void zswap_send_set(struct netdata_zswap_metric *metric)
{
    fprintf(stdout, "SET %s = %lld\n", metric->dimension, metric->value);
}

static void zswap_send_end_and_flush()
{
    fprintf(stdout, "END\n");
    fflush(stdout);
}

static void zswap_independent_chart(struct netdata_zswap_metric *metric, int update_every, const char *name)
{
    if (unlikely(!metric->chart_created)) {
        metric->chart_created = CONFIG_BOOLEAN_YES;

        zswap_send_chart(metric, update_every, name, NULL);
        zswap_send_dimension(metric);
    }

    zswap_send_begin(metric);
    zswap_send_set(metric);
    zswap_send_end_and_flush();
}

void zswap_reject_chart(int update_every, const char *name)
{
    struct netdata_zswap_metric *metric = &zswap_rejected_metrics[NETDATA_ZSWAP_REJECTED_CHART];

    if (unlikely(!metric->chart_created)) {
        metric->chart_created = CONFIG_BOOLEAN_YES;

        zswap_send_chart(metric, update_every, name, NULL);
        for (int i = NETDATA_ZSWAP_REJECTED_COMPRESS_POOR; zswap_rejected_metrics[i].filename; i++) {
            metric = &zswap_rejected_metrics[i];
            if (likely(metric->enabled))
                zswap_send_dimension(metric);
        }
    }

    metric = &zswap_rejected_metrics[NETDATA_ZSWAP_REJECTED_CHART];
    zswap_send_begin(metric);
    for (int i = NETDATA_ZSWAP_REJECTED_COMPRESS_POOR; zswap_rejected_metrics[i].filename; i++) {
        metric = &zswap_rejected_metrics[i];
        if (likely(metric->enabled))
            zswap_send_set(metric);
    }
    zswap_send_end_and_flush();
}

static void zswap_obsolete_charts(int update_every, const char *name)
{
    struct netdata_zswap_metric *metric = NULL;

    for (int i = 0; zswap_independent_metrics[i].filename; i++) {
        metric = &zswap_independent_metrics[i];
        if (likely(metric->chart_created))
            zswap_send_chart(metric, update_every, name, "obsolete");
    }

    metric = &zswap_rejected_metrics[NETDATA_ZSWAP_REJECTED_CHART];
    if (likely(metric->chart_created))
        zswap_send_chart(metric, update_every, name, "obsolete");

    metric = &zswap_calculated_metrics[NETDATA_ZSWAP_COMPRESSION_RATIO_CHART];
    if (likely(metric->chart_created))
        zswap_send_chart(metric, update_every, name, "obsolete");
}

#define ZSWAP_STATE_SIZE 1 // Y or N
static int debugfs_is_zswap_enabled()
{
    char filename[FILENAME_MAX + 1];
    snprintfz(filename, FILENAME_MAX, "/sys/module/zswap/parameters/enabled"); // host prefix is not needed here
    char state[ZSWAP_STATE_SIZE + 1];

    int ret = read_file(filename, state, ZSWAP_STATE_SIZE);

    if (unlikely(!ret && !strcmp(state, "Y"))) {
        return 0;
    }
    return 1;
}

int do_debugfs_zswap(int update_every, const char *name)
{
    static int check_if_enabled = 1;

    if (likely(check_if_enabled && debugfs_is_zswap_enabled())) {
        info("Zswap is disabled");
        return 1;
    }

    check_if_enabled = 0;

    system_page_size = sysconf(_SC_PAGESIZE);
    struct netdata_zswap_metric *metric = NULL;
    int enabled = 0;

    for (int i = 0; zswap_independent_metrics[i].filename; i++) {
        metric = &zswap_independent_metrics[i];
        if (unlikely(!metric->enabled))
            continue;
        if (unlikely(!(metric->enabled = !zswap_collect_data(metric))))
            continue;
        zswap_independent_chart(metric, update_every, name);
        enabled++;
    }

    struct netdata_zswap_metric *metric_size = &zswap_independent_metrics[NETDATA_ZSWAP_POOL_TOTAL_SIZE];
    struct netdata_zswap_metric *metric_raw_size = &zswap_independent_metrics[NETDATA_ZSWAP_STORED_PAGES];
    if (metric_size->enabled && metric_raw_size->enabled) {
        metric = &zswap_calculated_metrics[NETDATA_ZSWAP_COMPRESSION_RATIO_CHART];
        metric->value = 0;
        if (metric_size->value > 0)
            metric->value =
                (collected_number)((NETDATA_DOUBLE)metric_raw_size->value / (NETDATA_DOUBLE)metric_size->value * 100);
        zswap_independent_chart(metric, update_every, name);
    }

    int enabled_rejected = 0;
    for (int i = NETDATA_ZSWAP_REJECTED_COMPRESS_POOR; zswap_rejected_metrics[i].filename; i++) {
        metric = &zswap_rejected_metrics[i];
        if (unlikely(!metric->enabled))
            continue;
        if (unlikely(!(metric->enabled = !zswap_collect_data(metric))))
            continue;
        enabled++;
        enabled_rejected++;
    }

    if (likely(enabled_rejected > 0))
        zswap_reject_chart(update_every, name);

    if (unlikely(!enabled)) {
        zswap_obsolete_charts(update_every, name);
        return 1;
    }

    return 0;
}