OSDN Git Service

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