Update and clean Tomato RAF files
[tomato.git] / release / src / router / nginx / src / misc / ngx_google_perftools_module.c
blobf2f8221b560587ada36764713602d410d4ef4d7c
2 /*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Nginx, Inc.
5 */
8 #include <ngx_config.h>
9 #include <ngx_core.h>
12 * declare Profiler interface here because
13 * <google/profiler.h> is C++ header file
16 int ProfilerStart(u_char* fname);
17 void ProfilerStop(void);
18 void ProfilerRegisterThread(void);
21 static void *ngx_google_perftools_create_conf(ngx_cycle_t *cycle);
22 static ngx_int_t ngx_google_perftools_worker(ngx_cycle_t *cycle);
25 typedef struct {
26 ngx_str_t profiles;
27 } ngx_google_perftools_conf_t;
30 static ngx_command_t ngx_google_perftools_commands[] = {
32 { ngx_string("google_perftools_profiles"),
33 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
34 ngx_conf_set_str_slot,
36 offsetof(ngx_google_perftools_conf_t, profiles),
37 NULL },
39 ngx_null_command
43 static ngx_core_module_t ngx_google_perftools_module_ctx = {
44 ngx_string("google_perftools"),
45 ngx_google_perftools_create_conf,
46 NULL
50 ngx_module_t ngx_google_perftools_module = {
51 NGX_MODULE_V1,
52 &ngx_google_perftools_module_ctx, /* module context */
53 ngx_google_perftools_commands, /* module directives */
54 NGX_CORE_MODULE, /* module type */
55 NULL, /* init master */
56 NULL, /* init module */
57 ngx_google_perftools_worker, /* init process */
58 NULL, /* init thread */
59 NULL, /* exit thread */
60 NULL, /* exit process */
61 NULL, /* exit master */
62 NGX_MODULE_V1_PADDING
66 static void *
67 ngx_google_perftools_create_conf(ngx_cycle_t *cycle)
69 ngx_google_perftools_conf_t *gptcf;
71 gptcf = ngx_pcalloc(cycle->pool, sizeof(ngx_google_perftools_conf_t));
72 if (gptcf == NULL) {
73 return NULL;
77 * set by ngx_pcalloc()
79 * gptcf->profiles = { 0, NULL };
82 return gptcf;
86 static ngx_int_t
87 ngx_google_perftools_worker(ngx_cycle_t *cycle)
89 u_char *profile;
90 ngx_google_perftools_conf_t *gptcf;
92 gptcf = (ngx_google_perftools_conf_t *)
93 ngx_get_conf(cycle->conf_ctx, ngx_google_perftools_module);
95 if (gptcf->profiles.len == 0) {
96 return NGX_OK;
99 profile = ngx_alloc(gptcf->profiles.len + NGX_INT_T_LEN + 2, cycle->log);
100 if (profile == NULL) {
101 return NGX_OK;
104 if (getenv("CPUPROFILE")) {
105 /* disable inherited Profiler enabled in master process */
106 ProfilerStop();
109 ngx_sprintf(profile, "%V.%d%Z", &gptcf->profiles, ngx_pid);
111 if (ProfilerStart(profile)) {
112 /* start ITIMER_PROF timer */
113 ProfilerRegisterThread();
115 } else {
116 ngx_log_error(NGX_LOG_CRIT, cycle->log, ngx_errno,
117 "ProfilerStart(%s) failed", profile);
120 ngx_free(profile);
122 return NGX_OK;
126 /* ProfilerStop() is called on Profiler destruction */