1. 检查内核编译
编译开关
CONFIG_KALLSYMS_ALL=y
CONFIG_PROFILING=y
CONFIG_OPROFILE=y
2. 用户空间配置
(1). 拷贝vmlinux到目标机
adb push out/target/product/Q8145SA/vmlinux /data
(2). 禁止休眠
adb shell "mkdir /data/debug"
adb shell "mount -t debugfs nodev /data/debug"
adb shell "echo 1 > /data/debug/nohlt"
(3). 获取内核运行的开始、结束地址
adb shell "echo 0 > /proc/sys/kernel/kptr_restrict"
adb shell "opcontrol --setup"
adb shell cat /proc/kallsyms | grep "_text" | awk '{print $1}'
列出开始地址是:0xc0008000
adb shell cat /proc/kallsyms | grep "_etext" | awk '{print $1}'
列出结束地址是:0xc0896c18
(4). 配置内核开始、结束地址并指明vmlinux的位置
a. CPU支持NMI中断模式
adb shell "opcontrol --kernel-range=0xc0008000,0xc0896c18 --vmlinux=/data/vmlinux --event=CPU_CYCLES:1000::0:1"
b. timer 模式
adb shell "opcontrol --kernel-range=0xc0008000,0xc0896c18 --vmlinux=/data/vmlinux --timer"
(5). 设置调用层数
adb shell "opcontrol --callgraph=16"
(6). 启动 oprofile
adb shell opcontrol --start
运行一些程序...
(7). 查看状态
adb shell opcontrol --status
(8). 停止采样
adb shell opcontrol --stop
3. 结果分析
(1). 从目标机获取采样文件到PC
sudo ./external/oprofile/opimport_pull -r /tmp/profiling
(2). 报告结果
opreport --session-dir=/tmp/profiling/
opreport --session-dir=/tmp/profiling/ -p $PWD/out/target/product/Q8145SA/symbols -l -g libdvm.so
opreport --session-dir=/tmp/profiling/ -p $PWD/out/target/product/Q8145SA -l -g vmlinux
opannotate --session-dir=/tmp/profiling/ -p $PWD/out/target/product/Q8145SA -s vmlinux
(1). 运行./external/oprofile/opimport_pull可能会出错,需要修改opimport_pull中的一些配置路径。
(2). 有时运行 opcontrol --setup 时会提示“Terminated”, 需要把/data目录下的oprofile/删除再重新开机,或者格式化userdata分区重新开机。
(3). 需要修改内核代码支持timer模式,修改内核里面代码 oprof.c中的 timer=1;
(4). oprofile的代码主要看文件./external/oprofile/opcontrol/opcontrol.cpp