[sheepdog] [PATCH v3] sheep: avoid the OOM killer

Liu Yuan namei.unix at gmail.com
Fri Jul 5 08:26:23 CEST 2013


On Fri, Jul 05, 2013 at 03:18:48PM +0900, Hitoshi Mitake wrote:
> Because sheep provides virtual disks to many VMs, sheep is an
> important process and it shouldn't be killed by the OOM killer of
> Linux. This patch implements a mechanism for avoiding the OOM killer.
> 
> Signed-off-by: Hitoshi Mitake <mitake.hitoshi at lab.ntt.co.jp>
> ---
> 
> v3:
>  - renaming the function: avoid_oom_killer() -> disable_oom_killing()
>  - include <linux/oom.h> for the constant OOM_SCORE_ADJ_MIN
>  - move the function to lib/util.c for other programs like shepherd
> 
> v2:
>  - remove the meaningless comment
>  - add a description for the value -1000
> 
>  include/util.h |    1 +
>  lib/util.c     |   32 ++++++++++++++++++++++++++++++++
>  sheep/sheep.c  |    4 ++++
>  3 files changed, 37 insertions(+)
> 
> diff --git a/include/util.h b/include/util.h
> index e70bb9f..26e4bd9 100644
> --- a/include/util.h
> +++ b/include/util.h
> @@ -102,6 +102,7 @@ void trim_zero_blocks(void *buf, uint64_t *offset, uint32_t *len);
>  void untrim_zero_blocks(void *buf, uint64_t offset, uint32_t len,
>  			uint32_t requested_len);
>  int atomic_create_and_write(const char *path, char *buf, size_t len);
> +int disable_oom_killing(void);
>  
>  /* a type safe version of qsort() */
>  #define xqsort(base, nmemb, compar)					\
> diff --git a/lib/util.c b/lib/util.c
> index 6fd3817..85546dc 100644
> --- a/lib/util.c
> +++ b/lib/util.c
> @@ -23,6 +23,7 @@
>  #include <signal.h>
>  #include <sys/xattr.h>
>  #include <fcntl.h>
> +#include <linux/oom.h>
>  
>  #include "util.h"
>  #include "logger.h"
> @@ -539,3 +540,34 @@ close_fd:
>  end:
>  	return ret;
>  }
> +
> +int disable_oom_killing(void)
> +{
> +	int fd, ret = 0;
> +	char path[PATH_MAX], score_str[8];
> +
> +	snprintf(path, PATH_MAX, "/proc/%d/oom_score_adj", getpid());
> +	fd = open(path, O_WRONLY);
> +	if (fd < 0) {
> +		sd_eprintf("opening %s failed, %m", path);
> +		ret = -1;
> +		goto end;
> +	}
> +
> +	snprintf(score_str, 8, "%d\n", OOM_SCORE_ADJ_MIN);
> +
> +	/*
> +	 * -1000 is defined as OOM_SCORE_ADJ_MIN in the linux kernel. And
> +	 * processes which wrote OOM_SCORE_ADJ_MIN to
> +	 * /proc/<their pid>/oom_score_adj are excluded by the target list of
> +	 * the OOM killer certainly.
> +	 */
> +	if (xwrite(fd, score_str, strlen(score_str)) != 6) {
> +		sd_eprintf("writing to %s failed, %m", path);
> +		ret = -1;
> +	}
> +
> +	close(fd);
> +end:

s/end/out

Thanks
Yuan



More information about the sheepdog mailing list