Util-Linux Archive mirror
 help / color / mirror / Atom feed
From: Carsten Emde <C.Emde@osadl.org>
To: util-linux@vger.kernel.org
Cc: Carsten Emde <C.Emde@osadl.org>
Subject: [PATCH 1/1] Taskset: check first whether affinity is settable
Date: Fri, 05 Aug 2022 17:48:21 +0200	[thread overview]
Message-ID: <20220805154900.261920576@osadl.org> (raw)
In-Reply-To: 20220805154820.407972387@osadl.org

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);


      reply	other threads:[~2022-08-05 15:57 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-05 15:48 [PATCH 0/1] Taskset: check first whether affinity is settable Carsten Emde
2022-08-05 15:48 ` Carsten Emde [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220805154900.261920576@osadl.org \
    --to=c.emde@osadl.org \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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 read-only IMAP folder(s) and NNTP newsgroup(s).