summaryrefslogtreecommitdiff
path: root/ioblame/ioblame.sh
blob: ec907abbef5a060a5beee8bf9483d592a59d4b59 (plain)
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#!/bin/sh -u

parseoptions() {
    trace_reads=false
    trace_writes=false

    while [ $# -ge 1 ]
    do
	case $1 in
	    -r)
		trace_reads=true
		;;
	    -w)
		trace_writes=true
		;;
	    *)
		usage
		;;
	    esac
	shift
    done
}

usage() {
    echo "Usage: $0 [-r|-w]"
    exit 1
}

getmodel() {
    model=`adb shell getprop ro.product.name`
    # Releases are inconsistent with various trailing characters, remove them all
    model=`echo $model | sed 's/[ \t\r\n]*$//' `
    echo Found $model Device

    case $model in
	aosp_gobo)
	    get_go_devnames
            ;;
	marlin | sailfish)
	    get_marlin_sailfish_devnames
	    ;;
	angler)
	    get_angler_devnames
	    ;;
	bullhead)
	    get_bullhead_devnames
	    ;;
	volantis | volantisg)
	    get_volantis_devnames
	    ;;
	*)
	    echo Unknown Device $model
	    exit 1
	    ;;
    esac
}

get_go_devnames () {
    # Hardcoding all of the mmcblk0 device for now
    block_device=mmcblk0
    bdev_set=true
}

get_volantis_devnames() {
    bdev_set=true
    block_device=mmcblk0
}

get_bullhead_devnames() {
    bdev_set=true
    block_device=mmcblk0
}

get_marlin_sailfish_devnames() {
    bdev_set=true
    block_device=sda
}

get_angler_devnames () {
    # Get the underlying bdev from the "by-name" mapping
    block_device=`adb shell 'find /dev/block/platform -name by-name | xargs ls -l' | grep system | awk '{ print $10 }' `
    # extract the last component of the absolute device pathname we got above
    block_device=`echo $block_device | awk 'BEGIN { FS ="/" } ; { print $4 }' | sed 's/p.*//g' `
    bdev_set=true
}

disk_stats_before() {
    if [ $bdev_set == true ]; then
	DISKSTATS=`adb shell 'cat /proc/diskstats' | fgrep -w $block_device `
	if [ $trace_reads == true ]; then
	    # Get BEFORE read stats for bdev
	    BEFORE_RD_IOS=`echo $DISKSTATS | awk '{ print $4 }' `
	    BEFORE_RD_SECTORS=`echo $DISKSTATS | awk '{ print $6 }' `
	fi
	if [ $trace_writes == true ]; then
	    # Get BEFORE write stats for bdev
	    BEFORE_WR_IOS=`echo $DISKSTATS | awk '{ print $8 }' `
	    BEFORE_WR_SECTORS=`echo $DISKSTATS | awk '{ print $10 }' `
	fi
    fi
}

disk_stats_after() {
    if [ $bdev_set == true ]; then
	DISKSTATS=`adb shell 'cat /proc/diskstats' | fgrep -w $block_device `
	if [ $trace_reads == true ]; then
	    # Get AFTER read stats for bdev
	    AFTER_RD_IOS=`echo $DISKSTATS | awk '{ print $4 }' `
	    AFTER_RD_SECTORS=`echo $DISKSTATS | awk '{ print $6 }' `
	fi
	if [ $trace_writes == true ]; then
	    # Get BEFORE write stats for bdev
	    AFTER_WR_IOS=`echo $DISKSTATS | awk '{ print $8 }' `
	    AFTER_WR_SECTORS=`echo $DISKSTATS | awk '{ print $10 }' `
	fi
    fi
}

disk_stats_delta_rd() {
    file_data_KB=$1
    if [ $bdev_set == true ]; then
	# Sectors to KB
	READ_KB=`expr $AFTER_RD_SECTORS - $BEFORE_RD_SECTORS`
	READ_KB=`expr $READ_KB / 2`
	echo "Total (ALL) Read KB $block_device = "$READ_KB
	BLOCK_MINUS_FILE=`expr $READ_KB - $file_data_KB`
	echo "READ DELTA: Total Blockdev Reads KB - Total File Data Reads KB = "$BLOCK_MINUS_FILE KB
	echo "Total (ALL) Read IOs $block_device = "`expr $AFTER_RD_IOS - $BEFORE_RD_IOS`
    fi
}

