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

Li Wang liwang@redhat.com
Tue Dec 21 11:12:33 CET 2021


On Tue, Dec 21, 2021 at 5:57 PM Cyril Hrubis <chrubis@suse.cz> wrote:

> Hi!
> > Not exactly, if someone gives a wrong PID, that also cannot find
> > the score_path. So we shouldn't skip OOM adjustment only
> > with printing the TFINO.
>
> Right, we would have to check if the /proc/$PID/ directory exists
> first.

Not necessary, because we did check /proc/self/oom_score_adj
already, otherwise, we have no chance to get here.

> > +       if (access(score_path, F_OK) == -1)
> > +               tst_brk(TBROK, "%s does not exist, please check if PID is
> > valid");
>
> Maybe we should print the pid in the message here as well.

Yes, and the score_path will include the pid, here I forget in the sentence.

> > +       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.",
>
> I would say that we TBROK here, otherwise it could be silently ignored.
> Also I would shorten the message to something as:

Hmm, maybe tst_res(TWARN, ...), is enough, I don't want to
let people who run LTP in ordinary users get TBROK here
since they might even don't need the oom protection.

So, I will push (the improved) code like below, after getting
Petr and you ack again:

+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 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", score_path);
+       }
+
+       FILE_PRINTF(score_path, "%d", value);
+       FILE_SCANF(score_path, "%d", &val);
+
+       if (val != value) {
+               if (value < 0) {
+                       tst_res(TWARN, "'%s' cannot be set to %i, are
you root?",
+                               score_path, value);
+                       return
+               }
+               tst_brk(TBROK, "oom_score_adj = %d, but expect %d.",
val, value);
+       }
+}


-- 
Regards,
Li Wang



More information about the ltp mailing list