upstream nginx-0.7.31
[nginx.git] / nginx / src / misc / ngx_google_perftools_module.c
1
2 /*
3  * Copyright (C) Igor Sysoev
4  */
5
6
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9
10 /*
11  * declare Profiler here interface because
12  * <google/profiler.h> is C++ header file
13  */
14
15 int ProfilerStart(u_char* fname);
16 void ProfilerStop(void);
17 void ProfilerRegisterThread(void);
18
19
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);
22
23
24 typedef struct {
25     ngx_str_t  profiles;
26 } ngx_google_perftools_conf_t;
27
28
29 static ngx_command_t  ngx_google_perftools_commands[] = {
30
31     { ngx_string("google_perftools_profiles"),
32       NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
33       ngx_conf_set_str_slot,
34       0,
35       offsetof(ngx_google_perftools_conf_t, profiles),
36       NULL },
37
38     ngx_null_command
39 };
40
41
42 static ngx_core_module_t  ngx_google_perftools_module_ctx = {
43     ngx_string("google_perftools"),
44     ngx_google_perftools_create_conf,
45     NULL
46 };
47
48
49 ngx_module_t  ngx_google_perftools_module = {
50     NGX_MODULE_V1,
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 */
61     NGX_MODULE_V1_PADDING
62 };
63
64
65 static void *
66 ngx_google_perftools_create_conf(ngx_cycle_t *cycle)
67 {
68     ngx_google_perftools_conf_t  *gptcf;
69
70     gptcf = ngx_pcalloc(cycle->pool, sizeof(ngx_google_perftools_conf_t));
71     if (gptcf == NULL) {
72         return NULL;
73     }
74
75     /*
76      * set by pcalloc()
77      *
78      *     gptcf->profiles = { 0, NULL };
79      */
80
81     return gptcf;
82 }
83
84
85 static ngx_int_t
86 ngx_google_perftools_worker(ngx_cycle_t *cycle)
87 {
88     u_char                       *profile;
89     ngx_google_perftools_conf_t  *gptcf;
90
91     gptcf = (ngx_google_perftools_conf_t *)
92                 ngx_get_conf(cycle->conf_ctx, ngx_google_perftools_module);
93
94     if (gptcf->profiles.len == 0) {
95         return NGX_OK;
96     }
97
98     profile = ngx_alloc(gptcf->profiles.len + NGX_INT_T_LEN + 2, cycle->log);
99     if (profile == NULL) {
100         return NGX_OK;
101     }
102
103     if (getenv("CPUPROFILE")) {
104
105         /* disable inherited Profiler enabled in master process */
106         ProfilerStop();
107     }
108
109     ngx_sprintf(profile, "%V.%d%Z", &gptcf->profiles, ngx_pid);
110
111     if (ProfilerStart(profile)) {
112
113         /* start ITIMER_PROF timer */
114         ProfilerRegisterThread();
115
116     } else {
117         ngx_log_error(NGX_LOG_CRIT, cycle->log, ngx_errno,
118                       "ProfilerStart(%s) failed", profile);
119     }
120
121     ngx_free(profile);
122
123     return NGX_OK;
124 }
125
126
127 /* ProfilerStop() is called on Profiler destruction */