-
Notifications
You must be signed in to change notification settings - Fork 0
/
sys_logger.sh
executable file
·391 lines (341 loc) · 9.86 KB
/
sys_logger.sh
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
PID_FILE=/data/local/tmp/sys_logger.pid
MYDIR="$(dirname "$(realpath "$0")")"
# also trace the chrome governor
generate_report=true
trace_cg=false
trace_threads=true
trace_opengl=true
trace_binder=true
trace_record=false
verbose=false
default_interval=5
PARAMS="cpu=2"
fix_ro_filesystem() {
if [[ $(cat /proc/mounts | grep 'ro,') ]]; then
echo "File system is read only, fixing"
# mount -o rw,remount /system
mount -o rw,remount /
chmod 777 /system/lib
fi
}
fix_ro_filesystem
print_usage() {
echo "Usage: $0 (-v) [setup (-cg) (-nt) (-nb) (-nogl) (-i) | start | stop | finish (-nr)]"
echo ""
echo "Syslogger workflow: Setup -> Start -> Stop -> Finish"
echo ""
echo "-v Verbose"
echo ""
echo "Setup"
echo "-cg Trace Chrome governor"
echo "-nt Do not trace threads {sched:sched_process_fork}"
echo "-nb Do not trace binder {binder_transaction, cpu_idle, sched_switch}"
echo "-nogl Do not trace OpenGL {sys_logger:opengl_frame}"
echo "-i Syslogger logging interval, default = 5ms"
echo "-r Record straight to a trace.dat"
echo " "
echo "Finish"
echo "-r Must be given if recording was specified during setup"
echo "-nr Do not generate ftrace report (trace.report)"
}
is_loaded() {
if lsmod | grep -q sys_logger; then
return 0
fi
return 1
}
is_enabled() {
enabled=$(cat /sys/module/sys_logger/parameters/enabled 2>/dev/null)
if [ $enabled ]; then
if [ "$enabled" == 'Y' ]; then
return 0
fi
fi
return 1
}
setup() {
SYSLOG_EVENTS="-e sys_logger"
if is_loaded; then
echo "[Syslogger] Error: Already setup!"
fi
if lsmod | grep sys_logger &>/dev/null; then
if [ "$verbose" = true ]; then
echo "[Syslogger] Module ALREADY loaded"
fi
else
$(insmod /system/lib/modules/sys_logger.ko $PARAMS)
sleep 1
if [ "$verbose" = true ]; then
echo "[Syslogger] Module loaded"
fi
fi
if [ "$verbose" = true ]; then
echo "[Syslogger] insmod /system/lib/modules/sys_logger.ko $PARAMS"
fi
rm /data/local/tmp/trace.dat
while [ ! -w /sys/module/sys_logger/parameters/enabled ]; do
# Especially when switching to the interactive governor,
# the sysfs is sometimes messed up. We have to try reloading
# the module until it works. (happen also with other modules)
rmmod /system/lib/modules/sys_logger
sleep 3
$(insmod /system/lib/modules/sys_logger.ko $PARAMS)
ret=$?
if [ "$ret" != 0 ]; then
echo "[Syslogger] Error: Could not load kernel module"
exit 1
fi
sleep 5
if [ -w /sys/module/sys_logger/parameters/enabled ]; then
break
fi
done
printf "[Syslogger] Preparing to trace: "
if [ "$trace_cg" == true ]; then
APPEND="-e cpufreq_cg"
printf "Chrome governor, "
else
APPEND=""
fi
if [ "$trace_binder" == true ]; then
APPEND="${APPEND} -e binder_transaction -e cpu_idle -e sched_switch"
printf "Binder, CPU idle, Context switches, "
fi
BUFFER_SIZE=20000
if [ "$trace_threads" == true ]; then
# in order to detect all chrome threads, we have to trace forks early
APPEND="$APPEND -e sched:sched_process_fork"
# We trace rougly 50mb per 30 second (mostly on little CPUs), make the
# buffers big enough. 8 * 40 MB -> 320 MB
BUFFER_SIZE=40000
printf "Threads, "
fi
if [ "$trace_opengl" == true ]; then
SYSLOG_EVENTS="${SYSLOG_EVENTS} -e sys_logger:opengl_frame"
printf "OpenGL, "
fi
printf "\n"
# clear all events if enything is pending
if [ "$verbose" = true ]; then
echo "[Syslogger] Resetting"
$MYDIR/trace-cmd reset
else
$MYDIR/trace-cmd reset >/dev/null
fi
# start tracing so we can monitor forks of children (relevant for chrome)
echo "[Syslogger] Trace-cmd events: $SYSLOG_EVENTS"
if [ ! $trace_record -eq false ]; then
echo "[Syslogger] STARTING trace-cmd"
$MYDIR/trace-cmd start \
$SYSLOG_EVENTS \
-i \
-b $BUFFER_SIZE \
-d -D \
$APPEND
else
if [ "$verbose" = true ]; then
echo "[Syslogger] $MYDIR/trace-cmd record $SYSLOG_EVENTS -i -o /data/local/tmp/trace.dat -d -D $APPEND"
fi
$MYDIR/trace-cmd record $SYSLOG_EVENTS -i -o /data/local/tmp/trace.dat -d -D $APPEND >/dev/null 2>&1 &
echo "$(jobs -p)" >.trace.pid
sleep 5
echo "[Syslogger] RECORDING trace-cmd in background [$(jobs -p)], syslogger must still be enabled (started)"
fi
ret=$?
if [ "$ret" != 0 ]; then
echo "[Syslogger] Error: trace-cmd failed"
rmmod /system/lib/modules/sys_logger
exit 1
fi
}
start() {
if ! is_loaded; then
echo "[Syslogger] Error: Not setup!"
exit 1
elif is_enabled; then
echo "[Syslogger] Error: Already started!"
exit 1
fi
# detect if we are tracing threads via active fork tracing
tmp=$(cat /sys/kernel/debug/tracing/events/sched/sched_process_fork/enable 2>/dev/null)
if [[ $tmp -eq "1" ]]; then
# enable all expensive tracing
echo 1 >/sys/kernel/debug/tracing/events/sched/sched_wakeup/enable
echo 1 >/sys/kernel/debug/tracing/events/sched/sched_wakeup_new/enable
echo 1 >/sys/kernel/debug/tracing/events/sched/sched_stat_runtime/enable
fi
# start a new measurement run
# if [ ! $trace_record -eq true ]; then
echo 1 >/sys/module/sys_logger/parameters/enabled
if [ "$verbose" = true ]; then
echo "[Syslogger] echo 1 > /sys/module/sys_logger/parameters/enabled"
fi
echo "[Syslogger] enabled"
chmod 666 /dev/EGLSyslogger
}
stop() {
if ! is_loaded; then
echo "[Syslogger] Error: Not setup!"
exit 1
elif ! is_enabled; then
echo "[Syslogger] Error: Not started!"
exit 1
fi
# stop the measurement run
echo 0 >/sys/module/sys_logger/parameters/enabled
if [ "$verbose" = true ]; then
echo "[Syslogger] echo 0 > /sys/module/sys_logger/parameters/enabled"
fi
echo "[Syslogger] disabled"
# detect if we are tracing threads via active fork tracing
tmp=$(cat /sys/kernel/debug/tracing/events/sched/sched_process_fork/enable 2>/dev/null)
if [[ $tmp -eq "1" ]]; then
# disable all expensive tracing
echo 0 >/sys/kernel/debug/tracing/events/sched/sched_wakeup/enable
echo 0 >/sys/kernel/debug/tracing/events/sched/sched_wakeup_new/enable
echo 0 >/sys/kernel/debug/tracing/events/sched/sched_stat_runtime/enable
fi
}
finish() {
if ! is_loaded; then
echo "Error: Not setup!"
exit 1
elif is_enabled; then
stop
fi
if [ ! $trace_record -eq false ]; then
# stop tracing
$MYDIR/trace-cmd stop
if [ "$verbose" = true ]; then
echo "[Syslogger] $MYDIR/trace-cmd stop"
fi
# write the trace.dat file
$MYDIR/trace-cmd extract -o $MYDIR/trace.dat
if [ "$verbose" = true ]; then
echo "[Syslogger] $MYDIR/trace-cmd extract -o $MYDIR/trace.dat"
fi
else
while read x; do
echo "[Syslogger] Killing [$x]"
kill -INT $x
wait
sleep 5
done <.trace.pid
fi
rm .trace.pid
# turn of and reset all tracing
$MYDIR/trace-cmd reset
if [ "$verbose" = true ]; then
echo "[Syslogger] $MYDIR/trace-cmd reset"
fi
if [ "$generate_report" == true ]; then
echo "[Syslogger] Generating trace report"
rm $MYDIR/*.report
$MYDIR/trace-cmd report -i $MYDIR/trace.dat >$MYDIR/trace.report
if [ "$verbose" = true ]; then
echo "[Syslogger] $MYDIR/trace-cmd report -i $MYDIR/trace.dat > $MYDIR/trace.report"
fi
wait
fi
# unload the module
rmmod /system/lib/modules/sys_logger.ko
echo "[Syslogger] module unloaded"
}
if [ $# -lt 1 ]; then
print_usage
exit 1
fi
key="$1"
action=""
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
setup | start | stop | finish)
action=$key
shift # past argument
;;
-cg | --chrome-governor)
if [ "$action" != "setup" ]; then
print_usage
exit 1
fi
trace_cg=true
shift # past argument
;;
-nt | --no-threads)
if [ "$action" != "setup" ]; then
print_usage
exit 1
fi
trace_threads=0
shift # past argument
;;
-nr | --no-report)
if [ "$action" != "finish" ]; then
print_usage
exit 1
fi
generate_report=false
shift
;;
-nb | --no-binder)
if [ "$action" != "setup" ]; then
print_usage
exit 1
fi
trace_binder=false
shift
;;
-nogl | --no-opengl)
if [ "$action" != "setup" ]; then
print_usage
exit 1
fi
trace_opengl=false
shift
;;
-i)
if [ "$action" != "setup" ]; then
print_usage
exit 1
fi
shift
echo "interval set"
default_interval=$1
shift
;;
-r | --record)
trace_record=true
shift
;;
-v | --verbose)
echo "[Syslogger] Running verbose"
verbose=true
shift
;;
*)
print_usage
exit 1
;;
esac
done
PARAMS="${PARAMS} interval=${default_interval}"
case "$action" in
setup)
setup
;;
start)
start
;;
stop)
stop
;;
finish)
finish
;;
*)
print_usage
exit 1
;;
esac
exit 0