Ticket #189: monotonic_timers.2.patch

File monotonic_timers.2.patch, 6.7 KB (added by flixr@…, 9 years ago)

fixed previous patch

  • auto/unix

    diff --git a/auto/unix b/auto/unix
    index 10835f6c..512681db 100644
    a b if [ $ngx_found = no ]; then  
    299299    fi
    300300fi
    301301
     302ngx_feature="clock_gettime(CLOCK_MONOTONIC)"
     303ngx_feature_name="NGX_HAVE_CLOCK_MONOTONIC"
     304ngx_feature_run=no
     305ngx_feature_incs="#include <time.h>"
     306ngx_feature_path=
     307ngx_feature_libs=
     308ngx_feature_test="struct timespec monotime; clock_gettime(CLOCK_MONOTONIC, &monotime);"
     309. auto/feature
     310
     311
     312if [ $ngx_found != yes ]; then
     313
     314    ngx_feature="clock_gettime(CLOCK_MONOTONIC) in librt"
     315    ngx_feature_libs="-lrt"
     316    . auto/feature
     317
     318    if [ $ngx_found = yes ]; then
     319        CORE_LIBS="$CORE_LIBS -lrt"
     320    fi
     321fi
     322
     323if [ $ngx_found = yes ]; then
     324        # We have clock_gettime with CLOCK_MONOTONIC. Now we test for faster
     325        # variants (CLOCK_MONOTONIC_(FAST|COARSE)). ngx_feature_libs is set
     326        # by previous tests to include librt if necessary.
     327        ngx_feature="clock_gettime(CLOCK_MONOTONIC_FAST)"
     328        ngx_feature_name="NGX_HAVE_CLOCK_MONOTONIC_FAST"
     329        ngx_feature_test="struct timespec monotime; clock_gettime(CLOCK_MONOTONIC_FAST, &monotime);"
     330        . auto/feature
     331
     332        ngx_feature="clock_gettime(CLOCK_MONOTONIC_COARSE)"
     333        ngx_feature_name="NGX_HAVE_CLOCK_MONOTONIC_COARSE"
     334        ngx_feature_test="struct timespec monotime; clock_gettime(CLOCK_MONOTONIC_COARSE, &monotime);"
     335        . auto/feature
     336fi
    302337
    303338ngx_feature="sched_setaffinity()"
    304339ngx_feature_name="NGX_HAVE_SCHED_SETAFFINITY"
  • contrib/vim/syntax/nginx.vim

    diff --git a/contrib/vim/syntax/nginx.vim b/contrib/vim/syntax/nginx.vim
    index dc8c0cb4..4e87ba53 100644
    a b syn keyword ngxDirective contained merge_slashes  
    334334syn keyword ngxDirective contained min_delete_depth
    335335syn keyword ngxDirective contained modern_browser
    336336syn keyword ngxDirective contained modern_browser_value
     337syn keyword ngxDirective contained monotonic_timers
    337338syn keyword ngxDirective contained mp4
    338339syn keyword ngxDirective contained mp4_buffer_size
    339340syn keyword ngxDirective contained mp4_max_buffer_size
  • src/core/nginx.c

    diff --git a/src/core/nginx.c b/src/core/nginx.c
    index c3a29cc4..40108636 100644
    a b static ngx_command_t ngx_core_commands[] = {  
    152152      0,
    153153      NULL },
    154154
     155#if (NGX_HAVE_CLOCK_MONOTONIC)
     156    { ngx_string("monotonic_timers"),
     157      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_FLAG,
     158      ngx_conf_set_flag_slot,
     159      0,
     160      offsetof(ngx_core_conf_t, monotonic_timers),
     161      NULL },
     162#endif
     163
    155164      ngx_null_command
    156165};
    157166
    ngx_core_module_create_conf(ngx_cycle_t *cycle)  
    10281037    ccf->master = NGX_CONF_UNSET;
    10291038    ccf->timer_resolution = NGX_CONF_UNSET_MSEC;
    10301039    ccf->shutdown_timeout = NGX_CONF_UNSET_MSEC;
     1040    ccf->monotonic_timers = NGX_CONF_UNSET;
    10311041
    10321042    ccf->worker_processes = NGX_CONF_UNSET;
    10331043    ccf->debug_points = NGX_CONF_UNSET;
    ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf)  
    10571067    ngx_conf_init_value(ccf->master, 1);
    10581068    ngx_conf_init_msec_value(ccf->timer_resolution, 0);
    10591069    ngx_conf_init_msec_value(ccf->shutdown_timeout, 0);
     1070    ngx_conf_init_value(ccf->monotonic_timers, 0);
    10601071
    10611072    ngx_conf_init_value(ccf->worker_processes, 1);
    10621073    ngx_conf_init_value(ccf->debug_points, 0);
  • src/core/ngx_cycle.h

    diff --git a/src/core/ngx_cycle.h b/src/core/ngx_cycle.h
    index 2b48ccbd..f6cba531 100644
    a b typedef struct {  
    114114
    115115    ngx_array_t               env;
    116116    char                    **environment;
     117
     118    ngx_flag_t                monotonic_timers;
    117119} ngx_core_conf_t;
    118120
    119121
  • src/core/ngx_times.c

    diff --git a/src/core/ngx_times.c b/src/core/ngx_times.c
    index b2edf1a5..c0fe01aa 100644
    a b static char *week[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };  
    5959static char  *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
    6060                           "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
    6161
     62#if (NGX_HAVE_CLOCK_MONOTONIC)
     63static ngx_flag_t enable_monotonic = 0;
     64static clockid_t monotonic_clock;
     65#endif
     66
    6267void
    6368ngx_time_init(void)
    6469{
    ngx_time_init(void)  
    7378    ngx_time_update();
    7479}
    7580
     81void
     82ngx_time_enable_monotonic(ngx_flag_t enable)
     83{
     84#if (NGX_HAVE_CLOCK_MONOTONIC)
     85    struct timespec monotime;
     86
     87    if (enable) {
     88        /* Hack for the 'else if's to work correctly with the '#if's */
     89        if (0) {
     90        }
     91#if (NGX_HAVE_CLOCK_MONOTONIC_FAST)
     92        else if (clock_gettime(CLOCK_MONOTONIC_FAST, &monotime) == 0) {
     93            monotonic_clock = CLOCK_MONOTONIC_FAST;
     94        }
     95#endif
     96#if (NGX_HAVE_CLOCK_MONOTONIC_COARSE)
     97        else if (clock_gettime(CLOCK_MONOTONIC_COARSE, &monotime) == 0) {
     98            monotonic_clock = CLOCK_MONOTONIC_COARSE;
     99        }
     100#endif
     101        else if (clock_gettime(CLOCK_MONOTONIC, &monotime) == 0) {
     102            monotonic_clock = CLOCK_MONOTONIC;
     103        } else {
     104            /* No monotonic clock found at runtime, must disable. FIXME should this emit a warning? */
     105            enable = 0;
     106        }
     107    }
     108    enable_monotonic = enable;
     109    ngx_time_update();
     110#endif
     111}
    76112
    77113void
    78114ngx_time_update(void)
    ngx_time_update(void)  
    93129    sec = tv.tv_sec;
    94130    msec = tv.tv_usec / 1000;
    95131
    96     ngx_current_msec = (ngx_msec_t) sec * 1000 + msec;
     132#if (NGX_HAVE_CLOCK_MONOTONIC)
     133    if (enable_monotonic) {
     134        struct timespec monotime;
     135        clock_gettime(monotonic_clock, &monotime);
     136        ngx_current_msec = (ngx_msec_t) (monotime.tv_sec) * 1000 + (monotime.tv_nsec / 1000 / 1000);
     137    } else {
     138#endif
     139        ngx_current_msec = (ngx_msec_t) sec * 1000 + msec;
     140#if (NGX_HAVE_CLOCK_MONOTONIC)
     141    }
     142#endif
     143
    97144
    98145    tp = &cached_time[slot];
    99146
  • src/core/ngx_times.h

    diff --git a/src/core/ngx_times.h b/src/core/ngx_times.h
    index 94aedcdc..b9ffd9eb 100644
    a b typedef struct {  
    2121
    2222
    2323void ngx_time_init(void);
     24void ngx_time_enable_monotonic(ngx_flag_t enable);
    2425void ngx_time_update(void);
    2526void ngx_time_sigsafe_update(void);
    2627u_char *ngx_http_time(u_char *buf, time_t t);
  • src/event/ngx_event.c

    diff --git a/src/event/ngx_event.c b/src/event/ngx_event.c
    index 57af8132..5d9d6701 100644
    a b ngx_event_process_init(ngx_cycle_t *cycle)  
    601601    ngx_queue_init(&ngx_posted_accept_events);
    602602    ngx_queue_init(&ngx_posted_events);
    603603
     604    /*
     605     * Monotonic time must be enabled before the first timer is created. Otherwise
     606     * monotonic and realtime timestamps would be mixed in the rbtree.
     607     */
     608    ngx_time_enable_monotonic(ccf->monotonic_timers);
    604609    if (ngx_event_timer_init(cycle->log) == NGX_ERROR) {
    605610        return NGX_ERROR;
    606611    }