资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

AIX下C/C++函数性能统计实现方法-创新互联

在AIX中,xlc编译器有个选项-qfunctrace,使用此选项编译的程序,自动会在每个函数的入口出口处调用以下自定义函数。

创新互联公司主要从事成都网站设计、成都做网站、网页设计、企业做网站、公司建网站等业务。立足成都服务南丹,10余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220

extern "C" void
__func_trace_enter(const char * const proc_name,
                   const char * const file_name,
                   const int line_no,
                   void ** const id);

extern "C" void
__func_trace_exit(const char * const proc_name,
                   const char * const file_name,
                   const int line_no,
                   void ** const id);

extern "C" void
__func_trace_catch(const char * const proc_name,
                   const char * const file_name,
                   const int line_no,
                   void ** const id);

在函数调用前,执行__func_trace_enter(),函数正常返回后,执行__func_trace_exit()。如果函数是通过throw异常抛出的,那么在异常被catch捕获处,执行__func_trace_catch(),但是遇到catch(...)捕获不会执行。值得注意的是,如果时throw抛出,不会触发__func_trace_exit()。

使用这个功能,可以实现无需修改源程序,进行性能统计的效果。程序如下。

tr.cpp为自定义函数出入口程序,每个函数执行时都会经过。编译成为libfunctr.so。

  • #include
  • #include
  • #include
  • #include

  • using std::vector;
  • using std::string;
  • using std::clog;
  • using std::endl;

  • extern "C" void print_trace();

  • struct Stat
  • {
  •     int lvl;
  •     string name;
  •     long stm;
  •     long etm;
  •     long oitv;
  •     Stat(int l, const string& s, long st) : lvl(l), name(s), stm(st), etm(0), oitv(0) {}
  • };

  • static vector tracev;
  • static int clvl = 0;

  • extern "C" void
  • __func_trace_enter(const char * const proc_name,
  •                    const char * const file_name,
  •                    const int line_no,
  •                    void ** const id)
  • {
  • // printf("{ %s (%s:%d) %p %s\n", proc_name, file_name, line_no, id[0], (char*)*id);
  •     struct timeval nowtm;
  •     gettimeofday(&nowtm, NULL);
  •     ++clvl;
  •     tracev.push_back(Stat(clvl, string(proc_name)+"() : "+file_name, nowtm.tv_sec * 1000000 + nowtm.tv_usec));
  • }

  • extern "C" void
  • __func_trace_exit(const char * const proc_name,
  •                   const char * const file_name,
  •                   const int line_no,
  •                   void ** const id)
  • {
  • // printf("} %s (%s:%d) %p %s\n", proc_name, file_name, line_no, id[0], (char*)*id);
  •     struct timeval nowtm;
  •     int itv;
  •     gettimeofday(&nowtm, NULL);
  •     auto iter = tracev.end() - 1;
  •     while (iter->etm != 0)
  •         --iter;
  •     iter->etm = nowtm.tv_sec * 1000000 + nowtm.tv_usec;
  •     itv = iter->etm - iter->stm - iter->oitv;
  •     for (auto s = tracev.begin(); s!=tracev.end(); s++)
  •     {
  •         if (s->etm == 0)
  •             s->oitv += itv;
  •     }
  •     --clvl;
  •     if (clvl == 0)
  •         print_trace();
  • }

  • extern "C" void print_trace()
  • {
  •     time_t t;
  •     char buf[30];
  •     for (auto s = tracev.begin(); s!=tracev.end(); s++)
  •     {
  •         clog << s->lvl << "\t";
  •         t=s->stm/1000000;
  •         strftime(buf, sizeof(buf), "%Y%m%d%H%M%S", localtime(&t));
  •         clog << buf << "." << s->stm % 1000000 << "\t";
  •         t=s->etm/1000000;
  •         strftime(buf, sizeof(buf), "%Y%m%d%H%M%S", localtime(&t));
  •         clog << buf << "." << s->etm % 1000000 << "\t";
  •         clog << s->etm-s->stm << "\t" << s->oitv << "\t" << s->etm-s->stm-s->oitv << "\t";
  •         clog << string(s->lvl-1, ' ') << s->name << endl;
  •     }
  • }

  • 分享标题:AIX下C/C++函数性能统计实现方法-创新互联
    本文路径:http://cdkjz.cn/article/dhhhsh.html
    多年建站经验

    多一份参考,总有益处

    联系快上网,免费获得专属《策划方案》及报价

    咨询相关问题或预约面谈,可以通过以下方式与我们联系

    大客户专线   成都:13518219792   座机:028-86922220