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
8 # Variation: the "-T" option skips all of the atrace instramentation and
9 # attempts to start the apps as quickly as possible.
11 # Example 1: start all default apps. 2 iterations
13 # ./systemapps.sh -i 2
15 # Example 2: just start chrome, feedly, and the home screen in a loop
17 # ./systemapps.sh -L "chrome feedly home" -i 5
19 # Example 3: just start the default apps as quickly as possible
23 # Other options are described below.
26 tracecategories="gfx view am input memreclaim"
31 appList="gmail hangouts chrome youtube play home"
33 function processLocalOption {
37 (-F) forcecoldstart=1;;
38 (-L) appList=$2; shift; ret=1;;
39 (-T) totaltimetest=1;;
40 (-W) waitTime=$2; shift; ret=1;;
42 echo "$0: unrecognized option: $1"
43 echo; echo "Usage: $0 [options]"
44 echo "-A : use all known applications"
45 echo "-F : force cold-start for all apps"
46 echo "-L applist : list of applications"
47 echo " default: $appList"
48 echo "-T : total time to start all apps"
49 echo "-W : time to wait between apps"
50 echo "-g : generate activity strings"
52 echo "-n : keep trace files"
54 echo "-s : stop on error"
55 echo "-t trace categories"
61 CMDDIR=$(dirname $0 2>/dev/null)
65 tmpTraceOutBase=./tmptrace
67 if [ $user != "root" -a $totaltimetest -eq 0 ]; then
68 handleError Must be root on device
73 function computeStats {
83 curMax=$(eval "echo \$${label}max")
85 curMin=$(eval "echo \$${label}min")
86 curMin=${curMin:=100000}
87 curSum=$(eval "echo \$${label}sum")
89 curRestart=$(eval "echo \$${label}restart")
90 curRestart=${curRestart:=0}
91 curReclaim=$(eval "echo \$${label}reclaim")
92 curReclaim=${curReclaim:=0}
93 curFrames=$(eval "echo \$${label}frames")
94 curFrames=${curFrames:=0}
95 curJanks=$(eval "echo \$${label}janks")
96 curJanks=${curJanks:=0}
97 cur90=$(eval "echo \$${label}90")
99 cur95=$(eval "echo \$${label}95")
101 cur99=$(eval "echo \$${label}99")
103 if [ $curMax -lt $t ]; then
104 eval "${label}max=$t"
106 if [ $curMin -gt $t ]; then
107 eval "${label}min=$t"
110 eval "${label}sum=$curSum"
112 ((curRestart=curRestart+${restart:=0}))
113 eval "${label}restart=$curRestart"
114 ((curReclaim=curReclaim+${reclaim:=0}))
115 eval "${label}reclaim=$curReclaim"
116 ((curFrames=curFrames+${frames:=0}))
117 eval "${label}frames=$curFrames"
118 ((curJanks=curJanks+${janks:=0}))
119 eval "${label}janks=$curJanks"
120 ((cur90=cur90+${l90:=0}))
121 eval "${label}90=$cur90"
122 ((cur95=cur95+${l95:=0}))
123 eval "${label}95=$cur95"
124 ((cur99=cur99+${l99:=0}))
125 eval "${label}99=$cur99"
129 echo $(eval "echo \$${label}max") $(eval "echo \$${label}min") $(eval "echo \$${label}sum") \
130 $(eval "echo \$${label}restart") $(eval "echo \$${label}reclaim") \
131 $(eval "echo \$${label}frames") $(eval "echo \$${label}janks") \
132 $(eval "echo \$${label}90") $(eval "echo \$${label}95") $(eval "echo \$${label}99")
137 startTimestamp=$(date +"%s %N")
139 while [ $cur -le $iterations ]
141 if [ $iterations -gt 1 ]; then
142 echo =========================================
143 echo Iteration $cur of $iterations
144 echo =========================================
146 if [ $iterations -gt 1 -o $cur -eq 1 ]; then
147 if [ $totaltimetest -eq 0 ]; then
148 printf "%-6s %7s(ms) %6s(ms) %s %s %s %s\n" App Time AmTime Restart DirReclaim Jank Latency
155 vout Starting $app...
157 loopTimestamp=$(date +"%s %N")
159 resetJankyFrames $(getPackageName $app)
160 if [ $totaltimetest -eq 0 ]; then
161 tmpTraceOut="$tmpTraceOutBase-$app.out"
165 if [ $appnum -eq 0 ]; then
166 printf "%-8s %5s(ms) %3s(ms) %s %s\n" App Start Iter Jank Latency
169 if [ $forcecoldstart -eq 0 ]; then
170 t=$(startActivity $app)
172 t=$(forceStartActivity $app)
175 # let app finish drawing before checking janks
177 set -- $(getJankyFrames $(getPackageName $app))
183 set -- $(getJankyFrames)
189 ((frames=frames+systemFrames))
190 ((janks=janks+systemJanks))
195 loopEndTimestamp=$(date +"%s %N")
196 diffTime=$(computeTimeDiff $loopTimestamp $loopEndTimestamp)
198 if [ $frames -eq 0 ]; then
202 ((jankPct=100*janks/frames))
204 if [ $totaltimetest -gt 0 ]; then
205 # Note: using %f since %d doesn't work correctly
206 # when running on lollipop
207 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
208 ((totaltime=totaltime+t))
211 stopAndDumpInstramentation $tmpTraceOut
212 actName=$(getActivityName $app)
213 pkgName=$(getPackageName $app)
214 stime=$(getStartTime $actName $tmpTraceOut)
216 etime=$(getEndTime $pkgName $tmpTraceOut)
217 ((tdiff=$etime-$stime))
218 if [ $etime -eq 0 -o $stime -eq 0 ]; then
219 handleError $app : could not compute start time stime=$stime etime=$etime
220 # use AmTime so statistics make sense
223 checkForDirectReclaim $actName $tmpTraceOut
226 printf "%-12s %5d %5d %5d %5d %5d(%d%%) %d/%d/%d\n" "$app" "$tdiff" "$t" "$relaunch" "$directReclaim" "$janks" "$jankPct" $l90 $l95 $l99
227 computeStats "$app" "$tdiff" "$relaunch" "$directReclaim" "$frames" "$janks" $l90 $l95 $l99
229 if [ $savetmpfiles -eq 0 ]; then
236 endTimestamp=$(date +"%s %N")
237 diffTime=$(computeTimeDiff $startTimestamp $endTimestamp)
238 if [ $totaltimetest -gt 0 ]; then
239 printf "%-10s %5.0f %5.0f\n" TOTAL $totaltime $diffTime
242 if [ $iterations -gt 1 -a $totaltimetest -eq 0 ]; then
244 echo =========================================
245 printf "Stats after $iterations iterations:\n"
246 echo =========================================
247 printf "%-6s %7s(ms) %6s(ms) %6s(ms) %s %s %s %s\n" App Max Ave Min Restart DirReclaim Jank Latency
250 set -- $(getStats $app)
252 ((ave=sum/iterations))
258 ((ave90=l90/iterations))
259 ((ave95=l95/iterations))
260 ((ave99=l99/iterations))
261 ((jankPct=100*janks/frames))
262 printf "%-12s %5d %5d %5d %5d %5d %5d(%d%%) %d/%d/%d\n" $app $1 $ave $2 $4 $5 $janks $jankPct $ave90 $ave95 $ave99