1 # functions and definitions for workload automation scripts
3 # See recentfling.sh, systemapps.sh, and other scripts that use
7 dflttracecategories="gfx input view am rs power sched freq idle load memreclaim"
8 dfltAppList="gmail hangouts chrome youtube camera photos play maps calendar earth calculator sheets docs home"
11 # default activities. Can dynamically generate with -g.
12 gmailActivity='com.google.android.gm/com.google.android.gm.ConversationListActivityGmail'
13 hangoutsActivity='com.google.android.talk/com.google.android.talk.SigningInActivity'
14 chromeActivity='com.android.chrome/_not_used'
15 youtubeActivity='com.google.android.youtube/com.google.android.apps.youtube.app.WatchWhileActivity'
16 cameraActivity='com.google.android.GoogleCamera/com.android.camera.CameraActivity'
17 playActivity='com.android.vending/com.google.android.finsky.activities.MainActivity'
18 feedlyActivity='com.devhd.feedly/com.devhd.feedly.Main'
19 photosActivity='com.google.android.apps.photos/com.google.android.apps.photos.home.HomeActivity'
20 mapsActivity='com.google.android.apps.maps/com.google.android.maps.MapsActivity'
21 calendarActivity='com.google.android.calendar/com.android.calendar.AllInOneActivity'
22 earthActivity='com.google.earth/com.google.earth.EarthActivity'
23 calculatorActivity='com.google.android.calculator/com.android.calculator2.Calculator'
24 sheetsActivity='com.google.android.apps.docs.editors.sheets/com.google.android.apps.docs.app.NewMainProxyActivity'
25 docsActivity='com.google.android.apps.docs.editors.docs/com.google.android.apps.docs.app.NewMainProxyActivity'
26 operaActivity='com.opera.mini.native/com.opera.mini.android.Browser'
27 firefoxActivity='org.mozilla.firefox/org.mozilla.firefox.App'
28 suntempleActivity='com.BrueComputing.SunTemple/com.epicgames.ue4.GameActivity'
29 homeActivity='com.google.android.googlequicksearchbox/com.google.android.launcher.GEL'
32 echo "$0: unrecognized option: $1"
33 echo; echo "Usage: $0 [options]"
34 echo "-e : stop on error"
36 echo "-n : keep trace files"
38 echo "-s device : adb device"
39 echo "-t trace categories"
40 echo "-g : generate activity strings"
49 (-d) DEVICE=$2; shift;;
52 (-t) tracecategories=$2; shift;;
53 (-i) iterations=$2; shift;;
54 (-o) output=$2; shift;;
57 (-s) deviceName=$2; shift;;
58 (-g) generateActivities=1;;
61 chk1=$(functions 2>/dev/null)
62 chk2=$(typeset -F 2>/dev/null)
64 if echo $chk1 $chk2 | grep -q processLocalOption; then
65 if ! processLocalOption "$1" "$2"; then
76 # check if running on a device
77 if ls /etc/* 2>/dev/null | grep -q android.hardware; then
82 # do a throw-away adb in case the server is out-of-date
83 adb devices -l 2>&1 >/dev/null
85 if [ -z "$deviceName" ]; then
86 devInfo=$(adb devices -l | grep -v ^List | head -1)
88 devInfo=$(adb devices -l | grep $deviceName)
92 echo Error: could not find device $deviceName
96 ADB="adb -s $deviceName shell "
97 DEVICE=$(echo $4 | sed 's/product://')
101 if [ $isOnDevice -gt 0 ]; then
104 if ! echo $$ > /dev/cpuset/background/tasks; then
105 echo Could not put PID $$ in background
113 # default values if not set by options or calling script
114 appList=${appList:=$dfltAppList}
115 savetmpfiles=${savetmpfiles:=0}
116 stoponerror=${stoponerror:=0}
117 verbose=${verbose:=0}
118 compress=${compress:=1}
119 iterations=${iterations:=5}
120 tracecategories=${tracecategories:=$dflttracecategories}
122 output=${output:="./out"}
124 # clear the output file
125 if [ -f $output ]; then
130 AM_FORCE_START="${ADB}am start -W -S"
131 AM_START="${ADB}am start -W"
132 AM_START_NOWAIT="${ADB}am start"
133 AM_STOP="${ADB}am force-stop"
134 AM_LIST="${ADB}am stack list"
140 # debug output enabled by -v
141 if [ $verbose -gt 0 ]; then
143 echo DEBUG: $* >&2 >> $output
147 function findtimestamp {
148 # extract timestamp from atrace log entry
149 while [ "$2" != "" -a "$2" != "tracing_mark_write" ]
156 function computeTimeDiff {
157 # Compute time diff given: startSeconds startNs endSeconds endNS
159 # strip leading zeros
160 startS=$(expr 0 + $1)
162 if [ "$2" = N ]; then
166 startNs=$(expr 0 + $2)
170 ((startMs=startS*1000 + startNs/1000000))
171 ((endMs=endS*1000 + endNs/1000000))
172 ((diff=endMs-startMs))
179 set -- $(echo $in | tr . " ")
181 # shell addition via (( )) doesn't like leading zeroes in msecs
182 # field so remove leading zeroes
183 msecfield=$(expr 0 + $2)
185 ((msec=$1*1000000+msecfield))
190 function getStartTime {
191 # extract event indicating beginning of start sequence
192 # a) look for a "launching" event indicating start from scratch
193 # b) look for another activity getting a pause event
197 s=$(grep "Binder.*tracing_mark_write.*launching" $traceout 2>/dev/null | head -1| tr [\(\)\[\]
\r:] " ")
199 s=$(grep activityPause $traceout | head -1 2>/dev/null| tr [\(\)\[\]
\r:] " ")
201 vout $_app was restarted!
205 log2msec $(findtimestamp $s)
209 function getEndTime {
210 # extract event indicating end of start sequence. We use the
211 # first surfaceflinger event associated with the target activity
214 f=$(grep "surfaceflinger.*tracing_mark_write.*$_app" $traceout 2>/dev/null |
215 grep -v Starting | head -1 | tr [\(\)\[\]
\r:] " ")
217 # Hmm. sf symbols may not be there... get the pid
218 pid=$(${ADB}pidof /system/bin/surfaceflinger | tr "[
\r]" "[ ]")
219 f=$(grep " <...>-$pid.*tracing_mark_write.*$_app" $traceout 2>/dev/null |
220 grep -v Starting | head -1 | tr [\(\)\[\]
\r:] " ")
223 log2msec $(findtimestamp $f)
226 function resetJankyFrames {
228 _gfxapp=${_gfxapp:="com.android.systemui"}
229 ${ADB}dumpsys gfxinfo $_gfxapp reset 2>&1 >/dev/null
232 function getJankyFrames {
234 _gfxapp=${_gfxapp:="com.android.systemui"}
236 # Note: no awk or sed on devices so have to do this
241 ${ADB}dumpsys gfxinfo $_gfxapp | tr "\r" " " | egrep "9[059]th| frames" | while read line
243 if echo $line | grep -q "Total frames"; then
246 elif echo $line | grep -q "Janky frames"; then
249 elif echo $line | grep -q "90th"; then
250 set -- $(echo $line | tr m " ")
252 elif echo $line | grep -q "95th"; then
253 set -- $(echo $line | tr m " ")
255 elif echo $line | grep -q "99th"; then
256 set -- $(echo $line | tr m " ")
258 echo $total $janky $l90 $l95 $l99
264 function checkForDirectReclaim {
265 # look for any reclaim events in atrace output
268 if grep -qi reclaim $traceout; then
274 function startInstramentation {
278 enableAtrace=${enableAtrace:=1}
279 # Called at beginning of loop. Turn on instramentation like atrace
280 vout start instramentation $(date)
281 echo =============================== >> $output
282 echo Before iteration $_iter >> $output
283 echo =============================== >> $output
284 ${ADB}cat /proc/meminfo 2>&1 >> $output
285 ${ADB}dumpsys meminfo 2>&1 >> $output
286 if [ "$DEVICE" = volantis ]; then
287 ${ADB}cat /d/nvmap/iovmm/procrank 2>&1 >> $output
289 if [ "$user" = root -a $enableAtrace -gt 0 ]; then
290 vout ${ADB}atrace -b 32768 --async_start $tracecategories
291 ${ADB}atrace -b 32768 --async_start $tracecategories >> $output
296 function stopInstramentation {
298 enableAtrace=${enableAtrace:=1}
299 if [ "$user" = root -a $enableAtrace -gt 0 ]; then
300 vout ${ADB}atrace --async_stop
301 ${ADB}atrace --async_stop > /dev/null
305 function stopAndDumpInstramentation {
306 vout stop instramentation $(date)
307 echo =============================== >> $output
308 echo After iteration >> $output
309 echo =============================== >> $output
310 ${ADB}cat /proc/meminfo 2>&1 >> $output
311 ${ADB}dumpsys meminfo 2>&1 >> $output
312 if [ "$user" = root ]; then
314 traceout=${traceout:=$output}
315 echo =============================== >> $traceout
316 echo TRACE >> $traceout
317 echo =============================== >> $traceout
318 if [ $compress -gt 0 ]; then
319 tmpTrace=./tmptrace.$$
320 UNCOMPRESS=$CMDDIR/atrace-uncompress.py
323 ${ADB}atrace -z -b 32768 --async_dump >> $tmpTrace
324 python $UNCOMPRESS $tmpTrace >> $traceout
327 ${ADB}atrace -b 32768 --async_dump > $traceout
329 vout ${ADB}atrace $zarg -b 32768 --async_dump
330 vout ${ADB}atrace --async_stop
331 ${ADB}atrace --async_stop > /dev/null
335 function getActivityName {
336 cmd="actName=\$${1}Activity"
341 function getPackageName {
342 set -- $(getActivityName $1 | tr "[/]" "[ ]")
346 function startActivityFromPackage {
347 if [ "$1" = home ]; then
352 vout $AM_START_NOWAIT -p "$(getPackageName $1)" -c android.intent.category.LAUNCHER -a android.intent.action.MAIN
353 $AM_START_NOWAIT -p "$(getPackageName $1)" -c android.intent.category.LAUNCHER -a android.intent.action.MAIN 2>&1
357 function startActivity {
358 if [ "$1" = home ]; then
362 elif [ "$1" = chrome ]; then
363 if [ "$DEVICE" = volantis ]; then
364 vout $AM_START_NOWAIT -p "$(getPackageName $1)" http://www.theverge.com
365 $AM_START_NOWAIT -p "$(getPackageName $1)" http://www.theverge.com > /dev/null
368 vout $AM_START -p "$(getPackageName $1)" http://www.theverge.com
369 set -- $($AM_START -p "$(getPackageName $1)" http://www.theverge.com | grep ThisTime)
372 vout $AM_START "$(getActivityName $1)"
373 set -- $($AM_START "$(getActivityName $1)" | grep ThisTime)
375 echo $2 | tr "[\r]" "[\n]"
378 function forceStartActivity {
379 if [ "$1" = chrome ]; then
380 vout $AM_START -p "$(getPackageName $1)" http://www.theverge.com
381 set -- $($AM_FORCE_START -p "$(getPackageName $1)" http://www.theverge.com | grep ThisTime)
383 vout $AM_FORCE_START "$(getActivityName $1)"
384 set -- $($AM_FORCE_START "$(getActivityName $1)" | grep ThisTime)
386 echo $2 | tr "[\r]" "[\n]"
389 function checkActivity {
391 actName="$(getActivityName $1)"
392 $AM_LIST | grep $actName
395 #function stopActivity {
396 # vout $AM_STOP $(getActivityName $1)
397 # $AM_STOP $(getActivityName $1)
401 vout ${ADB}input swipe $*
402 ${ADB}nice input swipe $*
406 echo $* > ./tmpOutput
407 vout ${ADB}input text \"$*\"
408 ${ADB}input text "$(cat ./tmpOutput)"
413 vout ${ADB}input tap $*
417 function doKeyevent {
418 vout $INPUT keyevent $*
422 function checkIsRunning {
425 if ! $PS | grep $p | grep -qv grep; then
426 handleError $*: $p is not running
431 function checkStartTime {
432 vout checkStartTime $1 v $2
437 if [ "$1" -gt "$2" ]; then
445 function handleError {
447 stopAndDumpInstramentation
448 if [ $stoponerror -gt 0 ]; then
454 if ${ADB}ls /data 2>/dev/null | grep -q "Permission denied"; then
459 if [ $generateActivities -gt 0 ]; then
460 if [ $isOnDevice -gt 0 ]; then
461 echo Error: cannot generate activity list when run on device
464 echo Generating activities...
467 startActivityFromPackage $app 2>&1 > /dev/null
468 act=$(${ADB}am stack list | grep $(getPackageName $app) | sed -e 's/
\r//' | head -1 | awk '{ print $2; }')
469 eval "${app}Activity=$act"
470 echo "ACTIVITY: $app --> $(getActivityName $app)"