项目作者: Kohit

项目描述 :
A very lightweight C++ performance profiler
高级语言: C++
项目地址: git://github.com/Kohit/Profiler1.git
创建时间: 2020-10-24T14:12:42Z
项目社区:https://github.com/Kohit/Profiler1

开源协议:MIT License

下载


Profiler1

Profiler1 is a c++ profiler, aim to find out the time & memory cost
of each function call, with the help of compiler instrumentation,
using this library doesn’t need to modify your source code.
This profiler could also use to trace the call stack and to detect
memory leak.

Example

  1. #include "profiler1.h"
  2. // some code
  3. // a memory leak function
  4. void allocmemory(){
  5. int * arr = new int[100000];
  6. }
  7. void RunTest(int i){
  8. Foo foo;
  9. if (i < 2) {
  10. bla();
  11. } else if (i == 2){
  12. allocmemory();
  13. } else {
  14. int b = foo.add(a);
  15. }
  16. }
  17. int main()
  18. {
  19. g_objProfiler1.bEnableMemoryProfile = true;
  20. g_objProfiler1.Start();
  21. // suppose we have 5 frame
  22. for (int i = 0; i < 5; i++) {
  23. // start profiler logging
  24. g_objProfiler1.FrameStart();
  25. // do something in this frame
  26. RunTest(i);
  27. // end profiler logging
  28. g_objProfiler1.FrameEnd();
  29. }
  30. g_objProfiler1.Stop();
  31. // for better performance, profiler1 won't do analyze during target function call
  32. // so, we need to run analyze manually, this may takes a while.
  33. g_objProfiler1.Analyze();
  34. // write statistic result of each stackframe of frame 2
  35. g_objProfiler1.WriteStatistic("stats.csv", 2);
  36. // write statistic result of each frame
  37. g_objProfiler1.WriteFrameStatistic("statsFrame.csv");
  38. return 0;
  39. }

the output may look like:
stats.csv
|Address|Name|AvgSelfTime(us)|AvgTime(us)|AvgMemory(bytes)|TotalSelfTime(us)|TotalTime(us)|TotalMemory(bytes)|InvokeTimes|
|—|—|—|—|—|—|—|—|—|
|0XBDBDB0|allocmemory|205|205|405504|205|205|405504|1|
|0XBD93A0|RunTest|28|263|405504|28|263|405504|1|
|0XBD9460|Foo::Foo|18|30|0|18|30|0|1|
|0XBD9350|Bar::Bar|12|12|0|12|12|0|1|

statsFrame.csv
|Frame|StartTime|TotalTime(us)|TotalMemory(bytes)|InvokeTimes|
|—|—|—|—|—|
|0|43|85|0|4|
|1|153|78|0|4|
|2|260|268|405504|4|
|3|561|1335|4096|107|
|4|1927|1263|0|107|

see test.cpp for more detail.

Usage:

Compile with cl:

  1. simply compile profiler1.cpp alone, to generate (obj/lib/dll) binary.
  2. compile target source code, with /Gh /GH option, then link the binary.

For more compile detail, check:
https://docs.microsoft.com/en-us/cpp/build/reference/gh-enable-penter-hook-function?view=vs-2019

Compile with g++:

Not supported yet, you need to add -finstrument-functions compile option and change the _penter/_pexit implementation to

  1. void __cyg_profile_func_enter (void *, void *) __attribute__((no_instrument_function));
  2. void __cyg_profile_func_exit (void *, void *) __attribute__((no_instrument_function));

License

The MIT License

Copyright(C) 2020 kohit (kohits@outlook.com or https://github.com/Kohit)