1#!/bin/sh
2
3# This function just re-writes the timestamp of the strace entries to be
4# seconds.usecs since boot. To match the timestamping of ftrace (so we can
5# merge them later).
6process_strace()
7{
8    strace=$1
9    # parse in data/system/vendor and parse out /sys/devices/system/...
10    egrep '\/system\/|\/data\/|\/vendor\/' $strace | egrep -v '\/sys\/devices\/system\/' > bar
11    fgrep -v '= -1' bar > foo
12    mv foo bar
13    # begin_time is seconds since epoch
14    begin_time=`cat trace.begin`
15    # replace seconds since epoch with SECONDS SINCE BOOT in the
16    # strace files
17    awk -v begin="$begin_time" '{ printf "%f strace ", $1 - begin; $1=""; print $0}' bar > $2
18    rm bar
19}
20
21#
22# This function processes the ftrace file, removing the fields that we don't care
23# about, breaks up the ftrace file into one file per pid.
24# Input : One single fstrace file.
25# Output : Multiple fstrace.pid files.
26prep_fstrace()
27{
28    # Remove leading junk
29    fgrep android_fs_data $1 | sed 's/^.* \[.*\] //' | sed s/://g | sed s/,//g > foo
30    sed 's/android_fs_dataread_start/read/' foo > bar1
31    mv bar1 bar
32    # First column is timestamp SECONDS SINCE BOOT
33    awk '{ print $2, "ftrace", $3, $5, $7, $9, $13 }' bar > foo
34    rm bar
35    # Get all the uniq pids
36    awk '{print $7}' foo | sort | uniq > pidlist
37    for i in `cat pidlist`
38    do
39	awk -v pid=$i '{ if (pid == $7) print $0}' foo > fstrace.$i
40    done
41    rm pidlist
42    rm foo
43}
44
45# Merge straces and ftraces.
46# The goal here is to catch mmap'ed IO (reads) that won't be in the
47# strace file. The algorithm is to look for mmaps in the strace file,
48# use the files tha are mmap'ed to search in the ftraces to pick up
49# tracepoints from there, and merge those with the straces.
50# The output of this function is a set of parsed_input_trace.<pid>
51# files, that can then be compiled into .wl files
52merge_compile()
53{
54    for stracefile in trace.*
55    do
56	if [ $stracefile == trace.begin ] || [ $stracefile == trace.tar ];
57	then
58	    continue
59	fi
60	# Get the pid from the strace filename (pid is the extension)
61	pid=${stracefile##*.}
62	process_strace $stracefile foo.$pid
63	if ! [ -s foo.$pid ]; then
64	    rm foo.$pid
65	    continue
66	fi
67	#
68	# If we have matching strace and ftrace files, then look for mmaps in
69	# the strace pluck the corresponding entries for the mmap (mmaped IO)
70	# from the ftrace and merge them into the strace
71	#
72	if [ -f fstrace.$pid ]; then
73	    fgrep mmap foo.$pid > bar
74	    if [ -s bar ]; then
75		# Get all the unique mmap'ed filenames from the strace
76		awk '{ print $7 }' bar | sed 's/^[^<]*<//g' | sed 's/>,//g' > mapped_files
77		# Pluck all the lines from the ftrace corresponding to the mmaps
78		cat /dev/null > footemp
79		for j in `sort mapped_files | uniq`
80		do
81		    # Merge the readpage(s) traces from the ftrace into strace
82		    # for this mmaped file.
83		    grep -w $j fstrace.$pid > foobar
84		    if [ $? == 0 ]; then
85			sort foo.$pid foobar >> footemp
86		    fi
87		    rm foobar
88		done
89		rm mapped_files
90		if [ -s footemp ]; then
91		    mv footemp parsed_input_trace.$pid
92		else
93		    mv foo.$pid parsed_input_trace.$pid
94		fi
95	    else
96		mv foo.$pid parsed_input_trace.$pid
97	    fi
98	    rm bar
99	else
100	    mv foo.$pid parsed_input_trace.$pid
101	fi
102	echo compiling parsed_input_trace.$pid
103	compile_ioshark parsed_input_trace.$pid $pid.wl
104	rm parsed_input_trace.$pid
105	rm -f foo.$pid
106    done
107}
108
109# main() starts here
110
111rm -f *.wl
112rm -f parsed*
113rm -f ioshark_filenames
114
115# Pre-process the ftrace file
116prep_fstrace fstrace
117# Merge the ftrace file(s) with the strace files
118merge_compile
119
120# tar up the .wl files just created
121tar cf wl-test.tar ioshark_filenames *.wl
122