summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_thread_time.c
blob: 916aa5646a2dd090d65fb15b0e0bc010f61d7c90 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468

/*
 * Copyright (C) Igor Sysoev
 * Copyright (C) NGINX, Inc.
 */

#include <nxt_main.h>


/*
 * Each thread keeps several time representations in its thread local
 * storage:
 *   the monotonic time in nanoseconds since unspecified point in the past,
 *   the real time in seconds and nanoseconds since the Epoch,
 *   the local time and GMT time structs,
 *   and various user-defined text representations of local and GMT times.
 *
 * The monotonic time is used mainly by engine timers and is updated after
 * a kernel operation which can block for unpredictable duration like event
 * polling.  Besides getting the monotonic time is generally faster than
 * getting the real time, so the monotonic time is also used for milestones
 * to update cached real time seconds and, if debug log enabled, milliseconds.
 * As a result, the cached real time is updated at most one time per second
 * or millisecond respectively.  If there is a signal event support or in
 * multi-threaded mode, then the cached real time and local time structs
 * are updated only on demand.  In single-threaded mode without the signal
 * event support the cached real and local time are updated synchronously
 * with the monotonic time update.  GMT time structs and text representations
 * are always updated only on demand.
 */


#if (NXT_THREADS)
static void nxt_time_thread(void *data);
static void nxt_thread_time_shared(nxt_monotonic_time_t *now);

static nxt_bool_t                     nxt_use_shared_time = 0;
static volatile nxt_monotonic_time_t  nxt_shared_time;
#endif

static void nxt_thread_realtime_update(nxt_thread_t *thr,
    nxt_monotonic_time_t *now);
static u_char *nxt_thread_time_string_no_cache(nxt_thread_t *thr,
    nxt_time_string_t *ts, u_char *buf);
static nxt_atomic_uint_t nxt_thread_time_string_slot(nxt_time_string_t *ts);
static nxt_time_string_cache_t *nxt_thread_time_string_cache(nxt_thread_t *thr,
    nxt_atomic_uint_t slot);


static nxt_atomic_int_t               nxt_gmtoff;


void
nxt_thread_time_update(nxt_thread_t *thr)
{
#if (NXT_THREADS)

    if (nxt_use_shared_time) {
        nxt_thread_time_shared(&thr->time.now);

    } else {
        nxt_monotonic_time(&thr->time.now);
    }

#else

    nxt_monotonic_time(&thr->time.now);

    if (thr->time.signal >= 0) {
        nxt_time_t  s;

        /*
         * Synchronous real time update:
         * single-threaded mode without signal event support.
         */

        s = nxt_thread_time(thr);

        if (thr->time.signal == 0 && thr->time.last_localtime != s) {
            /* Synchronous local time update in non-signal context. */

            nxt_localtime(s, &thr->time.localtime);
            thr->time.last_localtime = s;

            nxt_gmtoff = nxt_timezone(&thr->time.localtime);
        }
    }

#endif
}


void
nxt_thread_time_free(nxt_thread_t *thr)
{
    nxt_uint_t               i;
    nxt_time_string_cache_t  *tsc;

    tsc = thr->time.strings;

    if (tsc) {
        thr->time.no_cache = 1;

        for (i = 0; i < thr->time.nstrings; i++) {
            nxt_free(tsc[i].string.start);
        }

        nxt_free(tsc);
        thr->time.strings = NULL;
    }
}


#if (NXT_THREADS)

void
nxt_time_thread_start(nxt_msec_t interval)
{
    nxt_thread_link_t    *link;
    nxt_thread_handle_t  handle;

    link = nxt_zalloc(sizeof(nxt_thread_link_t));

    if (nxt_fast_path(link != NULL)) {
        link->start = nxt_time_thread;
        link->data = (void *) (uintptr_t) interval;

        (void) nxt_thread_create(&handle, link);
    }
}


