fix(debugfs/zswap): don't collect metrics if Zswap is disabled (#15054)
Ilya Mashchenko committed
May 15, 2023 at 22:55 UTC
cb4c7201b2038f6279f2bfb83d5f91cf4bd57c23
1 file changed
+30
-25
collectors/debugfs.plugin/debugfs_zswap.c
+30
-25
@@ -247,37 +247,18 @@ static struct netdata_zswap_metric zswap_rejected_metrics[] = {
247
248
int zswap_collect_data(struct netdata_zswap_metric *metric)
249
{
250
- int fd;
251
- int ret = 0;
252
- char buffer[512];
253
-
250
char filename[FILENAME_MAX + 1];
251
snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, metric->filename);
256
- // we are not using profile_open/procfile_read, because they will generate error during runtime.
257
- fd = open(filename, O_RDONLY, 0444);
258
- if (fd < 0) {
259
- error("Cannot open file %s", filename);
260
- return -1;
261
- }
252
263
- ssize_t r = read(fd, buffer, 511);
264
- // We expect at list 1 character
265
- if (r < 2) {
266
- error("Cannot parse file %s", filename);
267
- ret = -1;
268
- goto zswap_collect_end;
253
+ if (read_single_number_file(filename, (unsigned long long *)&metric->value)) {
254
+ error("Cannot read file %s", filename);
255
+ return 1;
256
}
257
271
- // We discard breakline
272
- buffer[r - 1] = '\0';
273
- metric->value = str2ll(buffer, NULL);
274
-
258
if (metric->convertv)
259
metric->value = metric->convertv(metric->value);
260
278
-zswap_collect_end:
279
- close(fd);
280
- return ret;
261
+ return 0;
262
}
263
264
static void
@@ -382,8 +363,32 @@ static void zswap_obsolete_charts(int update_every, const char *name)
363
zswap_send_chart(metric, update_every, name, "obsolete");
364
}
365
366
+#define ZSWAP_STATE_SIZE 1 // Y or N
367
+static int debugfs_is_zswap_enabled()
368
+{
369
+ char filename[FILENAME_MAX + 1];
370
+ snprintfz(filename, FILENAME_MAX, "/sys/module/zswap/parameters/enabled"); // host prefix is not needed here
371
+ char state[ZSWAP_STATE_SIZE + 1];
372
+
373
+ int ret = read_file(filename, state, ZSWAP_STATE_SIZE);
374
+
375
+ if (unlikely(!ret && !strcmp(state, "Y"))) {
376
+ return 0;
377
+ }
378
+ return 1;
379
+}
380
+
381
int do_debugfs_zswap(int update_every, const char *name)
382
{
383
+ static int check_if_enabled = 1;
384
+
385
+ if (likely(check_if_enabled && debugfs_is_zswap_enabled())) {
386
+ info("Zswap is disabled");
387
+ return 1;
388
+ }
389
+
390
+ check_if_enabled = 0;
391
+
392
system_page_size = sysconf(_SC_PAGESIZE);
393
struct netdata_zswap_metric *metric = NULL;
394
int enabled = 0;
@@ -423,9 +428,9 @@ int do_debugfs_zswap(int update_every, const char *name)
428
if (likely(enabled_rejected > 0))
429
zswap_reject_chart(update_every, name);
430
426
- if (!enabled) {
431
+ if (unlikely(!enabled)) {
432
zswap_obsolete_charts(update_every, name);
428
- return -1;
433
+ return 1;
434
}
435
436
return 0;