From 245841a872a09f6325bb830247533a3671400085 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Mon, 25 Dec 2023 19:21:07 +0800 Subject: [PATCH] core/unit: don't log 0 values in unit_log_resources Prompted by #30573 --- src/core/unit.c | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/core/unit.c b/src/core/unit.c index c5d8483791..011261a7fc 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -2398,26 +2398,30 @@ static int unit_log_resources(Unit *u) { } for (CGroupMemoryAccountingMetric metric = 0; metric <= _CGROUP_MEMORY_ACCOUNTING_METRIC_CACHED_LAST; metric++) { - uint64_t v = UINT64_MAX; + uint64_t value = UINT64_MAX; assert(memory_fields[metric].journal_field); assert(memory_fields[metric].message_suffix); - (void) unit_get_memory_accounting(u, metric, &v); - if (v == UINT64_MAX) + (void) unit_get_memory_accounting(u, metric, &value); + if (value == UINT64_MAX) continue; - if (asprintf(&t, "%s=%" PRIu64, memory_fields[metric].journal_field, v) < 0) + if (asprintf(&t, "%s=%" PRIu64, memory_fields[metric].journal_field, value) < 0) return log_oom(); iovec[n_iovec++] = IOVEC_MAKE_STRING(TAKE_PTR(t)); + /* If value is 0, we don't log it in the MESSAGE= field. */ + if (value == 0) + continue; + if (strextendf_with_separator(&message, ", ", "%s %s", - FORMAT_BYTES(v), memory_fields[metric].message_suffix) < 0) + FORMAT_BYTES(value), memory_fields[metric].message_suffix) < 0) return log_oom(); log_level = raise_level(log_level, - v > MENTIONWORTHY_MEMORY_BYTES, - v > NOTICEWORTHY_MEMORY_BYTES); + value > MENTIONWORTHY_MEMORY_BYTES, + value > NOTICEWORTHY_MEMORY_BYTES); } for (CGroupIOAccountingMetric k = 0; k < _CGROUP_IO_ACCOUNTING_METRIC_MAX; k++) { @@ -2434,6 +2438,10 @@ static int unit_log_resources(Unit *u) { return log_oom(); iovec[n_iovec++] = IOVEC_MAKE_STRING(TAKE_PTR(t)); + /* If value is 0, we don't log it in the MESSAGE= field. */ + if (value == 0) + continue; + /* Format the IO accounting data for inclusion in the human language message string, but only * for the bytes counters (and not for the operations counters) */ if (io_fields[k].message_suffix) { @@ -2461,6 +2469,10 @@ static int unit_log_resources(Unit *u) { return log_oom(); iovec[n_iovec++] = IOVEC_MAKE_STRING(TAKE_PTR(t)); + /* If value is 0, we don't log it in the MESSAGE= field. */ + if (value == 0) + continue; + /* Format the IP accounting data for inclusion in the human language message string, but only * for the bytes counters (and not for the packets counters) */ if (ip_fields[m].message_suffix) {