library: add provision for displaying autogroup values

In the link referenced below there's an explanation of
the linux autogroup feature which has been around ever
since linux-2.6.38. With that explanation there's also
surprising (maybe shocking) revelations about the nice
and renice commands if CONFIG_SCHED_AUTOGROUP was set.

When autogroups are active, such programs are rendered
mostly useless because the nice value will only affect
scheduling priority relative to other processes in the
same autogroup. In order to accomplish what we thought
of as renice, that nice value in /proc/<pid>/autogroup
must be changed. Altering any single member of a group
will also affect every other member of that autogroup.

So, this commit will set the stage for users of newlib
to display autogroup identifiers plus their associated
nice values (now that their importance is understood).

Reference(s):
https://github.com/nlburgin/reallynice

Signed-off-by: Jim Warner <james.warner@comcast.net>
This commit is contained in:
Jim Warner
2021-08-05 00:00:00 -05:00
committed by Craig Small
parent 93c0a6cedf
commit 631e5d91f3
4 changed files with 43 additions and 1 deletions

View File

@ -1066,6 +1066,28 @@ static char *readlink_exe (const char *path){
}
// Provide the autogroup fields (or -1 if not available)
static void autogroup_fill (const char *path, proc_t *p) {
char buf[PROCPATHLEN], *str;
int fd, in;
p->autogrp_id = -1;
snprintf(buf, sizeof(buf), "%s/autogroup", path);
if ((fd = open(buf, O_RDONLY, 0)) != -1) {
in = read(fd, buf, sizeof(buf) - 1);
close(fd);
if (in > 0) {
buf[in] = '\0';
if ((str = strstr(buf, "-")))
p->autogrp_id = atoi(++str);
if ((str = strstr(buf, "nice")))
p->autogrp_nice = atoi(str + sizeof("nice"));
// above sizeof includes null, skips space ahead of #
}
}
}
///////////////////////////////////////////////////////////////////////
/* These are some nice GNU C expression subscope "inline" functions.
@ -1203,6 +1225,9 @@ static proc_t *simple_readproc(PROCTAB *restrict const PT, proc_t *restrict cons
rc += 1;
}
if (flags & PROC_FILLAUTOGRP) // value the 2 autogroup fields
autogroup_fill(path, p);
if (rc == 0) return p;
errno = ENOMEM;
next_proc:
@ -1322,6 +1347,9 @@ static proc_t *simple_readtask(PROCTAB *restrict const PT, proc_t *restrict cons
if (flags & PROC_FILL_LUID)
t->luid = login_uid(path);
if (flags & PROC_FILLAUTOGRP) // value the 2 autogroup fields
autogroup_fill(path, t);
if (rc == 0) return t;
errno = ENOMEM;
next_task: