diff --git a/src/lib/proxy.c b/src/lib/proxy.c index b1a3522..ad383aa 100644 --- a/src/lib/proxy.c +++ b/src/lib/proxy.c @@ -19,7 +19,6 @@ #include #include #include -#include #include static SSL_CTX *proxy_ctx = NULL; diff --git a/src/lib/utils.c b/src/lib/utils.c index 63e0d5c..f708c9f 100644 --- a/src/lib/utils.c +++ b/src/lib/utils.c @@ -11,6 +11,7 @@ #include #include #include +#include static const char base64_encode_table[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; @@ -203,3 +204,9 @@ int base64_encode(void *data, unsigned long data_len, char *output, unsigned lon return 0; } + +long clock_micros(void) { + struct timespec time; + clock_gettime(CLOCK_MONOTONIC, &time); + return time.tv_sec * 10000000 + time.tv_nsec / 1000; +} diff --git a/src/lib/utils.h b/src/lib/utils.h index 608a60a..504682d 100644 --- a/src/lib/utils.h +++ b/src/lib/utils.h @@ -37,4 +37,6 @@ int str_trim_lws(char **start, char **end); int base64_encode(void *data, unsigned long data_len, char *output, unsigned long *output_len); +long clock_micros(void); + #endif //SESIMOS_UTILS_H diff --git a/src/worker/func.h b/src/worker/func.h index a113bd0..7523422 100644 --- a/src/worker/func.h +++ b/src/worker/func.h @@ -23,7 +23,7 @@ typedef struct { char req_host[256], err_msg[256]; char log_prefix[512]; char _c_addr[INET6_ADDRSTRLEN + 1], _s_addr[INET6_ADDRSTRLEN + 1]; - struct timespec begin, end; + long cnx_s, cnx_e, req_s, res_ts, req_e; http_req req; http_res res; http_uri uri; diff --git a/src/worker/request_handler.c b/src/worker/request_handler.c index 00aa6c9..efa7588 100644 --- a/src/worker/request_handler.c +++ b/src/worker/request_handler.c @@ -71,7 +71,7 @@ static int request_handler(client_ctx_t *ctx) { status->origin = NONE; status->ws_key = NULL; - clock_gettime(CLOCK_MONOTONIC, &ctx->begin); + ctx->req_s = clock_micros(); // FIXME async poll ret = sock_poll_read(&client, NULL, NULL, 1, NULL, NULL, CLIENT_TIMEOUT * 1000); @@ -85,7 +85,8 @@ static int request_handler(client_ctx_t *ctx) { res->status = http_get_status(408); return 0; } - clock_gettime(CLOCK_MONOTONIC, &ctx->begin); + + ctx->req_s = clock_micros(); http_req *req = &ctx->req; ret = http_receive_request(client, req); @@ -298,12 +299,11 @@ int respond(client_ctx_t *ctx) { } http_send_response(client, res); - clock_gettime(CLOCK_MONOTONIC, &ctx->end); + ctx->res_ts = clock_micros(); const char *location = http_get_header_field(&res->hdr, "Location"); - unsigned long micros = (ctx->end.tv_nsec - ctx->begin.tv_nsec) / 1000 + (ctx->end.tv_sec - ctx->begin.tv_sec) * 1000000; info("%s%s%03i %s%s%s (%s)%s", http_get_status_color(res->status), ctx->use_proxy ? "-> " : "", res->status->code, res->status->msg, location != NULL ? " -> " : "", location != NULL ? location : "", - format_duration(micros, buf0), CLR_STR); + format_duration(ctx->res_ts - ctx->req_s, buf0), CLR_STR); // TODO access/error log file @@ -361,9 +361,8 @@ int request_complete(client_ctx_t *ctx) { //} char buf[32]; - clock_gettime(CLOCK_MONOTONIC, &ctx->end); - long micros = (ctx->end.tv_nsec - ctx->begin.tv_nsec) / 1000 + (ctx->end.tv_sec - ctx->begin.tv_sec) * 1000000; - info("Transfer complete: %s", format_duration(micros, buf)); + ctx->req_e = clock_micros(); + info("Transfer complete: %s", format_duration(ctx->req_e - ctx->req_s, buf)); uri_free(&ctx->uri); http_free_req(&ctx->req); diff --git a/src/worker/tcp_acceptor.c b/src/worker/tcp_acceptor.c index 4e9d526..baecf35 100644 --- a/src/worker/tcp_acceptor.c +++ b/src/worker/tcp_acceptor.c @@ -59,7 +59,7 @@ static int tcp_acceptor(client_ctx_t *ctx) { char buf[1024]; sock *client = &ctx->socket; - clock_gettime(CLOCK_MONOTONIC, &ctx->begin); + ctx->cnx_s = clock_micros(); if (config.dns_server[0] != 0) { sprintf(buf, "dig @%s +short +time=1 -x %s", config.dns_server, ctx->socket.addr); diff --git a/src/worker/tcp_closer.c b/src/worker/tcp_closer.c index 4c6d6e0..69e0d77 100644 --- a/src/worker/tcp_closer.c +++ b/src/worker/tcp_closer.c @@ -17,10 +17,9 @@ void tcp_closer_func(client_ctx_t *ctx) { sock_close(&ctx->socket); + ctx->cnx_e = clock_micros(); char buf[32]; - clock_gettime(CLOCK_MONOTONIC, &ctx->end); - unsigned long micros = (ctx->end.tv_nsec - ctx->begin.tv_nsec) / 1000 + (ctx->end.tv_sec - ctx->begin.tv_sec) * 1000000; - info("Connection closed (%s)", format_duration(micros, buf)); + info("Connection closed (%s)", format_duration(ctx->cnx_e - ctx->cnx_s, buf)); memset(ctx, 0, sizeof(*ctx)); }