-
Notifications
You must be signed in to change notification settings - Fork 1
/
TraceCMDParser.py
227 lines (195 loc) · 7.54 KB
/
TraceCMDParser.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
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
#!/usr/bin/env python
"""
Uses the tracecmd python module to parse the tracecmd events, found in a tracecmd .dat file, into event
objects, found in the SystemEvents module of the energy debugger.
"""
import sys
from SystemEvents import *
from tracecmd import Trace
__author__ = "Alex Hoffman"
__copyright__ = "Copyright 2019, Alex Hoffman"
__license__ = "GPL"
__version__ = "1.0"
__maintainer__ = "Alex Hoffman"
__email__ = "[email protected]"
__status__ = "Beta"
class EventCounts:
""" Used simply to track the number of different events that occured throughout the duration of a trace.
"""
def __init__(self):
self.sched_switch = 0
self.cpu_idle = 0
self.update_cpu_metric = 0
self.cpu_freq = 0
self.binder_transaction = 0
self.mali = 0
self.temp = 0
class TracecmdProcessor:
""" Using the tracecmd backend the ftrace events, recorded using tracecmd, are processed sequentially once
the trace date has been loaded.
"""
def __init__(self, filename, preamble):
self.processed_events = []
self.temp_events = []
self.idle_events = []
try:
self.trace = Trace(str(filename))
except Exception, e:
print "Tracecmd file could not be read: %s" % str(e)
sys.exit(1)
self.event_count = EventCounts()
self._process_trace(preamble)
def print_event_count(self):
try:
print "--- Total events: " + str(
self.event_count.sched_switch + self.event_count.cpu_idle +
self.event_count.sched_switch +
self.event_count.binder_transaction +
+self.event_count.cpu_freq + self.event_count.mali)
print "------ Sched switch: " + str(self.event_count.sched_switch)
print "------ CPU idle: " + str(self.event_count.cpu_idle)
print "------ CPU freq: " + str(self.event_count.cpu_freq)
print "------ Binder transactions: " + str(
self.event_count.binder_transaction)
print "------ Mali: " + str(self.event_count.mali)
print "------ Temp: " + str(self.event_count.temp)
except Exception, e:
print("Print event count failed, %s" % e)
def _process_trace(self, preamble):
""" Sequentially process trace events.
:return:
"""
start_time = 0
event = self.trace.read_next_event()
# Discard the first 2 seconds of tracing as syslogger initially causes
# spikes in system power
if start_time == 0 and event:
start_time = int(round(event.ts / 1000.0)) + (preamble * 1000000)
while event:
if int(round(event.ts / 1000.0)) > start_time:
self._handle_event(event)
event = self.trace.read_next_event()
def _handle_event(self, event):
""" Create an appropriate Event class child object that is then added to the list of unprocessed
python objects.
:param event: Tracecmd event object to be processed into Event object
"""
if not event:
return
if event.name == "sched_switch":
self.event_count.sched_switch += 1
prev_state = "R"
prev_state_int = event.num_field("prev_state")
if prev_state_int == 1:
prev_state = "S"
elif prev_state_int == 2:
prev_state = "D"
elif prev_state_int == 4:
prev_state = "T"
elif prev_state_int == 8:
prev_state = "t"
elif prev_state_int == 16:
prev_state = "Z"
elif prev_state_int == 32:
prev_state = "X"
elif prev_state_int == 64:
prev_state = "x"
elif prev_state_int == 128:
prev_state = "K"
elif prev_state_int == 256:
prev_state = "W"
elif prev_state_int == 512:
prev_state = "P"
name = event.str_field("prev_comm")
next_pid = event.num_field("next_pid")
next_name = event.str_field("next_comm")
self.processed_events.append(
EventSchedSwitch(
pid=event.pid,
ts=int(round(event.ts / 1000.0)),
cpu=event.cpu,
name=name,
prev_state=prev_state,
next_pid=next_pid,
next_name=next_name,
))
elif event.name == "cpu_idle":
self.event_count.cpu_idle += 1
state = event.num_field("state")
state = 1 if state == 4294967295 else 0
self.idle_events.append(
EventIdle(
ts=int(round(event.ts / 1000.0)),
cpu=event.cpu,
name=event.name,
state=state,
))
elif event.name == "cpu_freq":
self.event_count.cpu_freq += 1
target_cpu = event.num_field("cpu")
freq = event.num_field("freq") * 1000
self.processed_events.append(
EventFreqChange(
pid=event.pid,
ts=int(round(event.ts / 1000.0)),
cpu=event.cpu,
freq=freq,
util=0,
target_cpu=target_cpu,
))
elif event.name == "binder_transaction":
self.event_count.binder_transaction += 1
reply = event.num_field("reply")
flags = event.num_field("flags")
code = event.num_field("code")
to_proc = event.num_field("to_proc")
to_thread = event.num_field("to_thread")
if to_thread == 0:
to_thread = to_proc
trans_num = event.num_field("debug_id")
self.processed_events.append(
EventBinderTransaction(
pid=event.pid,
ts=int(round(event.ts / 1000.0)),
cpu=event.cpu,
name=event.name,
reply=reply,
dest_proc=to_proc,
target_pid=to_thread,
flags=flags,
code=code,
tran_num=trans_num,
))
elif event.name == "mali":
self.event_count.mali += 1
util = event.num_field("load")
freq = event.num_field("freq") * 1000000
self.processed_events.append(
EventMaliUtil(
pid=event.pid,
ts=int(round(event.ts / 1000.0)),
cpu=event.cpu,
util=util,
freq=freq,
))
elif event.name == "exynos_temp":
self.event_count.temp += 1
big0 = event.num_field("t0") / 1000
big1 = event.num_field("t1") / 1000
big2 = event.num_field("t2") / 1000
big3 = event.num_field("t3") / 1000
little = (big0 + big1 + big2 + big3) / 4.0
gpu = event.num_field("t4") / 1000
self.temp_events.append(
EventTempInfo(
ts=int(round(event.ts / 1000.0)),
cpu=event.cpu,
big0=big0,
big1=big1,
big2=big2,
big3=big3,
little=little,
gpu=gpu,
))
else:
pass # print "Unknown event %s" % event.name