* [PATCH v2 0/1] Taskset: check first whether affinity is settable
@ 2022-08-06 22:49 Carsten Emde
2022-08-06 22:49 ` [PATCH v2 1/1] " Carsten Emde
0 siblings, 1 reply; 4+ messages in thread
From: Carsten Emde @ 2022-08-06 22:49 UTC (permalink / raw)
To: util-linux
Sorry, need v2, forgot that task commands may have spaces nowadays.
Current version successfully tested with various commands:
# taskset -p 3fff 3
pid 3's current affinity mask: ffff
warning: affinity cannot be set due to PF_NO_SETAFFINITY flag set
taskset: failed to set pid 3's affinity: Invalid argument
# Command "PCIe PME"
# taskset -p 3fff 191
pid 191's current affinity mask: 8
pid 191's new affinity mask: 3fff
Signed-off-by: Carsten Emde <C.Emde@osadl.org>
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2 1/1] Taskset: check first whether affinity is settable
2022-08-06 22:49 [PATCH v2 0/1] Taskset: check first whether affinity is settable Carsten Emde
@ 2022-08-06 22:49 ` Carsten Emde
2022-08-08 13:35 ` Karel Zak
0 siblings, 1 reply; 4+ messages in thread
From: Carsten Emde @ 2022-08-06 22:49 UTC (permalink / raw)
To: util-linux; +Cc: Carsten Emde
If the PF_NO_SETAFFINITY process flag is set, the user may not
know why the affinity is not settable. Write a respective message
and do not attempt to set the affinity of this process.
Signed-off-by: Carsten Emde <C.Emde@osadl.org>
---
schedutils/taskset.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 53 insertions(+)
Index: util-linux/schedutils/taskset.c
===================================================================
--- util-linux.orig/schedutils/taskset.c
+++ util-linux/schedutils/taskset.c
@@ -35,6 +35,11 @@
#include "c.h"
#include "closestream.h"
+#ifndef PF_NO_SETAFFINITY
+#define PF_NO_SETAFFINITY 0x04000000
+#endif
+#define PF_NO_SETAFFINITY_ITEM 9
+
struct taskset {
pid_t pid; /* task PID */
cpu_set_t *set; /* task CPU mask */
@@ -114,6 +119,8 @@ static void __attribute__((__noreturn__)
static void do_taskset(struct taskset *ts, size_t setsize, cpu_set_t *set)
{
+ char statfilename[32];
+
/* read the current mask */
if (ts->pid) {
if (sched_getaffinity(ts->pid, ts->setsize, ts->set) < 0)
@@ -124,6 +131,52 @@ static void do_taskset(struct taskset *t
if (ts->get_only)
return;
+ /* check whether settable */
+ if (snprintf(statfilename, sizeof(statfilename), "/proc/%d/stat", ts->pid) > 0) {
+ int statfd;
+ char *nosetwarning =
+ "warning: affinity cannot be set due to PF_NO_SETAFFINITY flag set\n";
+
+ if ((statfd = open(statfilename, O_RDONLY)) > 0) {
+ char stat[1024];
+ int got;
+
+ if ((got = read(statfd, stat, sizeof(stat))) > 0) {
+ char *endptr, *token = stat;
+ int flags, item = PF_NO_SETAFFINITY_ITEM-1;
+
+ token[got] = '\0';
+ while (token != NULL && --item > 0) {
+ token = strchr(token, ' ');
+ if (token != NULL)
+ token++;
+ if (*token != '\0' && *token == '(') {
+ token = strchr(token, ')');
+ if (token != NULL && *token != '\0')
+ token++;
+ if (*token != '\0')
+ token++;
+ }
+ }
+ if (item == 0 && token != NULL && *token != '\0') {
+ char *nextspace = strchr(token, ' ');
+
+ if (nextspace != NULL)
+ *nextspace = '\0';
+ flags = strtol(token, &endptr, 10);
+ if (endptr == token + strlen(token)) {
+ if (flags & PF_NO_SETAFFINITY) {
+ fputs(nosetwarning, stderr);
+ errno = EINVAL;
+ err_affinity(ts->pid, 1);
+ }
+ }
+ }
+ }
+ close(statfd);
+ }
+ }
+
/* set new mask */
if (sched_setaffinity(ts->pid, setsize, set) < 0)
err_affinity(ts->pid, 1);
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2 1/1] Taskset: check first whether affinity is settable
2022-08-06 22:49 ` [PATCH v2 1/1] " Carsten Emde
@ 2022-08-08 13:35 ` Karel Zak
2022-08-09 21:54 ` Carsten Emde
0 siblings, 1 reply; 4+ messages in thread
From: Karel Zak @ 2022-08-08 13:35 UTC (permalink / raw)
To: Carsten Emde; +Cc: util-linux
On Sun, Aug 07, 2022 at 12:49:16AM +0200, Carsten Emde wrote:
> If the PF_NO_SETAFFINITY process flag is set, the user may not
> know why the affinity is not settable. Write a respective message
> and do not attempt to set the affinity of this process.
I'm unsure if constantly checking for the flag rather than calling
sched_setaffinity() is the right way. It seems pretty expensive.
I don't like when userspace tries to predict any kernel policy or
behavior. It seems better to call the syscall and, if necessary,
compose a better (detailed) error message.
What about the patch below? (I have moved /proc/#/stat parsing to
lib/procfs.c to keep tastset.c more readable.)
Karel
diff --git a/include/procfs.h b/include/procfs.h
index 5a730c94c..825689bc2 100644
--- a/include/procfs.h
+++ b/include/procfs.h
@@ -30,6 +30,7 @@ extern ssize_t procfs_process_get_cmdline(struct path_cxt *pc, char *buf, size_t
extern ssize_t procfs_process_get_cmdname(struct path_cxt *pc, char *buf, size_t bufsz);
extern ssize_t procfs_process_get_stat(struct path_cxt *pc, char *buf, size_t bufsz);
+extern int procfs_process_get_stat_nth(struct path_cxt *pc, int n, uintmax_t *re);
static inline ssize_t procfs_process_get_exe(struct path_cxt *pc, char *buf, size_t bufsz)
{
diff --git a/lib/procfs.c b/lib/procfs.c
index 0d58857c8..1072a9a2c 100644
--- a/lib/procfs.c
+++ b/lib/procfs.c
@@ -169,6 +169,35 @@ ssize_t procfs_process_get_stat(struct path_cxt *pc, char *buf, size_t bufsz)
return procfs_process_get_line_for(pc, buf, bufsz, "stat");
}
+int procfs_process_get_stat_nth(struct path_cxt *pc, int n, uintmax_t *re)
+{
+ ssize_t rc;
+ char *key = NULL, *tok, *p;
+ char buf[BUFSIZ];
+ int i;
+
+ if (n == 2 || n == 3) /* process name and status (strings) */
+ return -EINVAL;
+
+ rc = procfs_process_get_line_for(pc, buf, sizeof(buf), "stat");
+ if (rc < 0)
+ return rc;
+
+ for (i = 0, tok = strtok_r(buf, " ", &key); tok;
+ tok = strtok_r(NULL, " ", &key)) {
+
+ i++;
+ if (i == n)
+ return ul_strtou64(tok, re, 10);
+
+ /* skip rest of the process name */
+ if (i == 2 && (p = strchr(key, ')')))
+ key = p + 2;
+ }
+
+ return -EINVAL;
+}
+
int procfs_process_get_uid(struct path_cxt *pc, uid_t *uid)
{
struct stat sb;
@@ -547,6 +576,30 @@ static int test_isprocfs(int argc, char *argv[])
return is ? EXIT_SUCCESS : EXIT_FAILURE;
}
+static int test_process_stat_nth(int argc, char *argv[])
+{
+ pid_t pid;
+ struct path_cxt *pc;
+ uintmax_t num = 0;
+ int n;
+
+ if (argc != 3)
+ return EXIT_FAILURE;
+ pid = strtol(argv[1], (char **) NULL, 10);
+ n = strtol(argv[2], (char **) NULL, 10);
+
+ pc = ul_new_procfs_path(pid, NULL);
+ if (!pc)
+ err(EXIT_FAILURE, "cannot alloc procfs handler");
+
+ if (procfs_process_get_stat_nth(pc, n, &num) != 0)
+ err(EXIT_FAILURE, "read %dth number failed", n);
+
+ printf("%d: %dth %ju\n", (int) pid, n, num);
+ ul_unref_path(pc);
+ return EXIT_SUCCESS;
+}
+
int main(int argc, char *argv[])
{
if (argc < 2) {
@@ -554,7 +607,8 @@ int main(int argc, char *argv[])
" %1$s --fds <pid>\n"
" %1$s --is-procfs [<dir>]\n"
" %1$s --processes [---name <name>] [--uid <uid>]\n"
- " %1$s --one <pid>\n",
+ " %1$s --one <pid>\n"
+ " %1$s --stat-nth <pid> <n>\n",
program_invocation_short_name);
return EXIT_FAILURE;
}
@@ -569,6 +623,8 @@ int main(int argc, char *argv[])
return test_isprocfs(argc - 1, argv + 1);
if (strcmp(argv[1], "--one") == 0)
return test_one_process(argc - 1, argv + 1);
+ if (strcmp(argv[1], "--stat-nth") == 0)
+ return test_process_stat_nth(argc - 1, argv + 1);
return EXIT_FAILURE;
}
diff --git a/schedutils/taskset.c b/schedutils/taskset.c
index 0ab7d12e2..d2d1a869e 100644
--- a/schedutils/taskset.c
+++ b/schedutils/taskset.c
@@ -112,6 +112,8 @@ static void __attribute__((__noreturn__)) err_affinity(pid_t pid, int set)
err(EXIT_FAILURE, msg, pid ? pid : getpid());
}
+#define PF_NO_SETAFFINITY 0x04000000
+
static void do_taskset(struct taskset *ts, size_t setsize, cpu_set_t *set)
{
/* read the current mask */
@@ -125,8 +127,22 @@ static void do_taskset(struct taskset *ts, size_t setsize, cpu_set_t *set)
return;
/* set new mask */
- if (sched_setaffinity(ts->pid, setsize, set) < 0)
+ if (sched_setaffinity(ts->pid, setsize, set) < 0) {
+ uintmax_t flags = 0;
+ struct path_cxt *pc;
+ int errsv = errno;
+
+ if (errno != EPERM
+ && (pc = ul_new_procfs_path(ts->pid, NULL))
+ && procfs_process_get_stat_nth(pc, 9, &flags) == 0
+ && (flags & PF_NO_SETAFFINITY)) {
+ warnx(_("affinity cannot be set due to PF_NO_SETAFFINITY flag set"));
+ errno = EINVAL;
+ } else
+ errno = errsv;
+
err_affinity(ts->pid, 1);
+ }
/* re-read the current mask */
if (ts->pid) {
--
Karel Zak <kzak@redhat.com>
http://karelzak.blogspot.com
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v2 1/1] Taskset: check first whether affinity is settable
2022-08-08 13:35 ` Karel Zak
@ 2022-08-09 21:54 ` Carsten Emde
0 siblings, 0 replies; 4+ messages in thread
From: Carsten Emde @ 2022-08-09 21:54 UTC (permalink / raw)
To: Karel Zak; +Cc: util-linux
Hi Karel,
On 8/8/22 3:35 PM, Karel Zak wrote:
> On Sun, Aug 07, 2022 at 12:49:16AM +0200, Carsten Emde wrote:
>> If the PF_NO_SETAFFINITY process flag is set, the user may not
>> know why the affinity is not settable. Write a respective message
>> and do not attempt to set the affinity of this process.
>
> I'm unsure if constantly checking for the flag rather than calling
> sched_setaffinity() is the right way. It seems pretty expensive.
>
> I don't like when userspace tries to predict any kernel policy or
> behavior. It seems better to call the syscall and, if necessary,
> compose a better (detailed) error message.
Good point.
> What about the patch below? (I have moved /proc/#/stat parsing to
> lib/procfs.c to keep tastset.c more readable.)
Indeed, that is much better, thanks a lot!
Applied your patch and tested it successfully, you may add my
Tested-by: Carsten Emde <C.Emde@osadl.org>
if you wish.
Thanks again!
Carsten
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2022-08-09 22:01 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-06 22:49 [PATCH v2 0/1] Taskset: check first whether affinity is settable Carsten Emde
2022-08-06 22:49 ` [PATCH v2 1/1] " Carsten Emde
2022-08-08 13:35 ` Karel Zak
2022-08-09 21:54 ` Carsten Emde
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).