src/misc/ngx_google_perftools_module.c - nginx-1.7.10

Global variables defined

Data types defined

Functions defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #include <ngx_config.h>
  6. #include <ngx_core.h>

  7. /*
  8. * declare Profiler interface here because
  9. * <google/profiler.h> is C++ header file
  10. */

  11. int ProfilerStart(u_char* fname);
  12. void ProfilerStop(void);
  13. void ProfilerRegisterThread(void);


  14. static void *ngx_google_perftools_create_conf(ngx_cycle_t *cycle);
  15. static ngx_int_t ngx_google_perftools_worker(ngx_cycle_t *cycle);


  16. typedef struct {
  17.     ngx_str_t  profiles;
  18. } ngx_google_perftools_conf_t;


  19. static ngx_command_t  ngx_google_perftools_commands[] = {

  20.     { ngx_string("google_perftools_profiles"),
  21.       NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
  22.       ngx_conf_set_str_slot,
  23.       0,
  24.       offsetof(ngx_google_perftools_conf_t, profiles),
  25.       NULL },

  26.     ngx_null_command
  27. };


  28. static ngx_core_module_t  ngx_google_perftools_module_ctx = {
  29.     ngx_string("google_perftools"),
  30.     ngx_google_perftools_create_conf,
  31.     NULL
  32. };


  33. ngx_module_t  ngx_google_perftools_module = {
  34.     NGX_MODULE_V1,
  35.     &ngx_google_perftools_module_ctx,      /* module context */
  36.     ngx_google_perftools_commands,         /* module directives */
  37.     NGX_CORE_MODULE,                       /* module type */
  38.     NULL,                                  /* init master */
  39.     NULL,                                  /* init module */
  40.     ngx_google_perftools_worker,           /* init process */
  41.     NULL,                                  /* init thread */
  42.     NULL,                                  /* exit thread */
  43.     NULL,                                  /* exit process */
  44.     NULL,                                  /* exit master */
  45.     NGX_MODULE_V1_PADDING
  46. };


  47. static void *
  48. ngx_google_perftools_create_conf(ngx_cycle_t *cycle)
  49. {
  50.     ngx_google_perftools_conf_t  *gptcf;

  51.     gptcf = ngx_pcalloc(cycle->pool, sizeof(ngx_google_perftools_conf_t));
  52.     if (gptcf == NULL) {
  53.         return NULL;
  54.     }

  55.     /*
  56.      * set by ngx_pcalloc()
  57.      *
  58.      *     gptcf->profiles = { 0, NULL };
  59.      */

  60.     return gptcf;
  61. }


  62. static ngx_int_t
  63. ngx_google_perftools_worker(ngx_cycle_t *cycle)
  64. {
  65.     u_char                       *profile;
  66.     ngx_google_perftools_conf_t  *gptcf;

  67.     gptcf = (ngx_google_perftools_conf_t *)
  68.                 ngx_get_conf(cycle->conf_ctx, ngx_google_perftools_module);

  69.     if (gptcf->profiles.len == 0) {
  70.         return NGX_OK;
  71.     }

  72.     profile = ngx_alloc(gptcf->profiles.len + NGX_INT_T_LEN + 2, cycle->log);
  73.     if (profile == NULL) {
  74.         return NGX_OK;
  75.     }

  76.     if (getenv("CPUPROFILE")) {
  77.         /* disable inherited Profiler enabled in master process */
  78.         ProfilerStop();
  79.     }

  80.     ngx_sprintf(profile, "%V.%d%Z", &gptcf->profiles, ngx_pid);

  81.     if (ProfilerStart(profile)) {
  82.         /* start ITIMER_PROF timer */
  83.         ProfilerRegisterThread();

  84.     } else {
  85.         ngx_log_error(NGX_LOG_CRIT, cycle->log, ngx_errno,
  86.                       "ProfilerStart(%s) failed", profile);
  87.     }

  88.     ngx_free(profile);

  89.     return NGX_OK;
  90. }


  91. /* ProfilerStop() is called on Profiler destruction */