Add clock_micros()

This commit is contained in:
2022-12-31 00:53:36 +01:00
parent 9ad1ecf1da
commit 96f3225f51
7 changed files with 20 additions and 14 deletions

View File

@ -19,7 +19,6 @@
#include <errno.h> #include <errno.h>
#include <openssl/err.h> #include <openssl/err.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <sys/time.h>
#include <netdb.h> #include <netdb.h>
static SSL_CTX *proxy_ctx = NULL; static SSL_CTX *proxy_ctx = NULL;

View File

@ -11,6 +11,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h>
static const char base64_encode_table[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 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; return 0;
} }
long clock_micros(void) {
struct timespec time;
clock_gettime(CLOCK_MONOTONIC, &time);
return time.tv_sec * 10000000 + time.tv_nsec / 1000;
}

View File

@ -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); int base64_encode(void *data, unsigned long data_len, char *output, unsigned long *output_len);
long clock_micros(void);
#endif //SESIMOS_UTILS_H #endif //SESIMOS_UTILS_H

View File

@ -23,7 +23,7 @@ typedef struct {
char req_host[256], err_msg[256]; char req_host[256], err_msg[256];
char log_prefix[512]; char log_prefix[512];
char _c_addr[INET6_ADDRSTRLEN + 1], _s_addr[INET6_ADDRSTRLEN + 1]; 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_req req;
http_res res; http_res res;
http_uri uri; http_uri uri;

View File

@ -71,7 +71,7 @@ static int request_handler(client_ctx_t *ctx) {
status->origin = NONE; status->origin = NONE;
status->ws_key = NULL; status->ws_key = NULL;
clock_gettime(CLOCK_MONOTONIC, &ctx->begin); ctx->req_s = clock_micros();
// FIXME async poll // FIXME async poll
ret = sock_poll_read(&client, NULL, NULL, 1, NULL, NULL, CLIENT_TIMEOUT * 1000); 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); res->status = http_get_status(408);
return 0; return 0;
} }
clock_gettime(CLOCK_MONOTONIC, &ctx->begin);
ctx->req_s = clock_micros();
http_req *req = &ctx->req; http_req *req = &ctx->req;
ret = http_receive_request(client, req); ret = http_receive_request(client, req);
@ -298,12 +299,11 @@ int respond(client_ctx_t *ctx) {
} }
http_send_response(client, res); 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"); 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, 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 : "", 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 // TODO access/error log file
@ -361,9 +361,8 @@ int request_complete(client_ctx_t *ctx) {
//} //}
char buf[32]; char buf[32];
clock_gettime(CLOCK_MONOTONIC, &ctx->end); ctx->req_e = clock_micros();
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(ctx->req_e - ctx->req_s, buf));
info("Transfer complete: %s", format_duration(micros, buf));
uri_free(&ctx->uri); uri_free(&ctx->uri);
http_free_req(&ctx->req); http_free_req(&ctx->req);

View File

@ -59,7 +59,7 @@ static int tcp_acceptor(client_ctx_t *ctx) {
char buf[1024]; char buf[1024];
sock *client = &ctx->socket; sock *client = &ctx->socket;
clock_gettime(CLOCK_MONOTONIC, &ctx->begin); ctx->cnx_s = clock_micros();
if (config.dns_server[0] != 0) { if (config.dns_server[0] != 0) {
sprintf(buf, "dig @%s +short +time=1 -x %s", config.dns_server, ctx->socket.addr); sprintf(buf, "dig @%s +short +time=1 -x %s", config.dns_server, ctx->socket.addr);

View File

@ -17,10 +17,9 @@ void tcp_closer_func(client_ctx_t *ctx) {
sock_close(&ctx->socket); sock_close(&ctx->socket);
ctx->cnx_e = clock_micros();
char buf[32]; char buf[32];
clock_gettime(CLOCK_MONOTONIC, &ctx->end); info("Connection closed (%s)", format_duration(ctx->cnx_e - ctx->cnx_s, buf));
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));
memset(ctx, 0, sizeof(*ctx)); memset(ctx, 0, sizeof(*ctx));
} }