| 12034 | |
| 12035 | namespace { |
| 12036 | auto estimateClockResolution() -> uint64_t { |
| 12037 | uint64_t sum = 0; |
| 12038 | static const uint64_t iterations = 1000000; |
| 12039 | |
| 12040 | auto startTime = getCurrentNanosecondsSinceEpoch(); |
| 12041 | |
| 12042 | for( std::size_t i = 0; i < iterations; ++i ) { |
| 12043 | |
| 12044 | uint64_t ticks; |
| 12045 | uint64_t baseTicks = getCurrentNanosecondsSinceEpoch(); |
| 12046 | do { |
| 12047 | ticks = getCurrentNanosecondsSinceEpoch(); |
| 12048 | } while( ticks == baseTicks ); |
| 12049 | |
| 12050 | auto delta = ticks - baseTicks; |
| 12051 | sum += delta; |
| 12052 | |
| 12053 | // If we have been calibrating for over 3 seconds -- the clock |
| 12054 | // is terrible and we should move on. |
| 12055 | // TBD: How to signal that the measured resolution is probably wrong? |
| 12056 | if (ticks > startTime + 3 * nanosecondsInSecond) { |
| 12057 | return sum / ( i + 1u ); |
| 12058 | } |
| 12059 | } |
| 12060 | |
| 12061 | // We're just taking the mean, here. To do better we could take the std. dev and exclude outliers |
| 12062 | // - and potentially do more iterations if there's a high variance. |
| 12063 | return sum/iterations; |
| 12064 | } |
| 12065 | } |
| 12066 | auto getEstimatedClockResolution() -> uint64_t { |
| 12067 | static auto s_resolution = estimateClockResolution(); |
no test coverage detected