Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.13.12 ]​[ nginx-1.12.2 ]​

0001 
0002 /*
0003  * Copyright (C) Igor Sysoev
0004  * Copyright (C) Nginx, Inc.
0005  */
0006 
0007 
0008 #include <ngx_config.h>
0009 #include <ngx_core.h>
0010 
0011 /*
0012  * declare Profiler interface here because
0013  * <google/profiler.h> is C++ header file
0014  */
0015 
0016 int ProfilerStart(u_char* fname);
0017 void ProfilerStop(void);
0018 void ProfilerRegisterThread(void);
0019 
0020 
0021 static void *ngx_google_perftools_create_conf(ngx_cycle_t *cycle);
0022 static ngx_int_t ngx_google_perftools_worker(ngx_cycle_t *cycle);
0023 
0024 
0025 typedef struct {
0026     ngx_str_t  profiles;
0027 } ngx_google_perftools_conf_t;
0028 
0029 
0030 static ngx_command_t  ngx_google_perftools_commands[] = {
0031 
0032     { ngx_string("google_perftools_profiles"),
0033       NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
0034       ngx_conf_set_str_slot,
0035       0,
0036       offsetof(ngx_google_perftools_conf_t, profiles),
0037       NULL },
0038 
0039       ngx_null_command
0040 };
0041 
0042 
0043 static ngx_core_module_t  ngx_google_perftools_module_ctx = {
0044     ngx_string("google_perftools"),
0045     ngx_google_perftools_create_conf,
0046     NULL
0047 };
0048 
0049 
0050 ngx_module_t  ngx_google_perftools_module = {
0051     NGX_MODULE_V1,
0052     &ngx_google_perftools_module_ctx,      /* module context */
0053     ngx_google_perftools_commands,         /* module directives */
0054     NGX_CORE_MODULE,                       /* module type */
0055     NULL,                                  /* init master */
0056     NULL,                                  /* init module */
0057     ngx_google_perftools_worker,           /* init process */
0058     NULL,                                  /* init thread */
0059     NULL,                                  /* exit thread */
0060     NULL,                                  /* exit process */
0061     NULL,                                  /* exit master */
0062     NGX_MODULE_V1_PADDING
0063 };
0064 
0065 
0066 static void *
0067 ngx_google_perftools_create_conf(ngx_cycle_t *cycle)
0068 {
0069     ngx_google_perftools_conf_t  *gptcf;
0070 
0071     gptcf = ngx_pcalloc(cycle->pool, sizeof(ngx_google_perftools_conf_t));
0072     if (gptcf == NULL) {
0073         return NULL;
0074     }
0075 
0076     /*
0077      * set by ngx_pcalloc()
0078      *
0079      *     gptcf->profiles = { 0, NULL };
0080      */
0081 
0082     return gptcf;
0083 }
0084 
0085 
0086 static ngx_int_t
0087 ngx_google_perftools_worker(ngx_cycle_t *cycle)
0088 {
0089     u_char                       *profile;
0090     ngx_google_perftools_conf_t  *gptcf;
0091 
0092     gptcf = (ngx_google_perftools_conf_t *)
0093                 ngx_get_conf(cycle->conf_ctx, ngx_google_perftools_module);
0094 
0095     if (gptcf->profiles.len == 0) {
0096         return NGX_OK;
0097     }
0098 
0099     profile = ngx_alloc(gptcf->profiles.len + NGX_INT_T_LEN + 2, cycle->log);
0100     if (profile == NULL) {
0101         return NGX_OK;
0102     }
0103 
0104     if (getenv("CPUPROFILE")) {
0105         /* disable inherited Profiler enabled in master process */
0106         ProfilerStop();
0107     }
0108 
0109     ngx_sprintf(profile, "%V.%d%Z", &gptcf->profiles, ngx_pid);
0110 
0111     if (ProfilerStart(profile)) {
0112         /* start ITIMER_PROF timer */
0113         ProfilerRegisterThread();
0114 
0115     } else {
0116         ngx_log_error(NGX_LOG_CRIT, cycle->log, ngx_errno,
0117                       "ProfilerStart(%s) failed", profile);
0118     }
0119 
0120     ngx_free(profile);
0121 
0122     return NGX_OK;
0123 }
0124 
0125 
0126 /* ProfilerStop() is called on Profiler destruction */