From c8809728536437dba7168de95fd0077a18a9c518 Mon Sep 17 00:00:00 2001 From: Douglas RAILLARD Date: Thu, 6 Sep 2018 15:32:07 +0100 Subject: [PATCH 1/9] futurize: Run futurize to convert to Python2/3 code Run the futurize tool to convert to python 2 and 3 compatible code: https://pypi.org/project/future/ $ futurize -wn -u **.py --- bart/__init__.py | 1 + bart/common/Analyzer.py | 2 ++ bart/common/Utils.py | 2 ++ bart/common/__init__.py | 1 + bart/common/signal.py | 6 +++++- bart/sched/SchedAssert.py | 10 ++++++--- bart/sched/SchedMatrix.py | 4 ++++ bart/sched/SchedMultiAssert.py | 8 +++++--- bart/sched/__init__.py | 1 + bart/sched/functions.py | 9 ++++++--- bart/sched/pelt.py | 37 +++++++++++++++++++--------------- bart/thermal/ThermalAssert.py | 8 ++++++-- bart/thermal/__init__.py | 1 + bart/version.py | 1 + docs/api_reference/conf.py | 1 + docs/examples/thermal.py | 1 + setup.py | 2 ++ tests/pelt.py | 11 ++++++---- tests/test_common_utils.py | 1 + tests/test_pelt_sim.py | 13 +++++++----- tests/test_sched_assert.py | 1 + tests/test_sched_functions.py | 1 + tests/test_signal.py | 9 ++++++--- tests/utils_tests.py | 1 + 24 files changed, 92 insertions(+), 40 deletions(-) diff --git a/bart/__init__.py b/bart/__init__.py index 886dbc8..744e13a 100644 --- a/bart/__init__.py +++ b/bart/__init__.py @@ -14,6 +14,7 @@ # """Initialization for bart""" +from __future__ import unicode_literals import bart.sched import bart.common diff --git a/bart/common/Analyzer.py b/bart/common/Analyzer.py index 7bb55c9..ed405c0 100644 --- a/bart/common/Analyzer.py +++ b/bart/common/Analyzer.py @@ -18,7 +18,9 @@ also intended to have aggregator based functionality. This is not implemented yet. """ +from __future__ import unicode_literals +from builtins import object from trappy.stats.grammar import Parser import warnings import numpy as np diff --git a/bart/common/Utils.py b/bart/common/Utils.py index 034cf74..e026c79 100644 --- a/bart/common/Utils.py +++ b/bart/common/Utils.py @@ -14,7 +14,9 @@ # """Utility functions for sheye""" +from __future__ import unicode_literals +from past.builtins import basestring import trappy import numpy as np diff --git a/bart/common/__init__.py b/bart/common/__init__.py index f42522b..fde8341 100644 --- a/bart/common/__init__.py +++ b/bart/common/__init__.py @@ -14,6 +14,7 @@ # """Initialization for bart.common""" +from __future__ import unicode_literals from bart.common import Utils diff --git a/bart/common/signal.py b/bart/common/signal.py index acf7091..9551b2c 100644 --- a/bart/common/signal.py +++ b/bart/common/signal.py @@ -45,7 +45,11 @@ - :code:`"trappy.event.class:event_column"` """ +from __future__ import division +from __future__ import unicode_literals +from builtins import object +from past.utils import old_div from trappy.stats.grammar import Parser from trappy.stats import StatConf from bart.common.Utils import area_under_curve, interval_sum @@ -242,7 +246,7 @@ def conditional_compare(self, condition, **kwargs): duration = min(a_piv.last_valid_index(), b_piv.last_valid_index()) duration -= max(a_piv.first_valid_index(), b_piv.first_valid_index()) - duration = interval_sum(mask[pivot_val], step=step) / duration + duration = old_div(interval_sum(mask[pivot_val], step=step), duration) if self._pivot: result[self._pivot][pivot_val] = area, duration diff --git a/bart/sched/SchedAssert.py b/bart/sched/SchedAssert.py index 5ecfec9..31fb4b2 100755 --- a/bart/sched/SchedAssert.py +++ b/bart/sched/SchedAssert.py @@ -18,7 +18,11 @@ The analysis is based on TRAPpy's statistics framework and is potent enough to aggregate statistics over processor hierarchies. """ +from __future__ import division +from __future__ import unicode_literals +from builtins import object +from past.utils import old_div import trappy import itertools import math @@ -122,7 +126,7 @@ def _aggregator(self, aggfunc): :type: function(:mod:`pandas.Series`) """ - if aggfunc not in self._aggs.keys(): + if aggfunc not in list(self._aggs.keys()): self._aggs[aggfunc] = MultiTriggerAggregator(self._triggers, self._topology, aggfunc) @@ -181,7 +185,7 @@ def getResidency(self, level, node, window=None, percent=False): if percent: total = agg.aggregate(level="all", window=window)[0] node_value = node_value * 100 - node_value = node_value / total + node_value = old_div(node_value, total) return node_value @@ -392,7 +396,7 @@ def getRuntime(self, window=None, percent=False): total_time = self._ftrace.get_duration() run_time = run_time * 100 - run_time = run_time / total_time + run_time = old_div(run_time, total_time) return run_time diff --git a/bart/sched/SchedMatrix.py b/bart/sched/SchedMatrix.py index 4595469..46a25ce 100755 --- a/bart/sched/SchedMatrix.py +++ b/bart/sched/SchedMatrix.py @@ -63,8 +63,12 @@ assertSiblings(A3, 2, operator.eq) assertSiblings(A4, 2, operator.eq) """ +from __future__ import unicode_literals +from builtins import str +from builtins import range +from builtins import object import sys import trappy import numpy as np diff --git a/bart/sched/SchedMultiAssert.py b/bart/sched/SchedMultiAssert.py index 32ea17d..148f928 100755 --- a/bart/sched/SchedMultiAssert.py +++ b/bart/sched/SchedMultiAssert.py @@ -15,7 +15,9 @@ """A library for asserting scheduler scenarios based on the statistics aggregation framework""" +from __future__ import unicode_literals +from builtins import object import re import inspect import trappy @@ -253,7 +255,7 @@ def getCPUBusyTime(self, level, node, window=None, percent=False): """ residencies = self.getResidency(level, node, window=window) - busy_time = sum(v["residency"] for v in residencies.itervalues()) + busy_time = sum(v["residency"] for v in iter(residencies.values())) if percent: if window: @@ -273,7 +275,7 @@ def generate_events(self, level, window=None): """ events = {} - for s_assert in self._asserts.values(): + for s_assert in list(self._asserts.values()): events[s_assert.name] = s_assert.generate_events(level, window=window) return events @@ -291,7 +293,7 @@ def plot(self, level="cpu", window=None, xlim=None): xlim = list(window) events = self.generate_events(level, window) - names = [s.name for s in self._asserts.values()] + names = [s.name for s in list(self._asserts.values())] num_lanes = self._topology.level_span(level) lane_prefix = level.upper() + ": " return trappy.EventPlot(events, names, xlim, diff --git a/bart/sched/__init__.py b/bart/sched/__init__.py index c0cc5c4..cfc42a4 100644 --- a/bart/sched/__init__.py +++ b/bart/sched/__init__.py @@ -14,6 +14,7 @@ # """Initialization for bart.sched""" +from __future__ import unicode_literals from bart.sched import SchedAssert diff --git a/bart/sched/functions.py b/bart/sched/functions.py index d1b17d4..3dc6b9b 100644 --- a/bart/sched/functions.py +++ b/bart/sched/functions.py @@ -56,7 +56,10 @@ .. seealso:: :mod:`trappy.stats.Topology.Topology` """ +from __future__ import division +from __future__ import unicode_literals +from past.utils import old_div import numpy as np from trappy.stats.Trigger import Trigger @@ -180,7 +183,7 @@ def filter_small_gaps(series): """ start = None - for index, value in series.iteritems(): + for index, value in series.items(): if value == SCHED_SWITCH_IN: if start == None: @@ -298,7 +301,7 @@ def residency_sum(series, window=None): running = select_window(org_series.cumsum(), window) if running.values[0] == TASK_RUNNING and running.values[-1] == TASK_RUNNING: return window[1] - window[0] - except Exception,e: + except Exception as e: pass if len(s_in) != len(s_out): @@ -433,7 +436,7 @@ def binary_correlate(series_x, series_y): agree = len(series_x[series_x == series_y]) disagree = len(series_x[series_x != series_y]) - return (agree - disagree) / float(len(series_x)) + return old_div((agree - disagree), float(len(series_x))) def get_pids_for_process(ftrace, execname, cls=None): """Get the PIDs for a given process diff --git a/bart/sched/pelt.py b/bart/sched/pelt.py index ad5f88a..73a4403 100644 --- a/bart/sched/pelt.py +++ b/bart/sched/pelt.py @@ -31,7 +31,12 @@ behavior. Specifically, the Simulator requires also the PeriodicTask which signal has to be computed. """ +from __future__ import division +from __future__ import unicode_literals +from builtins import range +from builtins import object +from past.utils import old_div import math from collections import namedtuple as namedtuple @@ -143,8 +148,8 @@ def isRunning(self, time_ms): def __str__(self): return "PeriodicTask(start: {:.3f} [ms], period: {:.3f} [ms], run: {:.3f} [ms], "\ "duty_cycle: {:.3f} [%], pelt_avg: {:d})"\ - .format(self.start_us / 1e3, self.period_us / 1e3, - self.run_us / 1e3, self.duty_cycle_pct, + .format(old_div(self.start_us, 1e3), old_div(self.period_us, 1e3), + old_div(self.run_us, 1e3), self.duty_cycle_pct, self.pelt_avg) @@ -249,7 +254,7 @@ def __init__(self, init_value=0, half_life_ms=32, decay_cap_ms=None): self.half_life_ms = half_life_ms self.decay_cap_ms = decay_cap_ms - self._geom_y = pow(0.5, 1. / half_life_ms) + self._geom_y = pow(0.5, old_div(1., half_life_ms)) self._geom_u = float(self._signal_max) * (1. - self._geom_y) self.task = None @@ -313,7 +318,7 @@ def stableRange(self, task): raise ValueError("Wrong time for task parameter") def _to_pelt_samples(time_us): - return float(time_us) / self._sample_us + return old_div(float(time_us), self._sample_us) # Compute max value max_pelt = (1. - pow(self._geom_y, _to_pelt_samples(task.run_us))) @@ -428,8 +433,8 @@ def getSignal(self, task, start_s=0, end_s=10): pelt_value = self._geomSum(pelt_value, active_us) # Append PELT sample - sample = (_us_to_s(t_us), t_us / - self._sample_us, running, pelt_value) + sample = (_us_to_s(t_us), old_div(t_us, + self._sample_us), running, pelt_value) samples.append(sample) # Prepare for next sample computation @@ -537,7 +542,7 @@ def estimateInitialPeltValue(cls, first_val, first_event_time_s, :returns: int - Estimated value of PELT signal when the task starts """ - geom_y = pow(0.5, 1. / half_life_ms) + geom_y = pow(0.5, old_div(1., half_life_ms)) geom_u = float(cls._signal_max) * (1. - geom_y) # Compute period of time between when the task started and when the @@ -549,10 +554,10 @@ def estimateInitialPeltValue(cls, first_val, first_event_time_s, 'happens before the task starts') # Compute number of times the simulated PELT would be updated in this # period of time - updates_since_start = int(time_since_start / (cls._sample_us / 1e6)) + updates_since_start = int(old_div(time_since_start, (old_div(cls._sample_us, 1e6)))) pelt_val = first_val for i in range(updates_since_start): - pelt_val = (pelt_val - geom_u) / geom_y + pelt_val = old_div((pelt_val - geom_u), geom_y) return pelt_val @@ -588,8 +593,8 @@ def _s_to_us(time_s, interval_us=1e3, nearest_up=True): :type nearest_up: bool """ if nearest_up: - return interval_us * int(math.ceil((1e6 * time_s) / interval_us)) - return interval_us * int(math.floor((1e6 * time_s) / interval_us)) + return interval_us * int(math.ceil(old_div((1e6 * time_s), interval_us))) + return interval_us * int(math.floor(old_div((1e6 * time_s), interval_us))) def _ms_to_us(time_ms, interval_us=1e3, nearest_up=True): @@ -619,23 +624,23 @@ def _ms_to_us(time_ms, interval_us=1e3, nearest_up=True): :type nearest_up: bool """ if nearest_up: - return interval_us * int(math.ceil((1e3 * time_ms) / interval_us)) - return interval_us * int(math.floor((1e3 * time_ms) / interval_us)) + return interval_us * int(math.ceil(old_div((1e3 * time_ms), interval_us))) + return interval_us * int(math.floor(old_div((1e3 * time_ms), interval_us))) def _us_to_s(time_us): """Convert [us] into (float) [s] """ - return (float(time_us) / 1e6) + return (old_div(float(time_us), 1e6)) def _us_to_ms(time_us): """Convert [us] into (float) [ms] """ - return (float(time_us) / 1e3) + return (old_div(float(time_us), 1e3)) def _ms_to_s(time_ms): """Convert [ms] into (float) [s] """ - return (float(time_ms) / 1e3) + return (old_div(float(time_ms), 1e3)) diff --git a/bart/thermal/ThermalAssert.py b/bart/thermal/ThermalAssert.py index d0ffa78..d3f862d 100644 --- a/bart/thermal/ThermalAssert.py +++ b/bart/thermal/ThermalAssert.py @@ -15,7 +15,11 @@ """A thermal specific library to assert certain thermal behaviours """ +from __future__ import division +from __future__ import unicode_literals +from builtins import object +from past.utils import old_div from bart.common import Utils from bart.common.Analyzer import Analyzer import numpy as np @@ -77,8 +81,8 @@ def getThermalResidency(self, temp_range, window, percent=False): result[pivot] = sum((index - shift_index)[mask.values]) if percent: - result[pivot] = ( - result[pivot] * 100.0) / self._ftrace.get_duration() + result[pivot] = old_div(( + result[pivot] * 100.0), self._ftrace.get_duration()) return result diff --git a/bart/thermal/__init__.py b/bart/thermal/__init__.py index 6eefbc2..07507e3 100644 --- a/bart/thermal/__init__.py +++ b/bart/thermal/__init__.py @@ -14,6 +14,7 @@ # """Initialization for bart.thermal""" +from __future__ import unicode_literals import bart.thermal.ThermalAssert diff --git a/bart/version.py b/bart/version.py index e8f6831..f0791ca 100644 --- a/bart/version.py +++ b/bart/version.py @@ -1,3 +1,4 @@ +from __future__ import unicode_literals # Copyright 2016-2016 ARM Limited # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/docs/api_reference/conf.py b/docs/api_reference/conf.py index 32d6653..caec3c3 100644 --- a/docs/api_reference/conf.py +++ b/docs/api_reference/conf.py @@ -26,6 +26,7 @@ # All configuration values have a default; values that are commented out # serve to show the default. +from __future__ import unicode_literals import sys import os import shlex diff --git a/docs/examples/thermal.py b/docs/examples/thermal.py index 8fe3e95..95ea335 100644 --- a/docs/examples/thermal.py +++ b/docs/examples/thermal.py @@ -16,6 +16,7 @@ """ An example file for usage of Analyzer for thermal assertions """ +from __future__ import unicode_literals from bart.common.Analyzer import Analyzer from trappy.stats.Topology import Topology import unittest diff --git a/setup.py b/setup.py index 7d9f0ab..a6720ea 100644 --- a/setup.py +++ b/setup.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import unicode_literals +from past.builtins import execfile from setuptools import setup, find_packages diff --git a/tests/pelt.py b/tests/pelt.py index 6ba176d..bf6e43f 100644 --- a/tests/pelt.py +++ b/tests/pelt.py @@ -1,7 +1,10 @@ +from __future__ import division +from __future__ import unicode_literals +from past.utils import old_div from hypothesis import given from hypothesis.strategies import integers, tuples, none, one_of import unittest -from sys import maxint +from sys import maxsize from bart.sched.pelt import * @@ -22,7 +25,7 @@ nonneg_ints(), # start_sample nonneg_ints(), # run_samples none(), # duty_cycle_pct -).filter(lambda (period, _, run, __): run <= period) +).filter(lambda period___run___: period___run___[2] <= period___run___[0]) # Generate args for PeriodicTask::__init__ args using duty_cycle_pct periodic_task_args_pct = lambda: tuples( @@ -73,7 +76,7 @@ def test_signal_time_range(self, task_args, sim_args, signal_range): signal = sim.getSignal(task, start_s, end_s) # Should start no earlier than 1 sample before start_s - earliest_start = min(0, start_s - (sim._sample_us / 1.e6)) + earliest_start = min(0, start_s - (old_div(sim._sample_us, 1.e6))) self.assertGreaterEqual(signal.index[0], earliest_start) # Should start no later than start_s self.assertLessEqual(signal.index[0], start_s) @@ -81,7 +84,7 @@ def test_signal_time_range(self, task_args, sim_args, signal_range): # Should start no earlier than end_s self.assertGreaterEqual(signal.index[-1], end_s) # Should end no later than 1 sample after end_s - latest_start = end_s + (sim._sample_us / 1.e6) + latest_start = end_s + (old_div(sim._sample_us, 1.e6)) self.assertLessEqual(signal.index[-1], latest_start) if __name__ == "__main__": diff --git a/tests/test_common_utils.py b/tests/test_common_utils.py index 09b31e3..ce2d68a 100644 --- a/tests/test_common_utils.py +++ b/tests/test_common_utils.py @@ -1,3 +1,4 @@ +from __future__ import unicode_literals # Copyright 2015-2016 ARM Limited # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/tests/test_pelt_sim.py b/tests/test_pelt_sim.py index e609c75..b7f28e9 100644 --- a/tests/test_pelt_sim.py +++ b/tests/test_pelt_sim.py @@ -1,3 +1,5 @@ +from __future__ import division +from __future__ import unicode_literals # Copyright 2015-2016 ARM Limited # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,10 +15,11 @@ # limitations under the License. # +from past.utils import old_div from bart.sched.pelt import * from hypothesis import given from hypothesis.strategies import integers, tuples, none, one_of -from sys import maxint +from sys import maxsize from utils_tests import TestBART # Required to use `int` not `long` henx ma=maxint @@ -36,7 +39,7 @@ nonneg_ints(), # start_sample nonneg_ints(), # run_samples none(), # duty_cycle_pct -).filter(lambda (period, _, run, __): run <= period) +).filter(lambda period___run___: period___run___[2] <= period___run___[0]) # Generate args for PeriodicTask::__init__ args using duty_cycle_pct periodic_task_args_pct = lambda: tuples( @@ -92,7 +95,7 @@ def test_signal_time_range(self, task_args, sim_args, signal_range): signal = sim.getSignal(task, start_s, end_s) # Should start no earlier than 1 sample before start_s - earliest_start = min(0, start_s - (sim._sample_us / 1.e6)) + earliest_start = min(0, start_s - (old_div(sim._sample_us, 1.e6))) self.assertGreaterEqual(signal.index[0], earliest_start) # Should start no later than start_s self.assertLessEqual(signal.index[0], start_s) @@ -100,7 +103,7 @@ def test_signal_time_range(self, task_args, sim_args, signal_range): # Should start no earlier than end_s self.assertGreaterEqual(signal.index[-1], end_s) # Should end no later than 1 sample after end_s - latest_start = end_s + (sim._sample_us / 1.e6) + latest_start = end_s + (old_div(sim._sample_us, 1.e6)) self.assertLessEqual(signal.index[-1], latest_start) @given(periodic_task_args(), simulator_args()) @@ -113,7 +116,7 @@ def test_signal_mean_value(self, task_args, sim_args): signal = sim.getSignal(task) stats = sim.getStats() - expected_mean = (task.duty_cycle_pct * 1024) / 100 + expected_mean = old_div((task.duty_cycle_pct * 1024), 100) self.assertEqual(stats.pelt_avg, expected_mean) diff --git a/tests/test_sched_assert.py b/tests/test_sched_assert.py index 4f8c28b..9ff0fca 100644 --- a/tests/test_sched_assert.py +++ b/tests/test_sched_assert.py @@ -1,3 +1,4 @@ +from __future__ import unicode_literals # Copyright 2015-2016 ARM Limited # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/tests/test_sched_functions.py b/tests/test_sched_functions.py index 1a8d4ac..c09cc2b 100644 --- a/tests/test_sched_functions.py +++ b/tests/test_sched_functions.py @@ -1,3 +1,4 @@ +from __future__ import unicode_literals # Copyright 2016-2016 ARM Limited # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/tests/test_signal.py b/tests/test_signal.py index 48692a9..5dba388 100644 --- a/tests/test_signal.py +++ b/tests/test_signal.py @@ -1,3 +1,5 @@ +from __future__ import division +from __future__ import unicode_literals # Copyright 2015-2016 ARM Limited # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,6 +15,7 @@ # limitations under the License. # +from past.utils import old_div import pandas as pd import trappy from utils_tests import TestBART @@ -40,7 +43,7 @@ def test_conditional_compare(self): trace.add_parsed_event("event", df) s = SignalCompare(trace, "event:A", "event:B") - expected = (1.5, 2.0 / 7) + expected = (1.5, old_div(2.0, 7)) self.assertEqual( s.conditional_compare( "event:A > event:B", @@ -58,7 +61,7 @@ def test_get_overshoot(self): trace.add_parsed_event("event", df) s = SignalCompare(trace, "event:A", "event:B") - expected = (1.5, 2.0 / 7) + expected = (1.5, old_div(2.0, 7)) self.assertEqual( s.get_overshoot(method="rect"), expected) @@ -86,7 +89,7 @@ def test_get_undershoot(self): trace.add_parsed_event("event", df) s = SignalCompare(trace, "event:A", "event:B") - expected = (4.0 / 14.0, 1.0) + expected = (old_div(4.0, 14.0), 1.0) self.assertEqual( s.get_undershoot(method="rect"), expected) diff --git a/tests/utils_tests.py b/tests/utils_tests.py index 6dadca1..a7f0c5a 100644 --- a/tests/utils_tests.py +++ b/tests/utils_tests.py @@ -1,3 +1,4 @@ +from __future__ import unicode_literals # Copyright 2015-2016 ARM Limited # # Licensed under the Apache License, Version 2.0 (the "License"); From 1742c9c3c82447712a584fe14bd6df24f06e9287 Mon Sep 17 00:00:00 2001 From: Douglas RAILLARD Date: Thu, 6 Sep 2018 16:07:53 +0100 Subject: [PATCH 2/9] futurize: remove old_div Choose between true div and floor div and remove references to old_div. Since all files now have a from __future__ import division statement, both division operators (/ and //) will behave the same in Python 2 and 3 (with the Python 3 behavior). --- bart/common/signal.py | 4 ++-- bart/sched/SchedAssert.py | 6 +++--- bart/sched/functions.py | 4 ++-- bart/sched/pelt.py | 32 +++++++++++++++----------------- bart/thermal/ThermalAssert.py | 5 ++--- tests/pelt.py | 5 ++--- tests/test_pelt_sim.py | 9 ++++----- tests/test_signal.py | 7 +++---- 8 files changed, 33 insertions(+), 39 deletions(-) diff --git a/bart/common/signal.py b/bart/common/signal.py index 9551b2c..d01bc87 100644 --- a/bart/common/signal.py +++ b/bart/common/signal.py @@ -47,9 +47,9 @@ """ from __future__ import division from __future__ import unicode_literals +from __future__ import print_function from builtins import object -from past.utils import old_div from trappy.stats.grammar import Parser from trappy.stats import StatConf from bart.common.Utils import area_under_curve, interval_sum @@ -246,7 +246,7 @@ def conditional_compare(self, condition, **kwargs): duration = min(a_piv.last_valid_index(), b_piv.last_valid_index()) duration -= max(a_piv.first_valid_index(), b_piv.first_valid_index()) - duration = old_div(interval_sum(mask[pivot_val], step=step), duration) + duration = interval_sum(mask[pivot_val], step=step) / duration if self._pivot: result[self._pivot][pivot_val] = area, duration diff --git a/bart/sched/SchedAssert.py b/bart/sched/SchedAssert.py index 31fb4b2..ebb03e2 100755 --- a/bart/sched/SchedAssert.py +++ b/bart/sched/SchedAssert.py @@ -20,9 +20,9 @@ """ from __future__ import division from __future__ import unicode_literals +from __future__ import print_function from builtins import object -from past.utils import old_div import trappy import itertools import math @@ -185,7 +185,7 @@ def getResidency(self, level, node, window=None, percent=False): if percent: total = agg.aggregate(level="all", window=window)[0] node_value = node_value * 100 - node_value = old_div(node_value, total) + node_value = node_value / total return node_value @@ -396,7 +396,7 @@ def getRuntime(self, window=None, percent=False): total_time = self._ftrace.get_duration() run_time = run_time * 100 - run_time = old_div(run_time, total_time) + run_time = run_time / total_time return run_time diff --git a/bart/sched/functions.py b/bart/sched/functions.py index 3dc6b9b..9185f2f 100644 --- a/bart/sched/functions.py +++ b/bart/sched/functions.py @@ -58,8 +58,8 @@ """ from __future__ import division from __future__ import unicode_literals +from __future__ import print_function -from past.utils import old_div import numpy as np from trappy.stats.Trigger import Trigger @@ -436,7 +436,7 @@ def binary_correlate(series_x, series_y): agree = len(series_x[series_x == series_y]) disagree = len(series_x[series_x != series_y]) - return old_div((agree - disagree), float(len(series_x))) + return (agree - disagree) / len(series_x) def get_pids_for_process(ftrace, execname, cls=None): """Get the PIDs for a given process diff --git a/bart/sched/pelt.py b/bart/sched/pelt.py index 73a4403..ece7f9f 100644 --- a/bart/sched/pelt.py +++ b/bart/sched/pelt.py @@ -36,7 +36,6 @@ from builtins import range from builtins import object -from past.utils import old_div import math from collections import namedtuple as namedtuple @@ -148,8 +147,8 @@ def isRunning(self, time_ms): def __str__(self): return "PeriodicTask(start: {:.3f} [ms], period: {:.3f} [ms], run: {:.3f} [ms], "\ "duty_cycle: {:.3f} [%], pelt_avg: {:d})"\ - .format(old_div(self.start_us, 1e3), old_div(self.period_us, 1e3), - old_div(self.run_us, 1e3), self.duty_cycle_pct, + .format(self.start_us/1e3, self.period_us/1e3, + self.run_us/1e3, self.duty_cycle_pct, self.pelt_avg) @@ -254,7 +253,7 @@ def __init__(self, init_value=0, half_life_ms=32, decay_cap_ms=None): self.half_life_ms = half_life_ms self.decay_cap_ms = decay_cap_ms - self._geom_y = pow(0.5, old_div(1., half_life_ms)) + self._geom_y = pow(0.5, 1 / half_life_ms) self._geom_u = float(self._signal_max) * (1. - self._geom_y) self.task = None @@ -318,7 +317,7 @@ def stableRange(self, task): raise ValueError("Wrong time for task parameter") def _to_pelt_samples(time_us): - return old_div(float(time_us), self._sample_us) + return time_us / self._sample_us # Compute max value max_pelt = (1. - pow(self._geom_y, _to_pelt_samples(task.run_us))) @@ -433,8 +432,7 @@ def getSignal(self, task, start_s=0, end_s=10): pelt_value = self._geomSum(pelt_value, active_us) # Append PELT sample - sample = (_us_to_s(t_us), old_div(t_us, - self._sample_us), running, pelt_value) + sample = (_us_to_s(t_us), t_us/self._sample_us, running, pelt_value) samples.append(sample) # Prepare for next sample computation @@ -542,7 +540,7 @@ def estimateInitialPeltValue(cls, first_val, first_event_time_s, :returns: int - Estimated value of PELT signal when the task starts """ - geom_y = pow(0.5, old_div(1., half_life_ms)) + geom_y = pow(0.5, 1/half_life_ms) geom_u = float(cls._signal_max) * (1. - geom_y) # Compute period of time between when the task started and when the @@ -554,10 +552,10 @@ def estimateInitialPeltValue(cls, first_val, first_event_time_s, 'happens before the task starts') # Compute number of times the simulated PELT would be updated in this # period of time - updates_since_start = int(old_div(time_since_start, (old_div(cls._sample_us, 1e6)))) + updates_since_start = int(time_since_start/(cls._sample_us/1e6)) pelt_val = first_val for i in range(updates_since_start): - pelt_val = old_div((pelt_val - geom_u), geom_y) + pelt_val = (pelt_val - geom_u) / geom_y return pelt_val @@ -593,8 +591,8 @@ def _s_to_us(time_s, interval_us=1e3, nearest_up=True): :type nearest_up: bool """ if nearest_up: - return interval_us * int(math.ceil(old_div((1e6 * time_s), interval_us))) - return interval_us * int(math.floor(old_div((1e6 * time_s), interval_us))) + return interval_us * int(math.ceil((1e6 * time_s)/interval_us)) + return interval_us * int(math.floor((1e6 * time_s)/interval_us)) def _ms_to_us(time_ms, interval_us=1e3, nearest_up=True): @@ -624,23 +622,23 @@ def _ms_to_us(time_ms, interval_us=1e3, nearest_up=True): :type nearest_up: bool """ if nearest_up: - return interval_us * int(math.ceil(old_div((1e3 * time_ms), interval_us))) - return interval_us * int(math.floor(old_div((1e3 * time_ms), interval_us))) + return interval_us * int(math.ceil((1e3 * time_ms)/interval_us)) + return interval_us * int(math.floor((1e3 * time_ms)/interval_us)) def _us_to_s(time_us): """Convert [us] into (float) [s] """ - return (old_div(float(time_us), 1e6)) + return time_us/1e6 def _us_to_ms(time_us): """Convert [us] into (float) [ms] """ - return (old_div(float(time_us), 1e3)) + return time_us/1e3 def _ms_to_s(time_ms): """Convert [ms] into (float) [s] """ - return (old_div(float(time_ms), 1e3)) + return time_ms/1e3 diff --git a/bart/thermal/ThermalAssert.py b/bart/thermal/ThermalAssert.py index d3f862d..483746e 100644 --- a/bart/thermal/ThermalAssert.py +++ b/bart/thermal/ThermalAssert.py @@ -19,7 +19,6 @@ from __future__ import unicode_literals from builtins import object -from past.utils import old_div from bart.common import Utils from bart.common.Analyzer import Analyzer import numpy as np @@ -81,8 +80,8 @@ def getThermalResidency(self, temp_range, window, percent=False): result[pivot] = sum((index - shift_index)[mask.values]) if percent: - result[pivot] = old_div(( - result[pivot] * 100.0), self._ftrace.get_duration()) + result[pivot] = ( + (result[pivot] * 100.0)/self._ftrace.get_duration()) return result diff --git a/tests/pelt.py b/tests/pelt.py index bf6e43f..0abb065 100644 --- a/tests/pelt.py +++ b/tests/pelt.py @@ -1,6 +1,5 @@ from __future__ import division from __future__ import unicode_literals -from past.utils import old_div from hypothesis import given from hypothesis.strategies import integers, tuples, none, one_of import unittest @@ -76,7 +75,7 @@ def test_signal_time_range(self, task_args, sim_args, signal_range): signal = sim.getSignal(task, start_s, end_s) # Should start no earlier than 1 sample before start_s - earliest_start = min(0, start_s - (old_div(sim._sample_us, 1.e6))) + earliest_start = min(0, start_s - (im._sample_us/1.e6)) self.assertGreaterEqual(signal.index[0], earliest_start) # Should start no later than start_s self.assertLessEqual(signal.index[0], start_s) @@ -84,7 +83,7 @@ def test_signal_time_range(self, task_args, sim_args, signal_range): # Should start no earlier than end_s self.assertGreaterEqual(signal.index[-1], end_s) # Should end no later than 1 sample after end_s - latest_start = end_s + (old_div(sim._sample_us, 1.e6)) + latest_start = end_s + (sim._sample_us/1.e6) self.assertLessEqual(signal.index[-1], latest_start) if __name__ == "__main__": diff --git a/tests/test_pelt_sim.py b/tests/test_pelt_sim.py index b7f28e9..bc17621 100644 --- a/tests/test_pelt_sim.py +++ b/tests/test_pelt_sim.py @@ -15,7 +15,6 @@ # limitations under the License. # -from past.utils import old_div from bart.sched.pelt import * from hypothesis import given from hypothesis.strategies import integers, tuples, none, one_of @@ -95,7 +94,7 @@ def test_signal_time_range(self, task_args, sim_args, signal_range): signal = sim.getSignal(task, start_s, end_s) # Should start no earlier than 1 sample before start_s - earliest_start = min(0, start_s - (old_div(sim._sample_us, 1.e6))) + earliest_start = min(0, start_s - (sim._sample_us/1.e6)) self.assertGreaterEqual(signal.index[0], earliest_start) # Should start no later than start_s self.assertLessEqual(signal.index[0], start_s) @@ -103,7 +102,7 @@ def test_signal_time_range(self, task_args, sim_args, signal_range): # Should start no earlier than end_s self.assertGreaterEqual(signal.index[-1], end_s) # Should end no later than 1 sample after end_s - latest_start = end_s + (old_div(sim._sample_us, 1.e6)) + latest_start = end_s + (sim._sample_us/1.e6) self.assertLessEqual(signal.index[-1], latest_start) @given(periodic_task_args(), simulator_args()) @@ -116,7 +115,7 @@ def test_signal_mean_value(self, task_args, sim_args): signal = sim.getSignal(task) stats = sim.getStats() - expected_mean = old_div((task.duty_cycle_pct * 1024), 100) + expected_mean = (task.duty_cycle_pct * 1024)/100 - self.assertEqual(stats.pelt_avg, expected_mean) + self.assertEqual(math.floor(stats.pelt_avg), math.floor(expected_mean)) diff --git a/tests/test_signal.py b/tests/test_signal.py index 5dba388..ae78629 100644 --- a/tests/test_signal.py +++ b/tests/test_signal.py @@ -15,7 +15,6 @@ # limitations under the License. # -from past.utils import old_div import pandas as pd import trappy from utils_tests import TestBART @@ -43,7 +42,7 @@ def test_conditional_compare(self): trace.add_parsed_event("event", df) s = SignalCompare(trace, "event:A", "event:B") - expected = (1.5, old_div(2.0, 7)) + expected = (1.5, 2.0/7) self.assertEqual( s.conditional_compare( "event:A > event:B", @@ -61,7 +60,7 @@ def test_get_overshoot(self): trace.add_parsed_event("event", df) s = SignalCompare(trace, "event:A", "event:B") - expected = (1.5, old_div(2.0, 7)) + expected = (1.5, 2.0/7) self.assertEqual( s.get_overshoot(method="rect"), expected) @@ -89,7 +88,7 @@ def test_get_undershoot(self): trace.add_parsed_event("event", df) s = SignalCompare(trace, "event:A", "event:B") - expected = (old_div(4.0, 14.0), 1.0) + expected = (4.0/14.0, 1.0) self.assertEqual( s.get_undershoot(method="rect"), expected) From 96eb24a23f37f3cf1ef197a5acd308aeabf1b7a3 Mon Sep 17 00:00:00 2001 From: Douglas RAILLARD Date: Thu, 6 Sep 2018 16:16:12 +0100 Subject: [PATCH 3/9] futurize: Add from __future__ import ... Add a standard set of __future__ imports so that all the code will behave consistently accross the whole library. Otherwise, new code may be caught by a missing "from __future__ import division" and would get a different behavior than in another file of Bart. --- bart/__init__.py | 2 ++ bart/common/Analyzer.py | 2 ++ bart/common/Utils.py | 2 ++ bart/common/__init__.py | 3 ++- bart/sched/SchedMatrix.py | 3 ++- bart/sched/SchedMultiAssert.py | 2 ++ bart/sched/__init__.py | 3 ++- bart/sched/pelt.py | 3 ++- bart/thermal/ThermalAssert.py | 3 ++- bart/thermal/__init__.py | 3 ++- bart/version.py | 1 - docs/api_reference/conf.py | 4 +++- docs/examples/thermal.py | 3 +++ setup.py | 4 +++- tests/pelt.py | 4 +++- tests/test_common_utils.py | 4 +++- tests/test_pelt_sim.py | 5 +++-- tests/test_sched_assert.py | 5 +++-- tests/test_sched_functions.py | 4 +++- tests/test_signal.py | 5 +++-- tests/utils_tests.py | 5 +++-- 21 files changed, 50 insertions(+), 20 deletions(-) diff --git a/bart/__init__.py b/bart/__init__.py index 744e13a..6996bd8 100644 --- a/bart/__init__.py +++ b/bart/__init__.py @@ -15,6 +15,8 @@ """Initialization for bart""" from __future__ import unicode_literals +from __future__ import division +from __future__ import print_function import bart.sched import bart.common diff --git a/bart/common/Analyzer.py b/bart/common/Analyzer.py index ed405c0..2193e02 100644 --- a/bart/common/Analyzer.py +++ b/bart/common/Analyzer.py @@ -19,6 +19,8 @@ implemented yet. """ from __future__ import unicode_literals +from __future__ import division +from __future__ import print_function from builtins import object from trappy.stats.grammar import Parser diff --git a/bart/common/Utils.py b/bart/common/Utils.py index e026c79..e81e763 100644 --- a/bart/common/Utils.py +++ b/bart/common/Utils.py @@ -15,6 +15,8 @@ """Utility functions for sheye""" from __future__ import unicode_literals +from __future__ import division +from __future__ import print_function from past.builtins import basestring import trappy diff --git a/bart/common/__init__.py b/bart/common/__init__.py index fde8341..13577da 100644 --- a/bart/common/__init__.py +++ b/bart/common/__init__.py @@ -15,7 +15,8 @@ """Initialization for bart.common""" from __future__ import unicode_literals - +from __future__ import division +from __future__ import print_function from bart.common import Utils from bart.common import Analyzer diff --git a/bart/sched/SchedMatrix.py b/bart/sched/SchedMatrix.py index 46a25ce..aca791a 100755 --- a/bart/sched/SchedMatrix.py +++ b/bart/sched/SchedMatrix.py @@ -64,7 +64,8 @@ assertSiblings(A4, 2, operator.eq) """ from __future__ import unicode_literals - +from __future__ import division +from __future__ import print_function from builtins import str from builtins import range diff --git a/bart/sched/SchedMultiAssert.py b/bart/sched/SchedMultiAssert.py index 148f928..7c703c1 100755 --- a/bart/sched/SchedMultiAssert.py +++ b/bart/sched/SchedMultiAssert.py @@ -16,6 +16,8 @@ """A library for asserting scheduler scenarios based on the statistics aggregation framework""" from __future__ import unicode_literals +from __future__ import division +from __future__ import print_function from builtins import object import re diff --git a/bart/sched/__init__.py b/bart/sched/__init__.py index cfc42a4..3ea83af 100644 --- a/bart/sched/__init__.py +++ b/bart/sched/__init__.py @@ -15,7 +15,8 @@ """Initialization for bart.sched""" from __future__ import unicode_literals - +from __future__ import division +from __future__ import print_function from bart.sched import SchedAssert from bart.sched import SchedMultiAssert diff --git a/bart/sched/pelt.py b/bart/sched/pelt.py index ece7f9f..7ee2bde 100644 --- a/bart/sched/pelt.py +++ b/bart/sched/pelt.py @@ -31,8 +31,9 @@ behavior. Specifically, the Simulator requires also the PeriodicTask which signal has to be computed. """ -from __future__ import division from __future__ import unicode_literals +from __future__ import division +from __future__ import print_function from builtins import range from builtins import object diff --git a/bart/thermal/ThermalAssert.py b/bart/thermal/ThermalAssert.py index 483746e..e9dbfd3 100644 --- a/bart/thermal/ThermalAssert.py +++ b/bart/thermal/ThermalAssert.py @@ -15,8 +15,9 @@ """A thermal specific library to assert certain thermal behaviours """ -from __future__ import division from __future__ import unicode_literals +from __future__ import division +from __future__ import print_function from builtins import object from bart.common import Utils diff --git a/bart/thermal/__init__.py b/bart/thermal/__init__.py index 07507e3..b9627a2 100644 --- a/bart/thermal/__init__.py +++ b/bart/thermal/__init__.py @@ -15,6 +15,7 @@ """Initialization for bart.thermal""" from __future__ import unicode_literals - +from __future__ import division +from __future__ import print_function import bart.thermal.ThermalAssert diff --git a/bart/version.py b/bart/version.py index f0791ca..e8f6831 100644 --- a/bart/version.py +++ b/bart/version.py @@ -1,4 +1,3 @@ -from __future__ import unicode_literals # Copyright 2016-2016 ARM Limited # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/docs/api_reference/conf.py b/docs/api_reference/conf.py index caec3c3..8868bbd 100644 --- a/docs/api_reference/conf.py +++ b/docs/api_reference/conf.py @@ -25,8 +25,10 @@ # # All configuration values have a default; values that are commented out # serve to show the default. - from __future__ import unicode_literals +from __future__ import division +from __future__ import print_function + import sys import os import shlex diff --git a/docs/examples/thermal.py b/docs/examples/thermal.py index 95ea335..2ebc393 100644 --- a/docs/examples/thermal.py +++ b/docs/examples/thermal.py @@ -17,6 +17,9 @@ An example file for usage of Analyzer for thermal assertions """ from __future__ import unicode_literals +from __future__ import division +from __future__ import print_function + from bart.common.Analyzer import Analyzer from trappy.stats.Topology import Topology import unittest diff --git a/setup.py b/setup.py index a6720ea..0a2cdbe 100644 --- a/setup.py +++ b/setup.py @@ -14,10 +14,12 @@ # limitations under the License. # from __future__ import unicode_literals +from __future__ import division +from __future__ import print_function + from past.builtins import execfile from setuptools import setup, find_packages - execfile("bart/version.py") LONG_DESCRIPTION = """Behavioural Analysis involves the expressing the general diff --git a/tests/pelt.py b/tests/pelt.py index 0abb065..05323a6 100644 --- a/tests/pelt.py +++ b/tests/pelt.py @@ -1,5 +1,7 @@ -from __future__ import division from __future__ import unicode_literals +from __future__ import division +from __future__ import print_function + from hypothesis import given from hypothesis.strategies import integers, tuples, none, one_of import unittest diff --git a/tests/test_common_utils.py b/tests/test_common_utils.py index ce2d68a..2737e73 100644 --- a/tests/test_common_utils.py +++ b/tests/test_common_utils.py @@ -1,4 +1,3 @@ -from __future__ import unicode_literals # Copyright 2015-2016 ARM Limited # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,6 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import unicode_literals +from __future__ import division +from __future__ import print_function from bart.common import Utils from bart.common.Analyzer import Analyzer diff --git a/tests/test_pelt_sim.py b/tests/test_pelt_sim.py index bc17621..d6e4cc9 100644 --- a/tests/test_pelt_sim.py +++ b/tests/test_pelt_sim.py @@ -1,5 +1,3 @@ -from __future__ import division -from __future__ import unicode_literals # Copyright 2015-2016 ARM Limited # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,6 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import unicode_literals +from __future__ import division +from __future__ import print_function from bart.sched.pelt import * from hypothesis import given diff --git a/tests/test_sched_assert.py b/tests/test_sched_assert.py index 9ff0fca..18f293a 100644 --- a/tests/test_sched_assert.py +++ b/tests/test_sched_assert.py @@ -1,4 +1,3 @@ -from __future__ import unicode_literals # Copyright 2015-2016 ARM Limited # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,7 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. # - +from __future__ import unicode_literals +from __future__ import division +from __future__ import print_function from bart.sched.SchedAssert import SchedAssert from bart.sched.SchedMultiAssert import SchedMultiAssert diff --git a/tests/test_sched_functions.py b/tests/test_sched_functions.py index c09cc2b..ea3a746 100644 --- a/tests/test_sched_functions.py +++ b/tests/test_sched_functions.py @@ -1,4 +1,3 @@ -from __future__ import unicode_literals # Copyright 2016-2016 ARM Limited # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,6 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import unicode_literals +from __future__ import division +from __future__ import print_function import trappy diff --git a/tests/test_signal.py b/tests/test_signal.py index ae78629..71f2c5a 100644 --- a/tests/test_signal.py +++ b/tests/test_signal.py @@ -1,5 +1,3 @@ -from __future__ import division -from __future__ import unicode_literals # Copyright 2015-2016 ARM Limited # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,6 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import unicode_literals +from __future__ import division +from __future__ import print_function import pandas as pd import trappy diff --git a/tests/utils_tests.py b/tests/utils_tests.py index a7f0c5a..4c6ff12 100644 --- a/tests/utils_tests.py +++ b/tests/utils_tests.py @@ -1,4 +1,3 @@ -from __future__ import unicode_literals # Copyright 2015-2016 ARM Limited # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,7 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. # - +from __future__ import unicode_literals +from __future__ import division +from __future__ import print_function import unittest import os From 272fc61c77055bd5bda2aca1144aa20dffb1f533 Mon Sep 17 00:00:00 2001 From: Douglas RAILLARD Date: Thu, 6 Sep 2018 16:58:12 +0100 Subject: [PATCH 4/9] setup.py: Update for Python 3 compat * Add dependency on "future" package. It is used to provide Python-3-like helpers and modules that will run on both Python 2 and Python 3 * Add Python 3 as supported language * Use setup.py in .travis.yml so the requirements are aligned as much as possible --- .travis.yml | 14 ++++++-------- setup.py | 15 +++++++-------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/.travis.yml b/.travis.yml index d671e87..8ed2c9f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,7 @@ language: python python: - - "2.7" + - "2.7.12" + - "3.4.8" before_install: - sudo apt-get update -qq - sudo apt-get install -qq libfreetype6-dev @@ -8,15 +9,12 @@ before_install: - wget http://ftp.us.debian.org/debian/pool/main/t/trace-cmd/trace-cmd_2.4.0-1_amd64.deb - sudo dpkg -i trace-cmd_2.4.0-1_amd64.deb install: - - pip install matplotlib - pip install Cython --install-option="--no-cython-compile" - - pip install pandas - - pip install hypothesis - - pip install ipython[all] - - pip install --upgrade trappy + # IPython 6.0.0 requires Python 3.3. Use an older version so we can keep using + # Python 2.7 + - pip install "ipython[all]<6.0.0" + - python ./setup.py install script: nosetests -virtualenv: - system_site_packages: true notifications: email: recipients: diff --git a/setup.py b/setup.py index 0a2cdbe..a94aef2 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # Copyright 2015-2016 ARM Limited # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,14 +13,10 @@ # See the License for the specific language governing permissions and # limitations under the License. # -from __future__ import unicode_literals -from __future__ import division -from __future__ import print_function - -from past.builtins import execfile from setuptools import setup, find_packages -execfile("bart/version.py") +with open("bart/version.py") as f: + exec(f.read()) LONG_DESCRIPTION = """Behavioural Analysis involves the expressing the general expectation of the state of the system while targeting a single or set of heuristics. @@ -34,8 +30,10 @@ """ REQUIRES = [ - "TRAPpy>=3.0", + "pandas", + "trappy>=3.0", "hypothesis>=3.0", + "future", ] setup(name='bart-py', @@ -55,6 +53,7 @@ "License :: OSI Approved :: Apache Software License", "Operating System :: POSIX :: Linux", "Programming Language :: Python :: 2.7", + "Programming Language :: Python :: 3", # As we depend on trace data from the Linux Kernel/FTrace "Topic :: System :: Operating System Kernels :: Linux", "Topic :: Scientific/Engineering :: Visualization" From a42924c90a34e3a6e2c37636ce9098251aa5a612 Mon Sep 17 00:00:00 2001 From: Douglas RAILLARD Date: Thu, 6 Sep 2018 17:03:49 +0100 Subject: [PATCH 5/9] futurize: Cleanup useless list() calls Since a number of builtins like zip and map are returning iterators in Python 3 (instead of lists in Python 2), 2to3 adds calls to list() everywhere. Remove most of them as they are usually not needed. --- bart/sched/SchedAssert.py | 2 +- bart/sched/SchedMultiAssert.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bart/sched/SchedAssert.py b/bart/sched/SchedAssert.py index ebb03e2..1a6a73b 100755 --- a/bart/sched/SchedAssert.py +++ b/bart/sched/SchedAssert.py @@ -126,7 +126,7 @@ def _aggregator(self, aggfunc): :type: function(:mod:`pandas.Series`) """ - if aggfunc not in list(self._aggs.keys()): + if aggfunc not in self._aggs.keys(): self._aggs[aggfunc] = MultiTriggerAggregator(self._triggers, self._topology, aggfunc) diff --git a/bart/sched/SchedMultiAssert.py b/bart/sched/SchedMultiAssert.py index 7c703c1..175d333 100755 --- a/bart/sched/SchedMultiAssert.py +++ b/bart/sched/SchedMultiAssert.py @@ -277,7 +277,7 @@ def generate_events(self, level, window=None): """ events = {} - for s_assert in list(self._asserts.values()): + for s_assert in self._asserts.values(): events[s_assert.name] = s_assert.generate_events(level, window=window) return events @@ -295,7 +295,7 @@ def plot(self, level="cpu", window=None, xlim=None): xlim = list(window) events = self.generate_events(level, window) - names = [s.name for s in list(self._asserts.values())] + names = [s.name for s in self._asserts.values()] num_lanes = self._topology.level_span(level) lane_prefix = level.upper() + ": " return trappy.EventPlot(events, names, xlim, From ad290f49d5ae2912ec733ad017dfe01b30da01e3 Mon Sep 17 00:00:00 2001 From: Douglas RAILLARD Date: Thu, 6 Sep 2018 17:05:54 +0100 Subject: [PATCH 6/9] futurize: Cleanup useless iter() 2to3 seems to convert dict.iteritems() to iter(dict.items()) since .items() will return an iterator in Python 3 and a list in Python 2. This distinction is irrelevant in most places, so remove useless calls to iter(). --- bart/sched/SchedMultiAssert.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bart/sched/SchedMultiAssert.py b/bart/sched/SchedMultiAssert.py index 175d333..091ffc0 100755 --- a/bart/sched/SchedMultiAssert.py +++ b/bart/sched/SchedMultiAssert.py @@ -257,7 +257,7 @@ def getCPUBusyTime(self, level, node, window=None, percent=False): """ residencies = self.getResidency(level, node, window=window) - busy_time = sum(v["residency"] for v in iter(residencies.values())) + busy_time = sum(v["residency"] for v in residencies.values()) if percent: if window: From 9c6293e0e83f36a1f7843fa3df8b2155e14e2cdd Mon Sep 17 00:00:00 2001 From: Douglas RAILLARD Date: Fri, 7 Sep 2018 11:24:06 +0100 Subject: [PATCH 7/9] python3: remove use of inspect.ismethod In Python 3, class.method returns a function an not an unbound method as it used to in Python 2. This makes inspect.ismethod(class.method) False, so replace inspect.ismethod() with callable() builtin that will work in both cases. Also remove the use of dir() since it is meant for interactive use. Replace with inspect.getmembers(). Improve the debugging experience by using a function instead of a lambda to make it easier to find. --- bart/sched/SchedMultiAssert.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/bart/sched/SchedMultiAssert.py b/bart/sched/SchedMultiAssert.py index 091ffc0..e9b189b 100755 --- a/bart/sched/SchedMultiAssert.py +++ b/bart/sched/SchedMultiAssert.py @@ -22,6 +22,7 @@ from builtins import object import re import inspect +import functools import trappy from bart.sched import functions as sched_funcs from bart.sched.SchedAssert import SchedAssert @@ -180,21 +181,23 @@ def _populate_pids(self): return list(set(pids)) - def _create_method(self, attr_name): + def _create_method(self, attr, attr_name): """A wrapper function to create a dispatch function""" - return lambda *args, **kwargs: self._dispatch(attr_name, *args, **kwargs) + @functools.wraps(attr) + def wrapper(*args, **kwargs): + return self._dispatch(attr_name, *args, **kwargs) + + return wrapper def _populate_methods(self): """Populate Methods from SchedAssert""" - for attr_name in dir(SchedAssert): - attr = getattr(SchedAssert, attr_name) - + for attr_name, attr in inspect.getmembers(SchedAssert): valid_method = attr_name.startswith("get") or \ attr_name.startswith("assert") - if inspect.ismethod(attr) and valid_method: - func = self._create_method(attr_name) + if callable(attr) and valid_method: + func = self._create_method(attr, attr_name) setattr(self, attr_name, func) def get_task_name(self, pid): From 7c163aa036b1cfc2aac46704228c9789b1d64132 Mon Sep 17 00:00:00 2001 From: Douglas RAILLARD Date: Fri, 7 Sep 2018 11:40:25 +0100 Subject: [PATCH 8/9] python3: avoid using sys.maxint Since integers don't have limited size in Python 3, sys.maxint is gone. Replace its use by sys.maxsize, which is the closest approximation. --- tests/test_pelt_sim.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_pelt_sim.py b/tests/test_pelt_sim.py index d6e4cc9..c9957ca 100644 --- a/tests/test_pelt_sim.py +++ b/tests/test_pelt_sim.py @@ -19,10 +19,12 @@ from bart.sched.pelt import * from hypothesis import given from hypothesis.strategies import integers, tuples, none, one_of -from sys import maxsize from utils_tests import TestBART +import sys +import math -# Required to use `int` not `long` henx ma=maxint +# Required to use `int` not `long` on Python 2 ,hence ma=maxint +maxint = getattr(sys, 'maxint', sys.maxsize) nonneg_ints = lambda mi=0, ma=maxint: integers(min_value=mi, max_value=ma) # Generate a Simulator From b9a221e35f1bab2843043576b1718d63635995db Mon Sep 17 00:00:00 2001 From: Douglas RAILLARD Date: Fri, 7 Sep 2018 11:42:19 +0100 Subject: [PATCH 9/9] ftrace: avoid using raw trace text format TRAPpy does not require separate trace.txt and trace.raw.txt anymore. --- tests/test_sched_functions.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/tests/test_sched_functions.py b/tests/test_sched_functions.py index ea3a746..61828a6 100644 --- a/tests/test_sched_functions.py +++ b/tests/test_sched_functions.py @@ -46,7 +46,6 @@ def test_get_pids_for_process_funny_process_names(self): from bart.sched.functions import get_pids_for_process trace_file = "trace.txt" - raw_trace_file = "trace.raw.txt" in_data = """ -0 [001] 10826.894644: sched_switch: prev_comm=swapper/1 prev_pid=0 prev_prio=120 prev_state=0 next_comm=rt-app next_pid=3268 next_prio=120 wmig-3268 [001] 10826.894778: sched_switch: prev_comm=wmig prev_pid=3268 prev_prio=120 prev_state=1 next_comm=rt-app next_pid=3269 next_prio=120 wmig1-3269 [001] 10826.905152: sched_switch: prev_comm=wmig1 prev_pid=3269 prev_prio=120 prev_state=1 next_comm=wmig next_pid=3268 next_prio=120 @@ -57,16 +56,8 @@ def test_get_pids_for_process_funny_process_names(self): wmig1-3269 [005] 10827.031061: sched_switch: prev_comm=wmig1 prev_pid=3269 prev_prio=120 prev_state=0 next_comm=wmig next_pid=3268 next_prio=120 wmig-3268 [005] 10827.050645: sched_switch: prev_comm=wmig prev_pid=3268 prev_prio=120 prev_state=1 next_comm=swapper/5 next_pid=0 next_prio=120 """ - - # We create an empty trace.txt to please trappy ... with open(trace_file, "w") as fout: - fout.write("") - - # ... but we only put the sched_switch events in the raw trace - # file because that's where trappy is going to look for - with open(raw_trace_file, "w") as fout: fout.write(in_data) trace = trappy.FTrace(trace_file) - self.assertEquals(get_pids_for_process(trace, "wmig"), [3268])