Taskset: check first whether affinity is settable
Subsystems (2)
v2 cover Taskset: check first whether affinity is settable
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
PATCH v2 1/1 Taskset: check first whether affinity is settable
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.
--- schedutils/taskset.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+)
schedutils/taskset.c +53 -0
@@ -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 *tif (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 );
Karel Zak
maintainerQuoted 3 lines · Carsten Emde
If the
PF_NO_SETAFFINITYprocess 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
include/procfs.h +1 -0
@@ -30,6 +30,7 @@ extern ssize_t procfs_process_get_cmdline(struct path_cxt *pc, char *buf, size_textern 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) {
lib/procfs.c +57 -1
82 lines not on this page · raw message
schedutils/taskset.c +17 -1
@@ -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) {
Signature · 2 lines
Karel Zak <[email protected]> http://karelzak.blogspot.com
Carsten Emde
Tested-byHi Karel,
Quoted 11 lines · Karel Zak
Quoted 3 lines · Carsten Emde
If the
PF_NO_SETAFFINITYprocess 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.
Quoted 2 lines
What about the patch below? (I have moved /proc/#/stat parsing to
lib/procfs.cto keeptastset.cmore readable.)
Indeed, that is much better, thanks a lot!
Applied your patch and tested it successfully, you may add my
if you wish.
Thanks again!
Carsten