Home | History | Annotate | Download | only in workloads
      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 #
     25 iterations=1
     26 tracecategories="gfx view am input memreclaim"
     27 totaltimetest=0
     28 forcecoldstart=0
     29 waitTime=3.0
     30 
     31 appList="gmail hangouts chrome youtube play home"
     32 
     33 function processLocalOption {
     34 	ret=0
     35 	case "$1" in
     36 	(-A) unset appList;;
     37 	(-F) forcecoldstart=1;;
     38 	(-L) appList=$2; shift; ret=1;;
     39 	(-T) totaltimetest=1;;
     40 	(-W) waitTime=$2; shift; ret=1;;
     41 	(*)
     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"
     51 		echo "-i iterations"
     52 		echo "-n : keep trace files"
     53 		echo "-o output file"
     54 		echo "-s : stop on error"
     55 		echo "-t trace categories"
     56 		exit 1;;
     57 	esac
     58 	return $ret
     59 }
     60 
     61 CMDDIR=$(dirname $0 2>/dev/null)
     62 CMDDIR=${CMDDIR:=.}
     63 . $CMDDIR/defs.sh
     64 
     65 tmpTraceOutBase=./tmptrace
     66 
     67 if [ $user !=  "root" -a $totaltimetest -eq 0 ]; then
     68 	handleError Must be root on device
     69 	exit 1
     70 fi
     71 doKeyevent HOME
     72 
     73 function computeStats {
     74 	label=$1
     75 	t=$2
     76 	restart=$3
     77 	reclaim=$4
     78 	frames=$5
     79 	janks=$6
     80 	l90=$7
     81 	l95=$8
     82 	l99=$9
     83 	curMax=$(eval "echo \$${label}max")
     84 	curMax=${curMax:=0}
     85 	curMin=$(eval "echo \$${label}min")
     86 	curMin=${curMin:=100000}
     87 	curSum=$(eval "echo \$${label}sum")
     88 	curSum=${curSum:=0}
     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")
     98 	cur90=${cur90:=0}
     99 	cur95=$(eval "echo \$${label}95")
    100 	cur95=${cur95:=0}
    101 	cur99=$(eval "echo \$${label}99")
    102 	cur99=${cur99:=0}
    103 	if [ $curMax -lt $t ]; then
    104 		eval "${label}max=$t"
    105 	fi
    106 	if [ $curMin -gt $t ]; then
    107 		eval "${label}min=$t"
    108 	fi
    109 	((curSum=curSum+t))
    110 	eval "${label}sum=$curSum"
    111 
    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"
    126 }
    127 function getStats {
    128 	label=$1
    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")
    133 }
    134 
    135 cur=1
    136 totaltime=0
    137 startTimestamp=$(date +"%s %N")
    138 
    139 while [ $cur -le $iterations ]
    140 do
    141 	if [ $iterations -gt 1 ]; then
    142 		echo =========================================
    143 		echo Iteration $cur of $iterations
    144 		echo =========================================
    145 	fi
    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
    149 		fi
    150 	fi
    151 
    152 	appnum=-1
    153 	for app in $appList
    154 	do
    155 		vout Starting $app...
    156 		((appnum=appnum+1))
    157 		loopTimestamp=$(date +"%s %N")
    158 		resetJankyFrames
    159 		resetJankyFrames $(getPackageName $app)
    160 		if [ $totaltimetest -eq 0 ]; then
    161 			tmpTraceOut="$tmpTraceOutBase-$app.out"
    162 			>$tmpTraceOut
    163 			startInstramentation
    164 		else
    165 			if [ $appnum -eq 0 ]; then
    166 				printf "%-8s %5s(ms) %3s(ms) %s      %s\n" App Start Iter Jank Latency
    167 			fi
    168 		fi
    169 		if [ $forcecoldstart -eq 0 ]; then
    170 			t=$(startActivity $app)
    171 		else
    172 			t=$(forceStartActivity $app)
    173 		fi
    174 
    175 		# let app finish drawing before checking janks
    176 		sleep $waitTime
    177 		set -- $(getJankyFrames $(getPackageName $app))
    178 		frames=$1
    179 		janks=$2
    180 		l90=$3
    181 		l95=$4
    182 		l99=$5
    183 		set -- $(getJankyFrames)
    184 		systemFrames=$1
    185 		systemJanks=$2
    186 		s90=$3
    187 		s95=$4
    188 		s99=$5
    189 		((frames=frames+systemFrames))
    190 		((janks=janks+systemJanks))
    191 		((l90=l90+s90))
    192 		((l95=l95+s95))
    193 		((l99=l99+s99))
    194 
    195 		loopEndTimestamp=$(date +"%s %N")
    196 		diffTime=$(computeTimeDiff $loopTimestamp $loopEndTimestamp)
    197 
    198 		if [ $frames -eq 0 ]; then
    199 			janks=0
    200 			jankPct=0
    201 		else
    202 			((jankPct=100*janks/frames))
    203 		fi
    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))
    209 			continue
    210 		else
    211 			stopAndDumpInstramentation $tmpTraceOut
    212 			actName=$(getActivityName $app)
    213 			pkgName=$(getPackageName $app)
    214 			stime=$(getStartTime $actName $tmpTraceOut)
    215 			relaunch=$?
    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
    221 				tdiff=$t
    222 			fi
    223 			checkForDirectReclaim $actName $tmpTraceOut
    224 			directReclaim=$?
    225 
    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
    228 
    229 			if [ $savetmpfiles -eq 0 ]; then
    230 				rm -f $tmpTraceOut
    231 			fi
    232 		fi
    233 	done
    234 	((cur=cur+1))
    235 done
    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
    240 fi
    241 
    242 if [ $iterations -gt 1 -a $totaltimetest -eq 0 ]; then
    243 	echo
    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
    248 	for app in $appList
    249 	do
    250 		set -- $(getStats $app)
    251 		sum=$3
    252 		((ave=sum/iterations))
    253 		frames=$6
    254 		janks=$7
    255 		l90=$8
    256 		l95=$9
    257 		l99=${10}
    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
    263 	done
    264 fi
    265