Add clock_micros()
This commit is contained in:
		@@ -19,7 +19,6 @@
 | 
			
		||||
#include <errno.h>
 | 
			
		||||
#include <openssl/err.h>
 | 
			
		||||
#include <arpa/inet.h>
 | 
			
		||||
#include <sys/time.h>
 | 
			
		||||
#include <netdb.h>
 | 
			
		||||
 | 
			
		||||
static SSL_CTX *proxy_ctx = NULL;
 | 
			
		||||
 
 | 
			
		||||
@@ -11,6 +11,7 @@
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <time.h>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
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;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
 
 | 
			
		||||
@@ -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));
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user