现在我能够将 PAPI 用于系统指标,但只能在应用程序中使用。结构是——初始化库——创建事件——启动事件(指令总数和/或周期总数)——工作负载(应用程序的代码)——停止事件(返回测量值)。但是我想采用不同的方法,例如-初始化库-创建事件-启动事件(指令总数和/或周期总数)-“睡眠”-停止事件(返回测量值)。所以基本上我想在一个终端中运行一个程序,在另一个终端(睡眠)中运行 PAPI 实现,并能够获得测量结果。
下面显示了第一个结构:
#include <iostream>
#include <papi.h>
void powersOfOne(int);
int main()
{
int retval;
retval = PAPI_library_init(PAPI_VER_CURRENT);
if (retval != PAPI_VER_CURRENT)
{
std::cerr << "PAPI: init. failed: " << PAPI_strerror(retval) << std::endl;
return 1;
}
/* set up event set */
int eventset = PAPI_NULL;
retval = PAPI_create_eventset(&eventset);
if (retval != PAPI_OK)
{
std::cerr << "PAPI: could not create eventset: " << PAPI_strerror(retval) << std::endl;
return 1;
}
//adding total_cycles and total_instructions to event
retval = PAPI_add_named_event(eventset, "PAPI_TOT_CYC");
if (retval != PAPI_OK)
{
std::cerr << "PAPI: could not add PAPI_TOT_CYC: " << PAPI_strerror(retval) << std::endl;
}
retval = PAPI_add_named_event(eventset, "PAPI_TOT_INS");
if (retval != PAPI_OK)
{
std::cerr << "PAPI: could not add PAPI_TOT_INS: " << PAPI_strerror(retval) << std::endl;
}
/* sample */
long long count;
PAPI_reset(eventset);
retval = PAPI_start(eventset);
if (retval != PAPI_OK)
std::cerr << "PAPI: could not START sample: " << PAPI_strerror(retval) << std::endl;
/* the workload */
powersOfOne(10);
//stopping eventset
retval = PAPI_stop(eventset, count);
if (retval != PAPI_OK)
std::cerr << "PAPI: could not STOP sample: " << PAPI_strerror(retval) << std::endl;
std::cout << "PAPI: measured " << count << " cycles" << std::endl;
std::cout << "PAPI: measured " << count << " instructions" << std::endl;
/* (optional) cleanup */
PAPI_cleanup_eventset(eventset);
PAPI_destroy_eventset(&eventset);
return 0;
}
void powersOfOne(int num){
int val = 1;
for (int i = 1; i < num; i++){
val*=1;
}
std::cout << val << std::endl;
}