Ticket #189: monotonic_timers-3.patch

File monotonic_timers-3.patch, 6.3 KB (added by regnarg@…, 10 years ago)

Another version, which also checks the availability of each clock at runtime

  • 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
     320
     321if [ $ngx_found = yes ]; then
     322        # We have clock_gettime with CLOCK_MONOTONIC. Now we test for faster
     323        # variants (CLOCK_MONOTONIC_(FAST|COARSE)). ngx_feature_libs is set
     324        # by previous tests to include librt if necessary.
     325        ngx_feature="clock_gettime(CLOCK_MONOTONIC_FAST)"
     326        ngx_feature_name="NGX_HAVE_CLOCK_MONOTONIC_FAST"
     327        ngx_feature_test="struct timespec monotime; clock_gettime(CLOCK_MONOTONIC_FAST, &monotime);"
     328        . auto/feature
     329
     330        ngx_feature="clock_gettime(CLOCK_MONOTONIC_COARSE)"
     331        ngx_feature_name="NGX_HAVE_CLOCK_MONOTONIC_COARSE"
     332        ngx_feature_test="struct timespec monotime; clock_gettime(CLOCK_MONOTONIC_COARSE, &monotime);"
     333        . auto/feature
     334fi
    300335
    301336ngx_feature="SO_SETFIB"
    302337ngx_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  
    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{
     
    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_COARSE, &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)
     
    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 -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);
  • 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    }