-
Notifications
You must be signed in to change notification settings - Fork 20
/
time_adapter.py
150 lines (129 loc) · 6.2 KB
/
time_adapter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# Copyright (c) 2009-2014 Stefan Marr <http://www.stefan-marr.de/>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import re
import subprocess
from .adapter import GaugeAdapter, OutputNotParseable
from ..model.data_point import DataPoint
from ..model.measurement import Measurement
class TimeAdapter(GaugeAdapter):
"""TimePerformance uses the systems time utility to allow measurement of
unmodified programs or aspects which need to cover the whole program
execution time.
"""
re_time = re.compile(r"^(\w+)\s*(\d+)m(\d+\.\d+)s")
re_time2 = re.compile(r"^(\w+)(\s*)(\d+\.\d+)")
# To be sure about how to parse the output, give custom format
# This avoids issues with perhaps none-standard /bin/usr/time
time_format = '"max rss (kb): %M\nwall-time (secounds): %e\n"'
re_formatted_time = re.compile(r"^wall-time \(secounds\): (\d+\.\d+)")
re_formatted_rss = re.compile(r"^max rss \(kb\): (\d+)")
_completed_time_availability_check = False
_use_formatted_time = False
_time_bin = None
def __init__(self, include_faulty, executor):
GaugeAdapter.__init__(self, include_faulty, executor)
def _create_command(self, command):
assert self._completed_time_availability_check
if self._use_formatted_time:
return "%s -f %s %s" % (self._time_bin, TimeAdapter.time_format, command)
else:
# use standard, but without info on memory
# TODO: add support for reading out memory info on OS X
return "/usr/bin/time -p %s" % command
def acquire_command(self, run_id):
command = run_id.cmdline()
if not self._completed_time_availability_check:
self._check_which_time_command_is_available()
return self._create_command(command)
def _check_which_time_command_is_available(self):
time_bin = '/usr/bin/time'
try:
formatted_output = subprocess.call(
['/usr/bin/time', '-f', TimeAdapter.time_format, '/bin/sleep', '0'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except OSError:
formatted_output = 1
if formatted_output == 1:
try:
formatted_output = subprocess.call(
['/opt/local/bin/gtime', '-f', TimeAdapter.time_format, '/bin/sleep', '0'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if formatted_output == 0:
time_bin = '/opt/local/bin/gtime'
except OSError:
formatted_output = 1
TimeAdapter._use_formatted_time = formatted_output == 0
TimeAdapter._time_bin = time_bin
TimeAdapter._completed_time_availability_check = True
def parse_data(self, data, run_id, invocation):
iteration = 1
data_points = []
current = DataPoint(run_id)
total_measure = None
for line in data.split("\n"):
if self.check_for_error(line):
return None
if self._use_formatted_time:
match1 = self.re_formatted_rss.match(line)
match2 = self.re_formatted_time.match(line)
if match1:
mem_kb = float(match1.group(1))
measure = Measurement(invocation, iteration, mem_kb, 'kb', run_id, 'MaxRSS')
current.add_measurement(measure)
elif match2:
time = float(match2.group(1)) * 1000
measure = Measurement(invocation, iteration, time, 'ms', run_id, 'total')
current.add_measurement(measure)
data_points.append(current)
current = DataPoint(run_id)
iteration += 1
else:
match1 = self.re_time.match(line)
match2 = self.re_time2.match(line)
if match1 or match2:
match = match1 or match2
criterion = 'total' if match.group(1) == 'real' else match.group(1)
time = (float(match.group(2).strip() or 0) * 60 +
float(match.group(3))) * 1000
measure = Measurement(invocation, iteration, time, 'ms', run_id, criterion)
if measure.is_total():
total_measure = measure
else:
current.add_measurement(measure)
else:
measure = None
if current.number_of_measurements() == 3 and \
current.get_total_value() is not None:
data_points.append(current)
current = DataPoint(run_id)
iteration += 1
if total_measure:
current.add_measurement(total_measure)
data_points.append(current)
if not data_points:
raise OutputNotParseable(data)
return data_points
class TimeManualAdapter(TimeAdapter):
"""TimeManualPerformance works like TimePerformance but does expect the
user to use the /usr/bin/time manually.
This is useful for runs on remote machines like the Tilera or ARM boards.
"""
def acquire_command(self, run_id):
return run_id.cmdline()