chiark / gitweb /
core: expose consumed CPU time per unit
authorLennart Poettering <lennart@poettering.net>
Sun, 1 Mar 2015 15:24:19 +0000 (16:24 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 2 Mar 2015 11:15:25 +0000 (12:15 +0100)
This adds support for showing the accumulated consumed CPU time per-unit
in the "systemctl status" output. The property is also readable via the
bus.

16 files changed:
src/core/cgroup.c
src/core/cgroup.h
src/core/dbus-unit.c
src/core/mount.c
src/core/mount.h
src/core/scope.c
src/core/service.c
src/core/service.h
src/core/slice.c
src/core/socket.c
src/core/socket.h
src/core/swap.c
src/core/swap.h
src/core/unit.c
src/core/unit.h
src/systemctl/systemctl.c

index 10fdcc9984e39f4a5171c77d68c750d7b7d3d490..6b8abb4802ba76635b9ecaa0a17ff3ac796641af 100644 (file)
@@ -1029,16 +1029,100 @@ int manager_notify_cgroup_empty(Manager *m, const char *cgroup) {
         assert(cgroup);
 
         u = manager_get_unit_by_cgroup(m, cgroup);
-        if (u) {
-                r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, true);
-                if (r > 0) {
-                        if (UNIT_VTABLE(u)->notify_cgroup_empty)
-                                UNIT_VTABLE(u)->notify_cgroup_empty(u);
+        if (!u)
+                return 0;
 
-                        unit_add_to_gc_queue(u);
-                }
+        r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, true);
+        if (r <= 0)
+                return r;
+
+        if (UNIT_VTABLE(u)->notify_cgroup_empty)
+                UNIT_VTABLE(u)->notify_cgroup_empty(u);
+
+        unit_add_to_gc_queue(u);
+        return 0;
+}
+
+int unit_get_memory_current(Unit *u, uint64_t *ret) {
+        _cleanup_free_ char *v = NULL;
+        int r;
+
+        assert(u);
+        assert(ret);
+
+        if (!u->cgroup_path)
+                return -ENODATA;
+
+        if ((u->cgroup_realized_mask & CGROUP_MEMORY) == 0)
+                return -ENODATA;
+
+        r = cg_get_attribute("memory", u->cgroup_path, "memory.usage_in_bytes", &v);
+        if (r == -ENOENT)
+                return -ENODATA;
+        if (r < 0)
+                return r;
+
+        return safe_atou64(v, ret);
+}
+
+static int unit_get_cpu_usage_raw(Unit *u, nsec_t *ret) {
+        _cleanup_free_ char *v = NULL;
+        uint64_t ns;
+        int r;
+
+        assert(u);
+        assert(ret);
+
+        if (!u->cgroup_path)
+                return -ENODATA;
+
+        if ((u->cgroup_realized_mask & CGROUP_CPUACCT) == 0)
+                return -ENODATA;
+
+        r = cg_get_attribute("cpuacct", u->cgroup_path, "cpuacct.usage", &v);
+        if (r == -ENOENT)
+                return -ENODATA;
+        if (r < 0)
+                return r;
+
+        r = safe_atou64(v, &ns);
+        if (r < 0)
+                return r;
+
+        *ret = ns;
+        return 0;
+}
+
+int unit_get_cpu_usage(Unit *u, nsec_t *ret) {
+        nsec_t ns;
+        int r;
+
+        r = unit_get_cpu_usage_raw(u, &ns);
+        if (r < 0)
+                return r;
+
+        if (ns > u->cpuacct_usage_base)
+                ns -= u->cpuacct_usage_base;
+        else
+                ns = 0;
+
+        *ret = ns;
+        return 0;
+}
+
+int unit_reset_cpu_usage(Unit *u) {
+        nsec_t ns;
+        int r;
+
+        assert(u);
+
+        r = unit_get_cpu_usage_raw(u, &ns);
+        if (r < 0) {
+                u->cpuacct_usage_base = 0;
+                return r;
         }
 
+        u->cpuacct_usage_base = ns;
         return 0;
 }
 
index 993aa9db7d6c7f7341ba4a1acb108a7138dbcfea..869ddae8c40e15ee812f7311020c4c5a0486bb61 100644 (file)
@@ -126,5 +126,9 @@ pid_t unit_search_main_pid(Unit *u);
 
 int manager_notify_cgroup_empty(Manager *m, const char *group);
 
