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

Li Wang liwang@redhat.com
Tue Dec 21 10:40:35 CET 2021


On Tue, Dec 21, 2021 at 5:23 PM Li Wang <liwang@redhat.com> wrote:

> Consider about situation, I'd suggest go with non-safe macros and add
> additional check in the last.
>
> e.g.
>
> --- a/lib/tst_memutils.c
> +++ b/lib/tst_memutils.c
> @@ -108,17 +108,21 @@ static void set_oom_score_adj(pid_t pid, int value)
>         else
>                 sprintf(score_path, "/proc/%d/oom_score_adj", pid);
>
> -       if (access(score_path, R_OK | W_OK) == -1) {
> -               tst_res(TINFO, "Warning: %s cannot be accessed for reading/writing,
> -                       please check if test run with root user.",
> -                       score_path);
> -               return
> -       }
> -
> -       SAFE_FILE_PRINTF(score_path, "%d", value);
> -       SAFE_FILE_SCANF(score_path, "%d", &val);
> -       if (val != value)
> +       if (access(score_path, F_OK) == -1)
> +               tst_brk(TBROK, "%s does not exist, please check if PID is valid");
> +
> +       FILE_PRINTF(score_path, "%d", value);
> +       FILE_SCANF(score_path, "%d", &val);
> +
> +       if (val != value) {
> +               if (value < 0) {
> +                       tst_res(TINFO, "Warning: %s cannot be set to negative value,
> +                               please check if test run with root user.",
> +                               score_path);
> +                       return
> +               }
>                 tst_brk(TBROK, "oom_score_adj = %d, but expect %d.", val, value);
> +       }
>  }

Sorry, the above patch is based on my private v3.1, plz ignore it.

For better read, here give the whole function:

+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,
skipping OOM the adjustement");
+               return;
+       }
+
+       if (pid == 0)
+               sprintf(score_path, "/proc/self/oom_score_adj");
+       else
+               sprintf(score_path, "/proc/%d/oom_score_adj", pid);
+
+       if (access(score_path, F_OK) == -1)
+               tst_brk(TBROK, "%s does not exist, please check if PID
is valid");
+
+       FILE_PRINTF(score_path, "%d", value);
+       FILE_SCANF(score_path, "%d", &val);
+
+       if (val != value) {
+               if (value < 0) {
+                       tst_res(TINFO, "Warning: %s cannot be set to
negative value,
+                               please check if test run with root user.",
+                               score_path);
+                       return
+               }
+               tst_brk(TBROK, "oom_score_adj = %d, but expect %d.",
val, value);
+       }
+}

-- 
Regards,
Li Wang



More information about the ltp mailing list