Commit c40fc4df authored by Petr Baudis's avatar Petr Baudis
Browse files

Add support for the memsw limits that limit memory+swap

Can be disabled in common.h in case your kernel does not support it.
parent 4de33883
......@@ -7,6 +7,13 @@
#include <unistd.h>
#include "cgroup.h"
#include "common.h"
#if SWAP_LIMIT
#define MEMSW ".memsw"
#else
#define MEMSW
#endif
void (*cgroup_perror)(const char *s) = perror;
static const char cgroupfs[] = "/sys/fs/cgroup/";
......@@ -115,7 +122,7 @@ size_t
cgroup_get_mem_limit(const char *chier, const char *cgroup)
{
char limitfile[PATH_MAX];
snprintf(limitfile, sizeof(limitfile), "%s%s/%s/memory.limit_in_bytes", cgroupfs, chier, cgroup);
snprintf(limitfile, sizeof(limitfile), "%s%s/%s/memory"MEMSW".limit_in_bytes", cgroupfs, chier, cgroup);
FILE *limit = fopen(limitfile, "r");
if (!limit) {
cgroup_perror(limitfile);
......@@ -128,10 +135,10 @@ cgroup_get_mem_limit(const char *chier, const char *cgroup)
}
int
cgroup_set_mem_limit(const char *chier, const char *cgroup, size_t nlimit)
cgroup_set_mem_limit_do(const char *chier, const char *cgroup, size_t nlimit, char *memsw)
{
char limitfile[PATH_MAX];
snprintf(limitfile, sizeof(limitfile), "%s%s/%s/memory.limit_in_bytes", cgroupfs, chier, cgroup);
snprintf(limitfile, sizeof(limitfile), "%s%s/%s/memory%s.limit_in_bytes", cgroupfs, chier, cgroup, memsw);
FILE *limit = fopen(limitfile, "w");
if (!limit) {
cgroup_perror(limitfile);
......@@ -142,11 +149,24 @@ cgroup_set_mem_limit(const char *chier, const char *cgroup, size_t nlimit)
return 1;
}
int
cgroup_set_mem_limit(const char *chier, const char *cgroup, size_t nlimit)
{
/* We need to set both the "normal" and memsw limits, normal first,
* since normal <= memsw must hold. */
int ret = cgroup_set_mem_limit_do(chier, cgroup, nlimit, "");
#if SWAP_LIMIT
if (ret >= 0)
ret |= cgroup_set_mem_limit_do(chier, cgroup, nlimit, MEMSW);
#endif
return ret;
}
size_t
cgroup_get_mem_usage(const char *chier, const char *cgroup)
{
char usagefile[PATH_MAX];
snprintf(usagefile, sizeof(usagefile), "%s%s/%s/memory.usage_in_bytes", cgroupfs, chier, cgroup);
snprintf(usagefile, sizeof(usagefile), "%s%s/%s/memory"MEMSW".usage_in_bytes", cgroupfs, chier, cgroup);
FILE *usage = fopen(usagefile, "r");
if (!usage) {
cgroup_perror(usagefile);
......
......@@ -18,6 +18,10 @@
/* Default nice value for processes started using compctl --run. */
#define COMPNICE 12
/* Comment out if swap memory resource accounting is disabled
* and only RAM memory should be limited. */
#define SWAP_LIMIT 1
/* Other common definitions. */
/* See README for the high-level protocol description. */
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment