systemapps.sh revision ae1f93cc3cef9c8c790330dd09f2ff87bf38eabc
1# Script to start a set of apps in order and then in each iteration
2# switch the focus to each one. For each iteration, the time to start
3# the app is reported as measured using atrace events and via am ThisTime.
4# The output also reports if applications are restarted (eg, killed by
5# LMK since previous iteration) or if there were any direct reclaim
6# events.
7#
8# Variation: the "-T" option skips all of the atrace instramentation and
9# attempts to start the apps as quickly as possible.
10#
11# Example 1: start all default apps. 2 iterations
12#
13# ./systemapps.sh -i 2
14#
15# Example 2: just start chrome, feedly, and the home screen in a loop
16#
17# ./systemapps.sh -L "chrome feedly home" -i 5
18#
19# Example 3: just start the default apps as quickly as possible
20#
21# ./systemapps.sh -T
22#
23# Other options are described below.
24#
25iterations=1
26tracecategories="gfx view am input memreclaim"
27totaltimetest=0
28forcecoldstart=0
29
30appList="gmail hangouts chrome youtube play home"
31
32function processLocalOption {
33	ret=0
34	case "$1" in
35	(-A) unset appList;;
36	(-F) forcecoldstart=1;;
37	(-L) appList=$2; shift; ret=1;;
38	(-T) totaltimetest=1;;
39	(*)
40		echo "$0: unrecognized option: $1"
41		echo; echo "Usage: $0 [options]"
42		echo "-A : use all known applications"
43		echo "-F : force cold-start for all apps"
44		echo "-L applist : list of applications"
45		echo "   default: $appList"
46		echo "-T : total time to start all apps"
47		echo "-g : generate activity strings"
48		echo "-i iterations"
49		echo "-n : keep trace files"
50		echo "-o output file"
51		echo "-s : stop on error"
52		echo "-t trace categories"
53		exit 1;;
54	esac
55	return $ret
56}
57
58CMDDIR=$(dirname $0 2>/dev/null)
59CMDDIR=${CMDDIR:=.}
60. $CMDDIR/defs.sh
61
62tmpTraceOutBase=./tmptrace
63
64if [ $user !=  "root" -a $totaltimetest -eq 0 ]; then
65	handleError Must be root on device
66	exit 1
67fi
68doKeyevent HOME
69
70function computeStats {
71	label=$1
72	t=$2
73	restart=$3
74	reclaim=$4
75	frames=$5
76	janks=$6
77	l90=$7
78	l95=$8
79	l99=$9
80	curMax=$(eval "echo \$${label}max")
81	curMax=${curMax:=0}
82	curMin=$(eval "echo \$${label}min")
83	curMin=${curMin:=100000}
84	curSum=$(eval "echo \$${label}sum")
85	curSum=${curSum:=0}
86	curRestart=$(eval "echo \$${label}restart")
87	curRestart=${curRestart:=0}
88	curReclaim=$(eval "echo \$${label}reclaim")
89	curReclaim=${curReclaim:=0}
90	curFrames=$(eval "echo \$${label}frames")
91	curFrames=${curFrames:=0}
92	curJanks=$(eval "echo \$${label}janks")
93	curJanks=${curJanks:=0}
94	cur90=$(eval "echo \$${label}90")
95	cur90=${cur90:=0}
96	cur95=$(eval "echo \$${label}95")
97	cur95=${cur95:=0}
98	cur99=$(eval "echo \$${label}99")
99	cur99=${cur99:=0}
100	if [ $curMax -lt $t ]; then
101		eval "${label}max=$t"
102	fi
103	if [ $curMin -gt $t ]; then
104		eval "${label}min=$t"
105	fi
106	((curSum=curSum+t))
107	eval "${label}sum=$curSum"
108
109	((curRestart=curRestart+${restart:=0}))
110	eval "${label}restart=$curRestart"
111	((curReclaim=curReclaim+${reclaim:=0}))
112	eval "${label}reclaim=$curReclaim"
113	((curFrames=curFrames+${frames:=0}))
114	eval "${label}frames=$curFrames"
115	((curJanks=curJanks+${janks:=0}))
116	eval "${label}janks=$curJanks"
117	((cur90=cur90+${l90:=0}))
118	eval "${label}90=$cur90"
119	((cur95=cur95+${l95:=0}))
120	eval "${label}95=$cur95"
121	((cur99=cur99+${l99:=0}))
122	eval "${label}99=$cur99"
123}
124function getStats {
125	label=$1
126	echo $(eval "echo \$${label}max") $(eval "echo \$${label}min") $(eval "echo \$${label}sum") \
127		$(eval "echo \$${label}restart") $(eval "echo \$${label}reclaim") \
128		$(eval "echo \$${label}frames") $(eval "echo \$${label}janks") \
129		$(eval "echo \$${label}90") $(eval "echo \$${label}95") $(eval "echo \$${label}99")
130}
131
132cur=1
133totaltime=0
134startTimestamp=$(date +"%s %N")
135
136while [ $cur -le $iterations ]
137do
138	if [ $iterations -gt 1 ]; then
139		echo =========================================
140		echo Iteration $cur of $iterations
141		echo =========================================
142	fi
143	if [ $iterations -gt 1 -o $cur -eq 1 ]; then
144		if [ $totaltimetest -eq 0 ]; then
145			printf "%-6s    %7s(ms)  %6s(ms) %s %s %s     %s\n" App  Time AmTime Restart DirReclaim Jank Latency
146		fi
147	fi
148
149	appnum=-1
150	for app in $appList
151	do
152		vout Starting $app...
153		((appnum=appnum+1))
154		loopTimestamp=$(date +"%s %N")
155		resetJankyFrames
156		resetJankyFrames $(getPackageName $app)
157		if [ $totaltimetest -eq 0 ]; then
158			tmpTraceOut="$tmpTraceOutBase-$app.out"
159			>$tmpTraceOut
160			startInstramentation
161		else
162			if [ $appnum -eq 0 ]; then
163				printf "%-8s %5s(ms) %3s(ms) %s      %s\n" App Start Iter Jank Latency
164			fi
165		fi
166		if [ $forcecoldstart -eq 0 ]; then
167			t=$(startActivity $app)
168		else
169			t=$(forceStartActivity $app)
170		fi
171
172		loopEndTimestamp=$(date +"%s %N")
173		diffTime=$(computeTimeDiff $loopTimestamp $loopEndTimestamp)
174		# let app finish drawing before checking janks
175		sleep 3
176		set -- $(getJankyFrames $(getPackageName $app))
177		frames=$1
178		janks=$2
179		l90=$3
180		l95=$4
181		l99=$5
182		set -- $(getJankyFrames)
183		systemFrames=$1
184		systemJanks=$2
185		s90=$3
186		s95=$4
187		s99=$5
188		((frames=frames+systemFrames))
189		((janks=janks+systemJanks))
190		((l90=l90+s90))
191		((l95=l95+s95))
192		((l99=l99+s99))
193		if [ $frames -eq 0 ]; then
194			janks=0
195			jankPct=0
196		else
197			((jankPct=100*janks/frames))
198		fi
199		if [ $totaltimetest -gt 0 ]; then
200			# Note: using %f since %d doesn't work correctly
201			# when running on lollipop
202			printf "%-10s %5.0f   %5.0f    %4.0f(%2.0f%%) %2.0f/%2.0f/%2.0f\n" $app $t $diffTime $janks $jankPct $l90 $l95 $l99
203			((totaltime=totaltime+t))
204			continue
205		else
206			stopAndDumpInstramentation $tmpTraceOut
207			actName=$(getActivityName $app)
208			stime=$(getStartTime $actName $tmpTraceOut)
209			relaunch=$?
210			etime=$(getEndTime $actName $tmpTraceOut)
211			((tdiff=$etime-$stime))
212			if [ $etime -eq 0 -o $stime -eq 0 ]; then
213				handleError $app : could not compute start time stime=$stime  etime=$etime
214				# use AmTime so statistics make sense
215				tdiff=$t
216			fi
217			checkForDirectReclaim $actName $tmpTraceOut
218			directReclaim=$?
219
220			printf "%-12s %5d     %5d     %5d    %5d    %5d(%d%%) %d/%d/%d\n" "$app" "$tdiff" "$t" "$relaunch" "$directReclaim" "$janks" "$jankPct" $l90 $l95 $l99
221			computeStats "$app" "$tdiff" "$relaunch" "$directReclaim" "$frames" "$janks" $l90 $l95 $l99
222
223			if [ $savetmpfiles -eq 0 ]; then
224				rm -f $tmpTraceOut
225			fi
226		fi
227	done
228	((cur=cur+1))
229done
230endTimestamp=$(date +"%s %N")
231diffTime=$(computeTimeDiff $startTimestamp $endTimestamp)
232if [ $totaltimetest -gt 0 ]; then
233	printf "%-10s %5.0f   %5.0f\n" TOTAL $totaltime $diffTime
234fi
235
236if [ $iterations -gt 1 -a $totaltimetest -eq 0 ]; then
237	echo
238	echo =========================================
239	printf "Stats after $iterations iterations:\n"
240	echo =========================================
241	printf "%-6s    %7s(ms) %6s(ms) %6s(ms)    %s    %s %s     %s\n" App Max Ave Min Restart DirReclaim Jank Latency
242	for app in $appList
243	do
244		set -- $(getStats $app)
245		sum=$3
246		((ave=sum/iterations))
247		frames=$6
248		janks=$7
249		l90=$8
250		l95=$9
251		l99=${10}
252		((ave90=l90/iterations))
253		((ave95=l95/iterations))
254		((ave99=l99/iterations))
255		((jankPct=100*janks/frames))
256		printf "%-12s %5d      %5d      %5d      %5d      %5d     %5d(%d%%) %d/%d/%d\n" $app $1 $ave $2 $4 $5 $janks $jankPct $ave90 $ave95 $ave99
257	done
258fi
259