]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
oom: Introduce process_managed_oom_message()
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Thu, 2 Sep 2021 11:24:23 +0000 (13:24 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Thu, 16 Sep 2021 11:26:29 +0000 (12:26 +0100)
Gets rid of a few gotos, allows removing the extra ret variable and
will also be used in a future commit by the codepath that receives
cgroups from user instances of systemd.

src/oom/oomd-manager.c

index 4d7c28fe8ec1efd1852fe8668e6b69b60ced6c35..b0266219050e405b251e52472638b71e5c594fce 100644 (file)
 #include "path-util.h"
 #include "percent-util.h"
 
-typedef struct ManagedOOMReply {
+typedef struct ManagedOOMMessage {
         ManagedOOMMode mode;
         char *path;
         char *property;
         uint32_t limit;
-} ManagedOOMReply;
+} ManagedOOMMessage;
 
-static void managed_oom_reply_destroy(ManagedOOMReply *reply) {
-        assert(reply);
-        free(reply->path);
-        free(reply->property);
+static void managed_oom_message_destroy(ManagedOOMMessage *message) {
+        assert(message);
+        free(message->path);
+        free(message->property);
 }
 
 static int managed_oom_mode(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
@@ -40,93 +40,97 @@ static int managed_oom_mode(const char *name, JsonVariant *v, JsonDispatchFlags
         return 0;
 }
 
-static int process_managed_oom_reply(
-                Varlink *link,
-                JsonVariant *parameters,
-                const char *error_id,
-                VarlinkReplyFlags flags,
-                void *userdata) {
+static int process_managed_oom_message(Manager *m, JsonVariant *parameters) {
         JsonVariant *c, *cgroups;
-        Manager *m = userdata;
-        int r = 0;
-
-        assert(m);
+        int r;
 
         static const JsonDispatch dispatch_table[] = {
-                { "mode",     JSON_VARIANT_STRING,   managed_oom_mode,     offsetof(ManagedOOMReply, mode),     JSON_MANDATORY },
-                { "path",     JSON_VARIANT_STRING,   json_dispatch_string, offsetof(ManagedOOMReply, path),     JSON_MANDATORY },
-                { "property", JSON_VARIANT_STRING,   json_dispatch_string, offsetof(ManagedOOMReply, property), JSON_MANDATORY },
-                { "limit",    JSON_VARIANT_UNSIGNED, json_dispatch_uint32, offsetof(ManagedOOMReply, limit),    0 },
+                { "mode",     JSON_VARIANT_STRING,   managed_oom_mode,     offsetof(ManagedOOMMessage, mode),     JSON_MANDATORY },
+                { "path",     JSON_VARIANT_STRING,   json_dispatch_string, offsetof(ManagedOOMMessage, path),     JSON_MANDATORY },
+                { "property", JSON_VARIANT_STRING,   json_dispatch_string, offsetof(ManagedOOMMessage, property), JSON_MANDATORY },
+                { "limit",    JSON_VARIANT_UNSIGNED, json_dispatch_uint32, offsetof(ManagedOOMMessage, limit),    0 },
                 {},
         };
 
-        if (error_id) {
-                r = -EIO;
-                log_debug("Error getting ManagedOOM cgroups: %s", error_id);
-                goto finish;
-        }
+        assert(m);
+        assert(parameters);
 
         cgroups = json_variant_by_key(parameters, "cgroups");
-        if (!cgroups) {
-                r = -EINVAL;
-                goto finish;
-        }
+        if (!cgroups)
+                return -EINVAL;
 
         /* Skip malformed elements and keep processing in case the others are good */
         JSON_VARIANT_ARRAY_FOREACH(c, cgroups) {
-                _cleanup_(managed_oom_reply_destroy) ManagedOOMReply reply = {};
+                _cleanup_(managed_oom_message_destroy) ManagedOOMMessage message = {};
                 OomdCGroupContext *ctx;
                 Hashmap *monitor_hm;
                 loadavg_t limit;
-                int ret;
 
                 if (!json_variant_is_object(c))
                         continue;
 
-                ret = json_dispatch(c, dispatch_table, NULL, 0, &reply);
-                if (ret == -ENOMEM) {
-                        r = ret;
-                        goto finish;
-                }
-                if (ret < 0)
+                r = json_dispatch(c, dispatch_table, NULL, 0, &message);
+                if (r == -ENOMEM)
+                        return r;
+                if (r < 0)
                         continue;
 
-                monitor_hm = streq(reply.property, "ManagedOOMSwap") ?
+                monitor_hm = streq(message.property, "ManagedOOMSwap") ?
                                 m->monitored_swap_cgroup_contexts : m->monitored_mem_pressure_cgroup_contexts;
 
-                if (reply.mode == MANAGED_OOM_AUTO) {
-                        (void) oomd_cgroup_context_free(hashmap_remove(monitor_hm, empty_to_root(reply.path)));
+                if (message.mode == MANAGED_OOM_AUTO) {
+                        (void) oomd_cgroup_context_free(hashmap_remove(monitor_hm, empty_to_root(message.path)));
                         continue;
                 }
 
                 limit = m->default_mem_pressure_limit;
 
-                if (streq(reply.property, "ManagedOOMMemoryPressure") && reply.limit > 0) {
-                        int permyriad = UINT32_SCALE_TO_PERMYRIAD(reply.limit);
+                if (streq(message.property, "ManagedOOMMemoryPressure") && message.limit > 0) {
+                        int permyriad = UINT32_SCALE_TO_PERMYRIAD(message.limit);
 
-                        ret = store_loadavg_fixed_point(
+                        r = store_loadavg_fixed_point(
                                         (unsigned long) permyriad / 100,
                                         (unsigned long) permyriad % 100,
                                         &limit);
-                        if (ret < 0)
+                        if (r < 0)
                                 continue;
                 }
 
-                ret = oomd_insert_cgroup_context(NULL, monitor_hm, reply.path);
-                if (ret == -ENOMEM) {
-                        r = ret;
-                        goto finish;
-                }
-                if (ret < 0 && ret != -EEXIST)
-                        log_debug_errno(ret, "Failed to insert reply, ignoring: %m");
+                r = oomd_insert_cgroup_context(NULL, monitor_hm, message.path);
+                if (r == -ENOMEM)
+                        return r;
+                if (r < 0 && r != -EEXIST)
+                        log_debug_errno(r, "Failed to insert message, ignoring: %m");
 
                 /* Always update the limit in case it was changed. For non-memory pressure detection the value is
                  * ignored so always updating it here is not a problem. */
-                ctx = hashmap_get(monitor_hm, empty_to_root(reply.path));
+                ctx = hashmap_get(monitor_hm, empty_to_root(message.path));
                 if (ctx)
                         ctx->mem_pressure_limit = limit;
         }
 
+        return 0;
+}
+
+static int process_managed_oom_reply(
+                Varlink *link,
+                JsonVariant *parameters,
+                const char *error_id,
+                VarlinkReplyFlags flags,
+                void *userdata) {
+        Manager *m = userdata;
+        int r;
+
+        assert(m);
+
+        if (error_id) {
+                r = -EIO;
+                log_debug("Error getting ManagedOOM cgroups: %s", error_id);
+                goto finish;
+        }
+
+        r = process_managed_oom_message(m, parameters);
+
 finish:
         if (!FLAGS_SET(flags, VARLINK_REPLY_CONTINUES))
                 m->varlink = varlink_close_unref(link);