#output: round app start_time start_type rss pss ratio pagefault itlbmiss dtlbmiss cpustalled

echo "round app start_time start_type rss pss ratio pagefault itlbmiss dtlbmiss cpustalled" > result

for round in $(seq 1 2); do
	for app in `cat applist`; do
		echo "开始抓应用 $app"
		./cold_start_app.sh $app $round
		adb shell input keyevent 3

		# 计算启动耗时
		python3 handle_perfetto.py out/trace-"$app"-"$round".ptrace mm > tmp.txt
		
		if [ $(cat tmp.txt | wc -l) -ne 1 ]; then
			start_time=0
			start_type="NONE"
		else
			start_time=$(cat tmp.txt | awk '{print $1}')
			start_type=$(cat tmp.txt | awk '{print $2}')
		fi
		
		# 计算PSS RSS
		if [ $(cat out/meminfo-"$app"-"$round".txt | grep "MEMINFO in pid" -c) -ne 1 ]; then
			cat out/meminfo-"$app"-"$round".txt | grep "TOTAL PSS" | tail -n 1 > tmp.txt
			pss=$(cat tmp.txt | awk '{print $3}')
			rss=$(cat tmp.txt | awk '{print $6}')
		else
			cat out/meminfo-"$app"-"$round".txt | grep "TOTAL PSS" > tmp.txt
			pss=$(cat tmp.txt | awk '{print $3}')
			rss=$(cat tmp.txt | awk '{print $6}')
		fi
		
		pagefaults=$(cat out/simpleperf-"$app"-"$round".txt | grep 'page-faults' | awk '{print $4}')
		itlbmiss=$(cat out/simpleperf-"$app"-"$round".txt | grep 'iTLB-load-misses' | awk '{print $4}')
		dtlbmiss=$(cat out/simpleperf-"$app"-"$round".txt | grep 'dTLB-load-misses' | awk '{print $4}')
		cpustall=$(cat out/simpleperf-"$app"-"$round".txt | grep 'raw-stall-backend-mem' | awk '{print $4}')

		# 计算大页相关
		inc_ratio=$(cat out/ratio-"$app"-"$round".txt | awk '{print $1}')
		inc_total=$(cat out/ratio-"$app"-"$round".txt | awk '{print $2}')
		
		echo $round $app $start_time $start_type $inc_ratio $inc_total $rss $pss $pagefaults $itlbmiss $dtlbmiss $cpustall >> result

		rm tmp.txt

		echo next
		read -p "输入命令   " cmd

		if [ "$cmd" == "c" ]; then
			continue
		else
			break
		fi
	done
done
