util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/1] Taskset: check first whether affinity is settable
@ 2022-08-05 15:48 Carsten Emde
  2022-08-05 15:48 ` [PATCH 1/1] " Carsten Emde
  0 siblings, 1 reply; 2+ messages in thread
From: Carsten Emde @ 2022-08-05 15:48 UTC (permalink / raw)
  To: util-linux

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>

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH 1/1] Taskset: check first whether affinity is settable
  2022-08-05 15:48 [PATCH 0/1] Taskset: check first whether affinity is settable Carsten Emde
@ 2022-08-05 15:48 ` Carsten Emde
  0 siblings, 0 replies; 2+ messages in thread
From: Carsten Emde @ 2022-08-05 15:48 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 |   34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 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_FLAG 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,33 @@ 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;
+
+		if ((statfd = open(statfilename, S_IREAD)) > 0) {
+			char stat[1024];
+
+			if (read(statfd, stat, sizeof(stat)) > 0) {
+				char *endptr, *token = strtok(stat, " ");
+				int flags, flagno = PF_NO_SETAFFINITY_FLAG;
+
+				while (token && --flagno > 0)
+					token = strtok(NULL, " ");
+				if (flagno == 0 && *token != '\0') {
+					flags = strtol(token, &endptr, 10);
+					if (endptr == token + strlen(token)) {
+						if (flags & PF_NO_SETAFFINITY) {
+							fputs("warning: affinity cannot be set due to PF_NO_SETAFFINITY flag set\n", stderr);
+							errno = EINVAL;
+							err_affinity(ts->pid, 1);
+						}
+					}
+				}
+			}
+		}
+	}
+
 	/* set new mask */
 	if (sched_setaffinity(ts->pid, setsize, set) < 0)
 		err_affinity(ts->pid, 1);


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-08-05 15:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-05 15:48 [PATCH 0/1] Taskset: check first whether affinity is settable Carsten Emde
2022-08-05 15:48 ` [PATCH 1/1] " 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).