Title: Add CPU affinity to
Type: performance Stage:
Components: Benchmarks Versions: Python 3.6, Python 2.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, florin.papa, pitrou, vstinner, yselivanov
Priority: normal Keywords: patch

Created on 2016-02-03 09:36 by florin.papa, last changed 2016-02-03 14:45 by vstinner. This issue is now closed.

File name Uploaded Description Edit
affinity.patch florin.papa, 2016-02-03 09:36
affinity_v2.patch florin.papa, 2016-02-03 11:47
affinity_v3.patch florin.papa, 2016-02-03 13:10
affinity_warning.patch vstinner, 2016-02-03 13:47
Messages (11)
msg259464 - (view) Author: Florin Papa (florin.papa) * Date: 2016-02-03 09:36
Hi all,

This is Florin Papa from the Dynamic Scripting Languages Optimizations Team from Intel Corporation.

The patch submitted adds an affinity feature to the Grand Unified Python Benchmarks suite to allow running benchmarks on a given CPU/set of CPUs. It is implemented for Linux and uses the taskset command to bond a command to the CPUs specified. This minimizes run to run variation, as we can get considerable differences in measured performance from running a benchmark on different cores. The taskset command receives a CPU mask that specifies which cores in the system will be used for the command.

python –-affinity=0x1
     will use processor #0
python –-affinity=0x3
    will use processors #0 and #1 

Thank you,
msg259468 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-02-03 10:03
I like the feature, but I would prefer to use a syntax like --affinity=0-2 to use cores 0, 1 and 2, rather than having to compute manually a binary mask (--affinity=3).

It's a builtin feature of taskset: just use --cpu-list.
msg259474 - (view) Author: Florin Papa (florin.papa) * Date: 2016-02-03 11:47
Thank you for the feedback. Please see the updated patch.
msg259477 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-02-03 12:26
I don't think that it's useful to check at runtime that taskset works. At
least, I expect an error if the option is used and the command fails.

To use CPU affinity you need to know your hardware and your platform. For
most stable results you may even configure the Linux kernel to ignore some
cores. So i expect that users will not play with affinity if they don't
understand it.
msg259479 - (view) Author: Florin Papa (florin.papa) * Date: 2016-02-03 13:10
I have updated the patch to throw an exception if the taskset command is not available. Not checking that taskset works will generate an ambiguous error (if there is no taskset on the system) that does not explicitly point to the problem.

I believe that users working on performance have knowledge on the platform and OS setup they are using and they will draw benefit from this feature because it gives more accurate results.
msg259481 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-02-03 13:47
Hum, I modified your change:

* only display a warning if the option is used but taskset doesn't work
* remove check on Linux: taskset might be available on other platforms, it doesn't really matter to check the OS, we already check if the command works
* try to run "taskset --cpu-list 0 python -c pass" instead of "taskset -V" to check if taskset works or not

What do you think?
msg259482 - (view) Author: Florin Papa (florin.papa) * Date: 2016-02-03 13:55
I was thinking that a warning might go unnoticed, that is why I chose to 
end the program if taskset is not found. It would force the user to
install missing packages.

Otherwise the patch looks ok.
msg259483 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-02-03 14:22
Florent: "I was thinking that a warning might go unnoticed, that is why I chose to end the program if taskset is not found."

Oh right, I kept your behaviour and pushed a change.

changeset:   238:a97acad3bbf7
tag:         tip
user:        Victor Stinner <>
date:        Wed Feb 03 15:20:02 2016 +0100
Add --affinity command line option

If used, run the benchmark using taskset to pin the process to a set of CPU
cores to reduce context switching. See taskset manual page for more information.

Patch written by Florin Papa.
msg259484 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-02-03 14:22
Thanks for your contribution ;-)
msg259485 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-02-03 14:25
"Florent" oops, Florin, sorry :-p
msg259486 - (view) Author: Florin Papa (florin.papa) * Date: 2016-02-03 14:35
No problem :)
Date User Action Args
2016-02-03 14:45:20vstinnersetstatus: open -> closed
resolution: fixed
2016-02-03 14:35:06florin.papasetstatus: closed -> open
resolution: fixed -> (no value)
messages: + msg259486
2016-02-03 14:25:01vstinnersetmessages: + msg259485
2016-02-03 14:22:47vstinnersetstatus: open -> closed
resolution: fixed
messages: + msg259484
2016-02-03 14:22:36vstinnersetmessages: + msg259483
2016-02-03 13:55:51florin.papasetmessages: + msg259482
2016-02-03 13:47:53vstinnersetfiles: + affinity_warning.patch

messages: + msg259481
2016-02-03 13:10:10florin.papasetfiles: + affinity_v3.patch

messages: + msg259479
2016-02-03 12:26:05vstinnersetmessages: + msg259477
2016-02-03 11:47:34florin.papasetfiles: + affinity_v2.patch

messages: + msg259474
2016-02-03 10:03:21vstinnersettype: performance
2016-02-03 10:03:15vstinnersetnosy: + yselivanov
2016-02-03 10:03:02vstinnersetnosy: + vstinner
messages: + msg259468
2016-02-03 09:36:08florin.papacreate