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

发布于 2024-12-16 13:08:26 字数 267 浏览 3 评论 0原文

我想使用 C 中的 PAPI api 分析我的应用程序的系统性能。一般结构是
-- 初始化PAPI
-- 初始化感兴趣的计数器
-- 启动计数器
-- 运行应用程序的主要逻辑
-- 结束计数器和读取值

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

I want to analyze system's performance for my application using PAPI api in C. The general structure is that
-- Initialize PAPI
-- Initialize counters of interest
-- start counters
-- run main logic of the application
-- end counters and read values

I want to read the counters periodically say every 1 second instead of reading the final values at the end of the application. does the PAPI output give the aggregate values at end of program execution like the total number of L2 cache misses after the program execution. Another example would be to read number of instructions at every time instance rather than total number of instructions at the end of the program.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫腻 2024-12-23 13:08:26

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

作为记录,以下是我测试过且运行良好的示例:

#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 */


}

Did you get any ideas regarding this? I am doing the same thing, but I am getting strange results: my counters are not updated as often as I'd like them.

For the record, here is a sample that I tested and works well:

#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 */


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文