From 50590195351c11b218ad7edc50102a2e9eec7767 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Thu, 15 Dec 2016 11:06:14 +0100 Subject: [PATCH] clientlog: randomize alignment of log timestamps --- clientlog.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/clientlog.c b/clientlog.c index 1057326..0ace6d6 100644 --- a/clientlog.c +++ b/clientlog.c @@ -86,6 +86,10 @@ static unsigned int max_slots; #define TS_FRAC 4 #define INVALID_TS 0 +/* Static offset included in conversion to the fixed-point timestamps to + randomise their alignment */ +static uint32_t ts_offset; + /* Request rates are saved in the record as 8-bit scaled log2 values */ #define RATE_SCALE 4 #define MIN_RATE (-14 * RATE_SCALE) @@ -134,6 +138,8 @@ static uint32_t total_ntp_drops; static uint32_t total_cmd_drops; static uint32_t total_record_drops; +#define NSEC_PER_SEC 1000000000U + /* ================================================== */ static int expand_hashtable(void); @@ -333,6 +339,9 @@ CLG_Initialise(void) records = NULL; expand_hashtable(); + + UTI_GetRandomBytes(&ts_offset, sizeof (ts_offset)); + ts_offset %= NSEC_PER_SEC / (1U << TS_FRAC); } /* ================================================== */ @@ -353,6 +362,12 @@ get_ts_from_timespec(struct timespec *ts) { uint32_t sec = ts->tv_sec, nsec = ts->tv_nsec; + nsec += ts_offset; + if (nsec >= NSEC_PER_SEC) { + nsec -= NSEC_PER_SEC; + sec++; + } + /* This is fast and accurate enough */ return sec << TS_FRAC | (140740U * (nsec >> 15)) >> (32 - TS_FRAC); }