static void
nxt_time_thread(void *data)
{
    nxt_nsec_t            interval, rest;
    nxt_thread_t          *thr;
    nxt_monotonic_time_t  now;

    interval = (uintptr_t) data;
    interval *= 1000000;

    thr = nxt_thread();
    /*
     * The time thread is never preempted by asynchronous signals, since
     * the signals are processed synchronously by dedicated thread.
     */
    thr->time.signal = -1;

    nxt_log_debug(thr->log, "time thread");

    nxt_memzero(&now, sizeof(nxt_monotonic_time_t));

    nxt_monotonic_time(&now);
    nxt_thread_realtime_update(thr, &now);

    nxt_shared_time = now;
    nxt_use_shared_time = 1;

    for ( ;; ) {
        rest = 1000000000 - now.realtime.nsec;

        nxt_nanosleep(nxt_min(interval, rest));

        nxt_monotonic_time(&now);
        nxt_thread_realtime_update(thr, &now);

        nxt_shared_time = now;

#if 0
        thr->time.now = now;
        nxt_log_debug(thr->log, "time thread");
#endif

#if 0
        if (nxt_exiting) {
            nxt_use_shared_time = 0;
            return;
        }
#endif
    }
}


static void
nxt_thread_time_shared(nxt_monotonic_time_t *now)
{
    nxt_uint_t  n;
    nxt_time_t  t;
    nxt_nsec_t  m, u;

    /* Lock-free thread time update. */

    for ( ;; ) {
        *now = nxt_shared_time;

        t = nxt_shared_time.realtime.sec;
        n = nxt_shared_time.realtime.nsec;
        m = nxt_shared_time.monotonic;
        u = nxt_shared_time.update;

        if (now->realtime.sec == t && now->realtime.nsec == n
            && now->monotonic == m && now->update == u)
        {
            return;
        }
    }
}

#endif


nxt_time_t
nxt_thread_time(nxt_thread_t *thr)
{
    nxt_thread_realtime_update(thr, &thr->time.now);

    return thr->time.now.realtime.sec;
}


nxt_realtime_t *
nxt_thread_realtime(nxt_thread_t *thr)
{
    nxt_thread_realtime_update(thr, &thr->time.now);

    return &thr->time.now.realtime;
}


static void
nxt_thread_realtime_update(nxt_thread_t *thr, nxt_monotonic_time_t *now)
{
    nxt_nsec_t  delta;

#if (NXT_DEBUG)

    if (nxt_slow_path(thr->log->level == NXT_LOG_DEBUG || nxt_debug)) {

        if (now->monotonic >= now->update) {
            nxt_realtime(&now->realtime);

            delta = 1000000 - now->realtime.nsec % 1000000;
            now->update = now->monotonic + delta;
        }

        return;
    }

#endif

    if (now->monotonic >= now->update) {
        nxt_realtime(&now->realtime);

        delta = 1000000000 - now->realtime.nsec;
        now->update = now->monotonic + delta;
    }
}


