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