一、简介
PAPI(Performance Application Programming Interface)是一个跨平台的性能计数接口工具集。它提供了一套标准化的API,用于访问硬件性能监测计数器(PMU),在软件中实现高精度的性能分析与优化。
参考:PAPI性能测试工具的安装、使用及实例 - Satchmo丶 - 博客园 (cnblogs.com)
二、下载安装
下载地址:PAPI (utk.edu)
安装教程:~/papi-7.0.1/src/INSTALL.txt
# ./configure --help 可以提示修改默认安装路径
./configure
make
make install
# 需要将安装库路径添加到环境变量中
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
ldconfig
三、示例
PAPI示例程序在
papi-7.0.1/src/examples
目录中,入门参考PAPI_add_remove_event.c
。
以下示例是为了检测每次循环执行过程的事件信息。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include "papi.h"
#define NUM_EVENTS 10
#define LOOPS 10
#define SLEEP_TIME 1
#define FUNC_BEGIN() { printf("%s begin.\n", __FUNCTION__); }
#define FUNC_END() { printf("%s end.\n", __FUNCTION__); }
#define ERROR_RETURN(retval) \
{ \
fprintf(stderr, "Error:%d, %s:%d\n", retval, __FILE__, __LINE__); \
exit(retval); \
}
int event_set = PAPI_NULL;
int event_num = 0;
long long values[LOOPS][NUM_EVENTS] = {0};
int PAPI_init()
{
int retval = 0;
if ((retval = PAPI_library_init(PAPI_VER_CURRENT)) != PAPI_VER_CURRENT)
ERROR_RETURN(retval);
if ((retval = PAPI_create_eventset(&event_set)) != PAPI_OK)
ERROR_RETURN(retval);
/* Add Total Instructions Executed to the event_set */
if ((retval = PAPI_add_event(event_set, PAPI_TOT_INS)) != PAPI_OK)
ERROR_RETURN(retval);
/* Add Total Cycles event to the event_set */
if ((retval = PAPI_add_event(event_set, PAPI_TOT_CYC)) != PAPI_OK)
ERROR_RETURN(retval);
/* get the number of events in the event set */
if ((retval = PAPI_list_events(event_set, NULL, &event_num)) != PAPI_OK)
ERROR_RETURN(retval);
/* Start counting */
if ((retval = PAPI_start(event_set)) != PAPI_OK)
ERROR_RETURN(retval);
return 0;
}
void display()
{
int i, j;
int retval = 0;
int event[NUM_EVENTS] = {0};
char event_name[PAPI_MAX_STR_LEN];
printf("There are %d events in the event set\n", event_num);
if ((retval = PAPI_list_events(event_set, event, &event_num)) != PAPI_OK)
ERROR_RETURN(retval);
for (i = 0; i < event_num; i++) {
PAPI_event_code_to_name(event[i], event_name);
printf("%-15s\t", event_name);
}
printf("\n");
for (i = 0; i < LOOPS; i++) {
for (j = 0; j < event_num; j++)
printf("%-15lld\t", values[i][j]);
printf("\n");
}
}
int PAPI_uninit()
{
int retval = 0;
if ((retval = PAPI_stop(event_set, NULL)) != PAPI_OK)
ERROR_RETURN(retval);
display();
/* free the resources used by PAPI */
PAPI_shutdown();
exit(0);
}
void step1(unsigned second)
{
FUNC_BEGIN();
sleep(second);
FUNC_END();
}
void step2(unsigned second)
{
FUNC_BEGIN();
sleep(second);
FUNC_END();
}
void loop()
{
int i = 0;
int retval = 0;
while (i < LOOPS) {
printf("=======loop:%d=======\n", i);
step1(SLEEP_TIME);
step2(SLEEP_TIME);
if ((retval = PAPI_read(event_set, values[i++])) != PAPI_OK)
ERROR_RETURN(retval);
}
}
int main()
{
PAPI_init();
loop();
PAPI_uninit();
return 0;
}
输出结果:
[root@192 test]# gcc -g probe.c -lpapi
[root@192 test]# ./a.out
=======loop:0=======
step1 begin.
step1 end.
step2 begin.
step2 end.
=======loop:1=======
step1 begin.
step1 end.
step2 begin.
step2 end.
=======loop:2=======
step1 begin.
step1 end.
step2 begin.
step2 end.
=======loop:3=======
step1 begin.
step1 end.
step2 begin.
step2 end.
=======loop:4=======
step1 begin.
step1 end.
step2 begin.
step2 end.
=======loop:5=======
step1 begin.
step1 end.
step2 begin.
step2 end.
=======loop:6=======
step1 begin.
step1 end.
step2 begin.
step2 end.
=======loop:7=======
step1 begin.
step1 end.
step2 begin.
step2 end.
=======loop:8=======
step1 begin.
step1 end.
step2 begin.
step2 end.
=======loop:9=======
step1 begin.
step1 end.
step2 begin.
step2 end.
There are 2 events in the event set
PAPI_TOT_INS PAPI_TOT_CYC
6615 53534
10675 91137
14735 125911
18795 162284
22855 200171
26915 238497
30975 275839
35035 312412
39095 351813
43155 387570
Note:使用PAPI_read()
会累加计数,使用PAPI_accum()
则会清空上一次的计数,并重新统计。