disk_stats_delta_wr() {
    file_data_KB=$1
    if [ $bdev_set == true ]; then
	# Sectors to KB
	WRITE_KB=`expr $AFTER_WR_SECTORS - $BEFORE_WR_SECTORS`
	WRITE_KB=`expr $WRITE_KB / 2`
	echo "Total (ALL) Write KB $block_device = "$WRITE_KB
	BLOCK_MINUS_FILE=`expr $WRITE_KB - $file_data_KB`
	echo "WRITE DELTA: Total Blockdev Writes KB - Total File Data Writes KB = "$BLOCK_MINUS_FILE KB
	echo "Total (ALL) Write IOs $block_device = "`expr $AFTER_WR_IOS - $BEFORE_WR_IOS`
    fi
}

# For good measure clean up traces and reenable traces
clean_up_tracepoints() {
    # This is a good point to check if the Android FS tracepoints are enabled in the
    # kernel or not
    tracepoint_exists=`adb shell 'if [ -d /sys/kernel/debug/tracing/events/android_fs ]; then echo 0; else echo 1; fi' `
    if [ $tracepoint_exists == 1 ]; then
	echo "Android FS tracepoints not enabled in kernel. Exiting..."
	exit 1
    fi
    adb shell 'echo 0 > /sys/kernel/debug/tracing/tracing_on'
    adb shell 'echo 0 > /sys/kernel/debug/tracing/trace'
    if [ $trace_reads == true ]; then
	adb shell 'echo 1 > /sys/kernel/debug/tracing/events/android_fs/android_fs_dataread_start/enable'
	adb shell 'echo 1 > /sys/kernel/debug/tracing/events/android_fs/android_fs_dataread_end/enable'
    fi
    if [ $trace_writes == true ]; then
	adb shell 'echo 1 > /sys/kernel/debug/tracing/events/android_fs/android_fs_datawrite_start/enable'
	adb shell 'echo 1 > /sys/kernel/debug/tracing/events/android_fs/android_fs_datawrite_end/enable'
    fi
    adb shell 'echo 1 > /sys/kernel/debug/tracing/tracing_on'
}

# stream trace out of trace_pipe
# Start this in the background ('&')
streamtrace_start() {
    adb shell cat /sys/kernel/debug/tracing/trace_pipe > trace_saved
}

# When signal is received, the trace_pipe reader will get killed
# Call this (just to make sure anyway)
streamtrace_end() {
    ps_line=`ps -ef | grep trace_pipe | grep adb `
    if [ $? == 0 ]; then
	echo Killing `echo $ps_line | awk '{s = ""; for (i=8; i <= NF ; i++) s = s $i " "; print s}' `
	kill `echo $ps_line | awk '{print $2}' `
    fi
}

copyout_trace() {
    streamtrace_end
    if [ $trace_reads == true ]; then
	adb shell 'echo 0 > /sys/kernel/debug/tracing/events/android_fs/android_fs_dataread_start/enable'
	adb shell 'echo 0 > /sys/kernel/debug/tracing/events/android_fs/android_fs_dataread_end/enable'
    fi
    if [ $trace_writes == true ]; then
	adb shell 'echo 0 > /sys/kernel/debug/tracing/events/android_fs/android_fs_datawrite_start/enable'
	adb shell 'echo 0 > /sys/kernel/debug/tracing/events/android_fs/android_fs_datawrite_end/enable'
    fi
    adb shell 'echo 0 > /sys/kernel/debug/tracing/tracing_on'
}

prep_tracefile_common() {
    cp trace_saved $infile
    # Strip away all the extraneous stuff first
    fgrep $1 $infile | sed 's/^.* \[.*\] //' | sed s/://g | sed s/,//g > foo
    mv foo $infile
}

