From 6c067fe6469244f19f2e3f03fc738b557e8de970 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Mon, 22 Apr 2019 16:13:13 +0200 Subject: [PATCH] Convert Python scripts to Python 3 and Blacken them Because 2019. --- misc/combine-trace-files | 27 ++++--- misc/summarize-trace-files | 75 ++++++++++--------- perf/perf.py | 143 ++++++++++++++++++++----------------- 3 files changed, 134 insertions(+), 111 deletions(-) diff --git a/misc/combine-trace-files b/misc/combine-trace-files index e0f5304f9..a581270ed 100755 --- a/misc/combine-trace-files +++ b/misc/combine-trace-files @@ -1,28 +1,27 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 -import sys import json +import sys traces = {} for arg in sys.argv[1:]: - object = json.load(open(arg)) - for event in object["traceEvents"]: + events = json.load(open(arg))["traceEvents"] + for event in events: if event["name"] == "" and event["ph"] == "I": time = float(event["args"]["time"]) # print "%.6f" % time - traces[time] = object + traces[time] = events break -times = traces.keys() -mintime = min(times) -times.sort() +times = sorted(traces) +min_time = min(times) -events = [] +combined_events = [] for time in times: - offset = (time - mintime) * 1000000.0 - object = traces[time] - for event in object["traceEvents"]: + offset = (time - min_time) * 1000000.0 + events = traces[time] + for event in events: event["ts"] = int(event["ts"] + offset) - events.append(event) + combined_events.append(event) -print json.dumps({"traceEvents": events}) +print(json.dumps({"traceEvents": combined_events})) diff --git a/misc/summarize-trace-files b/misc/summarize-trace-files index a52aa365d..3c65f052d 100755 --- a/misc/summarize-trace-files +++ b/misc/summarize-trace-files @@ -1,20 +1,21 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 -import sys import json +import sys trace = json.load(sys.stdin) events = trace["traceEvents"] -slotevents = [] +slot_events = [] -events.sort(key = lambda event: event["ts"]) +events.sort(key=lambda event: event["ts"]) jobs = int(sys.argv[1]) pids = {} busy = [None] * jobs + def find_slot(pid): if pid in pids: return pids[pid] @@ -25,6 +26,7 @@ def find_slot(pid): return slot return None + def end_slot(pid): for slot in range(jobs): if busy[slot] == pid: @@ -33,50 +35,59 @@ def end_slot(pid): return slot return slot + name = {} slot = -1 for event in events: - cat = event['cat'] - pid = event['pid'] - phase = event['ph'] - args = event['args'] + cat = event["cat"] + pid = event["pid"] + phase = event["ph"] + args = event["args"] - if phase == 'M' and event['name'] == "thread_name": + if phase == "M" and event["name"] == "thread_name": name[pid] = args["name"] if cat != "program": continue - if phase == 'B' or phase == 'S': + if phase == "B" or phase == "S": slot = find_slot(pid) - elif phase == 'E' or phase == 'F': + elif phase == "E" or phase == "F": slot = end_slot(pid) - elif phase == 'M': + elif phase == "M": pass else: continue - event['pid'] = slot - event['tid'] = pid + event["pid"] = slot + event["tid"] = pid - slotevents.append(event) + slot_events.append(event) -slotevents.sort(key = lambda event: event["tid"]) +slot_events.sort(key=lambda event: event["tid"]) -for event in slotevents: - if event['cat'] == "program": - event['cat'] = "ccache" - if event['tid'] in name: - event['name'] = name[event['tid']] - else: - if event['tid'] in name: - event['name'] = event['name'] + ':' + name[event['tid']] - del event['tid'] - if event['ph'] == 'S': - event['ph'] = 'B' - if event['ph'] == 'F': - event['ph'] = 'E' +for event in slot_events: + if event["cat"] == "program": + event["cat"] = "ccache" + if event["tid"] in name: + event["name"] = name[event["tid"]] + elif event["tid"] in name: + event["name"] = event["name"] + ":" + name[event["tid"]] + del event["tid"] + if event["ph"] == "S": + event["ph"] = "B" + elif event["ph"] == "F": + event["ph"] = "E" for slot in range(jobs): - slotevents.append({"cat":"","pid":slot,"tid":0,"ph":"M","name":"process_name","args":{"name":"Job %d" % slot}}) - -json.dump({"traceEvents": slotevents}, sys.stdout, indent=4) + slot_events.append( + { + "cat": "", + "pid": slot, + "tid": 0, + "ph": "M", + "name": "process_name", + "args": {"name": "Job %d" % slot}, + } + ) + +json.dump({"traceEvents": slot_events}, sys.stdout, indent=4) diff --git a/perf/perf.py b/perf/perf.py index acb410f26..954a7558a 100755 --- a/perf/perf.py +++ b/perf/perf.py @@ -1,4 +1,4 @@ -#! /usr/bin/env python +#! /usr/bin/env python3 # # Copyright (C) 2010-2019 Joel Rosdahl # @@ -19,8 +19,15 @@ from optparse import OptionParser from os import access, environ, mkdir, getpid, X_OK from os.path import ( - abspath, basename, exists, isabs, isfile, join as joinpath, realpath, - splitext) + abspath, + basename, + exists, + isabs, + isfile, + join as joinpath, + realpath, + splitext, +) from shutil import rmtree from subprocess import call from time import time @@ -49,7 +56,8 @@ PHASES = [ "with ccache, direct mode, cache miss", "with ccache, direct mode, cache hit", "with ccache, depend mode, cache miss", - "with ccache, depend mode, cache hit"] + "with ccache, depend mode, cache hit", +] verbose = False @@ -80,10 +88,11 @@ def test(tmp_dir, options, compiler_args, source_file): progress("Creating source code\n") for i in range(times): - fp = open("%s/%d%s" % (src_dir, i, extension), "w") - fp.write(open(source_file).read()) - fp.write("\nint ccache_perf_test_%d;\n" % i) - fp.close() + with open("%s/%d%s" % (src_dir, i, extension), "w") as fp: + with open(source_file) as fp2: + content = fp2.read() + fp.write(content) + fp.write("\nint ccache_perf_test_%d;\n" % i) environment = {"CCACHE_DIR": ccache_dir, "PATH": environ["PATH"]} environment["CCACHE_COMPILERCHECK"] = options.compilercheck @@ -111,7 +120,8 @@ def test(tmp_dir, options, compiler_args, source_file): env["CCACHE_DEPEND"] = "1" if call(args, env=env) != 0: sys.stderr.write( - 'Error running "%s"; please correct\n' % " ".join(args)) + 'Error running "%s"; please correct\n' % " ".join(args) + ) sys.exit(1) # Warm up the disk cache. @@ -201,25 +211,29 @@ def test(tmp_dir, options, compiler_args, source_file): def print_result_as_text(result): - for (i, x) in enumerate(PHASES): - print "%-43s %6.2f s (%6.2f %%) (%5.2f x)" % ( - x.capitalize() + ":", - result[i], - 100 * (result[i] / result[0]), - result[0] / result[i]) + for i, x in enumerate(PHASES): + print( + "%-43s %6.2f s (%6.2f %%) (%5.2f x)" + % ( + x.capitalize() + ":", + result[i], + 100 * (result[i] / result[0]), + result[0] / result[i], + ) + ) def print_result_as_xml(result): - print '' - print "" - for (i, x) in enumerate(PHASES): - print "" - print "%s" % x.capitalize() - print "%.2f" % result[i] - print "%.2f" % (100 * (result[i] / result[0])) - print "%.2f" % (result[0] / result[i]) - print "" - print "" + print('') + print("") + for i, x in enumerate(PHASES): + print("") + print("%s" % x.capitalize()) + print("%.2f" % result[i]) + print("%.2f" % (100 * (result[i] / result[0]))) + print("%.2f" % (result[0] / result[i])) + print("") + print("") def on_off(x): @@ -241,55 +255,52 @@ def main(argv): op = OptionParser(usage=USAGE, description=DESCRIPTION) op.disable_interspersed_args() op.add_option( - "--ccache", - help="location of ccache (default: %s)" % DEFAULT_CCACHE) + "--ccache", help="location of ccache (default: %s)" % DEFAULT_CCACHE + ) op.add_option( - "--compilercheck", - help="specify compilercheck (default: mtime)") + "--compilercheck", help="specify compilercheck (default: mtime)" + ) + op.add_option("--compression", help="use compression", action="store_true") op.add_option( - "--compression", - help="use compression", - action="store_true") - op.add_option( - "-d", "--directory", + "-d", + "--directory", help=( "where to create the temporary directory with the cache and other" - " files (default: %s)" % DEFAULT_DIRECTORY)) - op.add_option( - "--hardlink", - help="use hard links", - action="store_true") + " files (default: %s)" % DEFAULT_DIRECTORY + ), + ) + op.add_option("--hardlink", help="use hard links", action="store_true") op.add_option( "--hit-factor", help=( "how many times more to compile the file for cache hits (default:" - " %d)" % DEFAULT_HIT_FACTOR), - type="int") + " %d)" % DEFAULT_HIT_FACTOR + ), + type="int", + ) op.add_option( - "--nostats", - help="don't write statistics", - action="store_true") + "--nostats", help="don't write statistics", action="store_true" + ) op.add_option( - "-n", "--times", + "-n", + "--times", help=( - "number of times to compile the file (default: %d)" - % DEFAULT_TIMES), - type="int") - op.add_option( - "-v", "--verbose", - help="print progress messages", - action="store_true") + "number of times to compile the file (default: %d)" % DEFAULT_TIMES + ), + type="int", + ) op.add_option( - "--xml", - help="print result as XML", - action="store_true") + "-v", "--verbose", help="print progress messages", action="store_true" + ) + op.add_option("--xml", help="print result as XML", action="store_true") op.set_defaults( ccache=DEFAULT_CCACHE, compilercheck="mtime", directory=DEFAULT_DIRECTORY, hit_factor=DEFAULT_HIT_FACTOR, - times=DEFAULT_TIMES) - (options, args) = op.parse_args(argv[1:]) + times=DEFAULT_TIMES, + ) + options, args = op.parse_args(argv[1:]) if len(args) < 2: op.error("Missing arguments; pass -h/--help for help") @@ -304,16 +315,18 @@ def main(argv): if "ccache" in basename(realpath(compiler)): op.error( "%s seems to be a symlink to ccache; please specify the path to" - " the real compiler instead" % compiler) + " the real compiler instead" % compiler + ) if not options.xml: - print "Compilation command: %s -c -o %s.o" % ( - " ".join(args), - splitext(argv[-1])[0]) - print "Compilercheck:", options.compilercheck - print "Compression:", on_off(options.compression) - print "Hardlink:", on_off(options.hardlink) - print "Nostats:", on_off(options.nostats) + print( + "Compilation command: %s -c -o %s.o" + % (" ".join(args), splitext(argv[-1])[0]) + ) + print("Compilercheck:", options.compilercheck) + print("Compression:", on_off(options.compression)) + print("Hardlink:", on_off(options.hardlink)) + print("Nostats:", on_off(options.nostats)) tmp_dir = "%s/perfdir.%d" % (abspath(options.directory), getpid()) recreate_dir(tmp_dir) -- 2.47.2