-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsys_logger_modified.sh
236 lines (201 loc) · 4.43 KB
/
sys_logger_modified.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
PID_FILE=/data/local/tmp/sys_logger.pid
MYDIR="$(dirname "$(realpath "$0")")"
# also trace the chrome governor
trace_cg=false
trace_threads=true
print_usage()
{
echo "Usage: $0 [setup (-cg) (-nt) | start | stop | finish]"
}
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()
{
if is_loaded; then
echo "Error: Already setup!"
fi
PARAMS="cpu=2 interval=5"
`insmod /system/lib/modules/sys_logger.ko $PARAMS`
ret=$?
if [ "$ret" != 0 ]; then
echo "Error: Could not load kernel module"
exit 1
fi
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 sys_logger
sleep 1
`insmod $MYDIR/sys_logger.ko $PARAMS`
ret=$?
if [ "$ret" != 0 ]; then
echo "Error: Could not load kernel module"
exit 1
fi
sleep 1
if [ -w /sys/module/sys_logger/parameters/enabled ]; then
break;
fi
done
if [ "$trace_cg" = true ]; then
echo "Preparing to trace sys_logger and chrome governor ..."
APPEND="-e cpufreq_cg"
else
echo "Preparing to trace sys_logger only ..."
APPEND=""
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
fi
# clear all events if enything is pending
$MYDIR/trace-cmd reset > /dev/null
# start tracing so we can monitor forks of children (relevant for chrome)
$MYDIR/trace-cmd start \
-e sys_logger \
-e binder_transaction \
-e cpu_idle \
-e sched_switch \
-i \
-b $BUFFER_SIZE \
-d -D \
$APPEND
ret=$?
if [ "$ret" != 0 ]; then
echo "Error: trace-cmd failed"
rmmod sys_logger
exit 1
fi
}
start()
{
if ! is_loaded; then
echo "Error: Not setup!"
exit 1
elif is_enabled; then
echo "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
echo 1 > /sys/module/sys_logger/parameters/enabled
}
stop()
{
if ! is_loaded; then
echo "Error: Not setup!"
exit 1
elif ! is_enabled; then
echo "Error: Not started!"
exit 1
fi
# stop the measurement run
echo 0 > /sys/module/sys_logger/parameters/enabled
# 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
# stop tracing
$MYDIR/trace-cmd stop
# write the trace.dat file
$MYDIR/trace-cmd extract -o $MYDIR/trace.dat
# turn of and reset all tracing
$MYDIR/trace-cmd reset
rm $MYDIR/*.report
#$MYDIR/trace-cmd report -i $MYDIR/trace.dat > $MYDIR/trace.report
# unload the module
rmmod $MYDIR/sys_logger.ko
}
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
;;
*)
print_usage
exit 1
esac
done
case "$action" in
setup)
setup
;;
start)
start
;;
stop)
stop
;;
finish)
finish
;;
*)
print_usage
exit 1
esac
exit 0