prep_tracefile_rd() {
    prep_tracefile_common android_fs_dataread
    # Strip away unnecessary stuff so we can compute latencies easily
    fgrep android_fs_dataread_start $infile > foo0
    # Throw away everything upto and including android_fs_dataread:
    cat foo0 | sed -n -e 's/^.*android_fs_dataread_start //p' > foo1
    mv foo1 $infile
    # At this stage, $infile should the following format :
    # entry_name <filename> offset <offset> bytes <bytes> cmdline <cmdline> pid <pid> i_size <i_size> ino <ino>
    rm foo0
}

# Latencies not supported for Writes. 'Write End' is just when the data has been
# written back to page cache.
prep_tracefile_wr() {
    prep_tracefile_common android_fs_datawrite
    fgrep android_fs_datawrite_start $infile > foo0
    # Throw away everything upto and including android_fs_datawrite:
    cat foo0 | sed -n -e 's/^.*android_fs_datawrite_start //p' > foo1
    mv foo1 $infile
    # At this stage, $infile should the following format :
    # entry_name <filename> offset <offset> bytes <bytes> cmdline <cmdline> pid <pid> i_size <i_size> ino <ino>
    rm foo0
}

get_unique_files() {
    # Sort first by filename, then by pid
    cat $infile | sed s/,//g  | sort -d -k2,2 -k8,8 > foo1
    mv foo1 $infile
    # $infile now contains lines sorted by <filename, pid>
    # How many unique files are there ?
    cat $infile | awk '{ print $2 }' > foo1
    cat foo1 | uniq > uniq_files
    rm foo1
}

get_unique_pids_byfile() {
    # How many unique pids are there reading this file ?
    cat $1 | awk '{ print $8 }' > foo1
    cat foo1 | uniq > uniq_pids_byfile
    rm foo1
}

get_unique_pids() {
    # Sort first by pid, then by filename
    cat $infile | sed s/,//g  | sort -d -k8,8 -k2,2 > foo1
    mv foo1 $infile
    # $infile now contains lines sorted by <pid, filename>
    # How many unique pids are there ?
    cat $infile | awk '{ print $8 }' > foo1
    cat foo1 | uniq > uniq_pids
    rm foo1
}

get_unique_files_bypid() {
    # How many unique files are there read by this pid ?
    cat $1 | awk '{ print $2 }' > foo1
    cat foo1 | uniq > uniq_files_bypid
    rm foo1
}

catch_sigint()
{
    echo "signal INT received, killing streaming trace capture"
    streamtrace_end
}


prep_to_do_something() {
#    adb shell "am force-stop com.android.chrome"
#    adb shell "am force-stop com.google.android.gm"
    adb shell 'echo 3 > /proc/sys/vm/drop_caches'
    sleep 1
}

do_something() {
    # Arrange things so that the first SIGINT will kill the
    # child process (sleep), but will return to the parent.
    trap 'catch_sigint'  INT
    echo "OK to kill sleep when test is done"
    sleep 30d
#    adb shell "am start -W -n com.android.chrome/com.google.android.apps.chrome.Main"
#    adb shell "am start -W -n com.google.android.gm/.ConversationListActivityGmail"
}

# Get the aggregate list of files read/written. For each file, break up the IOs by pid
process_files() {
    read_write=$1
    get_unique_files
    list_of_files=`cat uniq_files`
    # $list_of_files is a list of all the files involved in IO
    #
    # Loop over each file that was involved in IO
    # Find all the pids doing IO on that file
    # Aggregate the IO done by each pid on that file and dump it out
    #
    grand_total_KB=0
    for i in $list_of_files
    do
	echo "File: $i"
	total_file_KB=0
	# Get just the tracepoints for this file
	fgrep -w "$i" $infile > subtrace
	# Get all the pids doing IO on this file
	get_unique_pids_byfile subtrace
	list_of_pids=`cat uniq_pids_byfile`
	# $list_of_pids is a list of all the pids doing IO to file $i
	for j in $list_of_pids
	do
	    echo -n "            $j $read_write: "
	    pid_KB=`fgrep -w "$j" subtrace | awk '{ bytes += $6 } END { print bytes }' `
	    pid_KB=`expr $pid_KB / 1024`
	    echo "$pid_KB KB"
	    total_file_KB=`expr $total_file_KB + $pid_KB`
	done
	i_size=`tail -n1 subtrace  | awk '{ if ($12 > 1024) printf "%d KB", ($12/1024); else printf "%d bytes", $12; }' `
	echo "            Total $read_write: $total_file_KB KB i_size: $i_size"
	grand_total_KB=`expr $grand_total_KB + $total_file_KB`
    done
    echo "Grand Total File DATA KB $read_write $grand_total_KB"
}

