Ticket #189: monotonic_timers.patch

File monotonic_timers.patch, 6.1 KB (added by regnarg@…, 11 years ago)
  • auto/unix

    diff -r 78b4e10b4367 auto/unix
    a b  
    297297    fi
    298298fi
    299299
     300ngx_feature="clock_gettime(CLOCK_MONOTONIC)"
     301ngx_feature_name="NGX_HAVE_CLOCK_MONOTONIC"
     302ngx_feature_run=no
     303ngx_feature_incs="#include <time.h>"
     304ngx_feature_path=
     305ngx_feature_libs=
     306ngx_feature_test="struct timespec monotime; clock_gettime(CLOCK_MONOTONIC, &monotime);"
     307. auto/feature
     308
     309
     310if [ $ngx_found != yes ]; then
     311
     312    ngx_feature="clock_gettime(CLOCK_MONOTONIC) in librt"
     313    ngx_feature_libs="-lrt"
     314    . auto/feature
     315
     316    if [ $ngx_found = yes ]; then
     317        CORE_LIBS="$CORE_LIBS -lrt"
     318    fi
     319fi
    300320
    301321ngx_feature="SO_SETFIB"
    302322ngx_feature_name="NGX_HAVE_SETFIB"
  • contrib/vim/syntax/nginx.vim

    diff -r 78b4e10b4367 contrib/vim/syntax/nginx.vim
    a b  
    240240syn keyword ngxDirective min_delete_depth
    241241syn keyword ngxDirective modern_browser
    242242syn keyword ngxDirective modern_browser_value
     243syn keyword ngxDirective monotonic_timers
    243244syn keyword ngxDirective mp4
    244245syn keyword ngxDirective mp4_buffer_size
    245246syn keyword ngxDirective mp4_max_buffer_size
  • src/core/nginx.c

    diff -r 78b4e10b4367 src/core/nginx.c
    a b  
    133133      0,
    134134      NULL },
    135135
     136#if (NGX_HAVE_CLOCK_MONOTONIC)
     137    { ngx_string("monotonic_timers"),
     138      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_FLAG,
     139      ngx_conf_set_flag_slot,
     140      0,
     141      offsetof(ngx_core_conf_t, monotonic_timers),
     142      NULL },
     143#endif
     144
    136145      ngx_null_command
    137146};
    138147
     
    968977    ccf->daemon = NGX_CONF_UNSET;
    969978    ccf->master = NGX_CONF_UNSET;
    970979    ccf->timer_resolution = NGX_CONF_UNSET_MSEC;
     980    ccf->monotonic_timers = NGX_CONF_UNSET;
    971981
    972982    ccf->worker_processes = NGX_CONF_UNSET;
    973983    ccf->debug_points = NGX_CONF_UNSET;
     
    9961006    ngx_conf_init_value(ccf->daemon, 1);
    9971007    ngx_conf_init_value(ccf->master, 1);
    9981008    ngx_conf_init_msec_value(ccf->timer_resolution, 0);
     1009    ngx_conf_init_value(ccf->monotonic_timers, 0);
    9991010
    10001011    ngx_conf_init_value(ccf->worker_processes, 1);
    10011012    ngx_conf_init_value(ccf->debug_points, 0);
  • src/core/ngx_cycle.h

    diff -r 78b4e10b4367 src/core/ngx_cycle.h
    a b  
    103103
    104104     ngx_array_t              env;
    105105     char                   **environment;
     106
     107     ngx_flag_t               monotonic_timers;
    106108} ngx_core_conf_t;
    107109
    108110
  • src/core/ngx_times.c

    diff -r 78b4e10b4367 src/core/ngx_times.c
    a b  
    2424static ngx_atomic_t      ngx_time_lock;
    2525
    2626volatile ngx_msec_t      ngx_current_msec;
     27volatile ngx_msec_t      ngx_mono_msec;
    2728volatile ngx_time_t     *ngx_cached_time;
    2829volatile ngx_str_t       ngx_cached_err_log_time;
    2930volatile ngx_str_t       ngx_cached_http_time;
     
    5960static char  *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
    6061                           "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
    6162
     63#if (NGX_HAVE_CLOCK_MONOTONIC)
     64static ngx_flag_t enable_monotonic = 0;
     65#endif
     66
    6267void
    6368ngx_time_init(void)
    6469{
     
    7378    ngx_time_update();
    7479}
    7580
     81void
     82ngx_time_enable_monotonic(ngx_flag_t enable)
     83{
     84#if (NGX_HAVE_CLOCK_MONOTONIC)
     85    enable_monotonic = enable;
     86    ngx_time_update();
     87#endif
     88}
    7689
    7790void
    7891ngx_time_update(void)
     
    95108
    96109    ngx_current_msec = (ngx_msec_t) sec * 1000 + msec;
    97110
     111#if (NGX_HAVE_CLOCK_MONOTONIC)
     112    if (enable_monotonic) {
     113        struct timespec monotime;
     114        clock_gettime(CLOCK_MONOTONIC, &monotime);
     115        ngx_mono_msec = (ngx_msec_t) (monotime.tv_sec) * 1000 + (monotime.tv_nsec / 1000 / 1000);
     116    } else {
     117#endif
     118        ngx_mono_msec = ngx_current_msec;
     119#if (NGX_HAVE_CLOCK_MONOTONIC)
     120    }
     121#endif
     122
     123
    98124    tp = &cached_time[slot];
    99125
    100126    if (tp->sec == sec) {
  • src/core/ngx_times.h

    diff -r 78b4e10b4367 src/core/ngx_times.h
    a b  
    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);
     
    4748 * used in event timers
    4849 */
    4950extern volatile ngx_msec_t  ngx_current_msec;
     51extern volatile ngx_msec_t  ngx_mono_msec;
    5052
    5153
    5254#endif /* _NGX_TIMES_H_INCLUDED_ */
  • src/event/ngx_event.c

    diff -r 78b4e10b4367 src/event/ngx_event.c
    a b  
    602602    ngx_queue_init(&ngx_posted_accept_events);
    603603    ngx_queue_init(&ngx_posted_events);
    604604
     605    /*
     606     * Monotonic time must be enabled before the first timer is created. Otherwise
     607     * monotonic and realtime timestamps would be mixed in the rbtree.
     608     */
     609    ngx_time_enable_monotonic(ccf->monotonic_timers);
    605610    if (ngx_event_timer_init(cycle->log) == NGX_ERROR) {
    606611        return NGX_ERROR;
    607612    }
  • src/event/ngx_event_timer.c

    diff -r 78b4e10b4367 src/event/ngx_event_timer.c
    a b  
    4444
    4545    node = ngx_rbtree_min(root, sentinel);
    4646
    47     timer = (ngx_msec_int_t) (node->key - ngx_current_msec);
     47    timer = (ngx_msec_int_t) (node->key - ngx_mono_msec);
    4848
    4949    return (ngx_msec_t) (timer > 0 ? timer : 0);
    5050}
     
    6969
    7070        /* node->key > ngx_current_time */
    7171
    72         if ((ngx_msec_int_t) (node->key - ngx_current_msec) > 0) {
     72        if ((ngx_msec_int_t) (node->key - ngx_mono_msec) > 0) {
    7373            return;
    7474        }
    7575
  • src/event/ngx_event_timer.h

    diff -r 78b4e10b4367 src/event/ngx_event_timer.h
    a b  
    5353    ngx_msec_t      key;
    5454    ngx_msec_int_t  diff;
    5555
    56     key = ngx_current_msec + timer;
     56    key = ngx_mono_msec + timer;
    5757
    5858    if (ev->timer_set) {
    5959