Profile分析方法

定时采样 获取Stack Frame

gperftools CPUPROFILER实现分析
不支持Windows 支持目前主流的UNIX

1
2
#define SIGPROF 27      /* profiling time alarm */
#define ITIMER_PROF 2

libunwind

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct sigaction sa;
sa.sa_sigaction = prof_handler;
sa.sa_flags = SA_RESTART | SA_SIGINFO;
sigemptyset(&sa.sa_mask);
if (sigaction(SIGPROF, &sa, NULL) != 0) {
perror("sigaction");
exit(1);
}

struct itimerval timer;
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_usec = 1000;
timer.it_value = timer.it_interval;
setitimer(ITIMER_PROF, &timer, 0);
1
2
CpuProfiler CpuProfiler::instance_;
static CpuProfiler instance_;

静态初始化 模块载入时进入构造函数

win32支持仅限于内存

http://gaiacrtn.free.fr/articles/win32perftools.htmlfastallocator
https://github.com/kasicass/gperftools-win32heap-profiler

编译器指令插入Profile

MSVC /Gh /GH
GCC/Clang -finstrument-functions

https://github.com/botman99/AeonProfiler
The Aeon profiler works by using the Microsoft Visual Studio compiler options /Gh and /GH to insert hooks to external functions _penter() and _pexit() (respectively).

https://github.com/tyoma/micro-profiler
(Linux) Build the application with -finstrument-functions flag on. Link with micro-profiler_. The shared objects are located in MicroProfiler’s installation directory;
(Windows, MSVC) Build the application with /GH /Gh flags on. Link with micro-profiler_.lib;

外部采样 Debug权限

windows CPU profiler
https://github.com/VerySleepy/verysleepy
Very Sleepy, a sampling CPU profiler for Windows –> dbghelp
LookupPrivilegeValue( NULL, SE_DEBUG_NAME, &luid );
可以导出Callgrind格式

profiler-using-clang-based-ast-instrumentation
kcachegrind

misc

profiler
profiler
speedscope