开发者

Profiling for runtime in C

开发者 https://www.devze.com 2023-04-01 21:56 出处:网络
i am trying to find out a way in which i can write out run-time of selected 开发者_StackOverflow中文版number of functions when my tool runs. let say out of large list of functions which my tool have i

i am trying to find out a way in which i can write out run-time of selected 开发者_StackOverflow中文版number of functions when my tool runs. let say out of large list of functions which my tool have i need to write out the what is the time taken by individual calls y(), z() , a1() ....

X(){

 y();
 ..
 ..
 z();
 ..
 ..
 a1();
 ..
 ..
 b1();
 ..
 ..
}

I don't want to use gprof because it need to be separately run , and there is no way customer can run gprof at there end. In many cases i don't have access to the test which is producing long runtime. A way to log run time of high level function will be helpful. i dont't want to wrap around each and every function call around time() system call and then print it. Can macro help in this.? any ideas?


You said you don't want to wrap each call, but let me just show you how I handle timer calls so they can be removed at compile time, and maybe that can be wrapped up in another macro later:

#ifndef NOTIME
#  include <ctime>
#  define CLOCK_TICK(acc, ctr)  ctr = std::clock()
#  define CLOCK_TOCK(acc, ctr)  acc += (std::clock() - ctr)
#  define CLOCK_RESET(acc) acc = 0
#  define CLOCK_REPORT(acc) 1000. * double(acc) / double(CLOCKS_PER_SEC)

static clock_t t1a, t1c, t2a, t2c; // as many pairs as you need

#else
#  define CLOCK_TICK(acc, ctr)
#  define CLOCK_TOCK(acc, ctr)
#  define CLOCK_RESET(acc)
#  define CLOCK_REPORT(acc) 0
#endif

Now you can use it like this:

CLOCK_RESET(t1a);
for (int i = 0; i != 250000; ++i)
{
  CLOCK_TICK(t1a, t1c);
  some_expensive_function();
  CLOCK_TOCK(t1a, t1c);
}
std::cout << "Time spent in some_expensive_function(): " << CLOCK_REPORT(t1a) << "ms.\n";

With variadic macros you might even be able to wrap up the function call:

#define timed_call(ACC, CTR, func, ...) do { CLOCK_TICK(ACC, CTR); \
                                             func(__VA_ARGS__);    \
                                             CLOCK_TOCK(ACC,CTR);  \
                                           } while (false)


Your customer does not need to run gprof. If you compile your executable with profiling he just needs to run the app normally, then collect the gmon.out data file and deliver it to you. You will then run gprof to generate profiling data.

If you want to have more control over what parts of your code are profiled then you could try the open source Shiny profiler. To use this you have link with a small C++ library and insert a macro at the beginning of each function you want to profile. There are also options to profile blocks of code. The profiler output is generated via a function call from your application, so you can decide how and when the output is generated.


Why not wrap each call, with a macro like this:

#define WRAP(fcall) do {\
  .. before stuff .. \
  fcall; \
  .. after call .. \
  } while(0)

then in place of each

foo(x, y, z);

you write

WRAP(foo(x, y, z));

Then if you want to disable it, just comment out the before/after stuff.

It may not be pretty, but it works.

0

精彩评论

暂无评论...
验证码 换一张
取 消