首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何定期使用PAPI进行性能测量

如何定期使用PAPI进行性能测量
EN

Stack Overflow用户
提问于 2011-11-17 07:04:49
回答 1查看 2.9K关注 0票数 4

我想使用C语言中的PAPI来分析我的应用程序的系统性能。

--初始化PAPI

--初始化感兴趣的计数器

--启动计数器

代码语言:javascript
复制
      -- run main logic of the application

--结束计数器和读取值

我希望定期读取计数器,例如每1秒读取一次,而不是在应用程序结束时读取最终值。PAPI输出是否给出了程序执行结束时的聚合值,如程序执行后L2缓存未命中的总数。另一个例子是在每个时刻读取指令的数量,而不是在程序结束时读取指令的总数。

EN

回答 1

Stack Overflow用户

发布于 2012-09-12 17:16:00

你对此有什么想法吗?我正在做同样的事情,但我得到了奇怪的结果:我的计数器没有像我希望的那样频繁地更新。

这里有一个我测试过并运行良好的示例:

代码语言:javascript
复制
#include <papi.h>
#include <stdio.h>
#include <stdlib.h>

main()
{
int retval, EventSet = PAPI_NULL;
long_long values[3];
    unsigned counter;
    unsigned c;
    unsigned long fact;
    unsigned stoppoint;


        /* Initialize the PAPI library */
        retval = PAPI_library_init(PAPI_VER_CURRENT);

        if (retval != PAPI_VER_CURRENT) {
          fprintf(stderr, "PAPI library init error!\n");
          exit(1);
        }

        /* Create the Event Set */
        if (PAPI_create_eventset(&EventSet) != PAPI_OK)
            printf ("%s:%d\t ERROR\n", __FILE__, __LINE__);

        /* Add Total Instructions Executed to our EventSet */
        if (PAPI_add_event(EventSet, PAPI_TOT_INS) != PAPI_OK)
            printf ("%s:%d\t ERROR\n", __FILE__, __LINE__);

        /* Add Total Instructions Executed to our EventSet */
        if (PAPI_add_event(EventSet, PAPI_TOT_CYC) != PAPI_OK)
            printf ("%s:%d\t ERROR\n", __FILE__, __LINE__);

        /* Add Total Instructions Executed to our EventSet */
        if (PAPI_add_event(EventSet, PAPI_LST_INS) != PAPI_OK)
            printf ("%s:%d\t ERROR\n", __FILE__, __LINE__);


   srand ( time(NULL) );
   stoppoint = 50+(rand() % 100);
/* Do some computation here */
    for (counter = 0; counter < stoppoint; counter++)
    {
        /* Initialize the PAPI library */
        retval = PAPI_library_init(PAPI_VER_CURRENT);

        if (retval != PAPI_VER_CURRENT) {
          fprintf(stderr, "PAPI library init error!\n");
          exit(1);
        }


        /* Start counting */
        if (PAPI_start(EventSet) != PAPI_OK)
            printf ("%s:%d\t ERROR\n", __FILE__, __LINE__);


                fact = 1;
        for (c = 1; c <= counter; c++)
        {
                     fact = c * c;
        }




        printf("Factorial of %d is %lu\n", counter, fact);
        if (PAPI_read(EventSet, values) != PAPI_OK)
            printf ("%s:%d\t ERROR\n", __FILE__, __LINE__);
        printf ("\nTotal Instructions Completed:\t%lld\t Total Cycles:\t%lld\tLoad Store Instructions\t%lld\n\n", values[0], values[1], values[2]);
        /* Do some computation here */

        if (PAPI_stop(EventSet, values) != PAPI_OK)
            printf ("%s:%d\t ERROR\n", __FILE__, __LINE__);

            }
        /* End of computation */


}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8160004

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档