2

Is it possible to tell whether a process/thread has the PF_NO_SETAFFINITY flag set? I'm running taskset on a series of process ids and some are throwing errors of the following form:

taskset: failed to set pid 30's affinity: Invalid argument

I believe this is because some processes have PF_NO_SETAFFINITY set (see Answer).

Thank you!

Will
  • 1,171
  • 2
  • 14
  • 26

1 Answers1

3

Yes - look at /proc/PID/stat's 'flag' field

<linux/sched.h

#define PF_NO_SETAFFINITY 0x04000000        /* Userland is not allowed to meddle with cpus_allowed */

Look here for details on using /proc:

Example:

ps -eaf
www-data 30084 19962  0 07:09 ?        00:00:00 /usr/sbin/apache2 -k start
...

cat /proc/30084/stat
30084 (apache2) S 19962 19962 19962 0 -1 4194624 554 0 3 0 0 0 0 0 20 0 1 0 298837672 509616128 5510 18446744073709551615 1 1 0 0 0 0 0 16781312 201346799 0 0 0 17 0 0 0 0 0 0 0 0 0 0 0 0 0 0

The flags are 4194624


Q: Do you mind specifying how you'd write a simple script that outputs true/false based on whether you're allowed to set affinity?

A: I don't feel comfortable providing this without the opportunity to test, but you can try something like this...

flags=$(cut -f 9 -d ' ' /proc/30084/stat)
echo $(($flags & 0x40000000))
FoggyDay
  • 11,962
  • 4
  • 34
  • 48
  • This is amazing, thank you. Do you mind specifying how you'd write a simple script that outputs true/false based on whether you're allowed to set affinity? I'm not seeing references in any of the docs you linked to PF_NO_SETAFFINITY. Thank you! – Will Feb 06 '20 at 18:03
  • I would if I had a system handy to test it myself. But, unfortunately, I don't. Basically "true/false" is just "OR" 0x04000000. Look [here](https://stackoverflow.com/a/40667440/3135317) for a good example of doing bitwise operations in "bash". – FoggyDay Feb 06 '20 at 18:39
  • Ok, perhaps we can wait for someone to edit/post a solution using this info. – Will Feb 06 '20 at 19:48