u_char *
nxt_thread_time_string(nxt_thread_t *thr, nxt_time_string_t *ts, u_char *buf)
{
    u_char                   *p;
    struct tm                *tm;
    nxt_time_t               s;
    nxt_bool_t               update;
    nxt_atomic_uint_t        slot;
    nxt_time_string_cache_t  *tsc;

    if (nxt_slow_path(thr == NULL || thr->time.no_cache)) {
        return nxt_thread_time_string_no_cache(thr, ts, buf);
    }

    slot = nxt_thread_time_string_slot(ts);

    tsc = nxt_thread_time_string_cache(thr, slot);
    if (tsc == NULL) {
        return buf;
    }

    if (thr->time.signal < 0) {
        /*
         * Lazy real time update:
         * signal event support or multi-threaded mode.
         */
        nxt_thread_realtime_update(thr, &thr->time.now);
    }

    s = thr->time.now.realtime.sec;

    update = (s != tsc->last);

#if (NXT_DEBUG)

    if (ts->msec == NXT_THREAD_TIME_MSEC
        && (nxt_slow_path(thr->log->level == NXT_LOG_DEBUG || nxt_debug)))
    {
        nxt_msec_t  ms;

        ms = thr->time.now.realtime.nsec / 1000000;
        update |= (ms != tsc->last_msec);
        tsc->last_msec = ms;
    }

#endif

    if (nxt_slow_path(update)) {

        if (ts->timezone == NXT_THREAD_TIME_LOCAL) {

            tm = &thr->time.localtime;

            if (nxt_slow_path(s != thr->time.last_localtime)) {

                if (thr->time.signal < 0) {
                    /*
                     * Lazy local time update:
                     * signal event support or multi-threaded mode.
                     */
                    nxt_localtime(s, &thr->time.localtime);
                    thr->time.last_localtime = s;

                } else {
                    /*
                     * "thr->time.signal >= 0" means that a thread may be
                     * interrupted by a signal handler.  Since localtime()
                     * cannot be safely called in a signal context, the
                     * thread's thr->time.localtime must be updated regularly
                     * by nxt_thread_time_update() in non-signal context.
                     * Stale timestamp means that nxt_thread_time_string()
                     * is being called in a signal context, so here is
                     * Async-Signal-Safe localtime() emulation using the
                     * latest cached GMT offset.
                     *
                     * The timestamp is not set here intentionally to update
                     * thr->time.localtime later in non-signal context.  The
                     * real previously cached thr->localtime is used because
                     * Linux and Solaris strftime() depend on tm.tm_isdst
                     * and tm.tm_gmtoff fields.
                     */
                    nxt_gmtime(s + nxt_timezone(tm), tm);
                }
            }

        } else {
            tm = &thr->time.gmtime;

            if (nxt_slow_path(s != thr->time.last_gmtime)) {
                nxt_gmtime(s, tm);
                thr->time.last_gmtime = s;
            }

        }

        p = tsc->string.start;

        if (nxt_slow_path(p == NULL)) {

            thr->time.no_cache = 1;
            p = nxt_zalloc(ts->size);
            thr->time.no_cache = 0;

            if (p == NULL) {
                return buf;
            }

            tsc->string.start = p;
        }

        p = ts->handler(p, &thr->time.now.realtime, tm, ts->size, ts->format);

        tsc->string.length = p - tsc->string.start;

        if (nxt_slow_path(tsc->string.length == 0)) {
            return buf;
        }

        tsc->last = s;
    }

    return nxt_cpymem(buf, tsc->string.start, tsc->string.length);
}


static u_char *
nxt_thread_time_string_no_cache(nxt_thread_t *thr, nxt_time_string_t *ts,
    u_char *buf)
{
    struct tm       tm;
    nxt_realtime_t  now;

    nxt_realtime(&now);

    if (ts->timezone == NXT_THREAD_TIME_LOCAL) {

        if (thr == NULL || thr->time.signal <= 0) {
            /* Non-signal context */
            nxt_localtime(now.sec, &tm);

        } else {
            nxt_gmtime(now.sec + nxt_gmtoff, &tm);
        }

    } else {
        nxt_gmtime(now.sec, &tm);
    }

    return ts->handler(buf, &now, &tm, ts->size, ts->format);
}


static nxt_atomic_uint_t
nxt_thread_time_string_slot(nxt_time_string_t *ts)
{
    static nxt_atomic_t  slot;

    while (nxt_slow_path((nxt_atomic_int_t) ts->slot < 0)) {
        /*
         * Atomic allocation of a slot number.
         * -1 means an uninitialized slot,
         * -2 is the initializing lock to assure the single value for the slot.
         */
        if (nxt_atomic_cmp_set(&ts->slot, -1, -2)) {
            ts->slot = nxt_atomic_fetch_add(&slot, 1);

            /* No "break" here since it adds only dispensable "jmp". */
        }
    }

    return (nxt_atomic_uint_t) ts->slot;
}


static nxt_time_string_cache_t *
nxt_thread_time_string_cache(nxt_thread_t *thr, nxt_atomic_uint_t slot)
{
    size_t                   size;
    nxt_atomic_uint_t        i, nstrings;
    nxt_time_string_cache_t  *tsc;

    if (nxt_fast_path(slot < thr->time.nstrings)) {
        tsc = &thr->time.strings[slot];
        nxt_prefetch(tsc->string.start);
        return tsc;
    }

    nstrings = slot + 1;
    size = nstrings * sizeof(nxt_time_string_cache_t);

    thr->time.no_cache = 1;
    tsc = nxt_realloc(thr->time.strings, size);
    thr->time.no_cache = 0;

    if (tsc == NULL) {
        return NULL;
    }

    for (i = thr->time.nstrings; i < nstrings; i++) {
        tsc[i].last = -1;
        tsc[i].string.start = NULL;
    }

    thr->time.strings = tsc;
    thr->time.nstrings = nstrings;

    return &tsc[slot];
}