forked from google/perfetto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_tool
executable file
·254 lines (206 loc) · 7.94 KB
/
load_tool
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/usr/bin/env python
# Copyright (C) 2019 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import logging
import os
import sys
import subprocess
import tempfile
ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
ADB_PATH = os.path.join(ROOT_DIR, 'buildtools/android_sdk/platform-tools/adb')
TEMPLATED_PERFETTO_CFG = '''
buffers {{
size_kb: 65536
fill_policy: RING_BUFFER
}}
data_sources {{
config {{
name: "linux.ftrace"
target_buffer: 0
ftrace_config {{
ftrace_events: "sched_switch"
buffer_size_kb: {buffer_size_kb}
drain_period_ms: {drain_period_ms}
}}
}}
}}
duration_ms: 15000
'''
PERFETTO_PER_CPU_BUFFER_SIZE_KB_PARAMS = [
128,
256,
512,
1 * 1024, # 1 MB
2 * 1024, # 2 MB
4 * 1024, # 4 MB
]
PERFETTO_DRAIN_RATE_MS_PARAMS = [
100,
240,
500,
1 * 1000, # 1s
2 * 1000, # 2s
5 * 1000, # 5s
]
BUSY_THREADS_NUM_THREADS_PARAMS = [
8,
32,
128,
]
BUSY_THREADS_DUTY_CYCLE_PARAMS = [
10,
100,
]
BUSY_THREADS_PERIOD_US_PARAMS = [
500,
1 * 1000, # 1 ms
10 * 1000, # 10 ms
]
TRACE_PROCESSOR_QUERY = """
SELECT
a.value as num_sched,
b.value as num_overrun
FROM (
SELECT COUNT(*) as value
FROM sched
) as a, (
SELECT SUM(value) as value
FROM stats
WHERE name = 'ftrace_cpu_overrun_end'
) as b
"""
def AdbArgs(*args):
cmd = [ADB_PATH] list([str(x) for x in args])
logging.debug('> adb ' ' '.join([str(x) for x in args]))
return cmd
def AdbCall(*args):
return subprocess.check_output(AdbArgs(*args)).decode('utf-8').rstrip()
def SingleTraceRun(out_dir, prio_name, buffer_size_kb, drain_rate_ms,
num_threads, duty_cycle, period_us):
busy_threads_args = AdbArgs('shell', '/data/local/tmp/busy_threads',
'--threads={}'.format(num_threads),
'--duty_cycle={}'.format(duty_cycle),
'--period_us={}'.format(period_us))
perfetto_args = AdbArgs('shell', 'perfetto', '--txt', '-c', '-', '-o', '-')
# Create a file object to read the trace into.
with tempfile.NamedTemporaryFile() as trace_file:
logging.info(
"Starting trace with parameters ({}, {}, {}, {}, {}, {})".format(
prio_name, buffer_size_kb, drain_rate_ms, num_threads, duty_cycle,
period_us))
# Start the busy threads running.
busy_threads_handle = subprocess.Popen(busy_threads_args)
# Start the Perfetto trace.
perfetto_handle = subprocess.Popen(
perfetto_args, stdin=subprocess.PIPE, stdout=trace_file)
# Create the config with the parameters
config = TEMPLATED_PERFETTO_CFG.format(
buffer_size_kb=buffer_size_kb, drain_period_ms=drain_rate_ms)
# Send the config to the Perfetto binary and wait for response.
perfetto_handle.stdin.write(config.encode())
perfetto_handle.stdin.close()
perfetto_ret = perfetto_handle.wait()
# Stop busy threads from running.
busy_threads_handle.terminate()
# Return any errors from Perfetto.
if perfetto_ret:
raise subprocess.CalledProcessError(
cmd=perfetto_args, returncode=perfetto_ret)
# TODO(lalitm): allow trace processor to take the query file from stdin
# to prevent this hack from being required.
with tempfile.NamedTemporaryFile() as trace_query_file:
trace_query_file.file.write(TRACE_PROCESSOR_QUERY.encode())
trace_query_file.file.flush()
# Run the trace processor on the config.
tp_path = os.path.join(out_dir, 'trace_processor_shell')
tp_out = subprocess.check_output(
[tp_path, '-q', trace_query_file.name, trace_file.name])
# Get the CSV output from trace processor (stripping the header).
[num_sched, num_overrun] = str(tp_out).split('\n')[1].split(',')
# Print the row to stdout.
sys.stdout.write('"{}",{},{},{},{},{},{},{}\n'.format(
prio_name, buffer_size_kb, drain_rate_ms, num_threads, duty_cycle,
period_us, num_sched, num_overrun))
def SinglePriorityRun(out_dir, prio_name):
for buffer_size_kb in PERFETTO_PER_CPU_BUFFER_SIZE_KB_PARAMS:
for drain_rate_ms in PERFETTO_DRAIN_RATE_MS_PARAMS:
for num_threads in BUSY_THREADS_NUM_THREADS_PARAMS:
for duty_cycle in BUSY_THREADS_DUTY_CYCLE_PARAMS:
for period_us in BUSY_THREADS_PERIOD_US_PARAMS:
SingleTraceRun(out_dir, prio_name, buffer_size_kb, drain_rate_ms,
num_threads, duty_cycle, period_us)
def CycleTracedAndProbes():
AdbCall('shell', 'stop', 'traced')
AdbCall('shell', 'stop', 'traced_probes')
AdbCall('shell', 'start', 'traced')
AdbCall('shell', 'start', 'traced_probes')
AdbCall('shell', 'sleep', '5')
traced_pid = AdbCall('shell', 'pidof', 'traced')
probes_pid = AdbCall('shell', 'pidof', 'traced_probes')
assert (traced_pid is not None and probes_pid is not None)
return (traced_pid, probes_pid)
def Main():
parser = argparse.ArgumentParser()
parser.add_argument('linux_out_dir', help='out/android/')
parser.add_argument('android_out_dir', help='out/android/')
args = parser.parse_args()
# Root ourselves on the device.
logging.info('Waiting for device and rooting ...')
AdbCall('wait-for-device')
AdbCall('root')
AdbCall('wait-for-device')
# Push busy threads to device
busy_threads_path = os.path.join(args.android_out_dir, 'busy_threads')
AdbCall('shell', 'rm', '-rf', '/data/local/tmp/perfetto_load_test')
AdbCall('shell', 'mkdir', '/data/local/tmp/perfetto_load_test')
AdbCall('push', busy_threads_path, '/data/local/tmp/perfetto_load_test/')
# Stop and start traced and traced_probes
(traced_pid, probes_pid) = CycleTracedAndProbes()
# Print the header for csv.
sys.stdout.write('"{}","{}","{}","{}","{}","{}","{}","{}"\n'.format(
'prio_name', 'buffer_size_kb', 'drain_rate_ms', 'num_threads',
'duty_cycle', 'period_us', 'num_sched', 'num_overrun'))
# First, do a single run in all configurations without changing prio.
SinglePriorityRun(args.linux_out_dir, 'Default')
# Stop and start traced and traced_probes
(traced_pid, probes_pid) = CycleTracedAndProbes()
# Setup the nice values and check them.
AdbCall('shell', 'renice', '-n', '-19', '-p', traced_pid)
AdbCall('shell', 'renice', '-n', '-19', '-p', probes_pid)
logging.debug(AdbCall('shell', 'cat', '/proc/{}/stat'.format(traced_pid)))
logging.debug(AdbCall('shell', 'cat', '/proc/{}/stat'.format(probes_pid)))
# Do the run.
SinglePriorityRun(args.linux_out_dir, '-19 Nice')
# Stop and start traced and traced_probes
(traced_pid, probes_pid) = CycleTracedAndProbes()
# Then do a run with FIFO scheduling for traced and traced_probes.
AdbCall('shell', 'chrt', '-f', '-p', traced_pid, '99')
AdbCall('shell', 'chrt', '-f', '-p', probes_pid, '99')
logging.debug(AdbCall('shell', 'chrt', '-p', traced_pid))
logging.debug(AdbCall('shell', 'chrt', '-p', probes_pid))
# Do the run.
SinglePriorityRun(args.linux_out_dir, 'FIFO')
# Stop and start traced and traced_probes
(traced_pid, probes_pid) = CycleTracedAndProbes()
# Cleanup any pushed files, priorities etc.
logging.info("Cleaning up test")
AdbCall('shell', 'rm', '-rf', '/data/local/tmp/perfetto_load_test')
logging.debug(AdbCall('shell', 'cat', '/proc/{}/stat'.format(traced_pid)))
logging.debug(AdbCall('shell', 'cat', '/proc/{}/stat'.format(probes_pid)))
AdbCall('unroot')
return 0
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
sys.exit(Main())