xref: /aosp_15_r20/system/extras/ioblame/ioblame.sh (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1*288bf522SAndroid Build Coastguard Worker#!/bin/sh -u
2*288bf522SAndroid Build Coastguard Worker
3*288bf522SAndroid Build Coastguard Workerparseoptions() {
4*288bf522SAndroid Build Coastguard Worker    trace_reads=false
5*288bf522SAndroid Build Coastguard Worker    trace_writes=false
6*288bf522SAndroid Build Coastguard Worker    trace_writepages=false
7*288bf522SAndroid Build Coastguard Worker    pid_view=false
8*288bf522SAndroid Build Coastguard Worker
9*288bf522SAndroid Build Coastguard Worker    while [ $# -ge 1 ]
10*288bf522SAndroid Build Coastguard Worker    do
11*288bf522SAndroid Build Coastguard Worker	case $1 in
12*288bf522SAndroid Build Coastguard Worker	    -r)
13*288bf522SAndroid Build Coastguard Worker		trace_reads=true
14*288bf522SAndroid Build Coastguard Worker		;;
15*288bf522SAndroid Build Coastguard Worker	    -w)
16*288bf522SAndroid Build Coastguard Worker		trace_writes=true
17*288bf522SAndroid Build Coastguard Worker		;;
18*288bf522SAndroid Build Coastguard Worker	    -p)
19*288bf522SAndroid Build Coastguard Worker		trace_writepages=true
20*288bf522SAndroid Build Coastguard Worker		;;
21*288bf522SAndroid Build Coastguard Worker	    -v)
22*288bf522SAndroid Build Coastguard Worker		pid_view=true
23*288bf522SAndroid Build Coastguard Worker		;;
24*288bf522SAndroid Build Coastguard Worker	    *)
25*288bf522SAndroid Build Coastguard Worker		usage
26*288bf522SAndroid Build Coastguard Worker		;;
27*288bf522SAndroid Build Coastguard Worker	    esac
28*288bf522SAndroid Build Coastguard Worker	shift
29*288bf522SAndroid Build Coastguard Worker    done
30*288bf522SAndroid Build Coastguard Worker}
31*288bf522SAndroid Build Coastguard Worker
32*288bf522SAndroid Build Coastguard Workerusage() {
33*288bf522SAndroid Build Coastguard Worker    echo "Usage: $0 [-r|-w|-p|-v]"
34*288bf522SAndroid Build Coastguard Worker    exit 1
35*288bf522SAndroid Build Coastguard Worker}
36*288bf522SAndroid Build Coastguard Worker
37*288bf522SAndroid Build Coastguard Workergetmodel() {
38*288bf522SAndroid Build Coastguard Worker    model=`adb shell getprop ro.product.name`
39*288bf522SAndroid Build Coastguard Worker    # Releases are inconsistent with various trailing characters, remove them all
40*288bf522SAndroid Build Coastguard Worker    model=`echo $model | sed 's/[ \t\r\n]*$//' `
41*288bf522SAndroid Build Coastguard Worker    echo Found $model Device
42*288bf522SAndroid Build Coastguard Worker
43*288bf522SAndroid Build Coastguard Worker    case $model in
44*288bf522SAndroid Build Coastguard Worker	aosp_gobo | gobo | gobo_512)
45*288bf522SAndroid Build Coastguard Worker	    get_go_devnames
46*288bf522SAndroid Build Coastguard Worker            ;;
47*288bf522SAndroid Build Coastguard Worker	marlin | sailfish | walleye | taimen | blueline | crosshatch)
48*288bf522SAndroid Build Coastguard Worker	    get_marlin_sailfish_devnames
49*288bf522SAndroid Build Coastguard Worker	    ;;
50*288bf522SAndroid Build Coastguard Worker	angler)
51*288bf522SAndroid Build Coastguard Worker	    get_angler_devnames
52*288bf522SAndroid Build Coastguard Worker	    ;;
53*288bf522SAndroid Build Coastguard Worker	bullhead)
54*288bf522SAndroid Build Coastguard Worker	    get_bullhead_devnames
55*288bf522SAndroid Build Coastguard Worker	    ;;
56*288bf522SAndroid Build Coastguard Worker	volantis | volantisg)
57*288bf522SAndroid Build Coastguard Worker	    get_volantis_devnames
58*288bf522SAndroid Build Coastguard Worker	    ;;
59*288bf522SAndroid Build Coastguard Worker	*)
60*288bf522SAndroid Build Coastguard Worker	    echo Unknown Device $model -- trying Pixel config
61*288bf522SAndroid Build Coastguard Worker	    get_marlin_sailfish_devnames
62*288bf522SAndroid Build Coastguard Worker	    ;;
63*288bf522SAndroid Build Coastguard Worker    esac
64*288bf522SAndroid Build Coastguard Worker}
65*288bf522SAndroid Build Coastguard Worker
66*288bf522SAndroid Build Coastguard Workerget_go_devnames () {
67*288bf522SAndroid Build Coastguard Worker    # Hardcoding all of the mmcblk0 device for now
68*288bf522SAndroid Build Coastguard Worker    block_device=mmcblk0
69*288bf522SAndroid Build Coastguard Worker    bdev_set=true
70*288bf522SAndroid Build Coastguard Worker}
71*288bf522SAndroid Build Coastguard Worker
72*288bf522SAndroid Build Coastguard Workerget_volantis_devnames() {
73*288bf522SAndroid Build Coastguard Worker    bdev_set=true
74*288bf522SAndroid Build Coastguard Worker    block_device=mmcblk0
75*288bf522SAndroid Build Coastguard Worker}
76*288bf522SAndroid Build Coastguard Worker
77*288bf522SAndroid Build Coastguard Workerget_bullhead_devnames() {
78*288bf522SAndroid Build Coastguard Worker    bdev_set=true
79*288bf522SAndroid Build Coastguard Worker    block_device=mmcblk0
80*288bf522SAndroid Build Coastguard Worker}
81*288bf522SAndroid Build Coastguard Worker
82*288bf522SAndroid Build Coastguard Workerget_marlin_sailfish_devnames() {
83*288bf522SAndroid Build Coastguard Worker    bdev_set=true
84*288bf522SAndroid Build Coastguard Worker    block_device=sda
85*288bf522SAndroid Build Coastguard Worker}
86*288bf522SAndroid Build Coastguard Worker
87*288bf522SAndroid Build Coastguard Workerget_angler_devnames () {
88*288bf522SAndroid Build Coastguard Worker    # Get the underlying bdev from the "by-name" mapping
89*288bf522SAndroid Build Coastguard Worker    block_device=`adb shell 'find /dev/block/platform -name by-name | xargs ls -l' | grep system | awk '{ print $10 }' `
90*288bf522SAndroid Build Coastguard Worker    # extract the last component of the absolute device pathname we got above
91*288bf522SAndroid Build Coastguard Worker    block_device=`echo $block_device | awk 'BEGIN { FS ="/" } ; { print $4 }' | sed 's/p.*//g' `
92*288bf522SAndroid Build Coastguard Worker    bdev_set=true
93*288bf522SAndroid Build Coastguard Worker}
94*288bf522SAndroid Build Coastguard Worker
95*288bf522SAndroid Build Coastguard Workerdisk_stats_before() {
96*288bf522SAndroid Build Coastguard Worker    if [ $bdev_set == true ]; then
97*288bf522SAndroid Build Coastguard Worker	DISKSTATS=`adb shell 'cat /proc/diskstats' | fgrep -w $block_device `
98*288bf522SAndroid Build Coastguard Worker	# Get BEFORE read stats for bdev
99*288bf522SAndroid Build Coastguard Worker	BEFORE_RD_IOS=`echo $DISKSTATS | awk '{ print $4 }' `
100*288bf522SAndroid Build Coastguard Worker	BEFORE_RD_SECTORS=`echo $DISKSTATS | awk '{ print $6 }' `
101*288bf522SAndroid Build Coastguard Worker	# Get BEFORE write stats for bdev
102*288bf522SAndroid Build Coastguard Worker	BEFORE_WR_IOS=`echo $DISKSTATS | awk '{ print $8 }' `
103*288bf522SAndroid Build Coastguard Worker	BEFORE_WR_SECTORS=`echo $DISKSTATS | awk '{ print $10 }' `
104*288bf522SAndroid Build Coastguard Worker    fi
105*288bf522SAndroid Build Coastguard Worker    if [ $f2fs_fs == 1 ] ; then
106*288bf522SAndroid Build Coastguard Worker	adb shell 'mount -o remount,background_gc=off /data'
107*288bf522SAndroid Build Coastguard Worker	F2FS_GC_SEGMENTS_BEFORE=`adb shell 'cat /sys/kernel/debug/f2fs/status' | grep segments | egrep 'data|node' | awk '{ segments += $5 } END { print segments }' `
108*288bf522SAndroid Build Coastguard Worker    fi
109*288bf522SAndroid Build Coastguard Worker}
110*288bf522SAndroid Build Coastguard Worker
111*288bf522SAndroid Build Coastguard Workerdisk_stats_after() {
112*288bf522SAndroid Build Coastguard Worker    if [ $bdev_set == true ]; then
113*288bf522SAndroid Build Coastguard Worker	DISKSTATS=`adb shell 'cat /proc/diskstats' | fgrep -w $block_device `
114*288bf522SAndroid Build Coastguard Worker	# Get AFTER read stats for bdev
115*288bf522SAndroid Build Coastguard Worker	AFTER_RD_IOS=`echo $DISKSTATS | awk '{ print $4 }' `
116*288bf522SAndroid Build Coastguard Worker	AFTER_RD_SECTORS=`echo $DISKSTATS | awk '{ print $6 }' `
117*288bf522SAndroid Build Coastguard Worker	# Get BEFORE write stats for bdev
118*288bf522SAndroid Build Coastguard Worker	AFTER_WR_IOS=`echo $DISKSTATS | awk '{ print $8 }' `
119*288bf522SAndroid Build Coastguard Worker	AFTER_WR_SECTORS=`echo $DISKSTATS | awk '{ print $10 }' `
120*288bf522SAndroid Build Coastguard Worker    fi
121*288bf522SAndroid Build Coastguard Worker    if [ $f2fs_fs == 1 ] ; then
122*288bf522SAndroid Build Coastguard Worker	F2FS_GC_SEGMENTS_AFTER=`adb shell 'cat /sys/kernel/debug/f2fs/status' | grep segments | egrep 'data|node' | awk '{ segments += $5 } END { print segments }' `
123*288bf522SAndroid Build Coastguard Worker	adb shell 'mount -o remount,background_gc=on /data'
124*288bf522SAndroid Build Coastguard Worker    fi
125*288bf522SAndroid Build Coastguard Worker}
126*288bf522SAndroid Build Coastguard Worker
127*288bf522SAndroid Build Coastguard Workerdisk_stats_delta_rd() {
128*288bf522SAndroid Build Coastguard Worker    file_data_KB=$1
129*288bf522SAndroid Build Coastguard Worker    if [ $bdev_set == true ]; then
130*288bf522SAndroid Build Coastguard Worker	# Sectors to KB
131*288bf522SAndroid Build Coastguard Worker	READ_KB=`expr $AFTER_RD_SECTORS - $BEFORE_RD_SECTORS`
132*288bf522SAndroid Build Coastguard Worker	READ_KB=`expr $READ_KB / 2`
133*288bf522SAndroid Build Coastguard Worker	echo "Total (ALL) Read KB $block_device = "$READ_KB
134*288bf522SAndroid Build Coastguard Worker	BLOCK_MINUS_FILE=`expr $READ_KB - $file_data_KB`
135*288bf522SAndroid Build Coastguard Worker	echo "READ DELTA: Total Blockdev Reads KB - Total File Data Reads KB = "$BLOCK_MINUS_FILE KB
136*288bf522SAndroid Build Coastguard Worker	echo "Total (ALL) Read IOs $block_device = "`expr $AFTER_RD_IOS - $BEFORE_RD_IOS`
137*288bf522SAndroid Build Coastguard Worker    fi
138*288bf522SAndroid Build Coastguard Worker}
139*288bf522SAndroid Build Coastguard Worker
140*288bf522SAndroid Build Coastguard Workerdisk_stats_delta_wr() {
141*288bf522SAndroid Build Coastguard Worker    file_data_KB=$1
142*288bf522SAndroid Build Coastguard Worker    if [ $bdev_set == true ]; then
143*288bf522SAndroid Build Coastguard Worker	# Sectors to KB
144*288bf522SAndroid Build Coastguard Worker	WRITE_KB=`expr $AFTER_WR_SECTORS - $BEFORE_WR_SECTORS`
145*288bf522SAndroid Build Coastguard Worker	WRITE_KB=`expr $WRITE_KB / 2`
146*288bf522SAndroid Build Coastguard Worker	BLOCK_MINUS_FILE=`expr $WRITE_KB - $file_data_KB`
147*288bf522SAndroid Build Coastguard Worker	echo "WRITE DELTA: Total Blockdev Writes KB - Total File Data Writes KB = "$BLOCK_MINUS_FILE KB
148*288bf522SAndroid Build Coastguard Worker	echo "Total (ALL) Write IOs $block_device = "`expr $AFTER_WR_IOS - $BEFORE_WR_IOS`
149*288bf522SAndroid Build Coastguard Worker    fi
150*288bf522SAndroid Build Coastguard Worker    if [ $f2fs_fs == 1 ] ; then
151*288bf522SAndroid Build Coastguard Worker	F2FS_GC_SEGMENTS_DELTA=`expr $F2FS_GC_SEGMENTS_AFTER - $F2FS_GC_SEGMENTS_BEFORE`
152*288bf522SAndroid Build Coastguard Worker	F2FS_GC_KB_DELTA=`expr $F2FS_GC_SEGMENTS_DELTA \\* 2048`
153*288bf522SAndroid Build Coastguard Worker    fi
154*288bf522SAndroid Build Coastguard Worker}
155*288bf522SAndroid Build Coastguard Worker
156*288bf522SAndroid Build Coastguard Worker# For good measure clean up traces and reenable traces
157*288bf522SAndroid Build Coastguard Workerclean_up_tracepoints() {
158*288bf522SAndroid Build Coastguard Worker    # This is a good point to check if the Android FS tracepoints are enabled in the
159*288bf522SAndroid Build Coastguard Worker    # kernel or not
160*288bf522SAndroid Build Coastguard Worker    tracepoint_exists=`adb shell 'if [ -d /sys/kernel/debug/tracing/events/f2fs ]; then echo 0; else echo 1; fi' `
161*288bf522SAndroid Build Coastguard Worker    if [ $tracepoint_exists == 1 ]; then
162*288bf522SAndroid Build Coastguard Worker	echo "Android FS tracepoints not enabled in kernel. Exiting..."
163*288bf522SAndroid Build Coastguard Worker	exit 1
164*288bf522SAndroid Build Coastguard Worker    fi
165*288bf522SAndroid Build Coastguard Worker    adb shell 'echo 0 > /sys/kernel/debug/tracing/tracing_on'
166*288bf522SAndroid Build Coastguard Worker    adb shell 'echo 0 > /sys/kernel/debug/tracing/trace'
167*288bf522SAndroid Build Coastguard Worker    if [ $trace_reads == true ]; then
168*288bf522SAndroid Build Coastguard Worker	adb shell 'echo 1 > /sys/kernel/debug/tracing/events/f2fs/f2fs_dataread_start/enable'
169*288bf522SAndroid Build Coastguard Worker    fi
170*288bf522SAndroid Build Coastguard Worker    if [ $trace_writes == true ]; then
171*288bf522SAndroid Build Coastguard Worker	adb shell 'echo 1 > /sys/kernel/debug/tracing/events/f2fs/f2fs_datawrite_start/enable'
172*288bf522SAndroid Build Coastguard Worker    fi
173*288bf522SAndroid Build Coastguard Worker    if [ $f2fs_fs == 1 ] ; then
174*288bf522SAndroid Build Coastguard Worker	if [ $trace_writepages == true ]; then
175*288bf522SAndroid Build Coastguard Worker	    adb shell 'echo 1 > /sys/kernel/debug/tracing/events/f2fs/f2fs_writepages/enable'
176*288bf522SAndroid Build Coastguard Worker	fi
177*288bf522SAndroid Build Coastguard Worker    fi
178*288bf522SAndroid Build Coastguard Worker    adb shell 'echo 1 > /sys/kernel/debug/tracing/tracing_on'
179*288bf522SAndroid Build Coastguard Worker}
180*288bf522SAndroid Build Coastguard Worker
181*288bf522SAndroid Build Coastguard Worker# stream trace out of trace_pipe
182*288bf522SAndroid Build Coastguard Worker# Start this in the background ('&')
183*288bf522SAndroid Build Coastguard Workerstreamtrace_start() {
184*288bf522SAndroid Build Coastguard Worker    adb shell cat /sys/kernel/debug/tracing/trace_pipe > trace_saved
185*288bf522SAndroid Build Coastguard Worker}
186*288bf522SAndroid Build Coastguard Worker
187*288bf522SAndroid Build Coastguard Worker# When signal is received, the trace_pipe reader will get killed
188*288bf522SAndroid Build Coastguard Worker# Call this (just to make sure anyway)
189*288bf522SAndroid Build Coastguard Workerstreamtrace_end() {
190*288bf522SAndroid Build Coastguard Worker    ps_line=`ps -ef | grep trace_pipe | grep adb `
191*288bf522SAndroid Build Coastguard Worker    if [ $? == 0 ]; then
192*288bf522SAndroid Build Coastguard Worker	echo Killing `echo $ps_line | awk '{s = ""; for (i=8; i <= NF ; i++) s = s $i " "; print s}' `
193*288bf522SAndroid Build Coastguard Worker	kill `echo $ps_line | awk '{print $2}' `
194*288bf522SAndroid Build Coastguard Worker    fi
195*288bf522SAndroid Build Coastguard Worker}
196*288bf522SAndroid Build Coastguard Worker
197*288bf522SAndroid Build Coastguard Workercopyout_trace() {
198*288bf522SAndroid Build Coastguard Worker    streamtrace_end
199*288bf522SAndroid Build Coastguard Worker    if [ $trace_reads == true ]; then
200*288bf522SAndroid Build Coastguard Worker	adb shell 'echo 0 > /sys/kernel/debug/tracing/events/f2fs/f2fs_dataread_start/enable'
201*288bf522SAndroid Build Coastguard Worker    fi
202*288bf522SAndroid Build Coastguard Worker    if [ $trace_writes == true ]; then
203*288bf522SAndroid Build Coastguard Worker	adb shell 'echo 0 > /sys/kernel/debug/tracing/events/f2fs/f2fs_datawrite_start/enable'
204*288bf522SAndroid Build Coastguard Worker    fi
205*288bf522SAndroid Build Coastguard Worker    if [ $f2fs_fs == 1 ] ; then
206*288bf522SAndroid Build Coastguard Worker	if [ $trace_writepages == true ]; then
207*288bf522SAndroid Build Coastguard Worker	    adb shell 'echo 0 > /sys/kernel/debug/tracing/events/f2fs/f2fs_writepages/enable'
208*288bf522SAndroid Build Coastguard Worker	fi
209*288bf522SAndroid Build Coastguard Worker    fi
210*288bf522SAndroid Build Coastguard Worker    adb shell 'echo 0 > /sys/kernel/debug/tracing/tracing_on'
211*288bf522SAndroid Build Coastguard Worker}
212*288bf522SAndroid Build Coastguard Worker
213*288bf522SAndroid Build Coastguard Workerprep_tracefile_common() {
214*288bf522SAndroid Build Coastguard Worker    cp trace_saved $infile
215*288bf522SAndroid Build Coastguard Worker    # Strip away all the extraneous stuff first
216*288bf522SAndroid Build Coastguard Worker    fgrep $1 $infile | sed 's/^.* \[.*\] //' | sed s/://g | sed s/,//g > foo
217*288bf522SAndroid Build Coastguard Worker    mv foo $infile
218*288bf522SAndroid Build Coastguard Worker}
219*288bf522SAndroid Build Coastguard Worker
220*288bf522SAndroid Build Coastguard Workerprep_tracefile_rd() {
221*288bf522SAndroid Build Coastguard Worker    prep_tracefile_common f2fs_dataread
222*288bf522SAndroid Build Coastguard Worker    # Strip away unnecessary stuff so we can compute latencies easily
223*288bf522SAndroid Build Coastguard Worker    fgrep f2fs_dataread_start $infile > foo0
224*288bf522SAndroid Build Coastguard Worker    # Throw away everything upto and including f2fs_dataread:
225*288bf522SAndroid Build Coastguard Worker    cat foo0 | sed -n -e 's/^.*f2fs_dataread_start //p' > foo1
226*288bf522SAndroid Build Coastguard Worker    mv foo1 $infile
227*288bf522SAndroid Build Coastguard Worker    # At this stage, $infile should the following format :
228*288bf522SAndroid Build Coastguard Worker    # entry_name <filename> offset <offset> bytes <bytes> cmdline <cmdline> pid <pid> i_size <i_size> ino <ino>
229*288bf522SAndroid Build Coastguard Worker    rm foo0
230*288bf522SAndroid Build Coastguard Worker}
231*288bf522SAndroid Build Coastguard Worker
232*288bf522SAndroid Build Coastguard Workerprep_tracefile_writepages() {
233*288bf522SAndroid Build Coastguard Worker    prep_tracefile_common f2fs_writepages
234*288bf522SAndroid Build Coastguard Worker    # Throw away everything up to and including f2fs_writepages_start:
235*288bf522SAndroid Build Coastguard Worker    cat $infile | sed -n -e 's/^.*f2fs_writepages //p' > foo1
236*288bf522SAndroid Build Coastguard Worker    mv foo1 $infile
237*288bf522SAndroid Build Coastguard Worker    # At this stage, $infile should the following format :
238*288bf522SAndroid Build Coastguard Worker    # entry_name <filename> bytes <bytes> ino <ino>
239*288bf522SAndroid Build Coastguard Worker}
240*288bf522SAndroid Build Coastguard Worker
241*288bf522SAndroid Build Coastguard Worker# Latencies not supported for Writes. 'Write End' is just when the data has been
242*288bf522SAndroid Build Coastguard Worker# written back to page cache.
243*288bf522SAndroid Build Coastguard Workerprep_tracefile_wr() {
244*288bf522SAndroid Build Coastguard Worker    prep_tracefile_common f2fs_datawrite
245*288bf522SAndroid Build Coastguard Worker    fgrep f2fs_datawrite_start $infile > foo0
246*288bf522SAndroid Build Coastguard Worker    # Throw away everything upto and including f2fs_datawrite:
247*288bf522SAndroid Build Coastguard Worker    cat foo0 | sed -n -e 's/^.*f2fs_datawrite_start //p' > foo1
248*288bf522SAndroid Build Coastguard Worker    mv foo1 $infile
249*288bf522SAndroid Build Coastguard Worker    # At this stage, $infile should the following format :
250*288bf522SAndroid Build Coastguard Worker    # entry_name <filename> offset <offset> bytes <bytes> cmdline <cmdline> pid <pid> i_size <i_size> ino <ino>
251*288bf522SAndroid Build Coastguard Worker    rm foo0
252*288bf522SAndroid Build Coastguard Worker}
253*288bf522SAndroid Build Coastguard Worker
254*288bf522SAndroid Build Coastguard Workerget_unique_files_rw() {
255*288bf522SAndroid Build Coastguard Worker    # Sort first by filename, then by pid
256*288bf522SAndroid Build Coastguard Worker    cat $infile | sed s/,//g  | sort -d -k2,2 -k8,8 > foo1
257*288bf522SAndroid Build Coastguard Worker    mv foo1 $infile
258*288bf522SAndroid Build Coastguard Worker    # $infile now contains lines sorted by <filename, pid>
259*288bf522SAndroid Build Coastguard Worker    # How many unique files are there ?
260*288bf522SAndroid Build Coastguard Worker    cat $infile | awk '{ print $2 }' > foo1
261*288bf522SAndroid Build Coastguard Worker    cat foo1 | uniq > uniq_files
262*288bf522SAndroid Build Coastguard Worker    rm foo1
263*288bf522SAndroid Build Coastguard Worker}
264*288bf522SAndroid Build Coastguard Worker
265*288bf522SAndroid Build Coastguard Workerget_unique_files_writepages() {
266*288bf522SAndroid Build Coastguard Worker    cat $infile | sed s/,//g  | sort -d -k2,2 > foo1
267*288bf522SAndroid Build Coastguard Worker    # $infile now contains lines sorted by <filename>
268*288bf522SAndroid Build Coastguard Worker    mv foo1 $infile
269*288bf522SAndroid Build Coastguard Worker    # How many unique files are there ?
270*288bf522SAndroid Build Coastguard Worker    cat $infile | awk '{ print $2 }' > foo1
271*288bf522SAndroid Build Coastguard Worker    cat foo1 | uniq > uniq_files
272*288bf522SAndroid Build Coastguard Worker    rm foo1
273*288bf522SAndroid Build Coastguard Worker}
274*288bf522SAndroid Build Coastguard Worker
275*288bf522SAndroid Build Coastguard Workerget_unique_pids_byfile() {
276*288bf522SAndroid Build Coastguard Worker    # How many unique pids are there reading this file ?
277*288bf522SAndroid Build Coastguard Worker    cat $1 | awk '{ print $8 }' > foo1
278*288bf522SAndroid Build Coastguard Worker    cat foo1 | uniq > uniq_pids_byfile
279*288bf522SAndroid Build Coastguard Worker    rm foo1
280*288bf522SAndroid Build Coastguard Worker}
281*288bf522SAndroid Build Coastguard Worker
282*288bf522SAndroid Build Coastguard Workerget_unique_pids() {
283*288bf522SAndroid Build Coastguard Worker    # Sort first by pid, then by filename
284*288bf522SAndroid Build Coastguard Worker    cat $infile | sed s/,//g  | sort -d -k8,8 -k2,2 > foo1
285*288bf522SAndroid Build Coastguard Worker    mv foo1 $infile
286*288bf522SAndroid Build Coastguard Worker    # $infile now contains lines sorted by <pid, filename>
287*288bf522SAndroid Build Coastguard Worker    # How many unique pids are there ?
288*288bf522SAndroid Build Coastguard Worker    cat $infile | awk '{ print $8 }' > foo1
289*288bf522SAndroid Build Coastguard Worker    cat foo1 | uniq > uniq_pids
290*288bf522SAndroid Build Coastguard Worker    rm foo1
291*288bf522SAndroid Build Coastguard Worker}
292*288bf522SAndroid Build Coastguard Worker
293*288bf522SAndroid Build Coastguard Workerget_unique_files_bypid() {
294*288bf522SAndroid Build Coastguard Worker    # How many unique files are there read by this pid ?
295*288bf522SAndroid Build Coastguard Worker    cat $1 | awk '{ print $2 }' > foo1
296*288bf522SAndroid Build Coastguard Worker    cat foo1 | uniq > uniq_files_bypid
297*288bf522SAndroid Build Coastguard Worker    rm foo1
298*288bf522SAndroid Build Coastguard Worker}
299*288bf522SAndroid Build Coastguard Worker
300*288bf522SAndroid Build Coastguard Workercatch_sigint()
301*288bf522SAndroid Build Coastguard Worker{
302*288bf522SAndroid Build Coastguard Worker    echo "signal INT received, killing streaming trace capture"
303*288bf522SAndroid Build Coastguard Worker    streamtrace_end
304*288bf522SAndroid Build Coastguard Worker}
305*288bf522SAndroid Build Coastguard Worker
306*288bf522SAndroid Build Coastguard Worker
307*288bf522SAndroid Build Coastguard Workerprep_to_do_something() {
308*288bf522SAndroid Build Coastguard Worker#    adb shell "am force-stop com.android.chrome"
309*288bf522SAndroid Build Coastguard Worker#    adb shell "am force-stop com.google.android.gm"
310*288bf522SAndroid Build Coastguard Worker    adb shell 'echo 3 > /proc/sys/vm/drop_caches'
311*288bf522SAndroid Build Coastguard Worker    sleep 1
312*288bf522SAndroid Build Coastguard Worker}
313*288bf522SAndroid Build Coastguard Worker
314*288bf522SAndroid Build Coastguard Workerdo_something() {
315*288bf522SAndroid Build Coastguard Worker    # Arrange things so that the first SIGINT will kill the
316*288bf522SAndroid Build Coastguard Worker    # child process (sleep), but will return to the parent.
317*288bf522SAndroid Build Coastguard Worker    trap 'catch_sigint'  INT
318*288bf522SAndroid Build Coastguard Worker    echo "OK to kill sleep when test is done"
319*288bf522SAndroid Build Coastguard Worker    sleep 30d
320*288bf522SAndroid Build Coastguard Worker#    adb shell "am start -W -n com.android.chrome/com.google.android.apps.chrome.Main"
321*288bf522SAndroid Build Coastguard Worker#    adb shell "am start -W -n com.google.android.gm/.ConversationListActivityGmail"
322*288bf522SAndroid Build Coastguard Worker}
323*288bf522SAndroid Build Coastguard Worker
324*288bf522SAndroid Build Coastguard Worker# Get the aggregate list of files read/written. For each file, break up the IOs by pid
325*288bf522SAndroid Build Coastguard Workerprocess_files_rw() {
326*288bf522SAndroid Build Coastguard Worker    read_write=$1
327*288bf522SAndroid Build Coastguard Worker    get_unique_files_rw
328*288bf522SAndroid Build Coastguard Worker    # Loop over each file that was involved in IO
329*288bf522SAndroid Build Coastguard Worker    # Find all the pids doing IO on that file
330*288bf522SAndroid Build Coastguard Worker    # Aggregate the IO done by each pid on that file and dump it out
331*288bf522SAndroid Build Coastguard Worker    grand_total_KB=0
332*288bf522SAndroid Build Coastguard Worker    cp $infile tempfile
333*288bf522SAndroid Build Coastguard Worker    for i in `cat uniq_files`
334*288bf522SAndroid Build Coastguard Worker    do
335*288bf522SAndroid Build Coastguard Worker	# Get just the tracepoints for this file
336*288bf522SAndroid Build Coastguard Worker	fgrep -w "$i" tempfile > subtrace
337*288bf522SAndroid Build Coastguard Worker	if [ -s subtrace ]; then
338*288bf522SAndroid Build Coastguard Worker	    echo "File: $i"
339*288bf522SAndroid Build Coastguard Worker	    total_file_KB=0
340*288bf522SAndroid Build Coastguard Worker	    # Remove the tracepoints we just picked up
341*288bf522SAndroid Build Coastguard Worker	    fgrep -v -w "$i" tempfile > foo
342*288bf522SAndroid Build Coastguard Worker	    mv foo tempfile
343*288bf522SAndroid Build Coastguard Worker	    # Get all the pids doing IO on this file
344*288bf522SAndroid Build Coastguard Worker	    get_unique_pids_byfile subtrace
345*288bf522SAndroid Build Coastguard Worker	    for j in `cat uniq_pids_byfile`
346*288bf522SAndroid Build Coastguard Worker	    do
347*288bf522SAndroid Build Coastguard Worker		echo -n "            $j $read_write: "
348*288bf522SAndroid Build Coastguard Worker		pid_KB=`fgrep -w "$j" subtrace | awk '{ bytes += $6 } END { print bytes }' `
349*288bf522SAndroid Build Coastguard Worker		pid_KB=`expr $pid_KB / 1024`
350*288bf522SAndroid Build Coastguard Worker		echo "$pid_KB KB"
351*288bf522SAndroid Build Coastguard Worker		total_file_KB=`expr $total_file_KB + $pid_KB`
352*288bf522SAndroid Build Coastguard Worker	    done
353*288bf522SAndroid Build Coastguard Worker	    i_size=`tail -n1 subtrace  | awk '{ if ($12 > 1024) printf "%d KB", ($12/1024); else printf "%d bytes", $12; }' `
354*288bf522SAndroid Build Coastguard Worker	    echo "            Total $read_write: $total_file_KB KB i_size: $i_size"
355*288bf522SAndroid Build Coastguard Worker	    grand_total_KB=`expr $grand_total_KB + $total_file_KB`
356*288bf522SAndroid Build Coastguard Worker	fi
357*288bf522SAndroid Build Coastguard Worker    done
358*288bf522SAndroid Build Coastguard Worker    echo "Grand Total File DATA KB $read_write $grand_total_KB"
359*288bf522SAndroid Build Coastguard Worker    rm tempfile
360*288bf522SAndroid Build Coastguard Worker}
361*288bf522SAndroid Build Coastguard Worker
362*288bf522SAndroid Build Coastguard Workerprocess_files_writepages() {
363*288bf522SAndroid Build Coastguard Worker    get_unique_files_writepages
364*288bf522SAndroid Build Coastguard Worker    # Loop over each file that was involved in IO
365*288bf522SAndroid Build Coastguard Worker    # Aggregate the IO done on that file and dump it out
366*288bf522SAndroid Build Coastguard Worker    grand_total_KB=0
367*288bf522SAndroid Build Coastguard Worker    cp $infile tempfile
368*288bf522SAndroid Build Coastguard Worker    for i in `cat uniq_files`
369*288bf522SAndroid Build Coastguard Worker    do
370*288bf522SAndroid Build Coastguard Worker	# Get just the tracepoints for this file
371*288bf522SAndroid Build Coastguard Worker	fgrep -w "$i" tempfile > subtrace
372*288bf522SAndroid Build Coastguard Worker	if [ -s subtrace ]; then
373*288bf522SAndroid Build Coastguard Worker	    fgrep -v -w "$i" tempfile > foo
374*288bf522SAndroid Build Coastguard Worker	    mv foo tempfile
375*288bf522SAndroid Build Coastguard Worker	    total_file_KB=`cat subtrace | awk '{ bytes += $4 } END { print bytes }' `
376*288bf522SAndroid Build Coastguard Worker	    total_file_KB=`expr $total_file_KB / 1024`
377*288bf522SAndroid Build Coastguard Worker	    if [ $total_file_KB -gt 0 ]; then
378*288bf522SAndroid Build Coastguard Worker		echo "File: $i Total $read_write: $total_file_KB KB"
379*288bf522SAndroid Build Coastguard Worker		grand_total_KB=`expr $grand_total_KB + $total_file_KB`
380*288bf522SAndroid Build Coastguard Worker	    fi
381*288bf522SAndroid Build Coastguard Worker	fi
382*288bf522SAndroid Build Coastguard Worker    done
383*288bf522SAndroid Build Coastguard Worker    echo "Grand Total File DATA KB Writepages $grand_total_KB"
384*288bf522SAndroid Build Coastguard Worker    rm tempfile
385*288bf522SAndroid Build Coastguard Worker}
386*288bf522SAndroid Build Coastguard Worker
387*288bf522SAndroid Build Coastguard Worker# Get the aggregate list of pids. For each pid, break up the IOs by file
388*288bf522SAndroid Build Coastguard Workerprocess_pids() {
389*288bf522SAndroid Build Coastguard Worker    read_write=$1
390*288bf522SAndroid Build Coastguard Worker    get_unique_pids
391*288bf522SAndroid Build Coastguard Worker    list_of_pids=`cat uniq_pids`
392*288bf522SAndroid Build Coastguard Worker    # $list_of_pids is a list of all the pids involved in IO
393*288bf522SAndroid Build Coastguard Worker    #
394*288bf522SAndroid Build Coastguard Worker    # Loop over each pid that was involved in IO
395*288bf522SAndroid Build Coastguard Worker    # Find all the files the pid was doing IO on
396*288bf522SAndroid Build Coastguard Worker    # Aggregate the IO done by the pid for each file and dump it out
397*288bf522SAndroid Build Coastguard Worker    #
398*288bf522SAndroid Build Coastguard Worker    grand_total_KB=0
399*288bf522SAndroid Build Coastguard Worker    for i in $list_of_pids
400*288bf522SAndroid Build Coastguard Worker    do
401*288bf522SAndroid Build Coastguard Worker	echo "PID: $i"
402*288bf522SAndroid Build Coastguard Worker	total_pid_KB=0
403*288bf522SAndroid Build Coastguard Worker	# Get just the tracepoints for this pid
404*288bf522SAndroid Build Coastguard Worker	fgrep -w "$i" $infile > subtrace
405*288bf522SAndroid Build Coastguard Worker	# Get all the pids doing IO on this file
406*288bf522SAndroid Build Coastguard Worker	get_unique_files_bypid subtrace
407*288bf522SAndroid Build Coastguard Worker	list_of_files=`cat uniq_files_bypid`
408*288bf522SAndroid Build Coastguard Worker	# $list_of_files is a list of all the files IO'ed by this pid
409*288bf522SAndroid Build Coastguard Worker	for j in $list_of_files
410*288bf522SAndroid Build Coastguard Worker	do
411*288bf522SAndroid Build Coastguard Worker	    i_size=`fgrep -w "$j" subtrace | tail -n1 | awk '{ if ($12 > 1024) printf "%d KB", ($12/1024); else printf "%d bytes", $12; }' `
412*288bf522SAndroid Build Coastguard Worker	    file_KB=`fgrep -w "$j" subtrace | awk '{ bytes += $6 } END { print bytes }' `
413*288bf522SAndroid Build Coastguard Worker	    file_KB=`expr $file_KB / 1024`
414*288bf522SAndroid Build Coastguard Worker	    echo "            $j $read_write: $file_KB KB i_size: $i_size"
415*288bf522SAndroid Build Coastguard Worker	    total_pid_KB=`expr $total_pid_KB + $file_KB`
416*288bf522SAndroid Build Coastguard Worker	done
417*288bf522SAndroid Build Coastguard Worker	echo "            Total $read_write: $total_pid_KB KB"
418*288bf522SAndroid Build Coastguard Worker	grand_total_KB=`expr $grand_total_KB + $total_pid_KB`
419*288bf522SAndroid Build Coastguard Worker    done
420*288bf522SAndroid Build Coastguard Worker    echo "Grand Total File DATA KB $read_write $grand_total_KB"
421*288bf522SAndroid Build Coastguard Worker}
422*288bf522SAndroid Build Coastguard Worker
423*288bf522SAndroid Build Coastguard Worker# main() starts here :
424*288bf522SAndroid Build Coastguard Worker
425*288bf522SAndroid Build Coastguard Workerif [ $# -lt 1 ]; then
426*288bf522SAndroid Build Coastguard Worker    usage
427*288bf522SAndroid Build Coastguard Workerfi
428*288bf522SAndroid Build Coastguard Worker
429*288bf522SAndroid Build Coastguard Workerbdev_set=false
430*288bf522SAndroid Build Coastguard Workerinfile=tracefile.$$
431*288bf522SAndroid Build Coastguard Worker
432*288bf522SAndroid Build Coastguard Workerparseoptions $@
433*288bf522SAndroid Build Coastguard Workeradb root && sleep 2
434*288bf522SAndroid Build Coastguard Workergetmodel
435*288bf522SAndroid Build Coastguard Worker
436*288bf522SAndroid Build Coastguard Workerfound_f2fs=`adb shell 'mount | grep f2fs > /dev/null; echo $?' `
437*288bf522SAndroid Build Coastguard Worker
438*288bf522SAndroid Build Coastguard Workerif [ $found_f2fs == 0 ]; then
439*288bf522SAndroid Build Coastguard Worker    f2fs_fs=1
440*288bf522SAndroid Build Coastguard Workerelse
441*288bf522SAndroid Build Coastguard Worker    f2fs_fs=0
442*288bf522SAndroid Build Coastguard Workerfi
443*288bf522SAndroid Build Coastguard Worker
444*288bf522SAndroid Build Coastguard Workerif [ $f2fs_fs == 0 ] && [ $trace_writepages == true ]; then
445*288bf522SAndroid Build Coastguard Worker    echo "Writepages is only supported with f2fs, please use -r, -w"
446*288bf522SAndroid Build Coastguard Worker    exit 1
447*288bf522SAndroid Build Coastguard Workerfi
448*288bf522SAndroid Build Coastguard Worker
449*288bf522SAndroid Build Coastguard Workerprep_to_do_something
450*288bf522SAndroid Build Coastguard Worker
451*288bf522SAndroid Build Coastguard Workerclean_up_tracepoints
452*288bf522SAndroid Build Coastguard Workerdisk_stats_before
453*288bf522SAndroid Build Coastguard Worker# Start streaming the trace into the tracefile
454*288bf522SAndroid Build Coastguard Workerstreamtrace_start &
455*288bf522SAndroid Build Coastguard Worker
456*288bf522SAndroid Build Coastguard Workerdo_something
457*288bf522SAndroid Build Coastguard Worker
458*288bf522SAndroid Build Coastguard Workerstreamtrace_end
459*288bf522SAndroid Build Coastguard Workerdisk_stats_after
460*288bf522SAndroid Build Coastguard Worker
461*288bf522SAndroid Build Coastguard Workercopyout_trace
462*288bf522SAndroid Build Coastguard Worker
463*288bf522SAndroid Build Coastguard Workerif [ $trace_reads == true ]; then
464*288bf522SAndroid Build Coastguard Worker    echo
465*288bf522SAndroid Build Coastguard Worker    echo "READS :"
466*288bf522SAndroid Build Coastguard Worker    echo "_______"
467*288bf522SAndroid Build Coastguard Worker    echo
468*288bf522SAndroid Build Coastguard Worker    prep_tracefile_rd
469*288bf522SAndroid Build Coastguard Worker    # Get file specific stats - for each file, how many pids read that file ?
470*288bf522SAndroid Build Coastguard Worker    echo "FILE VIEW:"
471*288bf522SAndroid Build Coastguard Worker    process_files_rw Reads
472*288bf522SAndroid Build Coastguard Worker    if [ $pid_view == true ]; then
473*288bf522SAndroid Build Coastguard Worker	# Get pid specific stats - for each pid, what files do they do IO on ?
474*288bf522SAndroid Build Coastguard Worker	echo "PID VIEW:"
475*288bf522SAndroid Build Coastguard Worker	process_pids Reads
476*288bf522SAndroid Build Coastguard Worker    fi
477*288bf522SAndroid Build Coastguard Worker    disk_stats_delta_rd $grand_total_KB
478*288bf522SAndroid Build Coastguard Worker
479*288bf522SAndroid Build Coastguard Worker    debug_FileKB_rd=`cat $infile | awk '{ bytes += $6 } END { printf "%d", bytes/1024 }' `
480*288bf522SAndroid Build Coastguard Worker    echo Debug Grand Total KB READ $debug_FileKB_rd
481*288bf522SAndroid Build Coastguard Workerfi
482*288bf522SAndroid Build Coastguard Worker
483*288bf522SAndroid Build Coastguard Workerif [ $trace_writes == true ]; then
484*288bf522SAndroid Build Coastguard Worker    echo
485*288bf522SAndroid Build Coastguard Worker    echo "Writes :"
486*288bf522SAndroid Build Coastguard Worker    echo "_______"
487*288bf522SAndroid Build Coastguard Worker    echo
488*288bf522SAndroid Build Coastguard Worker    prep_tracefile_wr
489*288bf522SAndroid Build Coastguard Worker    # Get file specific stats - for each file, how many pids read that file ?
490*288bf522SAndroid Build Coastguard Worker
491*288bf522SAndroid Build Coastguard Worker    echo "FILE VIEW:"
492*288bf522SAndroid Build Coastguard Worker    process_files_rw Writes
493*288bf522SAndroid Build Coastguard Worker    if [ $pid_view == true ]; then
494*288bf522SAndroid Build Coastguard Worker	# Get pid specific stats - for each pid, what files do they do IO on ?
495*288bf522SAndroid Build Coastguard Worker	echo "PID VIEW:"
496*288bf522SAndroid Build Coastguard Worker	process_pids Writes
497*288bf522SAndroid Build Coastguard Worker    fi
498*288bf522SAndroid Build Coastguard Worker    disk_stats_delta_wr $grand_total_KB
499*288bf522SAndroid Build Coastguard Worker
500*288bf522SAndroid Build Coastguard Worker    if [ $f2fs_fs == 1 ] ; then
501*288bf522SAndroid Build Coastguard Worker	echo f2fs GC_KB delta = $F2FS_GC_KB_DELTA
502*288bf522SAndroid Build Coastguard Worker    fi
503*288bf522SAndroid Build Coastguard Workerfi
504*288bf522SAndroid Build Coastguard Worker
505*288bf522SAndroid Build Coastguard Workerif [ $f2fs_fs == 1 ] && [ $trace_writepages == true ] ; then
506*288bf522SAndroid Build Coastguard Worker    echo
507*288bf522SAndroid Build Coastguard Worker    echo "Writepages :"
508*288bf522SAndroid Build Coastguard Worker    echo "__________"
509*288bf522SAndroid Build Coastguard Worker    echo
510*288bf522SAndroid Build Coastguard Worker    prep_tracefile_writepages
511*288bf522SAndroid Build Coastguard Worker    # Get file specific stats - for each file, how much did we writepage ?
512*288bf522SAndroid Build Coastguard Worker
513*288bf522SAndroid Build Coastguard Worker    echo "FILE VIEW:"
514*288bf522SAndroid Build Coastguard Worker    process_files_writepages
515*288bf522SAndroid Build Coastguard Worker
516*288bf522SAndroid Build Coastguard Worker    disk_stats_delta_wr $grand_total_KB
517*288bf522SAndroid Build Coastguard Worker
518*288bf522SAndroid Build Coastguard Worker    echo f2fs GC_KB delta = $F2FS_GC_KB_DELTA
519*288bf522SAndroid Build Coastguard Workerfi
520*288bf522SAndroid Build Coastguard Worker
521*288bf522SAndroid Build Coastguard Workerrm -rf tracefile* uniq_* subtrace trace_saved
522