diff options
-rw-r--r-- | src/common/Makefile.am | 4 | ||||
-rw-r--r-- | src/common/util.c | 58 | ||||
-rw-r--r-- | src/common/util.h | 20 | ||||
-rw-r--r-- | src/or/circuit.c | 16 | ||||
-rw-r--r-- | src/or/command.c | 23 | ||||
-rw-r--r-- | src/or/connection.c | 41 | ||||
-rw-r--r-- | src/or/main.c | 6 | ||||
-rw-r--r-- | src/or/or.h | 1 |
8 files changed, 98 insertions, 71 deletions
diff --git a/src/common/Makefile.am b/src/common/Makefile.am index f2783b4ed8..a799bc466a 100644 --- a/src/common/Makefile.am +++ b/src/common/Makefile.am @@ -3,7 +3,7 @@ noinst_LIBRARIES = libor.a #CFLAGS = -Wall -Wpointer-arith -O2 -libor_a_SOURCES = log.c crypto.c fakepoll.c +libor_a_SOURCES = log.c crypto.c fakepoll.c util.c -noinst_HEADERS = log.h ss.h version.h crypto.h fakepoll.h test.h +noinst_HEADERS = log.h ss.h version.h crypto.h fakepoll.h test.h util.h diff --git a/src/common/util.c b/src/common/util.c new file mode 100644 index 0000000000..c448fbe894 --- /dev/null +++ b/src/common/util.c @@ -0,0 +1,58 @@ +/* Copyright 2003 Roger Dingledine */ +/* See LICENSE for licensing information */ +/* $Id$ */ + +#include <stdlib.h> +#include "util.h" +#include "log.h" + +void +my_gettimeofday(struct timeval *timeval) +{ + if (gettimeofday(timeval, NULL)) { + log(LOG_ERR, "my_gettimeofday: gettimeofday failed."); + /* If gettimeofday dies, we have either given a bad timezone (we didn't), + or segfaulted.*/ + exit(1); + } + return; +} + +long +tv_udiff(struct timeval *start, struct timeval *end) +{ + long secdiff = end->tv_sec - start->tv_sec; + if (secdiff+1 > LONG_MAX/1000000) { + log(LOG_NOTICE, "tv_udiff(): comparing times too far apart."); + return LONG_MAX; + } + if (end->tv_usec < start->tv_usec) { + end->tv_sec--; + end->tv_usec += 1000000L; + } + return secdiff*1000000L + (end->tv_usec - start->tv_usec); +} + +int tv_cmp(struct timeval *a, struct timeval *b) { + if (a->tv_sec > b->tv_sec) + return 1; + if (a->tv_sec < b->tv_sec) + return -1; + if (a->tv_usec > b->tv_usec) + return 1; + if (a->tv_usec < b->tv_usec) + return -1; + return 0; +} + +void tv_add(struct timeval *a, struct timeval *b) { + a->tv_usec += b->tv_usec; + a->tv_sec += b->tv_sec + (a->tv_usec / 1000000); + a->tv_usec %= 1000000; +} + +void tv_addms(struct timeval *a, long ms) { + a->tv_usec += (ms * 1000) % 1000000; + a->tv_sec += ((ms * 1000) / 1000000) + (a->tv_usec / 1000000); + a->tv_usec %= 1000000; +} diff --git a/src/common/util.h b/src/common/util.h new file mode 100644 index 0000000000..bc880d24e2 --- /dev/null +++ b/src/common/util.h @@ -0,0 +1,20 @@ +/* Copyright 2003 Roger Dingledine */ +/* See LICENSE for licensing information */ +/* $Id$ */ + +#ifndef __UTIL_H +#define __UTIL_H + +#include <sys/time.h> + +/* Same as gettimeofday, but no need to check exit value. */ +void my_gettimeofday(struct timeval *timeval); +/* Returns the number of microseconds between start and end. Requires that + * end >= start, and that the number of microseconds < LONG_MAX. */ +long tv_udiff(struct timeval *start, struct timeval *end); + +void tv_addms(struct timeval *a, long ms); +void tv_add(struct timeval *a, struct timeval *b); +int tv_cmp(struct timeval *a, struct timeval *b); + +#endif diff --git a/src/or/circuit.c b/src/or/circuit.c index 17dcc0662e..cc9f51b9b1 100644 --- a/src/or/circuit.c +++ b/src/or/circuit.c @@ -53,8 +53,7 @@ circuit_t *circuit_new(aci_t p_aci, connection_t *p_conn) { circuit_t *circ; struct timeval now; - if(gettimeofday(&now,NULL) < 0) - return NULL; + my_gettimeofday(&now); circ = (circuit_t *)malloc(sizeof(circuit_t)); if(!circ) @@ -157,7 +156,7 @@ int circuit_init(circuit_t *circ, int aci_type, onion_layer_t *layer) { log(LOG_DEBUG,"circuit_init(): aci_type = %u.",aci_type); - gettimeofday(&start,NULL); + my_gettimeofday(&start); circ->n_aci = get_unique_aci_by_addr_port(circ->n_addr, circ->n_port, aci_type); if(!circ->n_aci) { @@ -165,19 +164,12 @@ int circuit_init(circuit_t *circ, int aci_type, onion_layer_t *layer) { return -1; } - gettimeofday(&end,NULL); + my_gettimeofday(&end); - if(end.tv_usec < start.tv_usec) { - end.tv_sec--; - end.tv_usec += 1000000; - } - time_passed = ((end.tv_sec - start.tv_sec)*1000000) + (end.tv_usec - start.tv_usec); - if(time_passed > 1000) { /* more than 1ms */ + if (tv_udiff(&start, &end) > 1000) {/* more than 1ms */ log(LOG_NOTICE,"circuit_init(): get_unique_aci just took %d us!",time_passed); } - - log(LOG_DEBUG,"circuit_init(): Chosen ACI %u.",circ->n_aci); /* keys */ diff --git a/src/or/command.c b/src/or/command.c index 878d26e0a0..2ec250000b 100644 --- a/src/or/command.c +++ b/src/or/command.c @@ -14,24 +14,14 @@ void command_time_process_cell(cell_t *cell, connection_t *conn, *num += 1; - if(gettimeofday(&start,NULL) < 0) { - log(LOG_ERR,"command_time_process_cell(): gettimeofday failed."); - return; - } + my_gettimeofday(&start); (*func)(cell, conn); - if(gettimeofday(&end,NULL) < 0) { - log(LOG_ERR,"command_time_process_cell(): gettimeofday failed."); - return; - } + my_gettimeofday(&end); + time_passed = tv_udiff(&start, &end) ; - if(end.tv_usec < start.tv_usec) { - end.tv_sec--; - end.tv_usec += 1000000; - } - time_passed = ((end.tv_sec - start.tv_sec)*1000000) + (end.tv_usec - start.tv_usec); - if(time_passed > 5000) { /* more than 5ms */ + if (time_passed > 5000) { /* more than 5ms */ log(LOG_INFO,"command_time_process_cell(): That call just took %d ms.",time_passed/1000); } *time += time_passed; @@ -43,10 +33,7 @@ void command_process_cell(cell_t *cell, connection_t *conn) { static long current_second = 0; /* from previous calls to gettimeofday */ struct timeval now; - if(gettimeofday(&now,NULL) < 0) { - log(LOG_ERR,"command_process_cell(): gettimeofday failed."); - return; - } + my_gettimeofday(&now); if(now.tv_sec > current_second) { /* the second has rolled over */ /* print stats */ diff --git a/src/or/connection.c b/src/or/connection.c index 20b29dfa2c..c15eb00100 100644 --- a/src/or/connection.c +++ b/src/or/connection.c @@ -61,31 +61,6 @@ char *conn_state_to_string[][15] = { /********* END VARIABLES ************/ -/**************************************************************/ - -int tv_cmp(struct timeval *a, struct timeval *b) { - if (a->tv_sec > b->tv_sec) - return 1; - if (a->tv_sec < b->tv_sec) - return -1; - if (a->tv_usec > b->tv_usec) - return 1; - if (a->tv_usec < b->tv_usec) - return -1; - return 0; -} - -void tv_add(struct timeval *a, struct timeval *b) { - a->tv_usec += b->tv_usec; - a->tv_sec += b->tv_sec + (a->tv_usec / 1000000); - a->tv_usec %= 1000000; -} - -void tv_addms(struct timeval *a, long ms) { - a->tv_usec += (ms * 1000) % 1000000; - a->tv_sec += ((ms * 1000) / 1000000) + (a->tv_usec / 1000000); - a->tv_usec %= 1000000; -} /**************************************************************/ @@ -93,8 +68,7 @@ connection_t *connection_new(int type) { connection_t *conn; struct timeval now; - if(gettimeofday(&now,NULL) < 0) - return NULL; + my_gettimeofday(&now); conn = (connection_t *)malloc(sizeof(connection_t)); if(!conn) @@ -328,8 +302,8 @@ int connection_read_to_buf(connection_t *conn) { assert(conn->receiver_bucket < 0); } - if(gettimeofday(&now,NULL) < 0) - return -1; + my_gettimeofday(&now); + conn->timestamp_lastread = now.tv_sec; read_result = read_to_buf(conn->s, conn->receiver_bucket, &conn->inbuf, &conn->inbuflen, @@ -395,8 +369,7 @@ int connection_decompress_to_buf(char *string, int len, connection_t *conn, if (n < 0) return -1; - if(gettimeofday(&now,NULL) < 0) - return -1; + my_gettimeofday(&now,NULL); if(!n) return 0; @@ -430,8 +403,7 @@ int connection_flush_buf(connection_t *conn) { int connection_write_to_buf(char *string, int len, connection_t *conn) { struct timeval now; - if(gettimeofday(&now,NULL) < 0) - return -1; + my_gettimeofday(&now); if(!len) return 0; @@ -585,8 +557,7 @@ void connection_init_timeval(connection_t *conn) { assert(conn); - if(gettimeofday(&conn->send_timeval,NULL) < 0) - return; + my_gettimeofday(&conn->send_timeval); connection_increment_send_timeval(conn); } diff --git a/src/or/main.c b/src/or/main.c index 023836d4bf..e0341061eb 100644 --- a/src/or/main.c +++ b/src/or/main.c @@ -307,8 +307,7 @@ int prepare_for_poll(int *timeout) { cell_t cell; circuit_t *circ; - if(gettimeofday(&now,NULL) < 0) - return -1; + my_gettimeofday(&now); if(now.tv_sec > current_second) { /* the second has rolled over. check more stuff. */ @@ -535,8 +534,7 @@ void dumpstats(void) { /* dump stats to stdout */ extern char *conn_state_to_string[][15]; printf("Dumping stats:\n"); - if(gettimeofday(&now,NULL) < 0) - return ; + my_gettimeofday(&now); for(i=0;i<nfds;i++) { conn = connection_array[i]; diff --git a/src/or/or.h b/src/or/or.h index b0be6b91ea..5e84ec3b83 100644 --- a/src/or/or.h +++ b/src/or/or.h @@ -46,6 +46,7 @@ #include "../common/log.h" #include "../common/ss.h" #include "../common/version.h" +#include "../common/util.h" #define MAXCONNECTIONS 1000 /* upper bound on max connections. can be lowered by config file */ |