app=$1
round=$2

prev_alloc=$(adb shell cat /sys/kernel/mm/transparent_hugepage/hugepages-64kB/stats/anon_fault_alloc)
prev_alloc_fallback=$(adb shell cat /sys/kernel/mm/transparent_hugepage/hugepages-64kB/stats/anon_fault_fallback)

adb shell "simpleperf stat -e dTLB-loads,dTLB-load-misses,iTLB-loads,iTLB-load-misses,page-faults,raw-stall-backend-mem --app $app --duration 3 -o /data/local/tmp/simpleperf-"$app"-"$round".txt" &

echo "即将抓$app的perfetto, 请1秒后启动应用"

adb shell perfetto -c - --txt -o /data/misc/perfetto-traces/trace-"$app"-"$round".ptrace \
<<EOF

buffers: {
    size_kb: 707200
    fill_policy: DISCARD
}
buffers: {
    size_kb: 707200
    fill_policy: DISCARD
}
data_sources: {
    config {
        name: "linux.process_stats"
        target_buffer: 1
        process_stats_config {
            scan_all_processes_on_start: true
            proc_stats_poll_ms: 1000
        }
    }
}
data_sources: {
    config {
        name: "android.surfaceflinger.frametimeline"
    }
}
data_sources: {
    config {
        name: "linux.sys_stats"
        sys_stats_config {
            meminfo_period_ms: 250
            meminfo_counters: MEMINFO_MEM_FREE
            meminfo_counters: MEMINFO_MEM_AVAILABLE
            cpufreq_period_ms: 250
        }
    }
}
data_sources: {
    config {
        name: "linux.ftrace"
        ftrace_config {
            buffer_size_kb: 81920
            drain_period_ms: 100
            symbolize_ksyms: true
            ftrace_events: "power/cpu_frequency"
            ftrace_events: "sched/sched_switch"
            ftrace_events: "power/suspend_resume"
            ftrace_events: "sched/sched_wakeup"
            ftrace_events: "sched/sched_wakeup_new"
            ftrace_events: "sched/sched_waking"
            ftrace_events: "power/cpu_idle"
            ftrace_events: "vmscan/mm_vmscan_kswapd_wake"
            ftrace_events: "vmscan/mm_vmscan_kswapd_sleep"
            ftrace_events: "vmscan/mm_vmscan_direct_reclaim_begin"
            ftrace_events: "vmscan/mm_vmscan_direct_reclaim_end"
            ftrace_events: "compaction/mm_compaction_begin"
            ftrace_events: "compaction/mm_compaction_end"
            ftrace_events: "mm_filemap_add_to_page_cache"
            ftrace_events: "mm_filemap_delete_from_page_cache"
            ftrace_events: "sched/sched_process_exit"
            ftrace_events: "sched/sched_process_free"
            ftrace_events: "task/task_newtask"
            ftrace_events: "task/task_rename"
            ftrace_events: "lowmemorykiller/lowmemory_kill"
            ftrace_events: "oom/oom_score_adj_update"
            ftrace_events: "ftrace/print"
            ftrace_events: "binder/*"
            ftrace_events: "power/gpu_frequency"
            ftrace_events: "sched/sched_blocked_reason"
            atrace_categories: "input"
            atrace_categories: "gfx"
            atrace_categories: "view"
            atrace_categories: "webview"
            atrace_categories: "camera"
            atrace_categories: "dalvik"
            atrace_categories: "power"
            atrace_categories: "wm"
            atrace_categories: "am"
            atrace_categories: "ss"
            atrace_categories: "sched"
            atrace_categories: "freq"
            atrace_categories: "binder_driver"
            atrace_categories: "aidl"
            atrace_categories: "binder_lock"
            atrace_apps: "*"
        }
    }
}
duration_ms: 10000
write_into_file: true
flush_period_ms: 30000
incremental_state_config {
    clear_period_ms: 5000
}

EOF

# 后面的dumpsys meminfo也会分配大页
# 所以先看分配大页数
alloc=$(adb shell cat /sys/kernel/mm/transparent_hugepage/hugepages-64kB/stats/anon_fault_alloc)
alloc_fallback=$(adb shell cat /sys/kernel/mm/transparent_hugepage/hugepages-64kB/stats/anon_fault_fallback)

adb shell "dumpsys meminfo --package $app" > out/meminfo-"$app"-"$round".txt

# 计算大页分配成功率
inc_alloc=$((alloc-prev_alloc))
inc_alloc_fallback=$((alloc_fallback-prev_alloc_fallback))
inc_total=$((inc_alloc+inc_alloc_fallback))
if [ $inc_total -ne 0 ]; then
	inc_ratio=$(echo "scale=4; $inc_alloc / $inc_total" | bc)
else
	inc_ratio=1
fi

echo -e "$inc_ratio\t$inc_total" > out/ratio-"$app"-"$round".txt

adb pull /data/misc/perfetto-traces/trace-"$app"-"$round".ptrace out/
adb pull /data/local/tmp/simpleperf-"$app"-"$round".txt out/

adb shell "rm /data/misc/perfetto-traces/trace-"$app"-"$round".ptrace"
adb shell "rm /data/local/tmp/simpleperf-"$app"-"$round".txt"

