]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core/unit: don't log 0 values in unit_log_resources
authorMike Yuan <me@yhndnzj.com>
Mon, 25 Dec 2023 11:21:07 +0000 (19:21 +0800)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 25 Dec 2023 14:19:44 +0000 (23:19 +0900)
Prompted by #30573

src/core/unit.c

index c5d84837912663a7eb5cd54dd335be6f56f14dd4..011261a7fcaa92840785d1a7089ab7d2a9c8efc2 100644 (file)
@@ -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) {