Preparation for adding oomd hooks.
m->monitored_swap_cgroup_contexts : m->monitored_mem_pressure_cgroup_contexts;
if (message.mode == MANAGED_OOM_AUTO) {
- (void) oomd_cgroup_context_free(hashmap_remove(monitor_hm, empty_to_root(message.path)));
+ (void) oomd_cgroup_context_unref(hashmap_remove(monitor_hm, empty_to_root(message.path)));
continue;
}
path_hash_func,
path_compare,
OomdCGroupContext,
- oomd_cgroup_context_free);
+ oomd_cgroup_context_unref);
static int log_kill(const PidRef *pid, int sig, void *userdata) {
log_debug("oomd attempting to kill " PID_FMT " with %s", pid->pid, signal_to_string(sig));
return 0;
}
-OomdCGroupContext *oomd_cgroup_context_free(OomdCGroupContext *ctx) {
+static OomdCGroupContext *oomd_cgroup_context_free(OomdCGroupContext *ctx) {
if (!ctx)
return NULL;
return mfree(ctx);
}
+DEFINE_TRIVIAL_REF_UNREF_FUNC(OomdCGroupContext, oomd_cgroup_context, oomd_cgroup_context_free);
+
int oomd_pressure_above(Hashmap *h, Set **ret) {
_cleanup_set_free_ Set *targets = NULL;
OomdCGroupContext *ctx;
}
int oomd_cgroup_context_acquire(const char *path, OomdCGroupContext **ret) {
- _cleanup_(oomd_cgroup_context_freep) OomdCGroupContext *ctx = NULL;
+ _cleanup_(oomd_cgroup_context_unrefp) OomdCGroupContext *ctx = NULL;
_cleanup_free_ char *p = NULL, *val = NULL;
bool is_root;
int r;
if (!ctx)
return -ENOMEM;
+ *ctx = (OomdCGroupContext) {
+ .n_ref = 1,
+ .preference = MANAGED_OOM_PREFERENCE_NONE,
+ .path = strdup(empty_to_root(path)),
+ };
+ if (!ctx->path)
+ return -ENOMEM;
+
is_root = empty_or_root(path);
- ctx->preference = MANAGED_OOM_PREFERENCE_NONE;
r = cg_get_path(path, "memory.pressure", &p);
if (r < 0)
return log_debug_errno(r, "Error converting pgscan value to uint64_t: %m");
}
- r = strdup_to(&ctx->path, empty_to_root(path));
- if (r < 0)
- return r;
-
*ret = TAKE_PTR(ctx);
return 0;
}
}
int oomd_insert_cgroup_context(Hashmap *old_h, Hashmap *new_h, const char *path) {
- _cleanup_(oomd_cgroup_context_freep) OomdCGroupContext *curr_ctx = NULL;
+ _cleanup_(oomd_cgroup_context_unrefp) OomdCGroupContext *curr_ctx = NULL;
OomdCGroupContext *old_ctx;
int r;
typedef int (oomd_compare_t)(OomdCGroupContext * const *, OomdCGroupContext * const *);
struct OomdCGroupContext {
+ unsigned n_ref;
char *path;
ResourcePressure memory_pressure;
uint64_t swap_used;
};
-OomdCGroupContext *oomd_cgroup_context_free(OomdCGroupContext *ctx);
-DEFINE_TRIVIAL_CLEANUP_FUNC(OomdCGroupContext*, oomd_cgroup_context_free);
+OomdCGroupContext *oomd_cgroup_context_ref(OomdCGroupContext *p);
+OomdCGroupContext *oomd_cgroup_context_unref(OomdCGroupContext *p);
+DEFINE_TRIVIAL_CLEANUP_FUNC(OomdCGroupContext*, oomd_cgroup_context_unref);
/* All hashmaps used with these functions are expected to be of the form
* key: cgroup paths -> value: OomdCGroupContext. */
TEST(oomd_cgroup_context_acquire_and_insert) {
_cleanup_hashmap_free_ Hashmap *h1 = NULL, *h2 = NULL;
- _cleanup_(oomd_cgroup_context_freep) OomdCGroupContext *ctx = NULL;
+ _cleanup_(oomd_cgroup_context_unrefp) OomdCGroupContext *ctx = NULL;
OomdCGroupContext *c1, *c2;
CGroupMask mask;
ASSERT_EQ(ctx->swap_usage, 0u);
ASSERT_EQ(ctx->last_pgscan, 0u);
ASSERT_EQ(ctx->pgscan, 0u);
- ASSERT_NULL(ctx = oomd_cgroup_context_free(ctx));
+ ASSERT_NULL(ctx = oomd_cgroup_context_unref(ctx));
ASSERT_OK(oomd_cgroup_context_acquire("", &ctx));
ASSERT_STREQ(ctx->path, "/");
}
TEST(oomd_fetch_cgroup_oom_preference) {
- _cleanup_(oomd_cgroup_context_freep) OomdCGroupContext *ctx = NULL;
+ _cleanup_(oomd_cgroup_context_unrefp) OomdCGroupContext *ctx = NULL;
ManagedOOMPreference root_pref;
CGroupMask mask;
bool test_xattrs;
ASSERT_FAIL(oomd_fetch_cgroup_oom_preference(ctx, NULL));
ASSERT_EQ(ctx->preference, MANAGED_OOM_PREFERENCE_NONE);
}
- ctx = oomd_cgroup_context_free(ctx);
+ ctx = oomd_cgroup_context_unref(ctx);
/* also check when only avoid is set to true */
if (test_xattrs) {
ASSERT_OK(oomd_cgroup_context_acquire(cgroup, &ctx));
ASSERT_OK(oomd_fetch_cgroup_oom_preference(ctx, NULL));
ASSERT_EQ(ctx->preference, geteuid() == 0 ? MANAGED_OOM_PREFERENCE_AVOID : MANAGED_OOM_PREFERENCE_NONE);
- ctx = oomd_cgroup_context_free(ctx);
+ ctx = oomd_cgroup_context_unref(ctx);
}
/* Test the root cgroup */
/* Assert that avoid/omit are not set if the cgroup and prefix are not
* owned by the same user. */
if (test_xattrs && !empty_or_root(cgroup) && geteuid() == 0) {
- ctx = oomd_cgroup_context_free(ctx);
+ ctx = oomd_cgroup_context_unref(ctx);
ASSERT_OK(cg_set_access(cgroup, 61183, 0));
ASSERT_OK(oomd_cgroup_context_acquire(cgroup, &ctx));