Skip to content

Commit 23ba994

Browse files
authored
Merge pull request #1217 from giuseppe/set-mem-limit-cgroup-systemd
cgroup: set the memory limit on the system scope
2 parents e059e80 + 6494b69 commit 23ba994

File tree

1 file changed

+47
-17
lines changed

1 file changed

+47
-17
lines changed

src/libcrun/cgroup-systemd.c

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,40 @@ open_sd_bus_connection (sd_bus **bus, libcrun_error_t *err)
606606
return 0;
607607
}
608608

609+
static int
610+
get_value_from_unified_map (runtime_spec_schema_config_linux_resources *resources, const char *name,
611+
uint64_t *value, libcrun_error_t *err)
612+
{
613+
size_t i;
614+
615+
if (resources == NULL || resources->unified == NULL)
616+
return 0;
617+
618+
for (i = 0; i < resources->unified->len; i++)
619+
if (strcmp (resources->unified->keys[i], name) == 0)
620+
{
621+
errno = 0;
622+
*value = (uint64_t) strtoll (resources->unified->values[i], NULL, 10);
623+
if (UNLIKELY (errno))
624+
return crun_make_error (err, errno, "invalid value for `%s`: %s", name,
625+
resources->unified->values[i]);
626+
return 1;
627+
}
628+
return 0;
629+
}
630+
631+
static inline int
632+
get_memory_limit (runtime_spec_schema_config_linux_resources *resources, uint64_t *limit, libcrun_error_t *err)
633+
{
634+
if (resources->memory && resources->memory->limit_present)
635+
{
636+
*limit = resources->memory->limit;
637+
return 1;
638+
}
639+
640+
return get_value_from_unified_map (resources, "memory.max", limit, err);
641+
}
642+
609643
static inline int
610644
get_weight (runtime_spec_schema_config_linux_resources *resources, uint64_t *weight, libcrun_error_t *err)
611645
{
@@ -618,22 +652,7 @@ get_weight (runtime_spec_schema_config_linux_resources *resources, uint64_t *wei
618652
return 1;
619653
}
620654

621-
if (resources->unified)
622-
{
623-
size_t i;
624-
625-
for (i = 0; i < resources->unified->len; i++)
626-
if (strcmp (resources->unified->keys[i], "cpu.weight") == 0)
627-
{
628-
errno = 0;
629-
*weight = (uint64_t) strtoll (resources->unified->values[i], NULL, 10);
630-
if (UNLIKELY (errno))
631-
return crun_make_error (err, errno, "invalid value for `cpu.weight`: %s",
632-
resources->unified->values[i]);
633-
return 1;
634-
}
635-
}
636-
return 0;
655+
return get_value_from_unified_map (resources, "cpu.weight", weight, err);
637656
}
638657

639658
static int
@@ -642,17 +661,28 @@ append_resources (sd_bus_message *m,
642661
int cgroup_mode,
643662
libcrun_error_t *err)
644663
{
664+
uint64_t memory_limit;
645665
int sd_err;
666+
int ret;
646667

647668
if (resources == NULL)
648669
return 0;
649670

671+
ret = get_memory_limit (resources, &memory_limit, err);
672+
if (UNLIKELY (ret < 0))
673+
return ret;
674+
if (ret)
675+
{
676+
sd_err = sd_bus_message_append (m, "(sv)", "MemoryLimit", "t", memory_limit);
677+
if (UNLIKELY (sd_err < 0))
678+
return crun_make_error (err, -sd_err, "sd-bus message append MemoryLimit");
679+
}
680+
650681
switch (cgroup_mode)
651682
{
652683
case CGROUP_MODE_UNIFIED:
653684
{
654685
uint64_t weight;
655-
int ret;
656686

657687
ret = get_weight (resources, &weight, err);
658688
if (UNLIKELY (ret < 0))

0 commit comments

Comments
 (0)