Skip to content
Snippets Groups Projects
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
No related branches found
No related tags found
No related merge requests found
...@@ -7,6 +7,13 @@ ...@@ -7,6 +7,13 @@
#include <unistd.h> #include <unistd.h>
#include "cgroup.h" #include "cgroup.h"
#include "common.h"
#if SWAP_LIMIT
#define MEMSW ".memsw"
#else
#define MEMSW
#endif
void (*cgroup_perror)(const char *s) = perror; void (*cgroup_perror)(const char *s) = perror;
static const char cgroupfs[] = "/sys/fs/cgroup/"; static const char cgroupfs[] = "/sys/fs/cgroup/";
...@@ -115,7 +122,7 @@ size_t ...@@ -115,7 +122,7 @@ size_t
cgroup_get_mem_limit(const char *chier, const char *cgroup) cgroup_get_mem_limit(const char *chier, const char *cgroup)
{ {
char limitfile[PATH_MAX]; 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"); FILE *limit = fopen(limitfile, "r");
if (!limit) { if (!limit) {
cgroup_perror(limitfile); cgroup_perror(limitfile);
...@@ -128,10 +135,10 @@ cgroup_get_mem_limit(const char *chier, const char *cgroup) ...@@ -128,10 +135,10 @@ cgroup_get_mem_limit(const char *chier, const char *cgroup)
} }
int 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]; 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"); FILE *limit = fopen(limitfile, "w");
if (!limit) { if (!limit) {
cgroup_perror(limitfile); cgroup_perror(limitfile);
...@@ -142,11 +149,24 @@ cgroup_set_mem_limit(const char *chier, const char *cgroup, size_t nlimit) ...@@ -142,11 +149,24 @@ cgroup_set_mem_limit(const char *chier, const char *cgroup, size_t nlimit)
return 1; 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 size_t
cgroup_get_mem_usage(const char *chier, const char *cgroup) cgroup_get_mem_usage(const char *chier, const char *cgroup)
{ {
char usagefile[PATH_MAX]; 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"); FILE *usage = fopen(usagefile, "r");
if (!usage) { if (!usage) {
cgroup_perror(usagefile); cgroup_perror(usagefile);
......
...@@ -18,6 +18,10 @@ ...@@ -18,6 +18,10 @@
/* Default nice value for processes started using compctl --run. */ /* Default nice value for processes started using compctl --run. */
#define COMPNICE 12 #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. */ /* Other common definitions. */
/* See README for the high-level protocol description. */ /* See README for the high-level protocol description. */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment