Oprofile分析(android oprofile性能分析)
一、内核支持:
make menuconfig
1、评测菜单中启用 Oprofile ,在 .config 文件中设置?CONFIG_PROFILING=y?和?CONFIG_OPROFILE=y
2、Kernel Feature->[]Enable hardware performance counter support for perf events不要勾选
3、在boot options->console=ttyFIQ0 androidboot.console=ttyFIQ0 init=/init profile=1
二、修改源码 将kernel/arch/arm/oprofile中的common.文件修改
/**
* @file common.c
*
* @remark Copyright 2004 Oprofile Authors
* @remark Copyright 2010 ARM Ltd.
* @remark Read the file COPYING
*
* @author Zwane Mwaikambo
* @author Will Deacon [move to perf]
*/ #include <linux/cpumask.h>
#include <linux/err.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/mutex.h>
#include <linux/oprofile.h>
#include <linux/perf_event.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <asm/stacktrace.h>
#include <linux/uaccess.h> #include <asm/perf_event.h>
#include <asm/ptrace.h> #ifdef CONFIG_HW_PERF_EVENTS
/*
* Per performance monitor configuration as set via oprofilefs.
*/
struct op_counter_config {
unsigned long count;
unsigned long enabled;
unsigned long event;
unsigned long unit_mask;
unsigned long kernel;
unsigned long user;
struct perf_event_attr attr;
}; static int op_arm_enabled;
static DEFINE_MUTEX(op_arm_mutex); static struct op_counter_config *counter_config;
static struct perf_event **perf_events[nr_cpumask_bits];
static int perf_num_counters_bak; /*
* Overflow callback for oprofile.
*/
static void op_overflow_handler(struct perf_event *event, int unused,
struct perf_sample_data *data, struct pt_regs *regs)
{
int id;
u32 cpu = smp_processor_id(); for (id = ; id < perf_num_counters_bak; ++id)
if (perf_events[cpu][id] == event)
break; if (id != perf_num_counters_bak)
oprofile_add_sample(regs, id);
else
pr_warning("oprofile: ignoring spurious overflow "
on cpu %u\n, cpu);
} /*
* Called by op_arm_setup to create perf attributes to mirror the oprofile
* settings in counter_config. Attributes are created as `pinned' events and
* so are permanently scheduled on the PMU.
*/
static void op_perf_setup(void)
{
int i;
u32 size = sizeof(struct perf_event_attr);
struct perf_event_attr *attr; for (i = ; i < perf_num_counters_bak; ++i) {
attr = &counter_config[i].attr;
memset(attr, , size);
attr->type = PERF_TYPE_RAW;
attr->size = size;
attr->config = counter_config[i].event;
attr->sample_period = counter_config[i].count;
attr->pinned = ;
}
} static int op_create_counter(int cpu, int event)
{
int ret = ;
struct perf_event *pevent; if (!counter_config[event].enabled || (perf_events[cpu][event] != NULL))
return ret; pevent = perf_event_create_kernel_counter(&counter_config[event].attr,
cpu, NULL,
op_overflow_handler); if (IS_ERR(pevent)) {
ret = PTR_ERR(pevent);
} else if (pevent->state != PERF_EVENT_STATE_ACTIVE) {
pr_warning("oprofile: failed to enable event %d "
on CPU %d\n, event, cpu);
ret = -EBUSY;
} else {
perf_events[cpu][event] = pevent;
} return ret;
} static void op_destroy_counter(int cpu, int event)
{
struct perf_event *pevent = perf_events[cpu][event]; if (pevent) {
perf_event_release_kernel(pevent);
perf_events[cpu][event] = NULL;
}
} /*
* Called by op_arm_start to create active perf events based on the
* perviously configured attributes.
*/
static int op_perf_start(void)
{
int cpu, event, ret = ; for_each_online_cpu(cpu) {
for (event = ; event < perf_num_counters_bak; ++event) {
ret = op_create_counter(cpu, event);
if (ret)
goto out;
}
} out:
return ret;
} /*
* Called by op_arm_stop at the end of a profiling run.
*/
static void op_perf_stop(void)
{
int cpu, event; for_each_online_cpu(cpu)
for (event = ; event < perf_num_counters_bak; ++event)
op_destroy_counter(cpu, event);
} char *op_name_from_perf_id(void)
{
enum arm_perf_pmu_ids id = armpmu_get_pmu_id(); switch (id) {
case ARM_PERF_PMU_ID_XSCALE1:
return "arm/xscale1";
case ARM_PERF_PMU_ID_XSCALE2:
return "arm/xscale2";
case ARM_PERF_PMU_ID_V6:
return "arm/armv6";
case ARM_PERF_PMU_ID_V6MP:
return "arm/mpcore";
case ARM_PERF_PMU_ID_CA8:
return "arm/armv7";
case ARM_PERF_PMU_ID_CA9:
return "arm/armv7-ca9";
default:
return NULL;
}
} static int op_arm_create_files(struct super_block *sb, struct dentry *root)
{
unsigned int i; for (i = ; i < perf_num_counters_bak; i++) {
struct dentry *dir;
char buf[]; snprintf(buf, sizeof buf, "%d", i);
dir = oprofilefs_mkdir(sb, root, buf);
oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
} return ;
} static int op_arm_setup(void)
{
spin_lock(&oprofilefs_lock);
op_perf_setup();
spin_unlock(&oprofilefs_lock);
return ;
} static int op_arm_start(void)
{
int ret = -EBUSY; mutex_lock(&op_arm_mutex);
if (!op_arm_enabled) {
ret = ;
op_perf_start();
op_arm_enabled = ;
}
mutex_unlock(&op_arm_mutex);
return ret;
} static void op_arm_stop(void)
{
mutex_lock(&op_arm_mutex);
if (op_arm_enabled)
op_perf_stop();
op_arm_enabled = ;
mutex_unlock(&op_arm_mutex);
} #ifdef CONFIG_PM
static int op_arm_suspend(struct platform_device *dev, pm_message_t state)
{
mutex_lock(&op_arm_mutex);
if (op_arm_enabled)
op_perf_stop();
mutex_unlock(&op_arm_mutex);
return ;
} static int op_arm_resume(struct platform_device *dev)
{
mutex_lock(&op_arm_mutex);
if (op_arm_enabled && op_perf_start())
op_arm_enabled = ;
mutex_unlock(&op_arm_mutex);
return ;
} static struct platform_driver oprofile_driver = {
.driver = {
.name = "arm-oprofile",
},
.resume = op_arm_resume,
.suspend = op_arm_suspend,
}; static struct platform_device *oprofile_pdev; static int __init init_driverfs(void)
{
int ret; ret = platform_driver_register(&oprofile_driver);
if (ret)
goto out; oprofile_pdev = platform_device_register_simple(
oprofile_driver.driver.name, , NULL, );
if (IS_ERR(oprofile_pdev)) {
ret = PTR_ERR(oprofile_pdev);
platform_driver_unregister(&oprofile_driver);
} out:
return ret;
} static void exit_driverfs(void)
{
platform_device_unregister(oprofile_pdev);
platform_driver_unregister(&oprofile_driver);
}
#else
static int __init init_driverfs(void) { return ; }
#define exit_driverfs() do { } while (0)
#endif /* CONFIG_PM */ static int report_trace(struct stackframe *frame, void *d)
{
unsigned int *depth = d; if (*depth) {
oprofile_add_trace(frame->pc);
(*depth)--;
} return *depth == ;
} /*
* The registers we're interested in are at the end of the variable
* length saved register structure. The fp points at the end of this
* structure so the address of this struct is:
* (struct frame_tail *)(xxx->fp)-1
*/
struct frame_tail {
struct frame_tail *fp;
unsigned long sp;
unsigned long lr;
} __attribute__((packed)); static struct frame_tail* user_backtrace(struct frame_tail *tail)
{
struct frame_tail buftail[]; /* Also check accessibility of one struct frame_tail beyond */
if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
return NULL;
if (__copy_from_user_inatomic(buftail, tail, sizeof(buftail)))
return NULL; oprofile_add_trace(buftail[].lr); /* frame pointers should strictly progress back up the stack
* (towards higher addresses) */
if (tail + >= buftail[].fp)
return NULL; return buftail[].fp-;
} static void arm_backtrace(struct pt_regs * const regs, unsigned int depth)
{
struct frame_tail *tail = ((struct frame_tail *) regs->ARM_fp) - ; if (!user_mode(regs)) {
struct stackframe frame;
frame.fp = regs->ARM_fp;
frame.sp = regs->ARM_sp;
frame.lr = regs->ARM_lr;
frame.pc = regs->ARM_pc;
walk_stackframe(&frame, report_trace, &depth);
return;
} while (depth-- && tail && !((unsigned long) tail & ))
tail = user_backtrace(tail);
} int __init oprofile_arch_init(struct oprofile_operations *ops)
{
int cpu, ret = ; perf_num_counters_bak = armpmu_get_max_events(); counter_config = kcalloc(perf_num_counters_bak,
sizeof(struct op_counter_config), GFP_KERNEL); if (!counter_config) {
pr_info("oprofile: failed to allocate %d "
counters\n, perf_num_counters_bak);
return -ENOMEM;
} ret = init_driverfs();
if (ret) {
kfree(counter_config);
return ret;
} for_each_possible_cpu(cpu) {
perf_events[cpu] = kcalloc(perf_num_counters_bak,
sizeof(struct perf_event *), GFP_KERNEL);
if (!perf_events[cpu]) {
pr_info("oprofile: failed to allocate %d perf events "
for cpu %d\n, perf_num_counters_bak, cpu);
while (--cpu >= )
kfree(perf_events[cpu]);
return -ENOMEM;
}
} ops->backtrace = arm_backtrace;
ops->create_files = op_arm_create_files;
ops->setup = op_arm_setup;
ops->start = op_arm_start;
ops->stop = op_arm_stop;
ops->shutdown = op_arm_stop;
ops->cpu_type = op_name_from_perf_id(); if (!ops->cpu_type)
ret = -ENODEV;
else
pr_info("oprofile: using %s\n", ops->cpu_type); return ret;
} void oprofile_arch_exit(void)
{
int cpu, id;
struct perf_event *event; if (*perf_events) {
exit_driverfs();
for_each_possible_cpu(cpu) {
for (id = ; id < perf_num_counters_bak; ++id) {
event = perf_events[cpu][id];
if (event != NULL)
perf_event_release_kernel(event);
}
kfree(perf_events[cpu]);
}
} if (counter_config)
kfree(counter_config);
} #else
int __init oprofile_arch_init(struct oprofile_operations *ops)
{
pr_info("oprofile: hardware counters not available\n");
return -ENODEV;
}
void oprofile_arch_exit(void) {}
#endif /* CONFIG_HW_PERF_EVENTS */
三、相关
kernel/sched.c--------profile_hit(SCHED_PROFILING, __builtin_return_address(0));
下面是对profile的解释:Linux -- profile内核版本2.6.18-RC7
profile只是内核的一个调试性能的工具,这个可以通过menuconfig中的Instrumentation Support->profile打开。
1. 如何使用profile:
首先确认内核支持profile,然后在内核启动时加入以下参数:profile=1或者其它参数, 新的内核支持profile=schedule 1
2. 内核启动后会创建/proc/profile文件,这个文件可以通过readprofile读取,
如readprofile -m /proc/kallsyms | sort -nr > ~/cur_profile.log,
或者readprofile -r -m /proc/kallsyms |sort -nr,
或者readprofile -r && sleep 1 && readprofile -m /proc/kallsyms |sort -nr >~/cur_profile.log
3. 读取/proc/profile可获得哪些内容?
根据启动配置profile=?的不同,获取的内容不同:
如果配置成profile=? 可以获得每个函数执行次数,用来调试函数性能很有用
如果设置成profile=schedule ?可以获得每个函数调用schedule的次数,用来调试schedule很有用
四、总结一下:
opcontrol –init 加载模块, mout /dev/oprofile 创建必需的文件和目录
opcontrol --no-vmlinux 或者 opcontrol --vmlinux=/boot/vmlinux-`uname -r` 决定是否对 kernel 进行 profiling
opcontrol --list-events //命令可以查看此结构中支持的事件
opcontrol --event=CPU_CLK_UNHALTED:5000 --event=DATA_CACHE_MISSES:1000 --event=INSTRUCTION_CACHE_MISSES:1000 --event=MEMORY_REQUESTS:1000
opcontrol --reset 清楚当前会话中的数据
opcontrol --start 开始 profiling
./wls 运行应用程序, oprofile 会对它进行 profiling 通过opcontrol --event=L2_CACHE_MISS:500 --event=L1_DTLB_MISS_AND_L2_DTLB_HIT:500
opcontrol --dump 把收集到的数据写入文件 --event=.........命令来进行事件的设置;
opcontrol --stop 停止 profiling 此命令--event参数必须依次给出,无论有多少个,不可分行,切记
opcotrol -h 关闭守护进程 oprofiled
opcontrol --shutdown 停止 oprofiled
opcontrol --deinit 卸载模块
常用的是 3→7 这几个过程,得到性能数据之后,可以使用 opreport, opstack, opgprof, opannotate几个工具进行分析,我常用的是
opreport, opannotate 进行分析。
五、示例与常见问题解诀
Command format :$:tager board, #:Host
1.Extract opf.tar.gz and busybox.tar.gz ($: tar -zxvf ***.tar.gz)
2.Use adb push opf to /data/opf and busybox to /data/busybox
(#: adb push opf /data/opf and #: adb push busybox /data/busybox)
3.Run $:export PATH=$PATH:/data/opf
$: is command to begin
4.Run $:opcontrol --init
Remark: if target board have oprofile,please delete the files(path:/system/xbin)
Question:
grep: /etc/mtab: No such file or directory
grep: /etc/mtab: No such file or directory
Kernel support not available, missing opcontrol --init as root
Answer:
a.$:mount -o remount rw /
b.$:mount -o rw,remount -t yaffs2 /dev/block/mmcblk0p9 /system
c.$:touch /system/etc/mtab or touch /etc/mtab
d.$:echo nodev /dev/oprofile oprofilefs rw 0 0 > /system/etc/mtab or /etc/mtab
e.make sure mtab the nodev $:cat /system/etc/mtab
5.$:opcontrol --init
Question:
cat: can't open '/dev/oprofile/cpu_type': No such file or directory
Unable to open cpu_type file for reading
Make sure you have done opcontrol --init
cpu_type 'unset' is not valid
you should upgrade oprofile or force the use of timer mode
Answer:
a.$:mkdir /dev/oprofile (if not exist)
b.mount -t oprofilefs nodev /dev/oprofile
6.$:opcontrol --session-dir=/data/first --no-vmlinux --callgraph=5
7.$:opcontrol --start
Question:
oprofile: could not open unit mask description file /home/henry/workspace/opf/pc/build/oprofile/share/oprofile//arm/armv7-ca9/unit_masks
Using default event: CPU_CYCLES:100000:0:1:1
oprofile: could not open unit mask description file /home/henry/workspace/opf/pc/build/oprofile/share/oprofile//arm/armv7-ca9/unit_masks
Answer:
a.To target board running $:mkdir -p /home/henry/workspace/opf/pc/build/oprofile
b.Extract share.tar.gz
c.Use ADB push share (#:adb push share /home/henry/workspace/opf/pc/build/oprofile/share)
8.Run App to profile
9.$:opcontrol --dump
10.$:opcontrol --stop
11.$:opreport --session-dir=/data/first -l /data/test/test
$:opreport --session-dir=/data/first -l >log
$:opreport --session-dir=/data/first -l /data/data/Service/lib/libXXX.so >logXXX
$:opreport --session-dir=/data/first -l /data/data/Service/lib/libBBB.so >logBBB
12.$:opcontrol --reset
13.opcontrol --shutdown
Remark: $:opcontrol --shutdown and $:opcontrol --init only execution once
Oprofile分析(android oprofile性能分析)的更多相关文章
- Android APP性能分析方法及工具
近期读到<Speed up your app>一文.这是一篇关于Android APP性能分析.优化的文章.在这篇文章中,作者介绍他的APP分析优化规则.使用的工具和方法.我觉得值得大家借 ...
- Android SQLite性能分析
作为Android预置的数据库模块,对SQLite的深入理解是很有必要的,能够从中找到一些优化的方向. 这里对SQLite的性能和内存进行了一些測试分析.对照了不同操作的运行性能和内存占用的情况,粗略 ...
- 正确使用Android性能分析工具——TraceView
http://blog.jobbole.com/78995/ 首页 最新文章 IT 职场 前端 后端 移动端 数据库 运维 其他技术 - 导航条 - 首页 最新文章 IT 职场 前端 - Ja ...
- Cocos2d-x性能分析-Android版本之Gprof
在 iOS 平台下我们可以用 Xcode 自带的 Profile 工具来测试我们程序的性能,Android 平台使用的 gprof 这里整理了一下具体的cocos2dx 使用gprof进行性能分析的具 ...
- DevTools 实现原理与性能分析实战
一.引言 从 2008 年 Google 释放出第一版的 Chrome 后,整个 Web 开发领域仿佛被注入了一股新鲜血液,渐渐打破了 IE 一家独大的时代.Chrome 和 Firefox 是 W3 ...
- 性能分析神器VisualVM
VisualVM 是一款免费的,集成了多个 JDK 命令行工具的可视化工具,它能为您提供强大的分析能力,对 Java 应用程序做性能分析和调优.这些功能包括生成和分析海量数据.跟踪内存泄漏.监控垃圾回 ...
- Java application 性能分析分享
性能分析的主要方式 监视:监视是一种用来查看应用程序运行时行为的一般方法.通常会有多个视图(View)分别实时地显示 CPU 使用情况.内存使用情况.线程状态以及其他一些有用的信息,以便用户能很快地发 ...
- 老李分享:《Java Performance》笔记1——性能分析基础 1
老李分享:<Java Performance>笔记1——性能分析基础 1.性能分析两种方法: (1).自顶向下: 应用开发人员通过着眼于软件栈顶层的应用,从上往下寻找性能优化的机会. ...
- Android KLog源代码分析
Android KLog源代码分析 Android KLog源代码分析 代码结构 详细分析 BaseLog FileLog JsonLog XmlLog 核心文件KLogjava分析 遇到的问题 一直 ...
- Gson全解析(下)-Gson性能分析
前言 在之前的学习中,我们在Gson全解析(上)Gson使用的基础到分别运用了JsonSerializer和JsonDeserializer进行JSON和java实体类之间的相互转化. 在Gson全解 ...
随机推荐
- 使用vs2017创建项目并添加到git中
参考 https://blog.csdn.net/qq373591361/article/details/71194651 https://blog.csdn.net/boonya/article/d ...
- 【Oracle】数据库热备
1. 创建脚本 注:脚本第三行中的DB_NAME,需要改为自己的数据库名(show parameter name;): oracle用户下新建目录:/home/oracle/DB_NAME/hot_b ...
- 软件开发的MVC构架
MVC:IDE开发环境开发时,无意中使用的软件结构. 转自于wikipedia:http://zh.wikipedia.org/wiki/MVC 软件的层次划分:框架--组件(设计模式)--算法与数据 ...
- CorelDRAW 2019新品发布,行业大咖就差你了
近日,由苏州思杰马克丁软件公司独家代理的CorelDRAW 2019将在苏州开启一场设计上的饕餮盛宴,您报名了么? 不管您是专业的设计师还是热爱设计的狂热粉丝,都将有机会参与到我们的活动中,为了这场盛 ...
- ubuntu18.0安装redis
ubuntu版本:Ubuntu-Server 10.04.1 LTS redis版本:4.0.9 安装 使用apt安装 sudo apt-get update sudo apt-get install ...
- vc++图像保存,重绘
新建mfc应用程序,单文档 增加绘图 分别增加命令响应 添加成员变量UINIT 图形可以运行,如何保存呢?(一个集合类,CPtArt) 用一个类的对象来保存一个图形的三个要素 所以插入一个新的类(通常 ...
- Jenkins 部署 PHP 应用
安装 Jenkins 方式一:docker方式安装 拉取jenkins官方镜像,按照镜像文档启动镜像就可以了 方式二:手动安装 以下所有操作都使用 root 用户进行操作. 在各项目官网,下载 Jav ...
- 小白学习Spark系列三:RDD常用方法总结
上一节简单介绍了Spark的基本原理以及如何调用spark进行打包一个独立应用,那么这节我们来学习下在spark中如何编程,同样先抛出以下几个问题. Spark支持的数据集,如何理解? Spark编程 ...
- Bonjour/Zeroconf with Arduino
转自:http://gkaindl.com/software/arduino-ethernet/bonjour Bonjour/Zeroconf with Arduino DownloadVersio ...
- 训练1-W
有一个长度为n(n<=100)的数列,该数列定义为从2开始的递增有序偶数,现在要求你按照顺序每m个数求出一个平均值,如果最后不足m个,则以实际数量求平均值.编程输出该平均值序列. Input 输 ...