-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse_trace.go
351 lines (282 loc) · 8.97 KB
/
parse_trace.go
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package phonelab
import (
"fmt"
"reflect"
"regexp"
)
///////////////////////////////////////////////////////////////////////////////
// Top-level Kernel-Trace tag parser
// Everything that the KernelTraceParser parses implements this interface
type TraceInterface interface {
TraceTag() string
}
var TRACE_PATTERN = regexp.MustCompile(`` +
`\s*(?P<thread>.*?)` +
`\s+\[(?P<cpu>\d+)\]` +
`\s+(?P<unknown>.{4})` +
`\s+(?P<timestamp>\d+\.\d+)` +
`: ` +
`(?P<message>(?P<tag>.*?):` +
`\s+(?P<text>.*)` +
`)`)
//TODO: What about the commented fields?
type Trace struct {
Thread string `logcat:"thread"`
Cpu int `logcat:"cpu"`
Unknown string `logcat:"unknown"`
Timestamp float64 `logcat:"timestamp"`
Tag string `logcat:"tag"`
//Datetime time.Time `logcat:"-"`
//Logline *Logline `logcat:"-"`
}
func (t *Trace) TraceTag() string {
return t.Tag
}
type KernelTraceParser struct {
RegexParser *RegexParser
Subparsers map[string]Parser
// Parameters
ErrOnUnknownTag bool
}
func NewKernelTraceParser() *KernelTraceParser {
parser := &KernelTraceParser{ErrOnUnknownTag: true}
parser.RegexParser = NewRegexParser(parser)
// TODO: This should be ad hoc
parser.Subparsers = map[string]Parser{
"sched_cpu_hotplug": NewRegexParser(&SchedCPUHotplugParser{}),
"phonelab_num_online_cpus": NewRegexParser(&NumOnlineCpusParser{}),
"thermal_temp": NewRegexParser(&ThermalTempParser{}),
"cpu_frequency": NewRegexParser(&CpuFrequencyParser{}),
"phonelab_proc_foreground": NewRegexParser(&ProcForegroundParser{}),
"phonelab_periodic_ctx_switch_info": NewRegexParser(&PeriodicCtxSwitchInfoParser{}),
"phonelab_periodic_ctx_switch_marker": NewRegexParser(&PeriodicCtxSwitchMarkerParser{}),
}
return parser
}
func (p *KernelTraceParser) New() interface{} {
return &Trace{}
}
func (p *KernelTraceParser) Regex() *regexp.Regexp {
return TRACE_PATTERN
}
// Set the trace field of an object with an embedded Trace object.
// Panics if the log object isn't a Trace object
func setTrace(log interface{}, trace *Trace) {
val := reflect.ValueOf(log)
if val.Kind() != reflect.Ptr {
panic(fmt.Sprintf("Expected a ptr type, but got '%v'", val.Kind()))
}
elem := val.Elem()
// This panics of it can't find Trace
traceField := elem.FieldByName("Trace")
// Get the value of the source
valDest := reflect.ValueOf(trace).Elem()
traceField.Set(valDest)
}
func (p *KernelTraceParser) Parse(line string) (interface{}, error) {
var trace *Trace
// Parse trace using regex parser
if obj, err := p.RegexParser.Parse(line); err != nil {
return nil, err
} else {
trace = obj.(*Trace)
}
// TODO: Fix
// This one doesn't come from the payload
// trace.Datetime = logline.Datetime
// Uncomment this line if you want to add Logline information
// Or add it manually where required
//trace.Logline = logline
// Parse the payload
parser, ok := p.Subparsers[trace.Tag]
if !ok {
if p.ErrOnUnknownTag {
return nil, fmt.Errorf("No parser defined for trace tag '%v'", trace.Tag)
} else {
return trace, nil
}
}
if obj, err := parser.Parse(p.RegexParser.LastMap["text"]); err != nil {
return nil, err
} else {
setTrace(obj, trace)
return obj, err
}
}
var TraceParser = NewKernelTraceParser()
///////////////////////////////////////////////////////////////////////////////
// Sched CPU Hotplug
/* Format: cpu 1 offline error=0 */
var SCHED_CPU_HOTPLUG_PATTERN = regexp.MustCompile(`` +
`\s*cpu` +
`\s+(?P<cpu>\d+)` +
`\s+(?P<state>[a-zA-Z0-9_]+)` +
`\s+error=(?P<error>-?\d+)`)
type SchedCpuHotplug struct {
Trace `logcat:"-"`
Cpu int `logcat:"cpu"`
State string `logcat:"state"`
Error int `logcat:"error"`
}
type SchedCPUHotplugParser struct {
}
func (s *SchedCPUHotplugParser) New() interface{} {
return &SchedCpuHotplug{}
}
func (s *SchedCPUHotplugParser) Regex() *regexp.Regexp {
return SCHED_CPU_HOTPLUG_PATTERN
}
///////////////////////////////////////////////////////////////////////////////
// Thermal Temp
/* Format: sensor_id=5 temp=59 */
var THERMAL_TEMP_PATTERN = regexp.MustCompile(`` +
`\s*sensor_id=(?P<sensor_id>\d+)` +
`\s+temp=(?P<temp>\d+).*`)
type ThermalTemp struct {
Trace `logcat:"-"`
SensorId int `logcat:"sensor_id"`
Temp int `logcat:"temp"`
}
type ThermalTempParser struct {
}
func (s *ThermalTempParser) New() interface{} {
return &ThermalTemp{}
}
func (s *ThermalTempParser) Regex() *regexp.Regexp {
return THERMAL_TEMP_PATTERN
}
///////////////////////////////////////////////////////////////////////////////
// CPU Frequency
/* Format: cpu_frequency: state=2265600 cpu_id=0 */
var CPU_FREQUENCY_PATTERN = regexp.MustCompile(`` +
`\s*state=(?P<state>\d+)` +
`\s+cpu_id=(?P<cpu_id>\d+)`)
type CpuFrequency struct {
Trace `logcat:"-"`
State int `logcat:"state"`
CpuId int `logcat:"cpu_id"`
}
type CpuFrequencyParser struct {
}
func (s *CpuFrequencyParser) New() interface{} {
return &CpuFrequency{}
}
func (s *CpuFrequencyParser) Regex() *regexp.Regexp {
return CPU_FREQUENCY_PATTERN
}
///////////////////////////////////////////////////////////////////////////////
// # Online CPUs
/* Format: phonelab_num_online_cpus: num_online_cpus=4 */
var PHONELAB_NUM_ONLINE_CPUS_PATTERN = regexp.MustCompile(`` +
`\s*num_online_cpus=(?P<num_online_cpus>\d+)`)
type PhonelabNumOnlineCpus struct {
Trace `logcat:"-"`
NumOnlineCpus int `logcat:"num_online_cpus"`
}
type NumOnlineCpusParser struct {
}
func (s *NumOnlineCpusParser) New() interface{} {
return &PhonelabNumOnlineCpus{}
}
func (s *NumOnlineCpusParser) Regex() *regexp.Regexp {
return PHONELAB_NUM_ONLINE_CPUS_PATTERN
}
///////////////////////////////////////////////////////////////////////////////
// Proc Foreground
/* Format: phonelab_proc_foreground: pid=13759 tgid=13759 comm=.android.dialer */
var PHONELAB_PROC_FOREGROUND_PATTERN = regexp.MustCompile(`` +
`\s*pid=(?P<pid>\d+) tgid=(?P<tgid>\d+) comm=(?P<comm>\S+)`)
type PhonelabProcForeground struct {
Trace `logcat:"-"`
Pid int `logcat:"pid"`
Tgid int `logcat:"tgid"`
Comm string `logcat:"comm"`
}
type ProcForegroundParser struct {
}
func (s *ProcForegroundParser) New() interface{} {
return &PhonelabProcForeground{}
}
func (s *ProcForegroundParser) Regex() *regexp.Regexp {
return PHONELAB_PROC_FOREGROUND_PATTERN
}
///////////////////////////////////////////////////////////////////////////////
// Periodic context switch info
/* Format: phonelab_periodic_ctx_switch_info: cpu=0 pid=3 tgid=3 nice=0 comm=ksoftirqd/0 utime=0 stime=0 rtime=1009429 bg_utime=0 bg_stime=0 bg_rtime=0 s_run=0 s_int=17 s_unint=0 s_oth=0 log_idx=933300 rx=0 tx=0 */
var PHONELAB_PERIODIC_CTX_SWITCH_INFO_PATTERN = regexp.MustCompile(`` +
`\s*cpu=(?P<cpu>\d+)` +
`\s*pid=(?P<pid>\d+)` +
`\s*tgid=(?P<tgid>\d+)` +
`\s*nice=(?P<nice>-?\d+)` +
`\s*comm=(?P<comm>.*?)` +
`\s*utime=(?P<utime>\d+)` +
`\s*stime=(?P<stime>\d+)` +
`\s*rtime=(?P<rtime>\d+)` +
`\s*bg_utime=(?P<bg_utime>\d+)` +
`\s*bg_stime=(?P<bg_stime>\d+)` +
`\s*bg_rtime=(?P<bg_rtime>\d+)` +
`\s*s_run=(?P<s_run>\d+)` +
`\s*s_int=(?P<s_int>\d+)` +
`\s*s_unint=(?P<s_unint>\d+)` +
`\s*s_oth=(?P<s_oth>\d+)` +
`\s*log_idx=(?P<log_idx>\d+)` +
`(` +
`\s*rx=(?P<rx>\d+)` +
`\s*tx=(?P<tx>\d+)` +
`)?`)
type PhonelabPeriodicCtxSwitchInfo struct {
Trace `logcat:"-"`
Cpu int `logcat:"cpu"`
Pid int `logcat:"pid"`
Tgid int `logcat:"tgid"`
Nice int `logcat:"nice"`
Comm string `logcat:"comm"`
Utime int64 `logcat:"utime"`
Stime int64 `logcat:"stime"`
Rtime int64 `logcat:"rtime"`
BgUtime int64 `logcat:"bg_utime"`
BgStime int64 `logcat:"bg_stime"`
BgRtime int64 `logcat:"bg_rtime"`
SRun int64 `logcat:"s_run"`
SInt int64 `logcat:"s_int"`
SUnint int64 `logcat:"s_unint"`
SOth int64 `logcat:"s_oth"`
LogIdx int64 `logcat:"log_idx"`
Rx int64 `logcat:"rx"`
Tx int64 `logcat:"tx"`
}
type PeriodicCtxSwitchInfoParser struct {
}
func (s *PeriodicCtxSwitchInfoParser) New() interface{} {
return &PhonelabPeriodicCtxSwitchInfo{}
}
func (s *PeriodicCtxSwitchInfoParser) Regex() *regexp.Regexp {
return PHONELAB_PERIODIC_CTX_SWITCH_INFO_PATTERN
}
///////////////////////////////////////////////////////////////////////////////
// Periodic context switch marker
var PHONELAB_PERIODIC_CTX_SWITCH_MARKER_PATTERN = regexp.MustCompile(`` +
`\s*(?P<state>BEGIN|END)` +
`\s+cpu=(?P<cpu>\d+)` +
`\s+count=(?P<count>\d+)` +
`\s+log_idx=(?P<log_idx>\d+)`)
type PPCSMState string
const (
PPCSMBegin PPCSMState = "BEGIN"
PPCSMEnd PPCSMState = "END"
)
type PhonelabPeriodicCtxSwitchMarker struct {
Trace `logcat:"-"`
State PPCSMState `logcat:"state"`
Cpu int `logcat:"cpu"`
Count int `logcat:"count"`
LogIdx int64 `logcat:"log_idx"`
}
type PeriodicCtxSwitchMarkerParser struct {
}
func (s *PeriodicCtxSwitchMarkerParser) New() interface{} {
return &PhonelabPeriodicCtxSwitchMarker{}
}
func (s *PeriodicCtxSwitchMarkerParser) Regex() *regexp.Regexp {
return PHONELAB_PERIODIC_CTX_SWITCH_MARKER_PATTERN
}