mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-10 13:13:44 +01:00
Factor out timeval-related functions.
svn:r237
This commit is contained in:
parent
0c61bc3756
commit
33176c70a5
@ -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
|
||||
|
||||
|
58
src/common/util.c
Normal file
58
src/common/util.c
Normal file
@ -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;
|
||||
}
|
20
src/common/util.h
Normal file
20
src/common/util.h
Normal file
@ -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
|
@ -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 */
|
||||
|
@ -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 */
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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];
|
||||
|
@ -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 */
|
||||
|
Loading…
Reference in New Issue
Block a user