| 1 | /* Copyright (c) 2009, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | /* |
| 25 | rdtsc3 -- multi-platform timer code |
| 26 | |
| 27 | When you run rdtsc3, it will print the contents of |
| 28 | "my_timer_info". The display indicates |
| 29 | what timer routine is best for a given platform. |
| 30 | |
| 31 | For example, this is the display on production.mysql.com, |
| 32 | a 2.8GHz Xeon with Linux 2.6.17, gcc 3.3.3: |
| 33 | |
| 34 | cycles nanoseconds microseconds milliseconds ticks |
| 35 | ------------- ------------- ------------- ------------- ------------- |
| 36 | 1 11 13 18 17 |
| 37 | 2815019607 1000000000 1000000 1049 102 |
| 38 | 1 1000 1 1 1 |
| 39 | 88 4116 3888 4092 2044 |
| 40 | |
| 41 | The first line shows routines, e.g. 1 = MY_TIMER_ROUTINE_ASM_X86. |
| 42 | The second line shows frequencies, e.g. 2815019607 is nearly 2.8GHz. |
| 43 | The third line shows resolutions, e.g. 1000 = very poor resolution. |
| 44 | The fourth line shows overheads, e.g. ticks takes 2044 cycles. |
| 45 | */ |
| 46 | |
| 47 | #include <gtest/gtest.h> |
| 48 | |
| 49 | #include "my_inttypes.h" |
| 50 | #include "my_rdtsc.h" |
| 51 | |
| 52 | namespace mysys_my_rdtsc_unittest { |
| 53 | |
| 54 | const int LOOP_COUNT = 100; |
| 55 | |
| 56 | class RDTimeStampCounter : public ::testing::Test { |
| 57 | protected: |
| 58 | void SetUp() override { test_init(); } |
| 59 | void test_init(); |
| 60 | |
| 61 | MY_TIMER_INFO myt; |
| 62 | }; |
| 63 | |
| 64 | void RDTimeStampCounter::test_init() { |
| 65 | my_timer_init(&myt); |
| 66 | |
| 67 | fprintf(stdout, "----- Routine ---------------\n"); |
| 68 | fprintf(stdout, "myt.cycles.routine : %13llu\n", myt.cycles.routine); |
| 69 | fprintf(stdout, "myt.nanoseconds.routine : %13llu\n", |
| 70 | myt.nanoseconds.routine); |
| 71 | fprintf(stdout, "myt.microseconds.routine : %13llu\n", |
| 72 | myt.microseconds.routine); |
| 73 | fprintf(stdout, "myt.milliseconds.routine : %13llu\n", |
| 74 | myt.milliseconds.routine); |
| 75 | fprintf(stdout, "myt.ticks.routine : %13llu\n", myt.ticks.routine); |
| 76 | |
| 77 | fprintf(stdout, "----- Frequency -------------\n"); |
| 78 | fprintf(stdout, "myt.cycles.frequency : %13llu\n", |
| 79 | myt.cycles.frequency); |
| 80 | fprintf(stdout, "myt.nanoseconds.frequency : %13llu\n", |
| 81 | myt.nanoseconds.frequency); |
| 82 | fprintf(stdout, "myt.microseconds.frequency : %13llu\n", |
| 83 | myt.microseconds.frequency); |
| 84 | fprintf(stdout, "myt.milliseconds.frequency : %13llu\n", |
| 85 | myt.milliseconds.frequency); |
| 86 | fprintf(stdout, "myt.ticks.frequency : %13llu\n", |
| 87 | myt.ticks.frequency); |
| 88 | |
| 89 | fprintf(stdout, "----- Resolution ------------\n"); |
| 90 | fprintf(stdout, "myt.cycles.resolution : %13llu\n", |
| 91 | myt.cycles.resolution); |
| 92 | fprintf(stdout, "myt.nanoseconds.resolution : %13llu\n", |
| 93 | myt.nanoseconds.resolution); |
| 94 | fprintf(stdout, "myt.microseconds.resolution : %13llu\n", |
| 95 | myt.microseconds.resolution); |
| 96 | fprintf(stdout, "myt.milliseconds.resolution : %13llu\n", |
| 97 | myt.milliseconds.resolution); |
| 98 | fprintf(stdout, "myt.ticks.resolution : %13llu\n", |
| 99 | myt.ticks.resolution); |
| 100 | |
| 101 | fprintf(stdout, "----- Overhead --------------\n"); |
| 102 | fprintf(stdout, "myt.cycles.overhead : %13llu\n", |
| 103 | myt.cycles.overhead); |
| 104 | fprintf(stdout, "myt.nanoseconds.overhead : %13llu\n", |
| 105 | myt.nanoseconds.overhead); |
| 106 | fprintf(stdout, "myt.microseconds.overhead : %13llu\n", |
| 107 | myt.microseconds.overhead); |
| 108 | fprintf(stdout, "myt.milliseconds.overhead : %13llu\n", |
| 109 | myt.milliseconds.overhead); |
| 110 | fprintf(stdout, "myt.ticks.overhead : %13llu\n", myt.ticks.overhead); |
| 111 | } |
| 112 | |
| 113 | TEST_F(RDTimeStampCounter, TestCycle) { |
| 114 | ulonglong t1 = my_timer_cycles(); |
| 115 | ulonglong t2; |
| 116 | int i; |
| 117 | int backward [[maybe_unused]] = 0; |
| 118 | int nonzero = 0; |
| 119 | |
| 120 | for (i = 0; i < LOOP_COUNT; i++) { |
| 121 | t2 = my_timer_cycles(); |
| 122 | if (t1 >= t2) backward++; |
| 123 | if (t2 != 0) nonzero++; |
| 124 | t1 = t2; |
| 125 | } |
| 126 | |
| 127 | #if defined(__aarch64__) |
| 128 | /* |
| 129 | The ARM cycle timer can have low resolution, so backward can be |
| 130 | 0 or >0 depending on machine. |
| 131 | */ |
| 132 | EXPECT_EQ(LOOP_COUNT, nonzero); |
| 133 | #else |
| 134 | /* Expect at most 1 backward, the cycle value can overflow */ |
| 135 | EXPECT_TRUE((backward <= 1)) << "The cycle timer is strictly increasing"; |
| 136 | #endif |
| 137 | |
| 138 | if (myt.cycles.routine != 0) |
| 139 | EXPECT_TRUE((nonzero != 0)) << "The cycle timer is implemented"; |
| 140 | else |
| 141 | EXPECT_TRUE((nonzero == 0)) |
| 142 | << "The cycle timer is not implemented and returns 0"; |
| 143 | } |
| 144 | |
| 145 | TEST_F(RDTimeStampCounter, TestNanosecond) { |
| 146 | ulonglong t1 = my_timer_nanoseconds(); |
| 147 | ulonglong t2; |
| 148 | int i; |
| 149 | int backward = 0; |
| 150 | int nonzero = 0; |
| 151 | |
| 152 | for (i = 0; i < LOOP_COUNT; i++) { |
| 153 | t2 = my_timer_nanoseconds(); |
| 154 | if (t1 > t2) backward++; |
| 155 | if (t2 != 0) nonzero++; |
| 156 | t1 = t2; |
| 157 | } |
| 158 | |
| 159 | EXPECT_TRUE((backward == 0)) << "The nanosecond timer is increasing"; |
| 160 | |
| 161 | if (myt.nanoseconds.routine != 0) |
| 162 | EXPECT_TRUE((nonzero != 0)) << "The nanosecond timer is implemented"; |
| 163 | else |
| 164 | EXPECT_TRUE((nonzero == 0)) |
| 165 | << "The nanosecond timer is not implemented and returns 0"; |
| 166 | } |
| 167 | |
| 168 | TEST_F(RDTimeStampCounter, TestMicrosecond) { |
| 169 | ulonglong t1 = my_timer_microseconds(); |
| 170 | ulonglong t2; |
| 171 | int i; |
| 172 | int backward = 0; |
| 173 | int nonzero = 0; |
| 174 | |
| 175 | for (i = 0; i < LOOP_COUNT; i++) { |
| 176 | t2 = my_timer_microseconds(); |
| 177 | if (t1 > t2) backward++; |
| 178 | if (t2 != 0) nonzero++; |
| 179 | t1 = t2; |
| 180 | } |
| 181 | |
| 182 | EXPECT_TRUE((backward == 0)) << "The microsecond timer is increasing"; |
| 183 | |
| 184 | if (myt.microseconds.routine != 0) |
| 185 | EXPECT_TRUE((nonzero != 0)) << "The microsecond timer is implemented"; |
| 186 | else |
| 187 | EXPECT_TRUE((nonzero == 0)) |
| 188 | << "The microsecond timer is not implemented and returns 0"; |
| 189 | } |
| 190 | |
| 191 | TEST_F(RDTimeStampCounter, TestMillisecond) { |
| 192 | ulonglong t1 = my_timer_milliseconds(); |
| 193 | ulonglong t2; |
| 194 | int i; |
| 195 | int backward = 0; |
| 196 | int nonzero = 0; |
| 197 | |
| 198 | for (i = 0; i < LOOP_COUNT; i++) { |
| 199 | t2 = my_timer_milliseconds(); |
| 200 | if (t1 > t2) backward++; |
| 201 | if (t2 != 0) nonzero++; |
| 202 | t1 = t2; |
| 203 | } |
| 204 | |
| 205 | EXPECT_TRUE((backward == 0)) << "The millisecond timer is increasing"; |
| 206 | |
| 207 | if (myt.milliseconds.routine != 0) |
| 208 | EXPECT_TRUE((nonzero != 0)) << "The millisecond timer is implemented"; |
| 209 | else |
| 210 | EXPECT_TRUE((nonzero == 0)) |
| 211 | << "The millisecond timer is not implemented and returns 0"; |
| 212 | } |
| 213 | |
| 214 | TEST_F(RDTimeStampCounter, TestTick) { |
| 215 | ulonglong t1 = my_timer_ticks(); |
| 216 | ulonglong t2; |
| 217 | int i; |
| 218 | int backward = 0; |
| 219 | int nonzero = 0; |
| 220 | |
| 221 | for (i = 0; i < LOOP_COUNT; i++) { |
| 222 | t2 = my_timer_ticks(); |
| 223 | if (t1 > t2) backward++; |
| 224 | if (t2 != 0) nonzero++; |
| 225 | t1 = t2; |
| 226 | } |
| 227 | |
| 228 | EXPECT_TRUE((backward == 0)) << "The tick timer is increasing"; |
| 229 | |
| 230 | if (myt.ticks.routine != 0) |
| 231 | EXPECT_TRUE((nonzero != 0)) << "The tick timer is implemented"; |
| 232 | else |
| 233 | EXPECT_TRUE((nonzero == 0)) |
| 234 | << "The tick timer is not implemented and returns 0"; |
| 235 | } |
| 236 | |
| 237 | } // namespace mysys_my_rdtsc_unittest |