core/cgroup: actually make use of the cached accounting values
authorMike Yuan <me@yhndnzj.com>
Thu, 13 Jun 2024 09:01:03 +0000 (11:01 +0200)
committerLuca Boccassi <luca.boccassi@gmail.com>
Fri, 19 Jul 2024 22:33:14 +0000 (00:33 +0200)
If cgroup is already gone, i.e. CGRuntime.cgroup_path is NULL,
do not return -ENODATA prematurely, but check for cached values
first.

For #33149

(cherry picked from commit 3849d1f56b7ff9db94656ca84156eb9fe1574ecb)

src/core/cgroup.c

index 34fd2a250c41e7c4cb2c666259409eb8484086a3..81d044b12330daa7a8f2d5e1ec993114dd12fe07 100644 (file)
@@ -4646,11 +4646,11 @@ int unit_get_cpu_usage(Unit *u, nsec_t *ret) {
          * started. If the cgroup has been removed already, returns the last cached value. To cache the value, simply
          * call this function with a NULL return value. */
 
-        CGroupRuntime *crt = unit_get_cgroup_runtime(u);
-        if (!crt || !crt->cgroup_path)
+        if (!UNIT_CGROUP_BOOL(u, cpu_accounting))
                 return -ENODATA;
 
-        if (!UNIT_CGROUP_BOOL(u, cpu_accounting))
+        CGroupRuntime *crt = unit_get_cgroup_runtime(u);
+        if (!crt)
                 return -ENODATA;
 
         r = unit_get_cpu_usage_raw(u, &ns);
@@ -4694,7 +4694,7 @@ int unit_get_ip_accounting(
                 return -ENODATA;
 
         CGroupRuntime *crt = unit_get_cgroup_runtime(u);
-        if (!crt || !crt->cgroup_path)
+        if (!crt)
                 return -ENODATA;
 
         fd = IN_SET(metric, CGROUP_IP_INGRESS_BYTES, CGROUP_IP_INGRESS_PACKETS) ?
@@ -4869,7 +4869,7 @@ int unit_get_io_accounting(
                 return -ENODATA;
 
         CGroupRuntime *crt = unit_get_cgroup_runtime(u);
-        if (!crt || !crt->cgroup_path)
+        if (!crt)
                 return -ENODATA;
 
         if (allow_cache && crt->io_accounting_last[metric] != UINT64_MAX)