3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
11 * declare Profiler here interface because
12 * <google/profiler.h> is C++ header file
15 int ProfilerStart(u_char* fname);
16 void ProfilerStop(void);
17 void ProfilerRegisterThread(void);
20 static void *ngx_google_perftools_create_conf(ngx_cycle_t *cycle);
21 static ngx_int_t ngx_google_perftools_worker(ngx_cycle_t *cycle);
26 } ngx_google_perftools_conf_t;
29 static ngx_command_t ngx_google_perftools_commands[] = {
31 { ngx_string("google_perftools_profiles"),
32 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
33 ngx_conf_set_str_slot,
35 offsetof(ngx_google_perftools_conf_t, profiles),
42 static ngx_core_module_t ngx_google_perftools_module_ctx = {
43 ngx_string("google_perftools"),
44 ngx_google_perftools_create_conf,
49 ngx_module_t ngx_google_perftools_module = {
51 &ngx_google_perftools_module_ctx, /* module context */
52 ngx_google_perftools_commands, /* module directives */
53 NGX_CORE_MODULE, /* module type */
54 NULL, /* init master */
55 NULL, /* init module */
56 ngx_google_perftools_worker, /* init process */
57 NULL, /* init thread */
58 NULL, /* exit thread */
59 NULL, /* exit process */
60 NULL, /* exit master */
66 ngx_google_perftools_create_conf(ngx_cycle_t *cycle)
68 ngx_google_perftools_conf_t *gptcf;
70 gptcf = ngx_pcalloc(cycle->pool, sizeof(ngx_google_perftools_conf_t));
78 * gptcf->profiles = { 0, NULL };
86 ngx_google_perftools_worker(ngx_cycle_t *cycle)
89 ngx_google_perftools_conf_t *gptcf;
91 gptcf = (ngx_google_perftools_conf_t *)
92 ngx_get_conf(cycle->conf_ctx, ngx_google_perftools_module);
94 if (gptcf->profiles.len == 0) {
98 profile = ngx_alloc(gptcf->profiles.len + NGX_INT_T_LEN + 2, cycle->log);
99 if (profile == NULL) {
103 if (getenv("CPUPROFILE")) {
105 /* disable inherited Profiler enabled in master process */
109 ngx_sprintf(profile, "%V.%d%Z", &gptcf->profiles, ngx_pid);
111 if (ProfilerStart(profile)) {
113 /* start ITIMER_PROF timer */
114 ProfilerRegisterThread();
117 ngx_log_error(NGX_LOG_CRIT, cycle->log, ngx_errno,
118 "ProfilerStart(%s) failed", profile);
127 /* ProfilerStop() is called on Profiler destruction */