clientlog: randomize alignment of log timestamps

This commit is contained in:
Miroslav Lichvar 2016-12-15 11:06:14 +01:00
parent c6a38f5069
commit 5059019535

View file

@ -86,6 +86,10 @@ static unsigned int max_slots;
#define TS_FRAC 4 #define TS_FRAC 4
#define INVALID_TS 0 #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 */ /* Request rates are saved in the record as 8-bit scaled log2 values */
#define RATE_SCALE 4 #define RATE_SCALE 4
#define MIN_RATE (-14 * RATE_SCALE) #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_cmd_drops;
static uint32_t total_record_drops; static uint32_t total_record_drops;
#define NSEC_PER_SEC 1000000000U
/* ================================================== */ /* ================================================== */
static int expand_hashtable(void); static int expand_hashtable(void);
@ -333,6 +339,9 @@ CLG_Initialise(void)
records = NULL; records = NULL;
expand_hashtable(); 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; 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 */ /* This is fast and accurate enough */
return sec << TS_FRAC | (140740U * (nsec >> 15)) >> (32 - TS_FRAC); return sec << TS_FRAC | (140740U * (nsec >> 15)) >> (32 - TS_FRAC);
} }