# Get the aggregate list of pids. For each pid, break up the IOs by file
process_pids() {
    read_write=$1
    get_unique_pids
    list_of_pids=`cat uniq_pids`
    # $list_of_pids is a list of all the pids involved in IO
    #
    # Loop over each pid that was involved in IO
    # Find all the files the pid was doing IO on
    # Aggregate the IO done by the pid for each file and dump it out
    #
    grand_total_KB=0
    for i in $list_of_pids
    do
	echo "PID: $i"
	total_pid_KB=0
	# Get just the tracepoints for this pid
	fgrep -w "$i" $infile > subtrace
	# Get all the pids doing IO on this file
	get_unique_files_bypid subtrace
	list_of_files=`cat uniq_files_bypid`
	# $list_of_files is a list of all the files IO'ed by this pid
	for j in $list_of_files
	do
	    i_size=`fgrep -w "$j" subtrace | tail -n1 | awk '{ if ($12 > 1024) printf "%d KB", ($12/1024); else printf "%d bytes", $12; }' `
	    file_KB=`fgrep -w "$j" subtrace | awk '{ bytes += $6 } END { print bytes }' `
	    file_KB=`expr $file_KB / 1024`
	    echo "            $j $read_write: $file_KB KB i_size: $i_size"
	    total_pid_KB=`expr $total_pid_KB + $file_KB`
	done
	echo "            Total $read_write: $total_pid_KB KB"
	grand_total_KB=`expr $grand_total_KB + $total_pid_KB`
    done
    echo "Grand Total File DATA KB $read_write $grand_total_KB"
}

# main() starts here :

if [ $# -lt 1 ]; then
    usage
fi

bdev_set=false
infile=tracefile.$$

parseoptions $@
adb root && sleep 2
getmodel

prep_to_do_something

clean_up_tracepoints
disk_stats_before
# Start streaming the trace into the tracefile
streamtrace_start &

do_something

streamtrace_end
disk_stats_after

copyout_trace

if [ $trace_reads == true ]; then
    echo
    echo "READS :"
    echo "_______"
    echo
    prep_tracefile_rd
    # Get file specific stats - for each file, how many pids read that file ?
    echo "FILE VIEW:"
    process_files Reads
    # Get pid specific stats - for each pid, what files do they do IO on ?
    echo "PID VIEW:"
    process_pids Reads
    disk_stats_delta_rd $grand_total_KB

    debug_FileKB_rd=`cat $infile | awk '{ bytes += $6 } END { printf "%d", bytes/1024 }' `
    echo Debug Grand Total KB READ $debug_FileKB_rd
fi

if [ $trace_writes == true ]; then
    echo
    echo "Writes :"
    echo "_______"
    echo
    prep_tracefile_wr
    # Get file specific stats - for each file, how many pids read that file ?
    echo "FILE VIEW:"
    process_files Writes
    # Get pid specific stats - for each pid, what files do they do IO on ?
    echo "PID VIEW:"
    process_pids Writes
    disk_stats_delta_wr $grand_total_KB

    debug_FileKB_wr=`cat $infile | awk '{ bytes += $6 } END { printf "%d", bytes/1024 }' `
    echo Debug Grand Total KB WRITTEN $debug_FileKB_wr
fi

rm -rf tracefile* uniq_* subtrace trace_saved