OSDN Git Service

Merge "kernel.config: add CONFIG_NFS* testing"
[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 clockActivity='com.google.android.deskclock/com.android.deskclock.DeskClock'
14 hangoutsActivity='com.google.android.talk/com.google.android.talk.SigningInActivity'
15 chromeActivity='com.android.chrome/_not_used'
16 contactsActivity='com.google.android.contacts/com.android.contacts.activities.PeopleActivity'
17 youtubeActivity='com.google.android.youtube/com.google.android.apps.youtube.app.WatchWhileActivity'
18 cameraActivity='com.google.android.GoogleCamera/com.android.camera.CameraActivity'
19 playActivity='com.android.vending/com.google.android.finsky.activities.MainActivity'
20 feedlyActivity='com.devhd.feedly/com.devhd.feedly.Main'
21 photosActivity='com.google.android.apps.photos/com.google.android.apps.photos.home.HomeActivity'
22 mapsActivity='com.google.android.apps.maps/com.google.android.maps.MapsActivity'
23 calendarActivity='com.google.android.calendar/com.android.calendar.AllInOneActivity'
24 earthActivity='com.google.earth/com.google.earth.EarthActivity'
25 calculatorActivity='com.google.android.calculator/com.android.calculator2.Calculator'
26 calculatorLActivity='com.android.calculator2/com.android.calculator2.Calculator'
27 sheetsActivity='com.google.android.apps.docs.editors.sheets/com.google.android.apps.docs.app.NewMainProxyActivity'
28 docsActivity='com.google.android.apps.docs.editors.docs/com.google.android.apps.docs.app.NewMainProxyActivity'
29 operaActivity='com.opera.mini.native/com.opera.mini.android.Browser'
30 firefoxActivity='org.mozilla.firefox/org.mozilla.firefox.App'
31 suntempleActivity='com.BrueComputing.SunTemple/com.epicgames.ue4.GameActivity'
32 homeActivity='com.google.android.googlequicksearchbox/com.google.android.launcher.GEL'
33
34 function showUsage {
35         echo "$0: unrecognized option: $1"
36         echo; echo "Usage: $0 [options]"
37         echo "-e : stop on error"
38         echo "-i iterations"
39         echo "-n : keep trace files"
40         echo "-o output file"
41         echo "-s device : adb device"
42         echo "-t trace categories"
43         echo "-g : generate activity strings"
44 }
45
46 DEVICE=unknown
47
48 # handle args
49 while [ $# -gt 0 ]
50 do
51         case "$1" in
52         (-d) DEVICE=$2; shift;;
53         (-e) stoponerror=1;;
54         (-n) savetmpfiles=1;;
55         (-t) tracecategories=$2; shift;;
56         (-i) iterations=$2; shift;;
57         (-o) output=$2; shift;;
58         (-v) verbose=1;;
59         (-nz) compress=0;;
60         (-s) deviceName=$2; shift;;
61         (-g) generateActivities=1;;
62         (--) ;;
63         (*)
64                 chk1=$(functions 2>/dev/null)
65                 chk2=$(typeset -F 2>/dev/null)
66
67                 if echo $chk1 $chk2 | grep -q processLocalOption; then
68                         if ! processLocalOption "$1" "$2"; then
69                                 shift
70                         fi
71                 else
72                         showUsage $1
73                         exit 1
74                 fi;;
75         esac
76         shift
77 done
78
79 # check if running on a device
80 if ls /etc/* 2>/dev/null | grep -q android.hardware; then
81         ADB=""
82         compress=0
83         isOnDevice=1
84 else
85         # do a throw-away adb in case the server is out-of-date
86         adb devices -l 2>&1 >/dev/null
87
88         if [ -z "$deviceName" ]; then
89                 devInfo=$(adb devices -l | grep -v ^List | head -1)
90         else
91                 devInfo=$(adb devices -l | grep $deviceName)
92         fi
93         set -- $devInfo
94         if [ -z $1 ]; then
95                 echo Error: could not find device $deviceName
96                 exit 1
97         fi
98         deviceName=$1
99         ADB="adb -s $deviceName shell "
100         if [ "$DEVICE" = "" -o "$DEVICE" = unknown ]; then
101                 DEVICE=$(echo $4 | sed 's/product://')
102         fi
103         isOnDevice=0
104 fi
105
106 if [ $isOnDevice -gt 0 ]; then
107         case "$DEVICE" in
108         (bullhead|angler)
109                 if ! echo $$ > /dev/cpuset/background/tasks; then
110                         echo Could not put PID $$ in background
111                 fi
112                 ;;
113         (*)
114                 ;;
115         esac
116 fi
117
118 # default values if not set by options or calling script
119 appList=${appList:=$dfltAppList}
120 savetmpfiles=${savetmpfiles:=0}
121 stoponerror=${stoponerror:=0}
122 verbose=${verbose:=0}
123 compress=${compress:=1}
124 iterations=${iterations:=5}
125 tracecategories=${tracecategories:=$dflttracecategories}
126 ADB=${ADB:=""}
127 output=${output:="./out"}
128
129 # clear the output file
130 if [ -f $output ]; then
131         > $output
132 fi
133
134 # ADB commands
135 AM_FORCE_START="${ADB}am start -W -S"
136 AM_START="${ADB}am start -W"
137 AM_START_NOWAIT="${ADB}am start"
138 AM_STOP="${ADB}am force-stop"
139 AM_LIST="${ADB}am stack list"
140 WHO="${ADB}whoami"
141 INPUT="${ADB}input"
142 PS="${ADB}ps"
143
144 function vout {
145         # debug output enabled by -v
146         if [ $verbose -gt 0 ]; then
147             echo DEBUG: $* >&2
148             echo DEBUG: $* >&2 >> $output
149         fi
150 }
151
152 function findtimestamp {
153         # extract timestamp from atrace log entry
154         while [ "$2" != "" -a "$2" != "tracing_mark_write" ]
155         do
156                 shift
157         done
158         echo $1
159 }
160
161 function computeTimeDiff {
162         # Compute time diff given: startSeconds startNs endSeconds endNS
163
164         # strip leading zeros
165         startS=$(expr 0 + $1)
166         endS=$(expr 0 + $3)
167         if [ "$2" = N ]; then
168                 startNs=0
169                 endNs=0
170         else
171                 startNs=$(expr 0 + $2)
172                 endNs=$(expr 0 + $4)
173         fi
174
175         ((startMs=startS*1000 + startNs/1000000))
176         ((endMs=endS*1000 + endNs/1000000))
177         ((diff=endMs-startMs))
178         echo $diff
179 }
180
181 function log2msec {
182         in=$1
183         in=${in:=0.0}
184         set -- $(echo $in | tr . " ")
185
186         # shell addition via (( )) doesn't like leading zeroes in msecs
187         # field so remove leading zeroes
188         msecfield=$(expr 0 + $2)
189
190         ((msec=$1*1000000+msecfield))
191         ((msec=msec/1000))
192         echo $msec
193 }
194
195 function getStartTime {
196         # extract event indicating beginning of start sequence
197         # a) look for a "launching" event indicating start from scratch
198         # b) look for another activity getting a pause event
199         _app=$1
200         traceout=$2
201         ret=0
202         s=$(grep "Binder.*tracing_mark_write.*launching" $traceout 2>/dev/null | head -1| tr [\(\)\[\]\r:] " ")
203         if [ -z "$s" ]; then
204                 s=$(grep activityPause $traceout | head -1 2>/dev/null| tr [\(\)\[\]\r:] " ")
205         else
206                 vout $_app was restarted!
207                 ret=1
208         fi
209         vout STARTLOG: $s
210         log2msec $(findtimestamp $s)
211         return $ret
212 }
213
214 function getEndTime {
215         # extract event indicating end of start sequence. We use the
216         # first surfaceflinger event associated with the target activity
217         _app=$1
218         traceout=$2
219         f=$(grep "surfaceflinger.*tracing_mark_write.*$_app" $traceout 2>/dev/null |
220                 grep -v Starting | head -1 | tr [\(\)\[\]\r:] " ")
221         if [ -z "$f" ]; then
222                 # Hmm. sf symbols may not be there... get the pid
223                 pid=$(${ADB}pidof /system/bin/surfaceflinger | tr "[\r]" "[ ]")
224                 f=$(grep "           <...>-$pid.*tracing_mark_write.*$_app" $traceout 2>/dev/null |
225                         grep -v Starting | head -1 | tr [\(\)\[\]\r:] " ")
226         fi
227         vout ENDLOG: $f
228         log2msec $(findtimestamp $f)
229 }
230
231 function resetJankyFrames {
232         _gfxapp=$1
233         _gfxapp=${_gfxapp:="com.android.systemui"}
234         ${ADB}dumpsys gfxinfo $_gfxapp reset 2>&1 >/dev/null
235 }
236
237 function getJankyFrames {
238         _gfxapp=$1
239         _gfxapp=${_gfxapp:="com.android.systemui"}
240
241         # Note: no awk or sed on devices so have to do this
242         # purely with bash
243         total=0
244         janky=0
245         latency=0
246         ${ADB}dumpsys gfxinfo $_gfxapp | tr "\r" " " | egrep "9[059]th| frames" | while read line
247         do
248                 if echo $line | grep -q "Total frames"; then
249                         set -- $line
250                         total=$4
251                 elif echo $line | grep -q "Janky frames"; then
252                         set -- $line
253                         janky=$3
254                 elif echo $line | grep -q "90th"; then
255                         set -- $(echo $line | tr m " ")
256                         l90=$3
257                 elif echo $line | grep -q "95th"; then
258                         set -- $(echo $line | tr m " ")
259                         l95=$3
260                 elif echo $line | grep -q "99th"; then
261                         set -- $(echo $line | tr m " ")
262                         l99=$3
263                         echo $total $janky $l90 $l95 $l99
264                         break
265                 fi
266         done
267 }
268
269 function checkForDirectReclaim {
270         # look for any reclaim events in atrace output
271         _app=$1
272         traceout=$2
273         if grep -qi reclaim $traceout; then
274            return 1
275         fi
276         return 0
277 }
278
279 function startInstramentation {
280         _iter=$1
281         _iter=${_iter:=0}
282         enableAtrace=$2
283         enableAtrace=${enableAtrace:=1}
284         # Called at beginning of loop. Turn on instramentation like atrace
285         vout start instramentation $(date)
286         echo =============================== >> $output
287         echo Before iteration $_iter >> $output
288         echo =============================== >> $output
289         ${ADB}cat /proc/meminfo 2>&1 >> $output
290         ${ADB}dumpsys meminfo 2>&1 >> $output
291         if [ "$DEVICE" = volantis ]; then
292                 ${ADB}cat /d/nvmap/iovmm/procrank 2>&1 >> $output
293         fi
294         if [ "$user" = root -a $enableAtrace -gt 0 ]; then
295                 vout ${ADB}atrace -b 32768 --async_start $tracecategories
296                 ${ADB}atrace -b 32768 --async_start $tracecategories >> $output
297                 echo >> $output
298         fi
299 }
300
301 function stopInstramentation {
302         enableAtrace=$1
303         enableAtrace=${enableAtrace:=1}
304         if [ "$user" = root -a $enableAtrace -gt 0 ]; then
305                 vout ${ADB}atrace --async_stop
306                 ${ADB}atrace --async_stop > /dev/null
307         fi
308 }
309
310 function stopAndDumpInstramentation {
311         vout stop instramentation $(date)
312         echo =============================== >> $output
313         echo After iteration >> $output
314         echo =============================== >> $output
315         ${ADB}cat /proc/meminfo 2>&1 >> $output
316         ${ADB}dumpsys meminfo 2>&1 >> $output
317         if [ "$user" = root ]; then
318                 traceout=$1
319                 traceout=${traceout:=$output}
320                 echo =============================== >> $traceout
321                 echo TRACE >> $traceout
322                 echo =============================== >> $traceout
323                 if [ $compress -gt 0 ]; then
324                         tmpTrace=./tmptrace.$$
325                         UNCOMPRESS=$CMDDIR/atrace-uncompress.py
326                         > $tmpTrace
327                         zarg="-z"
328                         ${ADB}atrace -z -b 32768 --async_dump >> $tmpTrace
329                         python $UNCOMPRESS $tmpTrace >> $traceout
330                         rm -f $tmpTrace
331                 else
332                         ${ADB}atrace -b 32768 --async_dump > $traceout
333                 fi
334                 vout ${ADB}atrace $zarg -b 32768 --async_dump
335                 vout ${ADB}atrace --async_stop
336                 ${ADB}atrace --async_stop > /dev/null
337         fi
338 }
339
340 function getActivityName {
341         cmd="actName=\$${1}Activity"
342         eval $cmd
343         echo $actName
344 }
345
346 function getPackageName {
347         set -- $(getActivityName $1 | tr "[/]" "[ ]")
348         echo $1
349 }
350
351 function startActivityFromPackage {
352         if [ "$1" = home ]; then
353                 doKeyevent HOME
354                 echo 0
355                 return 0
356         fi
357         vout $AM_START_NOWAIT -p "$(getPackageName $1)" -c android.intent.category.LAUNCHER -a android.intent.action.MAIN
358         $AM_START_NOWAIT -p "$(getPackageName $1)" -c android.intent.category.LAUNCHER -a android.intent.action.MAIN 2>&1
359         echo 0
360 }
361
362 function startActivity {
363         if [ "$1" = home ]; then
364                 doKeyevent HOME
365                 echo 0
366                 return 0
367         elif [ "$1" = chrome ]; then
368                 if [ "$DEVICE" = volantis -o "$DEVICE" = ariel ]; then
369                         vout $AM_START_NOWAIT -p "$(getPackageName $1)" http://www.theverge.com
370                         $AM_START_NOWAIT -p "$(getPackageName $1)" http://www.theverge.com > /dev/null
371                         set -- 0 0
372                 else
373                         vout $AM_START -p "$(getPackageName $1)" http://www.theverge.com
374                         set -- $($AM_START -p "$(getPackageName $1)" http://www.theverge.com | grep ThisTime)
375                 fi
376         else
377                 vout $AM_START "$(getActivityName $1)"
378                 set -- $($AM_START "$(getActivityName $1)" | grep ThisTime)
379         fi
380         echo $2 | tr "[\r]" "[\n]"
381 }
382
383 function forceStartActivity {
384         if [ "$1" = chrome ]; then
385                 vout $AM_START -p "$(getPackageName $1)" http://www.theverge.com
386                 set -- $($AM_FORCE_START -p "$(getPackageName $1)" http://www.theverge.com | grep ThisTime)
387         else
388                 vout $AM_FORCE_START "$(getActivityName $1)"
389                 set -- $($AM_FORCE_START "$(getActivityName $1)" | grep ThisTime)
390         fi
391         echo $2 | tr "[\r]" "[\n]"
392 }
393
394 function checkActivity {
395         # requires root
396         actName="$(getActivityName $1)"
397         $AM_LIST | grep $actName
398 }
399
400 #function stopActivity {
401 #    vout $AM_STOP $(getActivityName $1)
402 #    $AM_STOP $(getActivityName $1)
403 #}
404
405 function doSwipe {
406         vout ${ADB}input swipe $*
407         ${ADB}nice input swipe $*
408 }
409
410 function doText {
411         echo $* > ./tmpOutput
412         vout ${ADB}input text \"$*\"
413         ${ADB}input text "$(cat ./tmpOutput)"
414         rm -f ./tmpOutput
415 }
416
417 function doTap {
418         vout ${ADB}input tap $*
419         ${ADB}input tap $*
420 }
421
422 function doKeyevent {
423         vout $INPUT keyevent $*
424         $INPUT keyevent $*
425 }
426
427 function checkIsRunning {
428         p=$1
429         shift
430         if ! $PS | grep $p | grep -qv grep; then
431            handleError $*: $p is not running
432            exit 1
433         fi
434 }
435
436 function checkStartTime {
437         vout checkStartTime $1 v $2
438         if [ -z "$2" ]; then
439             echo false
440             return 2
441         fi
442         if [ "$1" -gt "$2" ]; then
443             echo false
444             return 1
445         fi
446         echo true
447         return 0
448 }
449
450 function handleError {
451         echo Error: $*
452         stopAndDumpInstramentation
453         if [ $stoponerror -gt 0 ]; then
454                 exit 1
455         fi
456 }
457
458 user=root
459 if ${ADB}ls /data 2>/dev/null | grep -q "Permission denied"; then
460         user=shell
461 fi
462 vout User is $user
463
464 if [ $generateActivities -gt 0  ]; then
465         if [ $isOnDevice -gt 0 ]; then
466                 echo Error: cannot generate activity list when run on device
467                 exit 1
468         fi
469         echo Generating activities...
470         for app in $appList
471         do
472                 startActivityFromPackage $app 2>&1 > /dev/null
473                 act=$(${ADB}am stack list | grep $(getPackageName $app) | sed -e 's/\r//' | head -1 | awk '{ print $2; }')
474                 eval "${app}Activity=$act"
475                 echo "ACTIVITY: $app --> $(getActivityName $app)"
476         done
477 fi
478