OSDN Git Service

Merge "Simpleperf: remove unnecessary exit()." am: dd7f62ed57 am: 5345ea0e4f
[android-x86/system-extras.git] / tests / workloads / defs.sh
1 # functions and definitions for workload automation scripts
2 #
3 # See recentfling.sh, systemapps.sh, and other scripts that use
4 # these definitions.
5 #
6
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"
9 generateActivities=0
10
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'
30
31 function showUsage {
32         echo "$0: unrecognized option: $1"
33         echo; echo "Usage: $0 [options]"
34         echo "-e : stop on error"
35         echo "-i iterations"
36         echo "-n : keep trace files"
37         echo "-o output file"
38         echo "-s device : adb device"
39         echo "-t trace categories"
40         echo "-g : generate activity strings"
41 }
42
43 DEVICE=unknown
44
45 # handle args
46 while [ $# -gt 0 ]
47 do
48         case "$1" in
49         (-d) DEVICE=$2; shift;;
50         (-e) stoponerror=1;;
51         (-n) savetmpfiles=1;;
52         (-t) tracecategories=$2; shift;;
53         (-i) iterations=$2; shift;;
54         (-o) output=$2; shift;;
55         (-v) verbose=1;;
56         (-nz) compress=0;;
57         (-s) deviceName=$2; shift;;
58         (-g) generateActivities=1;;
59         (--) ;;
60         (*)
61                 chk1=$(functions 2>/dev/null)
62                 chk2=$(typeset -F 2>/dev/null)
63
64                 if echo $chk1 $chk2 | grep -q processLocalOption; then
65                         if ! processLocalOption "$1" "$2"; then
66                                 shift
67                         fi
68                 else
69                         showUsage $1
70                         exit 1
71                 fi;;
72         esac
73         shift
74 done
75
76 # check if running on a device
77 if ls /etc/* 2>/dev/null | grep -q android.hardware; then
78         ADB=""
79         compress=0
80         isOnDevice=1
81 else
82         # do a throw-away adb in case the server is out-of-date
83         adb devices -l 2>&1 >/dev/null
84
85         if [ -z "$deviceName" ]; then
86                 devInfo=$(adb devices -l | grep -v ^List | head -1)
87         else
88                 devInfo=$(adb devices -l | grep $deviceName)
89         fi
90         set -- $devInfo
91         if [ -z $1 ]; then
92                 echo Error: could not find device $deviceName
93                 exit 1
94         fi
95         deviceName=$1
96         ADB="adb -s $deviceName shell "
97         DEVICE=$(echo $4 | sed 's/product://')
98         isOnDevice=0
99 fi
100
101 if [ $isOnDevice -gt 0 ]; then
102         case "$DEVICE" in
103         (bullhead|angler)
104                 if ! echo $$ > /dev/cpuset/background/tasks; then
105                         echo Could not put PID $$ in background
106                 fi
107                 ;;
108         (*)
109                 ;;
110         esac
111 fi
112
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}
121 ADB=${ADB:=""}
122 output=${output:="./out"}
123
124 # clear the output file
125 if [ -f $output ]; then
126         > $output
127 fi
128
129 # ADB commands
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"
135 WHO="${ADB}whoami"
136 INPUT="${ADB}input"
137 PS="${ADB}ps"
138
139 function vout {
140         # debug output enabled by -v
141         if [ $verbose -gt 0 ]; then
142             echo DEBUG: $* >&2
143             echo DEBUG: $* >&2 >> $output
144         fi
145 }
146
147 function findtimestamp {
148         # extract timestamp from atrace log entry
149         while [ "$2" != "" -a "$2" != "tracing_mark_write" ]
150         do
151                 shift
152         done
153         echo $1
154 }
155
156 function computeTimeDiff {
157         # Compute time diff given: startSeconds startNs endSeconds endNS
158
159         # strip leading zeros
160         startS=$(expr 0 + $1)
161         endS=$(expr 0 + $3)
162         if [ "$2" = N ]; then
163                 startNs=0
164                 endNs=0
165         else
166                 startNs=$(expr 0 + $2)
167                 endNs=$(expr 0 + $4)
168         fi
169
170         ((startMs=startS*1000 + startNs/1000000))
171         ((endMs=endS*1000 + endNs/1000000))
172         ((diff=endMs-startMs))
173         echo $diff
174 }
175
176 function log2msec {
177         in=$1
178         in=${in:=0.0}
179         set -- $(echo $in | tr . " ")
180
181         # shell addition via (( )) doesn't like leading zeroes in msecs
182         # field so remove leading zeroes
183         msecfield=$(expr 0 + $2)
184
185         ((msec=$1*1000000+msecfield))
186         ((msec=msec/1000))
187         echo $msec
188 }
189
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
194         _app=$1
195         traceout=$2
196         ret=0
197         s=$(grep "Binder.*tracing_mark_write.*launching" $traceout 2>/dev/null | head -1| tr [\(\)\[\]\r:] " ")
198         if [ -z "$s" ]; then
199                 s=$(grep activityPause $traceout | head -1 2>/dev/null| tr [\(\)\[\]\r:] " ")
200         else
201                 vout $_app was restarted!
202                 ret=1
203         fi
204         vout STARTLOG: $s
205         log2msec $(findtimestamp $s)
206         return $ret
207 }
208
209 function getEndTime {
210         # extract event indicating end of start sequence. We use the
211         # first surfaceflinger event associated with the target activity
212         _app=$1
213         traceout=$2
214         f=$(grep "surfaceflinger.*tracing_mark_write.*$_app" $traceout 2>/dev/null |
215                 grep -v Starting | head -1 | tr [\(\)\[\]\r:] " ")
216         if [ -z "$f" ]; then
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:] " ")
221         fi
222         vout ENDLOG: $f
223         log2msec $(findtimestamp $f)
224 }
225
226 function resetJankyFrames {
227         _gfxapp=$1
228         _gfxapp=${_gfxapp:="com.android.systemui"}
229         ${ADB}dumpsys gfxinfo $_gfxapp reset 2>&1 >/dev/null
230 }
231
232 function getJankyFrames {
233         _gfxapp=$1
234         _gfxapp=${_gfxapp:="com.android.systemui"}
235
236         # Note: no awk or sed on devices so have to do this
237         # purely with bash
238         total=0
239         janky=0
240         latency=0
241         ${ADB}dumpsys gfxinfo $_gfxapp | tr "\r" " " | egrep "9[059]th| frames" | while read line
242         do
243                 if echo $line | grep -q "Total frames"; then
244                         set -- $line
245                         total=$4
246                 elif echo $line | grep -q "Janky frames"; then
247                         set -- $line
248                         janky=$3
249                 elif echo $line | grep -q "90th"; then
250                         set -- $(echo $line | tr m " ")
251                         l90=$3
252                 elif echo $line | grep -q "95th"; then
253                         set -- $(echo $line | tr m " ")
254                         l95=$3
255                 elif echo $line | grep -q "99th"; then
256                         set -- $(echo $line | tr m " ")
257                         l99=$3
258                         echo $total $janky $l90 $l95 $l99
259                         break
260                 fi
261         done
262 }
263
264 function checkForDirectReclaim {
265         # look for any reclaim events in atrace output
266         _app=$1
267         traceout=$2
268         if grep -qi reclaim $traceout; then
269            return 1
270         fi
271         return 0
272 }
273
274 function startInstramentation {
275         _iter=$1
276         _iter=${_iter:=0}
277         enableAtrace=$2
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
288         fi
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
292                 echo >> $output
293         fi
294 }
295
296 function stopInstramentation {
297         enableAtrace=$1
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
302         fi
303 }
304
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
313                 traceout=$1
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
321                         > $tmpTrace
322                         zarg="-z"
323                         ${ADB}atrace -z -b 32768 --async_dump >> $tmpTrace
324                         python $UNCOMPRESS $tmpTrace >> $traceout
325                         rm -f $tmpTrace
326                 else
327                         ${ADB}atrace -b 32768 --async_dump > $traceout
328                 fi
329                 vout ${ADB}atrace $zarg -b 32768 --async_dump
330                 vout ${ADB}atrace --async_stop
331                 ${ADB}atrace --async_stop > /dev/null
332         fi
333 }
334
335 function getActivityName {
336         cmd="actName=\$${1}Activity"
337         eval $cmd
338         echo $actName
339 }
340
341 function getPackageName {
342         set -- $(getActivityName $1 | tr "[/]" "[ ]")
343         echo $1
344 }
345
346 function startActivityFromPackage {
347         if [ "$1" = home ]; then
348                 doKeyevent HOME
349                 echo 0
350                 return 0
351         fi
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
354         echo 0
355 }
356
357 function startActivity {
358         if [ "$1" = home ]; then
359                 doKeyevent HOME
360                 echo 0
361                 return 0
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
366                         set -- 0 0
367                 else
368                         vout $AM_START -p "$(getPackageName $1)" http://www.theverge.com
369                         set -- $($AM_START -p "$(getPackageName $1)" http://www.theverge.com | grep ThisTime)
370                 fi
371         else
372                 vout $AM_START "$(getActivityName $1)"
373                 set -- $($AM_START "$(getActivityName $1)" | grep ThisTime)
374         fi
375         echo $2 | tr "[\r]" "[\n]"
376 }
377
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)
382         else
383                 vout $AM_FORCE_START "$(getActivityName $1)"
384                 set -- $($AM_FORCE_START "$(getActivityName $1)" | grep ThisTime)
385         fi
386         echo $2 | tr "[\r]" "[\n]"
387 }
388
389 function checkActivity {
390         # requires root
391         actName="$(getActivityName $1)"
392         $AM_LIST | grep $actName
393 }
394
395 #function stopActivity {
396 #    vout $AM_STOP $(getActivityName $1)
397 #    $AM_STOP $(getActivityName $1)
398 #}
399
400 function doSwipe {
401         vout ${ADB}input swipe $*
402         ${ADB}nice input swipe $*
403 }
404
405 function doText {
406         echo $* > ./tmpOutput
407         vout ${ADB}input text \"$*\"
408         ${ADB}input text "$(cat ./tmpOutput)"
409         rm -f ./tmpOutput
410 }
411
412 function doTap {
413         vout ${ADB}input tap $*
414         ${ADB}input tap $*
415 }
416
417 function doKeyevent {
418         vout $INPUT keyevent $*
419         $INPUT keyevent $*
420 }
421
422 function checkIsRunning {
423         p=$1
424         shift
425         if ! $PS | grep $p | grep -qv grep; then
426            handleError $*: $p is not running
427            exit 1
428         fi
429 }
430
431 function checkStartTime {
432         vout checkStartTime $1 v $2
433         if [ -z "$2" ]; then
434             echo false
435             return 2
436         fi
437         if [ "$1" -gt "$2" ]; then
438             echo false
439             return 1
440         fi
441         echo true
442         return 0
443 }
444
445 function handleError {
446         echo Error: $*
447         stopAndDumpInstramentation
448         if [ $stoponerror -gt 0 ]; then
449                 exit 1
450         fi
451 }
452
453 user=root
454 if ${ADB}ls /data 2>/dev/null | grep -q "Permission denied"; then
455         user=shell
456 fi
457 vout User is $user
458
459 if [ $generateActivities -gt 0  ]; then
460         if [ $isOnDevice -gt 0 ]; then
461                 echo Error: cannot generate activity list when run on device
462                 exit 1
463         fi
464         echo Generating activities...
465         for app in $appList
466         do
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)"
471         done
472 fi
473