read monotonic clock with clock_gettime() instead of gettimeofday()

There have been test failures on the osmo-pcu Jenkins builders due
to apparent clock drift. Switch relevant code from gettimeofday()
to clock_gettime() with CLOCK_MONOTONIC to prevent time from going
backwards and causing negative time deltas in calculations.

Change-Id: I775d85d0d3ac740330879e588bdab6fce7f0b46c
Related: OS#3225
This commit is contained in:
Stefan Sperling 2018-05-25 15:12:30 +02:00 committed by Harald Welte
parent 143b2da4f8
commit f0f7df1b87
3 changed files with 22 additions and 21 deletions

View File

@ -71,22 +71,22 @@ int gprs_rlcmac_meas_rep(Packet_Measurement_Report_t *pmr)
/* RSSI values received from MS */
int gprs_rlcmac_rssi(struct gprs_rlcmac_tbf *tbf, int8_t rssi)
{
struct timeval now_tv, *rssi_tv = &tbf->meas.rssi_tv;
struct timespec now_tv, *rssi_tv = &tbf->meas.rssi_tv;
uint32_t elapsed;
tbf->meas.rssi_sum += rssi;
tbf->meas.rssi_num++;
gettimeofday(&now_tv, NULL);
osmo_clock_gettime(CLOCK_MONOTONIC, &now_tv);
elapsed = ((now_tv.tv_sec - rssi_tv->tv_sec) << 7)
+ ((now_tv.tv_usec - rssi_tv->tv_usec) << 7) / 1000000;
+ (((now_tv.tv_nsec - rssi_tv->tv_nsec)/1000) << 7) / 1000000;
if (elapsed < 128)
return 0;
gprs_rlcmac_rssi_rep(tbf);
/* reset rssi values and timestamp */
memcpy(rssi_tv, &now_tv, sizeof(struct timeval));
memcpy(rssi_tv, &now_tv, sizeof(*rssi_tv));
tbf->meas.rssi_sum = 0;
tbf->meas.rssi_num = 0;
@ -115,7 +115,7 @@ int gprs_rlcmac_rssi_rep(struct gprs_rlcmac_tbf *tbf)
int gprs_rlcmac_received_lost(struct gprs_rlcmac_dl_tbf *tbf, uint16_t received,
uint16_t lost)
{
struct timeval now_tv, *loss_tv = &tbf->m_bw.dl_loss_tv;
struct timespec now_tv, *loss_tv = &tbf->m_bw.dl_loss_tv;
uint32_t elapsed;
uint16_t sum = received + lost;
@ -129,16 +129,16 @@ int gprs_rlcmac_received_lost(struct gprs_rlcmac_dl_tbf *tbf, uint16_t received,
tbf->m_bw.dl_loss_received += received;
tbf->m_bw.dl_loss_lost += lost;
gettimeofday(&now_tv, NULL);
osmo_clock_gettime(CLOCK_MONOTONIC, &now_tv);
elapsed = ((now_tv.tv_sec - loss_tv->tv_sec) << 7)
+ ((now_tv.tv_usec - loss_tv->tv_usec) << 7) / 1000000;
+ (((now_tv.tv_nsec - loss_tv->tv_nsec)/1000) << 7) / 1000000;
if (elapsed < 128)
return 0;
gprs_rlcmac_lost_rep(tbf);
/* reset lost values and timestamp */
memcpy(loss_tv, &now_tv, sizeof(struct timeval));
memcpy(loss_tv, &now_tv, sizeof(*loss_tv));
tbf->m_bw.dl_loss_received = 0;
tbf->m_bw.dl_loss_lost = 0;
@ -168,14 +168,14 @@ int gprs_rlcmac_lost_rep(struct gprs_rlcmac_dl_tbf *tbf)
int gprs_rlcmac_dl_bw(struct gprs_rlcmac_dl_tbf *tbf, uint16_t octets)
{
struct timeval now_tv, *bw_tv = &tbf->m_bw.dl_bw_tv;
struct timespec now_tv, *bw_tv = &tbf->m_bw.dl_bw_tv;
uint32_t elapsed;
tbf->m_bw.dl_bw_octets += octets;
gettimeofday(&now_tv, NULL);
osmo_clock_gettime(CLOCK_MONOTONIC, &now_tv);
elapsed = ((now_tv.tv_sec - bw_tv->tv_sec) << 7)
+ ((now_tv.tv_usec - bw_tv->tv_usec) << 7) / 1000000;
+ (((now_tv.tv_nsec - bw_tv->tv_nsec)/1000) << 7) / 1000000;
if (elapsed < 128)
return 0;
@ -186,7 +186,7 @@ int gprs_rlcmac_dl_bw(struct gprs_rlcmac_dl_tbf *tbf, uint16_t octets)
tbf->m_bw.dl_bw_octets / elapsed);
/* reset bandwidth values timestamp */
memcpy(bw_tv, &now_tv, sizeof(struct timeval));
memcpy(bw_tv, &now_tv, sizeof(*bw_tv));
tbf->m_bw.dl_bw_octets = 0;
return 0;

View File

@ -40,6 +40,7 @@ extern "C" {
#include <osmocom/core/talloc.h>
#include <osmocom/core/stats.h>
#include <osmocom/core/logging.h>
#include <osmocom/core/timer_compat.h>
#include <osmocom/core/bitvec.h>
#include <osmocom/core/rate_ctr.h>
#include <osmocom/gsm/protocol/gsm_04_08.h>
@ -182,7 +183,7 @@ gprs_rlcmac_tbf::Meas::Meas() :
rssi_sum(0),
rssi_num(0)
{
timerclear(&rssi_tv);
timespecclear(&rssi_tv);
}
gprs_rlcmac_tbf::gprs_rlcmac_tbf(BTS *bts_, gprs_rlcmac_tbf_direction dir) :
@ -913,7 +914,7 @@ static int setup_tbf(struct gprs_rlcmac_tbf *tbf, GprsMs *ms, int8_t use_trx, ui
}
/* set timestamp */
gettimeofday(&tbf->meas.rssi_tv, NULL);
osmo_clock_gettime(CLOCK_MONOTONIC, &tbf->meas.rssi_tv);
tbf->set_ms(ms);
@ -1024,8 +1025,8 @@ gprs_rlcmac_dl_tbf::BandWidth::BandWidth() :
dl_loss_lost(0),
dl_loss_received(0)
{
timerclear(&dl_bw_tv);
timerclear(&dl_loss_tv);
timespecclear(&dl_bw_tv);
timespecclear(&dl_loss_tv);
}
gprs_rlcmac_dl_tbf::gprs_rlcmac_dl_tbf(BTS *bts_) :
@ -1114,8 +1115,8 @@ struct gprs_rlcmac_dl_tbf *tbf_alloc_dl_tbf(struct gprs_rlcmac_bts *bts, GprsMs
tbf->m_last_dl_poll_fn = -1;
tbf->m_last_dl_drained_fn = -1;
gettimeofday(&tbf->m_bw.dl_bw_tv, NULL);
gettimeofday(&tbf->m_bw.dl_loss_tv, NULL);
osmo_clock_gettime(CLOCK_MONOTONIC, &tbf->m_bw.dl_bw_tv);
osmo_clock_gettime(CLOCK_MONOTONIC, &tbf->m_bw.dl_loss_tv);
return tbf;
}

View File

@ -318,7 +318,7 @@ struct gprs_rlcmac_tbf {
unsigned int num_fT_exp; /* number of consecutive fT expirations */
struct Meas {
struct timeval rssi_tv; /* timestamp for rssi calculation */
struct timespec rssi_tv; /* timestamp for rssi calculation */
int32_t rssi_sum; /* sum of rssi values */
int rssi_num; /* number of rssi values added since rssi_tv */
@ -665,11 +665,11 @@ struct gprs_rlcmac_dl_tbf : public gprs_rlcmac_tbf {
int32_t m_last_dl_drained_fn;
struct BandWidth {
struct timeval dl_bw_tv; /* timestamp for dl bw calculation */
struct timespec dl_bw_tv; /* timestamp for dl bw calculation */
uint32_t dl_bw_octets; /* number of octets since bw_tv */
uint32_t dl_throughput; /* throughput to be displayed in stats */
struct timeval dl_loss_tv; /* timestamp for loss calculation */
struct timespec dl_loss_tv; /* timestamp for loss calculation */
uint16_t dl_loss_lost; /* sum of lost packets */
uint16_t dl_loss_received; /* sum of received packets */