reduce the calls to gettime().

This commit is contained in:
Nikos Mavrogiannopoulos
2013-11-09 09:26:42 +01:00
parent 31b08654a5
commit ac6c0dd5c7
3 changed files with 17 additions and 14 deletions

View File

@@ -27,22 +27,19 @@
#include <stdio.h>
int _bandwidth_update(bandwidth_st* b, size_t bytes, size_t mtu)
int _bandwidth_update(bandwidth_st* b, size_t bytes, size_t mtu, struct timespec *now)
{
size_t sum;
struct timespec now;
ssize_t t, remain;
unsigned int diff;
size_t transferred_kb;
gettime(&now);
diff = timespec_sub_ms(&now, &b->count_start);
diff = timespec_sub_ms(now, &b->count_start);
if (diff >= COUNT_UPDATE_MS) {
transferred_kb = b->transferred_bytes / 1000;
transferred_kb = (transferred_kb*COUNT_UPDATE_MS)/diff;
memcpy(&b->count_start, &now, sizeof(now));
memcpy(&b->count_start, now, sizeof(*now));
remain = b->allowed_kb - transferred_kb;
t = b->allowed_kb_per_count + remain;

View File

@@ -21,6 +21,7 @@
#ifndef WORKER_BANDWIDTH_H
# define WORKER_BANDWIDTH_H
#include <gettime.h>
#include <time.h>
#include <unistd.h>
@@ -43,18 +44,18 @@ inline static void bandwidth_init(bandwidth_st* b, size_t kb_per_sec)
b->allowed_kb_per_count = (b->kb_per_sec*COUNT_UPDATE_MS)/1000;
}
int _bandwidth_update(bandwidth_st* b, size_t bytes, size_t mtu);
int _bandwidth_update(bandwidth_st* b, size_t bytes, size_t mtu, struct timespec* now);
/* returns true or false, depending on whether to send
* the bytes */
inline static
int bandwidth_update(bandwidth_st* b, size_t bytes, size_t mtu)
int bandwidth_update(bandwidth_st* b, size_t bytes, size_t mtu, struct timespec* now)
{
/* if bandwidth control is disabled */
if (b->kb_per_sec == 0)
return 1;
return _bandwidth_update(b, bytes, mtu);
return _bandwidth_update(b, bytes, mtu, now);
}

View File

@@ -40,6 +40,7 @@
#include <arpa/inet.h>
#include <system.h>
#include <time.h>
#include <gettime.h>
#include <common.h>
#include <worker-bandwidth.h>
@@ -764,6 +765,7 @@ char *p;
struct timeval tv;
unsigned tls_pending, dtls_pending = 0, i;
time_t udp_recv_time = 0, now;
struct timespec tnow;
unsigned mtu_overhead = 0;
socklen_t sl;
bandwidth_st b_tx;
@@ -1041,7 +1043,8 @@ bandwidth_st b_rx;
SEND_ERR(ret);
/* start dead peer detection */
ws->last_msg_tcp = ws->last_msg_udp = time(0);
gettime(&tnow);
ws->last_msg_tcp = ws->last_msg_udp = tnow.tv_sec;
if (ws->rx_per_sec == 0)
ws->rx_per_sec = ws->config->rx_per_sec;
@@ -1097,7 +1100,9 @@ bandwidth_st b_rx;
goto exit;
}
}
now = time(0);
gettime(&tnow);
now = tnow.tv_sec;
if (periodic_check(ws, mtu_overhead, now) < 0)
goto exit;
@@ -1119,7 +1124,7 @@ bandwidth_st b_rx;
}
/* only transmit if allowed */
if (bandwidth_update(&b_tx, l-1, ws->conn_mtu) != 0) {
if (bandwidth_update(&b_tx, l-1, ws->conn_mtu, &tnow) != 0) {
tls_retry = 0;
oclog(ws, LOG_DEBUG, "sending %d byte(s)\n", l);
if (ws->udp_state == UP_ACTIVE) {
@@ -1169,7 +1174,7 @@ bandwidth_st b_rx;
if (ret > 0) {
l = ret;
if (bandwidth_update(&b_rx, l-8, ws->conn_mtu) != 0) {
if (bandwidth_update(&b_rx, l-8, ws->conn_mtu, &tnow) != 0) {
ret = parse_cstp_data(ws, ws->buffer, l, now);
if (ret < 0) {
oclog(ws, LOG_INFO, "error parsing CSTP data");
@@ -1199,7 +1204,7 @@ bandwidth_st b_rx;
l = ret;
ws->udp_state = UP_ACTIVE;
if (bandwidth_update(&b_rx, l-1, ws->conn_mtu) != 0) {
if (bandwidth_update(&b_rx, l-1, ws->conn_mtu, &tnow) != 0) {
ret = parse_dtls_data(ws, ws->buffer, l, now);
if (ret < 0) {
oclog(ws, LOG_INFO, "error parsing CSTP data");