|
19 | 19 | #endif
|
20 | 20 | #endif
|
21 | 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 | + |
22 | 33 | /* Calculate "x * n / d" without unnecessary overflow or loss of precision.
|
23 | 34 | *
|
24 | 35 | * Reference:
|
25 | 36 | * https://elixir.bootlin.com/linux/v6.10.7/source/include/linux/math.h#L121
|
26 | 37 | */
|
27 |
| -static inline uint64_t mult_frac(uint64_t x, uint64_t n, uint64_t d) |
| 38 | +static inline uint64_t mult_frac(uint64_t x, double n, uint64_t d) |
28 | 39 | {
|
29 | 40 | const uint64_t q = x / d;
|
30 | 41 | const uint64_t r = x % d;
|
31 | 42 |
|
32 | 43 | return q * n + r * n / d;
|
33 | 44 | }
|
34 | 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 | + |
35 | 89 | void semu_timer_init(semu_timer_t *timer, uint64_t freq)
|
36 | 90 | {
|
| 91 | + measure_bogomips_ns(freq); /* Measure the time taken by 'clock_gettime' */ |
| 92 | + |
37 | 93 | timer->freq = freq;
|
38 | 94 | semu_timer_rebase(timer, 0);
|
39 | 95 | }
|
40 | 96 |
|
41 |
| -static uint64_t semu_timer_clocksource(uint64_t freq) |
| 97 | +static uint64_t semu_timer_clocksource(semu_timer_t *timer) |
42 | 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 | + |
43 | 108 | #if defined(HAVE_POSIX_TIMER)
|
44 |
| - struct timespec t; |
45 |
| - clock_gettime(CLOCKID, &t); |
46 |
| - return t.tv_sec * freq + mult_frac(t.tv_nsec, freq, 1e9); |
| 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 | + } |
47 | 127 | #elif defined(HAVE_MACH_TIMER)
|
48 |
| - static mach_timebase_info_data_t t; |
49 |
| - if (t.denom == 0) |
50 |
| - (void) mach_timebase_info(&t); |
51 |
| - return mult_frac(mult_frac(mach_absolute_time(), t.numer, t.denom), freq, |
52 |
| - 1e9); |
| 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 | + } |
53 | 149 | #else
|
54 |
| - return time(0) * freq; |
| 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 | + } |
55 | 167 | #endif
|
56 | 168 | }
|
57 | 169 |
|
58 | 170 | uint64_t semu_timer_get(semu_timer_t *timer)
|
59 | 171 | {
|
60 |
| - return semu_timer_clocksource(timer->freq) - timer->begin; |
| 172 | + return semu_timer_clocksource(timer) - timer->begin; |
61 | 173 | }
|
62 | 174 |
|
63 | 175 | void semu_timer_rebase(semu_timer_t *timer, uint64_t time)
|
64 | 176 | {
|
65 |
| - timer->begin = semu_timer_clocksource(timer->freq) - time; |
| 177 | + timer->begin = semu_timer_clocksource(timer) - time; |
66 | 178 | }
|
0 commit comments