[LTP] [PATCH v2 1/3] lib: add functions to adjust oom score

Cyril Hrubis chrubis@suse.cz
Fri Dec 17 15:36:55 CET 2021


Hi!
> +/*
> + * Enable OOM protection to prevent process($PID) being killed by OOM Killer.
> + *   echo -1000 >/proc/$PID/oom_score_adj
> + *
> + * Note:
> + *  This exported tst_enable_oom_protection function can be used at anywhere
> + *  you want to protect, but please remember that if you do enable protection
> + *  on a process($PID) that all the children will inherit its score and be
> + *  ignored by OOM Killer as well. So that's why tst_cancel_oom_protection is
> + *  recommended to combination in use.
> + */
> +void tst_enable_oom_protection(pid_t pid);
> +
> +/*
> + * Cancel the OOM protection for the process($PID).
> + *   echo 0 >/proc/$PID/oom_score_adj
> + */
> +void tst_cancel_oom_protection(pid_t pid);

Minor nit: opposite of enable is disable not cancel.

>  #endif /* TST_MEMUTILS_H__ */
> diff --git a/lib/tst_memutils.c b/lib/tst_memutils.c
> index bd09cf6fa..d97b35007 100644
> --- a/lib/tst_memutils.c
> +++ b/lib/tst_memutils.c
> @@ -3,6 +3,7 @@
>   * Copyright (c) 2020 SUSE LLC <mdoucha@suse.cz>
>   */
>  
> +#include <stdio.h>
>  #include <unistd.h>
>  #include <limits.h>
>  #include <sys/sysinfo.h>
> @@ -91,3 +92,31 @@ long long tst_available_mem(void)
>  
>  	return mem_available;
>  }
> +
> +static void set_oom_score_adj(pid_t pid, int value)
> +{
> +	int val;
> +	char score_path[64];
> +
> +	if (access("/proc/self/oom_score_adj", F_OK) == -1) {
> +		tst_res(TINFO, "Warning: oom_score_adj does not exist");
> +		return;
> +	}
> +
> +	sprintf(score_path, "/proc/%d/oom_score_adj", pid);
> +	SAFE_FILE_PRINTF(score_path, "%d", value);
> +
> +	SAFE_FILE_SCANF(score_path, "%d", &val);
> +	if (val != value)
> +		tst_brk(TBROK, "oom_score_adj = %d, but expect %d.", val, value);
> +}
> +
> +void tst_enable_oom_protection(pid_t pid)
> +{
> +	set_oom_score_adj(pid, -1000);
> +}
> +
> +void tst_cancel_oom_protection(pid_t pid)
> +{
> +	set_oom_score_adj(pid, 0);
> +}
> -- 
> 2.31.1
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

-- 
Cyril Hrubis
chrubis@suse.cz


More information about the ltp mailing list