如何解决使用 PAPI 库进行性能指标
现在我可以将 PAPI 用于系统指标,但只能在应用程序中使用。结构是--初始化库--创建事件--启动事件(指令总数和/或周期总数)--工作负载(应用程序代码)--停止事件(返回测量)。但我想采用不同的方法,例如 -- 初始化库 -- 创建事件 -- 启动事件(指令总数和/或循环总数) -- "SLEEP" --停止事件(返回测量值)。所以基本上我想在一个终端中运行一个程序,在另一个终端(睡眠)中运行 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;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。