refactoring

This commit is contained in:
Devaev Maxim
2018-09-30 04:29:34 +03:00
parent 05b18fb68d
commit ec2a704ca0
5 changed files with 26 additions and 29 deletions

View File

@@ -22,7 +22,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#include <time.h>
#include <assert.h> #include <assert.h>
#include <event2/event.h> #include <event2/event.h>
@@ -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) { static void _http_callback_snapshot(struct evhttp_request *request, void *v_exposed) {
struct exposed_t *exposed = (struct exposed_t *)v_exposed; struct exposed_t *exposed = (struct exposed_t *)v_exposed;
struct evbuffer *buf; struct evbuffer *buf;
struct timespec x_timestamp_spec;
char x_timestamp_buf[64]; char x_timestamp_buf[64];
PROCESS_HEAD_REQUEST; PROCESS_HEAD_REQUEST;
@@ -196,17 +194,11 @@ static void _http_callback_snapshot(struct evhttp_request *request, void *v_expo
assert((buf = evbuffer_new())); assert((buf = evbuffer_new()));
assert(!evbuffer_add(buf, (const void *)exposed->picture.data, exposed->picture.size)); 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("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("Cache-Control", "no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0");
ADD_HEADER("Pragma", "no-cache"); ADD_HEADER("Pragma", "no-cache");
ADD_HEADER("Expires", "Mon, 3 Jan 2000 12:34:56 GMT"); 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("X-Timestamp", x_timestamp_buf);
ADD_HEADER("Content-Type", "image/jpeg"); 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) { 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 stream_client_t *client = (struct stream_client_t *)v_client;
struct evbuffer *buf; struct evbuffer *buf;
struct timespec x_timestamp_spec;
assert((buf = evbuffer_new())); assert((buf = evbuffer_new()));
assert(!clock_gettime(CLOCK_REALTIME, &x_timestamp_spec));
if (client->need_initial) { if (client->need_initial) {
assert(evbuffer_add_printf(buf, 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, assert(evbuffer_add_printf(buf,
"Content-Type: image/jpeg" RN "Content-Type: image/jpeg" RN
"Content-Length: %lu" RN "Content-Length: %lu" RN
"X-Timestamp: %u.%06u" RN "X-Timestamp: %.06Lf" RN
RN, RN,
client->server->run->exposed->picture.size * sizeof(*client->server->run->exposed->picture.data), client->server->run->exposed->picture.size * sizeof(*client->server->run->exposed->picture.data),
(unsigned)x_timestamp_spec.tv_sec, now_real_ms()
(unsigned)(x_timestamp_spec.tv_nsec / 1000) // TODO: round?
)); ));
assert(!evbuffer_add(buf, assert(!evbuffer_add(buf,
(void *)client->server->run->exposed->picture.data, (void *)client->server->run->exposed->picture.data,

View File

@@ -71,7 +71,7 @@ pthread_mutex_t log_mutex;
#define LOG_ERROR(_x_msg, ...) { \ #define LOG_ERROR(_x_msg, ...) { \
LOGGING_LOCK; \ 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; \ LOGGING_UNLOCK; \
} }
@@ -79,24 +79,24 @@ pthread_mutex_t log_mutex;
char _buf[1024] = ""; \ char _buf[1024] = ""; \
strerror_r(errno, _buf, 1024); \ strerror_r(errno, _buf, 1024); \
LOGGING_LOCK; \ 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; \ LOGGING_UNLOCK; \
} }
#define LOG_INFO(_x_msg, ...) { \ #define LOG_INFO(_x_msg, ...) { \
LOGGING_LOCK; \ 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; \ LOGGING_UNLOCK; \
} }
#define LOG_INFO_NOLOCK(_x_msg, ...) { \ #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, ...) { \ #define LOG_PERF(_x_msg, ...) { \
if (log_level >= LOG_LEVEL_PERF) { \ if (log_level >= LOG_LEVEL_PERF) { \
LOGGING_LOCK; \ 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; \ LOGGING_UNLOCK; \
} \ } \
} }
@@ -104,7 +104,7 @@ pthread_mutex_t log_mutex;
#define LOG_VERBOSE(_x_msg, ...) { \ #define LOG_VERBOSE(_x_msg, ...) { \
if (log_level >= LOG_LEVEL_VERBOSE) { \ if (log_level >= LOG_LEVEL_VERBOSE) { \
LOGGING_LOCK; \ 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; \ LOGGING_UNLOCK; \
} \ } \
} }
@@ -112,7 +112,7 @@ pthread_mutex_t log_mutex;
#define LOG_DEBUG(_x_msg, ...) { \ #define LOG_DEBUG(_x_msg, ...) { \
if (log_level >= LOG_LEVEL_DEBUG) { \ if (log_level >= LOG_LEVEL_DEBUG) { \
LOGGING_LOCK; \ 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; \ LOGGING_UNLOCK; \
} \ } \
} }

View File

@@ -35,7 +35,7 @@
#define LOG_OMX_ERROR(_error, _msg, ...) { \ #define LOG_OMX_ERROR(_error, _msg, ...) { \
LOGGING_LOCK; \ 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)); \ syscall(SYS_gettid), ##__VA_ARGS__, omx_error_to_string(_error)); \
LOGGING_UNLOCK; \ LOGGING_UNLOCK; \
} }

View File

@@ -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) { if (now < grab_after) {
fluency_passed += 1; fluency_passed += 1;
@@ -407,7 +407,7 @@ static void *_stream_worker_thread(void *v_ctx) {
long double start_time; long double start_time;
long double last_comp_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); 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->job_start_time = start_time;
*ctx->has_job = false; *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); A_PTHREAD_M_LOCK(ctx->last_comp_time_mutex);
*ctx->last_comp_time = last_comp_time; *ctx->last_comp_time = last_comp_time;

View File

@@ -61,10 +61,10 @@ INLINE unsigned max_u(unsigned a, unsigned b) {
return (a > b ? a : 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; struct timespec spec;
assert(!clock_gettime(CLOCK_MONOTONIC_RAW, &spec)); assert(!clock_gettime(clk_id, &spec));
*sec = spec.tv_sec; *sec = spec.tv_sec;
*msec = round(spec.tv_nsec / 1.0e6); *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; time_t sec;
long msec; 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; return (long double)sec + ((long double)msec) / 1000;
} }