[Python-checkins] benchmarks: Add --affinity command line option

victor.stinner python-checkins at python.org
Wed Feb 3 09:21:37 EST 2016


https://hg.python.org/benchmarks/rev/a97acad3bbf7
changeset:   238:a97acad3bbf7
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Wed Feb 03 15:20:02 2016 +0100
summary:
  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.

files:
  perf.py |  40 ++++++++++++++++++++++++++++++++++++++++
  1 files changed, 40 insertions(+), 0 deletions(-)


diff --git a/perf.py b/perf.py
--- a/perf.py
+++ b/perf.py
@@ -88,6 +88,30 @@
 info = logging.info
 
 
+def check_taskset():
+    """Check the taskset command is available.
+
+    Taskset specifies CPU affinity for a given workload, meaning that we can
+    run the benchmarks on a given core to minimize run to run variation.
+
+    Return None on success. Return an error message on error.
+    """
+    with open(os.devnull, "wb") as dev_null:
+        # Try to pin a temporary Python process to CPU #0
+        command = ["taskset", "--cpu-list", "0",
+                   sys.executable, "-c", "pass"]
+        try:
+            exitcode = subprocess.call(command,
+                                       stdout=dev_null, stderr=dev_null)
+        except OSError as exc:
+            return ("Command taskset not found: %s" % exc)
+
+    if exitcode != 0:
+        return ("Command taskset failed with exit code %s" % exitcode)
+
+    return None
+
+
 def interpreter_version(python, _cache={}):
     """Return the interpreter version for the given Python interpreter.
     *python* is the base command (as a list) to execute the interpreter.
@@ -2557,6 +2581,11 @@
                             " Unladen Swallow binaries. This is useful for"
                             " examining many benchmarks for optimization"
                             " effects."))
+    parser.add_option("--affinity", metavar="CPU_LIST", default=None,
+                      help=("Specify CPU affinity for benchmark runs. This "
+                            "way, benchmarks can be forced to run on a given "
+                            "CPU to minimize run to run variation. This uses "
+                            "the taskset command."))
 
 
     options, args = parser.parse_args(argv)
@@ -2575,6 +2604,17 @@
     base_cmd_prefix = [base] + base_args
     changed_cmd_prefix = [changed] + changed_args
 
+    if options.affinity:
+        err_msg = check_taskset()
+        if err_msg:
+            print("ERROR: Option --affinity not available. %s" % err_msg,
+                  file=sys.stderr)
+            sys.exit(1)
+
+        taskset_prefix = ["taskset", "--cpu-list", options.affinity]
+        base_cmd_prefix = taskset_prefix + base_cmd_prefix
+        changed_cmd_prefix = taskset_prefix + changed_cmd_prefix
+
     logging.basicConfig(level=logging.INFO)
 
     if options.track_memory:

-- 
Repository URL: https://hg.python.org/benchmarks


More information about the Python-checkins mailing list