From: Zbigniew Jędrzejewski-Szmek Date: Wed, 27 Sep 2017 15:54:06 +0000 (+0200) Subject: core/cgroup: add a helper macro for a common pattern (#6926) X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=b7e036edb9b4fea677bc92ca57f0c0f1efc25410;p=elogind.git core/cgroup: add a helper macro for a common pattern (#6926) --- diff --git a/src/core/cgroup.c b/src/core/cgroup.c index c1e51a7f6..092c15148 100644 --- a/src/core/cgroup.c +++ b/src/core/cgroup.c @@ -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); diff --git a/src/core/cgroup.h b/src/core/cgroup.h index a7100d1fc..2d09c4cc3 100644 --- a/src/core/cgroup.h +++ b/src/core/cgroup.h @@ -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