chiark / gitweb /
core/cgroup: add a helper macro for a common pattern (#6926)
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 27 Sep 2017 15:54:06 +0000 (17:54 +0200)
committerSven Eden <yamakuzure@gmx.net>
Wed, 22 Nov 2017 07:25:11 +0000 (08:25 +0100)
src/core/cgroup.c
src/core/cgroup.h

index c1e51a7f607b66a3a6f1c592065bbc6d4c5c323c..092c15148c855a5b3aecebff6b4712addb7499ec 100644 (file)
@@ -2145,16 +2145,12 @@ int manager_notify_cgroup_empty(Manager *m, const char *cgroup) {
 #if 0 /// UNNEEDED by elogind
 int unit_get_memory_current(Unit *u, uint64_t *ret) {
         _cleanup_free_ char *v = NULL;
-        CGroupContext *cc;
         int r;
 
         assert(u);
         assert(ret);
 
-        cc = unit_get_cgroup_context(u);
-        if (!cc)
-                return -ENODATA;
-        if (!cc->memory_accounting)
+        if (!UNIT_CGROUP_BOOL(u, memory_accounting))
                 return -ENODATA;
 
         if (!u->cgroup_path)
@@ -2180,16 +2176,12 @@ int unit_get_memory_current(Unit *u, uint64_t *ret) {
 
 int unit_get_tasks_current(Unit *u, uint64_t *ret) {
         _cleanup_free_ char *v = NULL;
-        CGroupContext *cc;
         int r;
 
         assert(u);
         assert(ret);
 
-        cc = unit_get_cgroup_context(u);
-        if (!cc)
-                return -ENODATA;
-        if (!cc->tasks_accounting)
+        if (!UNIT_CGROUP_BOOL(u, tasks_accounting))
                 return -ENODATA;
 
         if (!u->cgroup_path)
@@ -2258,7 +2250,6 @@ static int unit_get_cpu_usage_raw(Unit *u, nsec_t *ret) {
 }
 
 int unit_get_cpu_usage(Unit *u, nsec_t *ret) {
-        CGroupContext *cc;
         nsec_t ns;
         int r;
 
@@ -2268,10 +2259,7 @@ 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. */
 
-        cc = unit_get_cgroup_context(u);
-        if (!cc)
-                return -ENODATA;
-        if (!cc->cpu_accounting)
+        if (!UNIT_CGROUP_BOOL(u, cpu_accounting))
                 return -ENODATA;
 
         r = unit_get_cpu_usage_raw(u, &ns);
@@ -2303,7 +2291,6 @@ int unit_get_ip_accounting(
                 CGroupIPAccountingMetric metric,
                 uint64_t *ret) {
 
-        CGroupContext *cc;
         uint64_t value;
         int fd, r;
 
@@ -2319,10 +2306,7 @@ int unit_get_ip_accounting(
         if (u->type == UNIT_SLICE)
                 return -ENODATA;
 
-        cc = unit_get_cgroup_context(u);
-        if (!cc)
-                return -ENODATA;
-        if (!cc->ip_accounting)
+        if (!UNIT_CGROUP_BOOL(u, ip_accounting))
                 return -ENODATA;
 
         fd = IN_SET(metric, CGROUP_IP_INGRESS_BYTES, CGROUP_IP_INGRESS_PACKETS) ?
@@ -2382,18 +2366,6 @@ int unit_reset_ip_accounting(Unit *u) {
         return r < 0 ? r : q;
 }
 
-bool unit_cgroup_delegate(Unit *u) {
-        CGroupContext *c;
-
-        assert(u);
-
-        c = unit_get_cgroup_context(u);
-        if (!c)
-                return false;
-
-        return c->delegate;
-}
-
 void unit_invalidate_cgroup(Unit *u, CGroupMask m) {
         assert(u);
 
index a7100d1fcf657797de6d15c8140c6b5660405ee3..2d09c4cc3c71130ed37d9054dc048a94b2ae49aa 100644 (file)
@@ -198,7 +198,11 @@ int unit_get_ip_accounting(Unit *u, CGroupIPAccountingMetric metric, uint64_t *r
 int unit_reset_cpu_accounting(Unit *u);
 int unit_reset_ip_accounting(Unit *u);
 
-bool unit_cgroup_delegate(Unit *u);
+#define UNIT_CGROUP_BOOL(u, name)                       \
+        ({                                              \
+        CGroupContext *cc = unit_get_cgroup_context(u); \
+        cc ? cc->name : false;                          \
+        })
 
 int unit_notify_cgroup_empty(Unit *u);
 #endif // 0