| 1 |
// Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 2 |
// |
| 3 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 4 |
// software; you can redistribute it and/or modify it under the |
| 5 |
// terms of the GNU General Public License as published by the |
| 6 |
// Free Software Foundation; either version 3, or (at your option) |
| 7 |
// any later version. |
| 8 |
|
| 9 |
// This library is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
|
| 14 |
// You should have received a copy of the GNU General Public License along |
| 15 |
// with this library; see the file COPYING3. If not see |
| 16 |
// <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
// { dg-options "-O3" } |
| 19 |
// { dg-do run { target c++11 } } |
| 20 |
// { dg-additional-options "-pthread" { target pthread } } |
| 21 |
// { dg-require-gthreads "" } |
| 22 |
// { dg-skip-if "no high resolution timer support" { hppa*-*-linux* } } |
| 23 |
|
| 24 |
#include <future> |
| 25 |
#include <chrono> |
| 26 |
#include <iostream> |
| 27 |
#include <testsuite_hooks.h> |
| 28 |
|
| 29 |
int iterations = 200; |
| 30 |
|
| 31 |
using namespace std; |
| 32 |
|
| 33 |
template<typename Duration> |
| 34 |
double |
| 35 |
print(const char* desc, Duration dur) |
| 36 |
{ |
| 37 |
auto ns = chrono::duration_cast<chrono::nanoseconds>(dur).count(); |
| 38 |
double d = double(ns) / iterations; |
| 39 |
cout << desc << ": " << ns << "ns for " << iterations |
| 40 |
<< " calls, avg " << d << "ns per call\n"; |
| 41 |
return d; |
| 42 |
} |
| 43 |
|
| 44 |
static void |
| 45 |
calibrate() |
| 46 |
{ |
| 47 |
/* After set_value, wait_for is faster, so use that for the |
| 48 |
calibration loops to avoid zero at low clock resultions. */ |
| 49 |
promise<int> p = {}; |
| 50 |
future<int> f = p.get_future(); |
| 51 |
p.set_value(1); |
| 52 |
|
| 53 |
auto start = chrono::high_resolution_clock::now(); |
| 54 |
auto stop = start; |
| 55 |
/* Loop until the clock advances, so that start is right after a |
| 56 |
time increment. */ |
| 57 |
do |
| 58 |
stop = chrono::high_resolution_clock::now(); |
| 59 |
while (start == stop); |
| 60 |
|
| 61 |
/* This approximates the smallest time increment we may expect to be |
| 62 |
able to measure. It doesn't have to be very precise, just a |
| 63 |
ballpart of the right magnitude. */ |
| 64 |
auto tick = stop - start; |
| 65 |
|
| 66 |
int i = 0; |
| 67 |
start = stop; |
| 68 |
/* Now until the clock advances again, so that stop is right |
| 69 |
after another time increment. */ |
| 70 |
do |
| 71 |
{ |
| 72 |
f.wait_for(chrono::seconds(0)); |
| 73 |
stop = chrono::high_resolution_clock::now(); |
| 74 |
i++; |
| 75 |
} |
| 76 |
while (start == stop); |
| 77 |
|
| 78 |
/* Aim for some 10 ticks. This won't be quite right if now() takes |
| 79 |
up a significant portion of the loop time, but we'll measure |
| 80 |
without that and adjust in the loop below. */ |
| 81 |
if (iterations < i * 10) |
| 82 |
iterations = i * 10; |
| 83 |
|
| 84 |
/* We aim for some 10 ticks for the loop that's expected to be fastest, |
| 85 |
but even if we don't get quite that many, we're still fine. */ |
| 86 |
iterations /= 2; |
| 87 |
do |
| 88 |
{ |
| 89 |
iterations *= 2; |
| 90 |
start = chrono::high_resolution_clock::now(); |
| 91 |
for(int i = 0; i < iterations; i++) |
| 92 |
f.wait_for(chrono::seconds(0)); |
| 93 |
stop = chrono::high_resolution_clock::now(); |
| 94 |
} |
| 95 |
while (stop - start < 5 * tick); |
| 96 |
} |
| 97 |
|
| 98 |
int main() |
| 99 |
{ |
| 100 |
/* First, calibrate the iteration count so that we don't get any of |
| 101 |
the actual measurement loops to complete in less than the clock |
| 102 |
granularity. */ |
| 103 |
calibrate (); |
| 104 |
|
| 105 |
promise<int> p; |
| 106 |
future<int> f = p.get_future(); |
| 107 |
|
| 108 |
auto start = chrono::high_resolution_clock::now(); |
| 109 |
for(int i = 0; i < iterations; i++) |
| 110 |
f.wait_for(chrono::seconds(0)); |
| 111 |
auto stop = chrono::high_resolution_clock::now(); |
| 112 |
|
| 113 |
double wait_for_0 = print("wait_for(0s)", stop - start); |
| 114 |
|
| 115 |
start = chrono::high_resolution_clock::now(); |
| 116 |
for(int i = 0; i < iterations; i++) |
| 117 |
f.wait_until(chrono::system_clock::time_point::min()); |
| 118 |
stop = chrono::high_resolution_clock::now(); |
| 119 |
double wait_until_sys_min __attribute__((unused)) |
| 120 |
= print("wait_until(system_clock minimum)", stop - start); |
| 121 |
|
| 122 |
start = chrono::high_resolution_clock::now(); |
| 123 |
for(int i = 0; i < iterations; i++) |
| 124 |
f.wait_until(chrono::steady_clock::time_point::min()); |
| 125 |
stop = chrono::high_resolution_clock::now(); |
| 126 |
double wait_until_steady_min __attribute__((unused)) |
| 127 |
= print("wait_until(steady_clock minimum)", stop - start); |
| 128 |
|
| 129 |
start = chrono::high_resolution_clock::now(); |
| 130 |
for(int i = 0; i < iterations; i++) |
| 131 |
f.wait_until(chrono::system_clock::time_point()); |
| 132 |
stop = chrono::high_resolution_clock::now(); |
| 133 |
double wait_until_sys_epoch __attribute__((unused)) |
| 134 |
= print("wait_until(system_clock epoch)", stop - start); |
| 135 |
|
| 136 |
start = chrono::high_resolution_clock::now(); |
| 137 |
for(int i = 0; i < iterations; i++) |
| 138 |
f.wait_until(chrono::steady_clock::time_point()); |
| 139 |
stop = chrono::high_resolution_clock::now(); |
| 140 |
double wait_until_steady_epoch __attribute__((unused)) |
| 141 |
= print("wait_until(steady_clock epoch", stop - start); |
| 142 |
|
| 143 |
p.set_value(1); |
| 144 |
|
| 145 |
start = chrono::high_resolution_clock::now(); |
| 146 |
for(int i = 0; i < iterations; i++) |
| 147 |
f.wait_for(chrono::seconds(0)); |
| 148 |
stop = chrono::high_resolution_clock::now(); |
| 149 |
double ready = print("wait_for when ready", stop - start); |
| 150 |
|
| 151 |
// Polling before ready with wait_for(0s) should be almost as fast as |
| 152 |
// after the result is ready. |
| 153 |
VERIFY( wait_for_0 < (ready * 30) ); |
| 154 |
|
| 155 |
// Polling before ready using wait_until(min) should not be terribly |
| 156 |
// slow. We hope for no more than 100x slower, but a little over |
| 157 |
// 100x has been observed, and since the measurements may have a lot |
| 158 |
// of noise, and increasing the measurement precision through |
| 159 |
// additional iterations would make the test run for too long on |
| 160 |
// systems with very low clock precision (60Hz clocks are not |
| 161 |
// unheard of), we tolerate a lot of error. |
| 162 |
VERIFY( wait_until_sys_min < (ready * 200) ); |
| 163 |
VERIFY( wait_until_steady_min < (ready * 200) ); |
| 164 |
|
| 165 |
// The following two tests fail with GCC 11, see |
| 166 |
// https://gcc.gnu.org/pipermail/libstdc++/2020-November/051422.html |
| 167 |
#if 0 |
| 168 |
// Polling before ready using wait_until(epoch) should not be terribly slow. |
| 169 |
VERIFY( wait_until_sys_epoch < (ready * 200) ); |
| 170 |
VERIFY( wait_until_steady_epoch < (ready * 200) ); |
| 171 |
#endif |
| 172 |
} |