可以使用 OProfile 来分析处理器周期、TLB 失误、内存引用、分支预测失误、缓存失误、中断处理程序,等等。使用opcontrol 的 --list-events 选项来提供完整的 特定处理器上可监视事件列表。
1 监视处理器周期
#include <stdio.h>
int fast_multiply(x, y)
{
return x * y;
}
int slow_multiply(x, y)
{
int i, j, z;
for (i = 0, z = 0; i < x; i++)
z= z + y;
returnz;
}
int main()
{
int i,j;
int x,y;
for (i = 0; i < 200; i ++) {
for (j = 0; j < 30 ; j++) {
x= fast_multiply(i, j);
y= slow_multiply(i, j);
}
}
return 0;
}
编译加入调试信息:
#gcc -g test.c -o a.out
-g 标志意味着要添加调试信息。
然后使用 CYCLES 事件计算处理器周期,以分析结果。
清除当前回话信息
#opcontrol --reset
设置事件(查看支持的事件#opcontrol --list-evnets)
#opcontrol --setup --event=CPU_CLK_UNHALTED:6000
启动oprofile数据收集
#opcontrol --start
运行测试程序
#./a.out
耍一下profiling数据
#opcontrol --dump
停止数据收集
#opcontrol --stop
关闭后台服务
#opcontrol -h
然后使用opannotate 工具和 --source 选项生成源代码,或者和 --assembly 选项一起生成汇编代码。具体使用这两个选项中的哪一个选项,或者是否同时使用这两个选项,则取决于想要分析的详细程度。
1.1 分析结果
#opannotate --source ./a.out
理是这个理。但是每款CPU支持的事件是不太一样的。
2 缓存命中例子
2.1 代码
#include<stdlib.h>
#include <sched.h>
struct shared_data_struct {
unsigned int data1;
unsigned int data2;
};
struct shared_data_structshared_data;
static int inc_second(structshared_data_struct *);
int main(){
int i, j, pid;
void *child_stack;
/* allocate memory for otherprocess to execute in */
if((child_stack = (void *)malloc(4096)) == NULL) {
perror("Cannot allocate stackfor child");
exit(1);
}
/* clone process and run in thesame memory space */
if ((pid = clone((void *)&inc_second,child_stack,
CLONE_VM, &shared_data)) <0) {
perror("clone calledfailed.");
exit(1);
}
/* increment first member of sharedstruct */
for (j = 0; j < 2000; j++) {
for (i = 0; i < 100000; i++) {
shared_data.data1++;
}
}
return 0;
}
int inc_second(structshared_data_struct *sd)
{
int i,j;
/* increment second member ofshared struct */
for (j = 1; j < 2000; j++) {
for (i = 1; i < 100000; i++) {
sd->data2++;
}
编译
#gcc --o cache-miss cache-miss.c
#opcontrol --reset
设置事件(查看支持的事件 #opcontrol--list-evnets)
#opcontrol --setup --event=LLC_MISSES:6000
启动oprofile数据收集
#opcontrol --start
运行测试程序
#./a.out
耍一下profiling数据
#opcontrol --dump
停止数据收集
#opcontrol --stop
关闭后台服务
#opcontrol -h