+int unit_get_memory_current(Unit *u, uint64_t *ret);
+int unit_get_cpu_usage(Unit *u, nsec_t *ret);
+int unit_reset_cpu_usage(Unit *u);
+
 const char* cgroup_device_policy_to_string(CGroupDevicePolicy i) _const_;
 CGroupDevicePolicy cgroup_device_policy_from_string(const char *s) _pure_;
index 0ff9a01e1138f8599566146db86083e0fd7a94c3..af7dc26241cbf688cd0ff70fe3a99162573b3d5d 100644 (file)
@@ -661,30 +661,43 @@ static int property_get_current_memory(
                 void *userdata,
                 sd_bus_error *error) {
 
-        Unit *u = userdata;
         uint64_t sz = (uint64_t) -1;
+        Unit *u = userdata;
         int r;
 
         assert(bus);
         assert(reply);
         assert(u);
 
-        if (u->cgroup_path &&
-            (u->cgroup_realized_mask & CGROUP_MEMORY)) {
-                _cleanup_free_ char *v = NULL;
+        r = unit_get_memory_current(u, &sz);
+        if (r < 0 && r != -ENODATA)
+                log_unit_warning_errno(u->id, r, "Failed to get memory.usage_in_bytes attribute: %m");
 
-                r = cg_get_attribute("memory", u->cgroup_path, "memory.usage_in_bytes", &v);
-                if (r < 0 && r != -ENOENT)
-                        log_unit_warning_errno(u->id, r, "Couldn't read memory.usage_in_bytes attribute: %m");
+        return sd_bus_message_append(reply, "t", sz);
+}
 
-                if (v) {
-                        r = safe_atou64(v, &sz);
-                        if (r < 0)
-                                log_unit_warning_errno(u->id, r, "Failed to parse memory.usage_in_bytes attribute: %m");
-                }
-        }
+static int property_get_cpu_usage(
+                sd_bus *bus,
+                const char *path,
+                const char *interface,
+                const char *property,
+                sd_bus_message *reply,
+                void *userdata,
+                sd_bus_error *error) {
 
-        return sd_bus_message_append(reply, "t", sz);
+        nsec_t ns = (nsec_t) -1;
+        Unit *u = userdata;
+        int r;
+
+        assert(bus);
+        assert(reply);
+        assert(u);
+
+        r = unit_get_cpu_usage(u, &ns);
+        if (r < 0 && r != -ENODATA)
+                log_unit_warning_errno(u->id, r, "Failed to get cpuacct.usage attribute: %m");
+
+        return sd_bus_message_append(reply, "t", ns);
 }
 
 const sd_bus_vtable bus_unit_cgroup_vtable[] = {
@@ -692,6 +705,7 @@ const sd_bus_vtable bus_unit_cgroup_vtable[] = {
         SD_BUS_PROPERTY("Slice", "s", property_get_slice, 0, 0),
         SD_BUS_PROPERTY("ControlGroup", "s", NULL, offsetof(Unit, cgroup_path), 0),
         SD_BUS_PROPERTY("MemoryCurrent", "t", property_get_current_memory, 0, 0),
+        SD_BUS_PROPERTY("CPUUsageNSec", "t", property_get_cpu_usage, 0, 0),
         SD_BUS_VTABLE_END
 };
 
index 8e4a3769446a497ba9bfd5d72054e2bc6d6c09cc..5ee679da7c5bbb6107bf2cd58c9b858c5ccc1a55 100644 (file)
@@ -706,7 +706,11 @@ static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) {
         assert(c);
         assert(_pid);
 
-        unit_realize_cgroup(UNIT(m));
+        (void) unit_realize_cgroup(UNIT(m));
+        if (m->reset_cpu_usage) {
+                (void) unit_reset_cpu_usage(UNIT(m));
+                m->reset_cpu_usage = false;
+        }
 
         r = unit_setup_exec_runtime(UNIT(m));
         if (r < 0)
@@ -1030,6 +1034,7 @@ static int mount_start(Unit *u) {
 
         m->result = MOUNT_SUCCESS;
         m->reload_result = MOUNT_SUCCESS;
+        m->reset_cpu_usage = true;
 
         mount_enter_mounting(m);
         return 1;
index 76771ab7a92fe1542f83b06044a726e2f20bf159..072b0e044757d792aab0cdf7b13e05716ca8f50e 100644 (file)
@@ -86,6 +86,8 @@ struct Mount {
         bool just_mounted:1;
         bool just_changed:1;
 
+        bool reset_cpu_usage:1;
+
         bool sloppy_options;
 
         MountResult result;
index a0e473253349e252a169dd60f24b5117145a96c0..1c3c6bb5409a381b2e5c07f1d5c0d91d0db0dcda 100644 (file)
@@ -286,6 +286,9 @@ static int scope_start(Unit *u) {
         if (!u->transient && UNIT(s)->manager->n_reloading <= 0)
                 return -ENOENT;
 
+        (void) unit_realize_cgroup(u);
+        (void) unit_reset_cpu_usage(u);
+
         r = unit_attach_pids_to_cgroup(u);
         if (r < 0)
                 return r;
index c7b35050bd2f61d9b8181559f9270c0dda830100..a89ff3f96c3e8c1d640ac580c244f95d10c242d1 100644 (file)
@@ -1057,7 +1057,11 @@ static int service_spawn(
         assert(c);
         assert(_pid);
 
-        unit_realize_cgroup(UNIT(s));
+        (void) unit_realize_cgroup(UNIT(s));
+        if (s->reset_cpu_usage) {
+                (void) unit_reset_cpu_usage(UNIT(s));
+                s->reset_cpu_usage = false;
+        }
 
         r = unit_setup_exec_runtime(UNIT(s));
         if (r < 0)
@@ -1828,6 +1832,7 @@ static int service_start(Unit *u) {
         s->main_pid_known = false;
         s->main_pid_alien = false;
         s->forbid_restart = false;
+        s->reset_cpu_usage = true;
 
         free(s->status_text);
         s->status_text = NULL;
index fe5afef46b16a3c02f975e8376a4cbd70420b108..23124e8eabc278fd4575c70021f8993ddde49a3e 100644 (file)
@@ -189,6 +189,8 @@ struct Service {
         bool forbid_restart:1;
         bool start_timeout_defined:1;
 
+        bool reset_cpu_usage:1;
+
         char *bus_name;
 
         char *status_text;
index 4d2eaf7ed64b8729cb1da52a475db8c40be38f99..0bebdbcbc649591637ed2595f344eae088ee900d 100644 (file)
@@ -181,7 +181,8 @@ static int slice_start(Unit *u) {
         assert(t);
         assert(t->state == SLICE_DEAD);
 
-        unit_realize_cgroup(u);
+        (void) unit_realize_cgroup(u);
+        (void) unit_reset_cpu_usage(u);
 
         slice_set_state(t, SLICE_ACTIVE);
         return 1;
index 7d052f2ef97808ae2d8e4e1b6757f64207860788..9606ac2b1c33b085e763f9dde64d3a28a170db42 100644 (file)
@@ -1392,7 +1392,11 @@ static int socket_spawn(Socket *s, ExecCommand *c, pid_t *_pid) {
         assert(c);
         assert(_pid);
 
-        unit_realize_cgroup(UNIT(s));
+        (void) unit_realize_cgroup(UNIT(s));
+        if (s->reset_cpu_usage) {
+                (void) unit_reset_cpu_usage(UNIT(s));
+                s->reset_cpu_usage = false;
+        }
 
         r = unit_setup_exec_runtime(UNIT(s));
         if (r < 0)
@@ -1948,6 +1952,8 @@ static int socket_start(Unit *u) {
         assert(s->state == SOCKET_DEAD || s->state == SOCKET_FAILED);
 
         s->result = SOCKET_SUCCESS;
+        s->reset_cpu_usage = true;
+
         socket_enter_start_pre(s);
 
         return 1;
index 5acf214e1c73ca9202c1c188ec1ab92e85a8591d..fa3ebdafa064f050268b0dd15495fdadb4e106a6 100644 (file)
@@ -166,6 +166,8 @@ struct Socket {
         bool selinux_context_from_net;
 
         char *user, *group;
+
+        bool reset_cpu_usage:1;
 };
 
 /* Called from the service code when collecting fds */
index de3a5d8b10f1755c23a941bd5e3dae654f569398..4dd6be8c9a6e1f8c0ce11bda34c4dbca6f365fe8 100644 (file)
@@ -604,7 +604,11 @@ static int swap_spawn(Swap *s, ExecCommand *c, pid_t *_pid) {
         assert(c);
         assert(_pid);
 
-        unit_realize_cgroup(UNIT(s));
+        (void) unit_realize_cgroup(UNIT(s));
+        if (s->reset_cpu_usage) {
+                (void) unit_reset_cpu_usage(UNIT(s));
+                s->reset_cpu_usage = false;
+        }
 
         r = unit_setup_exec_runtime(UNIT(s));
         if (r < 0)
@@ -830,6 +834,8 @@ static int swap_start(Unit *u) {
                         return -EAGAIN;
 
         s->result = SWAP_SUCCESS;
+        s->reset_cpu_usage = true;
+
         swap_enter_activating(s);
         return 1;
 }
index 5de8c20c042f97d5df42a6be38c1adafc89de63a..9136b9abab724083dc27abf34b1235bf2d15b1bf 100644 (file)
@@ -87,6 +87,8 @@ struct Swap {
         bool is_active:1;
         bool just_activated:1;
 
+        bool reset_cpu_usage:1;
+
         SwapResult result;
 
         usec_t timeout_usec;
index 7cd704351cb0e30300cf37436fed9c0c49156cd7..b639d686adda3ad13811f21003a6836aeb62282a 100644 (file)
@@ -2602,6 +2602,7 @@ int unit_serialize(Unit *u, FILE *f, FDSet *fds, bool serialize_jobs) {
                 unit_serialize_item(u, f, "assert-result", yes_no(u->assert_result));
 
         unit_serialize_item(u, f, "transient", yes_no(u->transient));
+        unit_serialize_item_format(u, f, "cpuacct-usage-base", "%" PRIu64, u->cpuacct_usage_base);
 
         if (u->cgroup_path)
                 unit_serialize_item(u, f, "cgroup", u->cgroup_path);
@@ -2776,6 +2777,12 @@ int unit_deserialize(Unit *u, FILE *f, FDSet *fds) {
                                 u->transient = b;
 
                         continue;
+                } else if (streq(l, "cpuacct-usage-base")) {
+
+                        r = safe_atou64(v, &u->cpuacct_usage_base);
+                        if (r < 0)
+                                log_debug("Failed to parse CPU usage %s", v);
+
                 } else if (streq(l, "cgroup")) {
                         char *s;
 
@@ -2787,8 +2794,7 @@ int unit_deserialize(Unit *u, FILE *f, FDSet *fds) {
                                 void *p;
 
                                 p = hashmap_remove(u->manager->cgroup_unit, u->cgroup_path);
-                                log_info("Removing cgroup_path %s from hashmap (%p)",
-                                         u->cgroup_path, p);
+                                log_info("Removing cgroup_path %s from hashmap (%p)", u->cgroup_path, p);
                                 free(u->cgroup_path);
                         }
 
index b3775d4d897849e25a40aa58712f36271c7042c5..ac5647a7f2677d55e8e98a87859b9eb328136efd 100644 (file)
@@ -176,6 +176,9 @@ struct Unit {
         UnitFileState unit_file_state;
         int unit_file_preset;
 
+        /* Where the cpuacct.usage cgroup counter was at the time the unit was started */
+        nsec_t cpuacct_usage_base;
+
         /* Counterparts in the cgroup filesystem */
         char *cgroup_path;
         CGroupControllerMask cgroup_realized_mask;
index 10213afbc244a2c1b892b3d685a06828a5edd28c..e915f6f3cfede573c5020c084e643fa087142e06 100644 (file)
@@ -3195,6 +3195,7 @@ typedef struct UnitStatusInfo {
         /* CGroup */
         uint64_t memory_current;
         uint64_t memory_limit;
+        uint64_t cpu_usage_nsec;
 
         LIST_HEAD(ExecStatusInfo, exec);
 } UnitStatusInfo;
@@ -3465,6 +3466,11 @@ static void print_status_info(
                         printf("\n");
         }
 
+        if (i->cpu_usage_nsec != (uint64_t) -1) {
+                char buf[FORMAT_TIMESPAN_MAX];
+                printf("      CPU: %s\n", format_timespan(buf, sizeof(buf), i->cpu_usage_nsec / NSEC_PER_USEC, USEC_PER_MSEC));
+        }
+
         if (i->control_group &&
             (i->main_pid > 0 || i->control_pid > 0 ||
              ((arg_transport != BUS_TRANSPORT_LOCAL && arg_transport != BUS_TRANSPORT_MACHINE) || cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, i->control_group, false) == 0))) {
@@ -3683,6 +3689,8 @@ static int status_property(const char *name, sd_bus_message *m, UnitStatusInfo *
                         i->memory_current = u;
                 else if (streq(name, "MemoryLimit"))
                         i->memory_limit = u;
+                else if (streq(name, "CPUUsageNSec"))
+                        i->cpu_usage_nsec = u;
 
                 break;
         }
@@ -4156,6 +4164,7 @@ static int show_one(
         UnitStatusInfo info = {
                 .memory_current = (uint64_t) -1,
                 .memory_limit = (uint64_t) -1,
+                .cpu_usage_nsec = (uint64_t) -1,
         };
         ExecStatusInfo *p;
         int r;