|
| 1 | +/************************************************************************* |
| 2 | +// Copyright IBM Corp. 2023 |
| 3 | +// |
| 4 | +// Licensed under the Apache License 2.0 (the "License"). You may not use |
| 5 | +// this file except in compliance with the License. You can obtain a copy |
| 6 | +// in the file LICENSE in the source distribution. |
| 7 | +*************************************************************************/ |
| 8 | + |
| 9 | +/************************************************************************* |
| 10 | +// Description: High resolution timing code |
| 11 | +// Leverages the event counter code we use for RNG |
| 12 | +// seeding. |
| 13 | +// |
| 14 | +*************************************************************************/ |
| 15 | + |
| 16 | + |
| 17 | +#if defined(_WIN32) |
| 18 | +# include <windows.h> |
| 19 | +#else |
| 20 | +# include <stdio.h> |
| 21 | +# include <sys/time.h> |
| 22 | +# include <string.h> |
| 23 | +# include <stdlib.h> |
| 24 | +#endif |
| 25 | +#include "TRNG/timer_entropy.h" |
| 26 | +#include "DELTA/delta_t.h" |
| 27 | + |
| 28 | +extern ICC_UINT64 RdCTR_raw(); |
| 29 | +extern int Shift(); |
| 30 | + |
| 31 | + |
| 32 | +#if defined(_WIN32) |
| 33 | + |
| 34 | +struct timezone { |
| 35 | + long tz; |
| 36 | +}; |
| 37 | +/*! @brief Approximate gettimeofday for windows |
| 38 | + All this does is grab time and populate values |
| 39 | + to generate one-off data |
| 40 | + it's NOT accurate |
| 41 | + \Platf Windows only |
| 42 | + @param tv Pointer to a Unixy "struct timeval" |
| 43 | + @param tz Pointer to "Unixy" "struct timezone" (Unused) |
| 44 | + @return 0 |
| 45 | +*/ |
| 46 | +static int gettimeofday(struct timeval *tv, struct timezone *tz) |
| 47 | +{ |
| 48 | + /* Contains a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC).*/ |
| 49 | + FILETIME ft; |
| 50 | + unsigned long long ull; |
| 51 | + |
| 52 | + if( NULL != tv) { |
| 53 | + GetSystemTimeAsFileTime(&ft); |
| 54 | + ull = ft.dwHighDateTime; |
| 55 | + ull <<= 32; |
| 56 | + ull |= ft.dwLowDateTime; |
| 57 | + ull /= 10; /* usec */ |
| 58 | + tv->tv_usec = ull % 1000000; |
| 59 | + tv->tv_sec = ull / 1000000; |
| 60 | + } |
| 61 | + return 0; |
| 62 | +} |
| 63 | + |
| 64 | +#endif |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | +static double span = 0.0; |
| 70 | +static int done = 0; |
| 71 | + |
| 72 | + |
| 73 | +/*! @brief return the counter span in counts, |
| 74 | + The limit before the counter overflows |
| 75 | + @return The counter span in counts |
| 76 | +*/ |
| 77 | +unsigned long Delta_spanC() |
| 78 | +{ |
| 79 | + unsigned long rv = (unsigned int)(-1); |
| 80 | + /* !FIXME !, work out what this is on various OS's */ |
| 81 | + return rv; |
| 82 | + |
| 83 | +} |
| 84 | + |
| 85 | + |
| 86 | +unsigned long Delta_T(int mode, unsigned long *d) |
| 87 | +{ |
| 88 | + unsigned long rv = 0; |
| 89 | + unsigned long t; |
| 90 | + if(mode == 1) { |
| 91 | + *d = RdCTR_raw(); |
| 92 | + } else { |
| 93 | + t = RdCTR_raw(); |
| 94 | + if(t > *d) { |
| 95 | + rv = t - *d; |
| 96 | + } else { |
| 97 | + /* We need to know when this rolls over #!#! as this is HW/word size |
| 98 | + dependent |
| 99 | + This is good for x86_64, fixups are in Delta_spanC() |
| 100 | + */ |
| 101 | + rv = (Delta_spanC() - (*d)) + t; |
| 102 | + } |
| 103 | + } |
| 104 | + return rv; |
| 105 | +} |
| 106 | + |
| 107 | +/*! @brief get the estimate for the base event counter resolution |
| 108 | + @return the estimated counter resolution (in counts) |
| 109 | + @note quite often the lower bits are stuck-at and are unusable |
| 110 | + */ |
| 111 | +unsigned long Delta_res() |
| 112 | +{ |
| 113 | + unsigned long j; |
| 114 | + j = (unsigned long)Shift(); |
| 115 | + return (unsigned long) (1 << j); |
| 116 | +} |
| 117 | + |
| 118 | +/*! @brief return the APPOXIMATE time span of the counter in nS |
| 119 | + @return The approximate time span of the counter |
| 120 | + @note This will run Delta2Time() if it hasn't already been run |
| 121 | +*/ |
| 122 | +double Delta_spanT() |
| 123 | +{ |
| 124 | + if(!done) { |
| 125 | + Delta2Time(0); |
| 126 | + } |
| 127 | + return span; |
| 128 | +} |
| 129 | +/*! @brief |
| 130 | + Calculates delta times from struct timeval's, |
| 131 | + @param x the after timeval |
| 132 | + @param y the before timeval |
| 133 | + @return Time difference in nS |
| 134 | + @note struct timeval only has uS resolution |
| 135 | + |
| 136 | +*/ |
| 137 | +static double tv_sub ( struct timeval *x, struct timeval *y) |
| 138 | +{ |
| 139 | + |
| 140 | + long ds,dus,d; |
| 141 | + double result = 0.0; |
| 142 | + |
| 143 | + |
| 144 | + ds = x->tv_sec - y->tv_sec; |
| 145 | + dus = x->tv_usec - y->tv_usec; |
| 146 | + /* Normalize to uS */ |
| 147 | + d = ds * 1000000 + dus; |
| 148 | + /* Convert to nS */ |
| 149 | + result = d *1000.0; |
| 150 | + |
| 151 | + return result; |
| 152 | +} |
| 153 | + |
| 154 | + |
| 155 | +double Delta2Time(int recalc) |
| 156 | +{ |
| 157 | + |
| 158 | + static double rv = 1.0; |
| 159 | + struct timeval tv_now, tv_then; |
| 160 | + unsigned long c_now = 0L; |
| 161 | + unsigned long delta = 0L; |
| 162 | + memset(&tv_then,0,sizeof(struct timeval)); |
| 163 | + |
| 164 | + memset(&tv_now,0,sizeof(struct timeval)); |
| 165 | + |
| 166 | + if((!done) | recalc) { |
| 167 | + Delta_T(1,&c_now); |
| 168 | + gettimeofday(&tv_then,NULL); |
| 169 | + do { |
| 170 | + delta = Delta_T(0,&c_now); |
| 171 | + } while(delta < (1<<28)); |
| 172 | + |
| 173 | + gettimeofday(&tv_now,NULL); |
| 174 | + |
| 175 | + /* Now do the calcs, we have something near the max count available, and the elapsed time |
| 176 | + */ |
| 177 | + rv = tv_sub(&tv_now,&tv_then); |
| 178 | + /* Calculate the longest usable run time (span) |
| 179 | + Needs an OS/wordlength specific switch here |
| 180 | + */ |
| 181 | + span = rv * ((double)((unsigned int)(-1))/delta); |
| 182 | + |
| 183 | + rv = (rv/(double)delta); |
| 184 | + |
| 185 | + done = 1; |
| 186 | + } |
| 187 | + return rv; |
| 188 | +} |
0 commit comments