|
| 1 | +#include <time.h> |
| 2 | + |
| 3 | +#include "timer.h" |
| 4 | + |
| 5 | +#if defined(__APPLE__) |
| 6 | +#define HAVE_MACH_TIMER |
| 7 | +#include <mach/mach_time.h> |
| 8 | +#elif !defined(_WIN32) && !defined(_WIN64) |
| 9 | +#define HAVE_POSIX_TIMER |
| 10 | + |
| 11 | +/* |
| 12 | + * Use a faster but less precise clock source because we need quick |
| 13 | + * timestamps rather than fine-grained precision. |
| 14 | + */ |
| 15 | +#ifdef CLOCK_MONOTONIC_COARSE |
| 16 | +#define CLOCKID CLOCK_MONOTONIC_COARSE |
| 17 | +#else |
| 18 | +#define CLOCKID CLOCK_REALTIME_COARSE |
| 19 | +#endif |
| 20 | +#endif |
| 21 | + |
| 22 | +#ifndef SEMU_SMP |
| 23 | +#define SEMU_SMP 1 |
| 24 | +#endif |
| 25 | + |
| 26 | +#ifndef SEMU_BOOT_TARGET_TIME |
| 27 | +#define SEMU_BOOT_TARGET_TIME 10 |
| 28 | +#endif |
| 29 | + |
| 30 | +bool boot_complete = false; |
| 31 | +static double scale_factor; |
| 32 | + |
| 33 | +/* Calculate "x * n / d" without unnecessary overflow or loss of precision. |
| 34 | + * |
| 35 | + * Reference: |
| 36 | + * https://elixir.bootlin.com/linux/v6.10.7/source/include/linux/math.h#L121 |
| 37 | + */ |
| 38 | +static inline uint64_t mult_frac(uint64_t x, double n, uint64_t d) |
| 39 | +{ |
| 40 | + const uint64_t q = x / d; |
| 41 | + const uint64_t r = x % d; |
| 42 | + |
| 43 | + return q * n + r * n / d; |
| 44 | +} |
| 45 | + |
| 46 | +/* Use timespec and frequency to calculate how many ticks to increment. For |
| 47 | + * example, if the frequency is set to 65,000,000, then there are 65,000,000 |
| 48 | + * ticks per second. Respectively, if the time is set to 1 second, then there |
| 49 | + * are 65,000,000 ticks. |
| 50 | + * |
| 51 | + * Thus, by seconds * frequency + nanoseconds * frequency / 1,000,000,000, we |
| 52 | + * can get the number of ticks. |
| 53 | + */ |
| 54 | +static inline uint64_t get_ticks(struct timespec *ts, double freq) |
| 55 | +{ |
| 56 | + return ts->tv_sec * freq + mult_frac(ts->tv_nsec, freq, 1000000000ULL); |
| 57 | +} |
| 58 | + |
| 59 | +/* Measure how long a single 'clock_gettime' takes, to scale real time in order |
| 60 | + * to set the emulator time. |
| 61 | + */ |
| 62 | +static void measure_bogomips_ns(uint64_t target_loop) |
| 63 | +{ |
| 64 | + struct timespec start, end; |
| 65 | + clock_gettime(CLOCKID, &start); |
| 66 | + |
| 67 | + for (uint64_t loops = 0; loops < target_loop; loops++) |
| 68 | + clock_gettime(CLOCKID, &end); |
| 69 | + |
| 70 | + int64_t sec_diff = end.tv_sec - start.tv_sec; |
| 71 | + int64_t nsec_diff = end.tv_nsec - start.tv_nsec; |
| 72 | + double ns_per_call = (sec_diff * 1e9 + nsec_diff) / target_loop; |
| 73 | + |
| 74 | + /* Based on simple statistics, 'semu_timer_clocksource' accounts for |
| 75 | + * approximately 10% of the boot process execution time. Since the logic |
| 76 | + * inside 'semu_timer_clocksource' is relatively simple, it can be assumed |
| 77 | + * that its execution time is roughly equivalent to that of a |
| 78 | + * 'clock_gettime' call. |
| 79 | + * |
| 80 | + * Similarly, based on statistics, 'semu_timer_clocksource' is called |
| 81 | + * approximately 2*1e8 times. Therefore, we can roughly estimate that the |
| 82 | + * boot process will take '(ns_per_call/1e9) * SEMU_SMP * 2 * 1e8 * |
| 83 | + * (100%/10%)' seconds. |
| 84 | + */ |
| 85 | + double predict_sec = ns_per_call * SEMU_SMP * 2; |
| 86 | + scale_factor = SEMU_BOOT_TARGET_TIME / predict_sec; |
| 87 | +} |
| 88 | + |
| 89 | +void semu_timer_init(semu_timer_t *timer, uint64_t freq) |
| 90 | +{ |
| 91 | + measure_bogomips_ns(freq); /* Measure the time taken by 'clock_gettime' */ |
| 92 | + |
| 93 | + timer->freq = freq; |
| 94 | + semu_timer_rebase(timer, 0); |
| 95 | +} |
| 96 | + |
| 97 | +static uint64_t semu_timer_clocksource(semu_timer_t *timer) |
| 98 | +{ |
| 99 | + /* After boot process complete, the timer will switch to real time. Thus, |
| 100 | + * there is an offset between the real time and the emulator time. |
| 101 | + * |
| 102 | + * After switching to real time, the correct way to update time is to |
| 103 | + * calculate the increment of time. Then add it to the emulator time. |
| 104 | + */ |
| 105 | + static int64_t offset = 0; |
| 106 | + static bool first_switch = true; |
| 107 | + |
| 108 | +#if defined(HAVE_POSIX_TIMER) |
| 109 | + struct timespec emulator_time; |
| 110 | + clock_gettime(CLOCKID, &emulator_time); |
| 111 | + |
| 112 | + if (!boot_complete) { |
| 113 | + return get_ticks(&emulator_time, timer->freq * scale_factor); |
| 114 | + } else { |
| 115 | + if (first_switch) { |
| 116 | + first_switch = false; |
| 117 | + uint64_t real_ticks = get_ticks(&emulator_time, timer->freq); |
| 118 | + uint64_t scaled_ticks = |
| 119 | + get_ticks(&emulator_time, timer->freq * scale_factor); |
| 120 | + |
| 121 | + offset = (int64_t) (real_ticks - scaled_ticks); |
| 122 | + } |
| 123 | + |
| 124 | + uint64_t real_freq_ticks = get_ticks(&emulator_time, timer->freq); |
| 125 | + return real_freq_ticks - offset; |
| 126 | + } |
| 127 | +#elif defined(HAVE_MACH_TIMER) |
| 128 | + static mach_timebase_info_data_t emulator_time; |
| 129 | + if (emulator_time.denom == 0) |
| 130 | + (void) mach_timebase_info(&emulator_time); |
| 131 | + |
| 132 | + uint64_t now = mach_absolute_time(); |
| 133 | + uint64_t ns = mult_frac(now, emulator_time.numer, emulator_time.denom); |
| 134 | + if (!boot_complete) { |
| 135 | + return mult_frac(ns, (uint64_t) (timer->freq * scale_factor), |
| 136 | + 1000000000ULL); |
| 137 | + } else { |
| 138 | + if (first_switch) { |
| 139 | + first_switch = false; |
| 140 | + uint64_t real_ticks = mult_frac(ns, timer->freq, 1000000000ULL); |
| 141 | + uint64_t scaled_ticks = mult_frac( |
| 142 | + ns, (uint64_t) (timer->freq * scale_factor), 1000000000ULL); |
| 143 | + offset = (int64_t) (real_ticks - scaled_ticks); |
| 144 | + } |
| 145 | + |
| 146 | + uint64_t real_freq_ticks = mult_frac(ns, timer->freq, 1000000000ULL); |
| 147 | + return real_freq_ticks - offset; |
| 148 | + } |
| 149 | +#else |
| 150 | + time_t now_sec = time(0); |
| 151 | + |
| 152 | + if (!boot_complete) { |
| 153 | + return ((uint64_t) now_sec) * (uint64_t) (timer->freq * scale_factor); |
| 154 | + } else { |
| 155 | + if (first_switch) { |
| 156 | + first_switch = false; |
| 157 | + uint64_t real_val = ((uint64_t) now_sec) * (uint64_t) (timer->freq); |
| 158 | + uint64_t scaled_val = |
| 159 | + ((uint64_t) now_sec) * (uint64_t) (timer->freq * scale_factor); |
| 160 | + offset = (int64_t) real_val - (int64_t) scaled_val; |
| 161 | + } |
| 162 | + |
| 163 | + uint64_t real_freq_val = |
| 164 | + ((uint64_t) now_sec) * (uint64_t) (timer->freq); |
| 165 | + return real_freq_val - offset; |
| 166 | + } |
| 167 | +#endif |
| 168 | +} |
| 169 | + |
| 170 | +uint64_t semu_timer_get(semu_timer_t *timer) |
| 171 | +{ |
| 172 | + return semu_timer_clocksource(timer) - timer->begin; |
| 173 | +} |
| 174 | + |
| 175 | +void semu_timer_rebase(semu_timer_t *timer, uint64_t time) |
| 176 | +{ |
| 177 | + timer->begin = semu_timer_clocksource(timer) - time; |
| 178 | +} |
0 commit comments