diff --git a/src/http.c b/src/http.c index e0cbce5..050766e 100644 --- a/src/http.c +++ b/src/http.c @@ -22,7 +22,6 @@ #include #include #include -#include #include #include @@ -188,7 +187,6 @@ static void _http_callback_ping(struct evhttp_request *request, void *v_server) static void _http_callback_snapshot(struct evhttp_request *request, void *v_exposed) { struct exposed_t *exposed = (struct exposed_t *)v_exposed; struct evbuffer *buf; - struct timespec x_timestamp_spec; char x_timestamp_buf[64]; PROCESS_HEAD_REQUEST; @@ -196,17 +194,11 @@ static void _http_callback_snapshot(struct evhttp_request *request, void *v_expo assert((buf = evbuffer_new())); assert(!evbuffer_add(buf, (const void *)exposed->picture.data, exposed->picture.size)); - assert(!clock_gettime(CLOCK_REALTIME, &x_timestamp_spec)); - sprintf( - x_timestamp_buf, "%u.%06u", - (unsigned)x_timestamp_spec.tv_sec, - (unsigned)(x_timestamp_spec.tv_nsec / 1000) // TODO: round? - ); - ADD_HEADER("Access-Control-Allow-Origin:", "*"); ADD_HEADER("Cache-Control", "no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0"); ADD_HEADER("Pragma", "no-cache"); ADD_HEADER("Expires", "Mon, 3 Jan 2000 12:34:56 GMT"); + sprintf(x_timestamp_buf, "%.06Lf", now_real_ms()); ADD_HEADER("X-Timestamp", x_timestamp_buf); ADD_HEADER("Content-Type", "image/jpeg"); @@ -264,10 +256,8 @@ static void _http_callback_stream(struct evhttp_request *request, void *v_server static void _http_callback_stream_write(struct bufferevent *buf_event, void *v_client) { struct stream_client_t *client = (struct stream_client_t *)v_client; struct evbuffer *buf; - struct timespec x_timestamp_spec; assert((buf = evbuffer_new())); - assert(!clock_gettime(CLOCK_REALTIME, &x_timestamp_spec)); if (client->need_initial) { assert(evbuffer_add_printf(buf, @@ -287,11 +277,10 @@ static void _http_callback_stream_write(struct bufferevent *buf_event, void *v_c assert(evbuffer_add_printf(buf, "Content-Type: image/jpeg" RN "Content-Length: %lu" RN - "X-Timestamp: %u.%06u" RN + "X-Timestamp: %.06Lf" RN RN, client->server->run->exposed->picture.size * sizeof(*client->server->run->exposed->picture.data), - (unsigned)x_timestamp_spec.tv_sec, - (unsigned)(x_timestamp_spec.tv_nsec / 1000) // TODO: round? + now_real_ms() )); assert(!evbuffer_add(buf, (void *)client->server->run->exposed->picture.data, diff --git a/src/logging.h b/src/logging.h index fb7618b..59ab28d 100644 --- a/src/logging.h +++ b/src/logging.h @@ -71,7 +71,7 @@ pthread_mutex_t log_mutex; #define LOG_ERROR(_x_msg, ...) { \ LOGGING_LOCK; \ - printf("-- ERROR [%.03Lf tid=%ld] -- " _x_msg "\n", now_ms_ld(), syscall(SYS_gettid), ##__VA_ARGS__); \ + printf("-- ERROR [%.03Lf tid=%ld] -- " _x_msg "\n", now_monotonic_ms(), syscall(SYS_gettid), ##__VA_ARGS__); \ LOGGING_UNLOCK; \ } @@ -79,24 +79,24 @@ pthread_mutex_t log_mutex; char _buf[1024] = ""; \ strerror_r(errno, _buf, 1024); \ LOGGING_LOCK; \ - printf("-- ERROR [%.03Lf tid=%ld] -- " _x_msg ": %s\n", now_ms_ld(), syscall(SYS_gettid), ##__VA_ARGS__, _buf); \ + printf("-- ERROR [%.03Lf tid=%ld] -- " _x_msg ": %s\n", now_monotonic_ms(), syscall(SYS_gettid), ##__VA_ARGS__, _buf); \ LOGGING_UNLOCK; \ } #define LOG_INFO(_x_msg, ...) { \ LOGGING_LOCK; \ - printf("-- INFO [%.03Lf tid=%ld] -- " _x_msg "\n", now_ms_ld(), syscall(SYS_gettid), ##__VA_ARGS__); \ + printf("-- INFO [%.03Lf tid=%ld] -- " _x_msg "\n", now_monotonic_ms(), syscall(SYS_gettid), ##__VA_ARGS__); \ LOGGING_UNLOCK; \ } #define LOG_INFO_NOLOCK(_x_msg, ...) { \ - printf("-- INFO [%.03Lf tid=%ld] -- " _x_msg "\n", now_ms_ld(), syscall(SYS_gettid), ##__VA_ARGS__); \ + printf("-- INFO [%.03Lf tid=%ld] -- " _x_msg "\n", now_monotonic_ms(), syscall(SYS_gettid), ##__VA_ARGS__); \ } #define LOG_PERF(_x_msg, ...) { \ if (log_level >= LOG_LEVEL_PERF) { \ LOGGING_LOCK; \ - printf("-- PERF [%.03Lf tid=%ld] -- " _x_msg "\n", now_ms_ld(), syscall(SYS_gettid), ##__VA_ARGS__); \ + printf("-- PERF [%.03Lf tid=%ld] -- " _x_msg "\n", now_monotonic_ms(), syscall(SYS_gettid), ##__VA_ARGS__); \ LOGGING_UNLOCK; \ } \ } @@ -104,7 +104,7 @@ pthread_mutex_t log_mutex; #define LOG_VERBOSE(_x_msg, ...) { \ if (log_level >= LOG_LEVEL_VERBOSE) { \ LOGGING_LOCK; \ - printf("-- VERB [%.03Lf tid=%ld] -- " _x_msg "\n", now_ms_ld(), syscall(SYS_gettid), ##__VA_ARGS__); \ + printf("-- VERB [%.03Lf tid=%ld] -- " _x_msg "\n", now_monotonic_ms(), syscall(SYS_gettid), ##__VA_ARGS__); \ LOGGING_UNLOCK; \ } \ } @@ -112,7 +112,7 @@ pthread_mutex_t log_mutex; #define LOG_DEBUG(_x_msg, ...) { \ if (log_level >= LOG_LEVEL_DEBUG) { \ LOGGING_LOCK; \ - printf("-- DEBUG [%.03Lf tid=%ld] -- " _x_msg "\n", now_ms_ld(), syscall(SYS_gettid), ##__VA_ARGS__); \ + printf("-- DEBUG [%.03Lf tid=%ld] -- " _x_msg "\n", now_monotonic_ms(), syscall(SYS_gettid), ##__VA_ARGS__); \ LOGGING_UNLOCK; \ } \ } diff --git a/src/omx/formatters.h b/src/omx/formatters.h index e015103..3bcd873 100644 --- a/src/omx/formatters.h +++ b/src/omx/formatters.h @@ -35,7 +35,7 @@ #define LOG_OMX_ERROR(_error, _msg, ...) { \ LOGGING_LOCK; \ - printf("-- ERROR [%.03Lf tid=%ld] -- " _msg ": %s\n", now_ms_ld(), \ + printf("-- ERROR [%.03Lf tid=%ld] -- " _msg ": %s\n", now_monotonic_ms(), \ syscall(SYS_gettid), ##__VA_ARGS__, omx_error_to_string(_error)); \ LOGGING_UNLOCK; \ } diff --git a/src/stream.c b/src/stream.c index 155a1a6..0a8d5b0 100644 --- a/src/stream.c +++ b/src/stream.c @@ -187,7 +187,7 @@ void stream_loop(struct stream_t *stream) { } { - long double now = now_ms_ld(); + long double now = now_monotonic_ms(); if (now < grab_after) { fluency_passed += 1; @@ -407,7 +407,7 @@ static void *_stream_worker_thread(void *v_ctx) { long double start_time; long double last_comp_time; - start_time = now_ms_ld(); + start_time = now_monotonic_ms(); LOG_DEBUG("Worker %u compressing JPEG from buffer %d ...", ctx->number, ctx->buf_index); @@ -419,7 +419,7 @@ static void *_stream_worker_thread(void *v_ctx) { *ctx->job_start_time = start_time; *ctx->has_job = false; - last_comp_time = now_ms_ld() - start_time; + last_comp_time = now_monotonic_ms() - start_time; A_PTHREAD_M_LOCK(ctx->last_comp_time_mutex); *ctx->last_comp_time = last_comp_time; diff --git a/src/tools.h b/src/tools.h index ed2107c..7a08738 100644 --- a/src/tools.h +++ b/src/tools.h @@ -61,10 +61,10 @@ INLINE unsigned max_u(unsigned a, unsigned b) { return (a > b ? a : b); } -INLINE void now_ms(time_t *sec, long *msec) { +INLINE void now_ms(clockid_t clk_id, time_t *sec, long *msec) { struct timespec spec; - assert(!clock_gettime(CLOCK_MONOTONIC_RAW, &spec)); + assert(!clock_gettime(clk_id, &spec)); *sec = spec.tv_sec; *msec = round(spec.tv_nsec / 1.0e6); @@ -74,10 +74,18 @@ INLINE void now_ms(time_t *sec, long *msec) { } } -INLINE long double now_ms_ld(void) { +INLINE long double now_monotonic_ms(void) { time_t sec; long msec; - now_ms(&sec, &msec); + now_ms(CLOCK_MONOTONIC_RAW, &sec, &msec); + return (long double)sec + ((long double)msec) / 1000; +} + +INLINE long double now_real_ms(void) { + time_t sec; + long msec; + + now_ms(CLOCK_REALTIME, &sec, &msec); return (long double)sec + ((long double)msec) / 1000; }