-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Scale frequency to suppress RCU CPU stall warning #67
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,48 +19,156 @@ | |
#endif | ||
#endif | ||
|
||
#ifndef SEMU_SMP | ||
#define SEMU_SMP 1 | ||
#endif | ||
|
||
#ifndef SEMU_BOOT_TARGET_TIME | ||
#define SEMU_BOOT_TARGET_TIME 10 | ||
#endif | ||
|
||
bool boot_complete = false; | ||
static double scale_factor; | ||
|
||
/* Calculate "x * n / d" without unnecessary overflow or loss of precision. | ||
* | ||
* Reference: | ||
* https://elixir.bootlin.com/linux/v6.10.7/source/include/linux/math.h#L121 | ||
*/ | ||
static inline uint64_t mult_frac(uint64_t x, uint64_t n, uint64_t d) | ||
static inline uint64_t mult_frac(uint64_t x, double n, uint64_t d) | ||
{ | ||
const uint64_t q = x / d; | ||
const uint64_t r = x % d; | ||
|
||
return q * n + r * n / d; | ||
} | ||
|
||
/* Use timespec and frequency to calculate how many ticks to increment. For | ||
* example, if the frequency is set to 65,000,000, then there are 65,000,000 | ||
* ticks per second. Respectively, if the time is set to 1 second, then there | ||
* are 65,000,000 ticks. | ||
* | ||
* Thus, by seconds * frequency + nanoseconds * frequency / 1,000,000,000, we | ||
* can get the number of ticks. | ||
*/ | ||
static inline uint64_t get_ticks(struct timespec *ts, double freq) | ||
{ | ||
return ts->tv_sec * freq + mult_frac(ts->tv_nsec, freq, 1000000000ULL); | ||
} | ||
|
||
/* Measure how long it takes for the high resolution timer to update once, to | ||
* scale real time in order to set the emulator time. | ||
*/ | ||
static void measure_bogomips_ns(uint64_t target_loop) | ||
{ | ||
struct timespec start, end; | ||
clock_gettime(CLOCKID, &start); | ||
|
||
for (uint64_t loops = 0; loops < target_loop; loops++) | ||
clock_gettime(CLOCKID, &end); | ||
Comment on lines
+64
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did not get the idea behind this code snip. What did you update the value There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is meant to measure the execution time of struct timespec start, end;
clock_gettime(CLOCKID, &start);
for (uint64_t loops = 0; loops < target_loop - 1; loops++) {
struct timespec ts;
clock_gettime(CLOCKID, &ts);
}
clock_gettime(CLOCKID, &end); However, in the for loop, the variable Then, by dividing |
||
|
||
int64_t sec_diff = end.tv_sec - start.tv_sec; | ||
int64_t nsec_diff = end.tv_nsec - start.tv_nsec; | ||
double ns_per_call = (sec_diff * 1e9 + nsec_diff) / target_loop; | ||
|
||
/* Based on simple statistics, 'semu_timer_clocksource' accounts for | ||
* approximately 10% of the boot process execution time. Since the logic | ||
* inside 'semu_timer_clocksource' is relatively simple, it can be assumed | ||
* that its execution time is roughly equivalent to that of a | ||
* 'clock_gettime' call. | ||
* | ||
* Similarly, based on statistics, 'semu_timer_clocksource' is called | ||
* approximately 2*1e8 times. Therefore, we can roughly estimate that the | ||
* boot process will take '(ns_per_call/1e9) * SEMU_SMP * 2 * 1e8 * | ||
* (100%/10%)' seconds. | ||
*/ | ||
double predict_sec = ns_per_call * SEMU_SMP * 2; | ||
scale_factor = SEMU_BOOT_TARGET_TIME / predict_sec; | ||
} | ||
|
||
void semu_timer_init(semu_timer_t *timer, uint64_t freq) | ||
{ | ||
measure_bogomips_ns(freq); /* Measure the time taken by 'clock_gettime' */ | ||
|
||
timer->freq = freq; | ||
semu_timer_rebase(timer, 0); | ||
} | ||
|
||
static uint64_t semu_timer_clocksource(uint64_t freq) | ||
static uint64_t semu_timer_clocksource(semu_timer_t *timer) | ||
{ | ||
/* After boot process complete, the timer will switch to real time. Thus, | ||
* there is an offset between the real time and the emulator time. | ||
* | ||
* After switching to real time, the correct way to update time is to | ||
* calculate the increment of time. Then add it to the emulator time. | ||
*/ | ||
static int64_t offset = 0; | ||
static bool first_switch = true; | ||
|
||
#if defined(HAVE_POSIX_TIMER) | ||
struct timespec t; | ||
clock_gettime(CLOCKID, &t); | ||
return t.tv_sec * freq + mult_frac(t.tv_nsec, freq, 1e9); | ||
struct timespec emulator_time; | ||
clock_gettime(CLOCKID, &emulator_time); | ||
|
||
if (!boot_complete) | ||
return get_ticks(&emulator_time, timer->freq * scale_factor); | ||
|
||
if (first_switch) { | ||
first_switch = false; | ||
uint64_t real_ticks = get_ticks(&emulator_time, timer->freq); | ||
uint64_t scaled_ticks = | ||
get_ticks(&emulator_time, timer->freq * scale_factor); | ||
|
||
offset = (int64_t) (real_ticks - scaled_ticks); | ||
} | ||
|
||
uint64_t real_freq_ticks = get_ticks(&emulator_time, timer->freq); | ||
return real_freq_ticks - offset; | ||
#elif defined(HAVE_MACH_TIMER) | ||
static mach_timebase_info_data_t t; | ||
if (t.denom == 0) | ||
(void) mach_timebase_info(&t); | ||
return mult_frac(mult_frac(mach_absolute_time(), t.numer, t.denom), freq, | ||
1e9); | ||
static mach_timebase_info_data_t emulator_time; | ||
if (emulator_time.denom == 0) | ||
(void) mach_timebase_info(&emulator_time); | ||
|
||
uint64_t now = mach_absolute_time(); | ||
uint64_t ns = mult_frac(now, emulator_time.numer, emulator_time.denom); | ||
if (!boot_complete) | ||
return mult_frac(ns, (uint64_t) (timer->freq * scale_factor), | ||
1000000000ULL); | ||
|
||
if (first_switch) { | ||
first_switch = false; | ||
uint64_t real_ticks = mult_frac(ns, timer->freq, 1000000000ULL); | ||
uint64_t scaled_ticks = mult_frac( | ||
ns, (uint64_t) (timer->freq * scale_factor), 1000000000ULL); | ||
offset = (int64_t) (real_ticks - scaled_ticks); | ||
} | ||
|
||
uint64_t real_freq_ticks = mult_frac(ns, timer->freq, 1000000000ULL); | ||
return real_freq_ticks - offset; | ||
#else | ||
return time(0) * freq; | ||
time_t now_sec = time(0); | ||
|
||
if (!boot_complete) | ||
return ((uint64_t) now_sec) * (uint64_t) (timer->freq * scale_factor); | ||
|
||
if (first_switch) { | ||
first_switch = false; | ||
uint64_t real_val = ((uint64_t) now_sec) * (uint64_t) (timer->freq); | ||
uint64_t scaled_val = | ||
((uint64_t) now_sec) * (uint64_t) (timer->freq * scale_factor); | ||
offset = (int64_t) real_val - (int64_t) scaled_val; | ||
} | ||
|
||
uint64_t real_freq_val = ((uint64_t) now_sec) * (uint64_t) (timer->freq); | ||
return real_freq_val - offset; | ||
#endif | ||
} | ||
|
||
uint64_t semu_timer_get(semu_timer_t *timer) | ||
{ | ||
return semu_timer_clocksource(timer->freq) - timer->begin; | ||
return semu_timer_clocksource(timer) - timer->begin; | ||
} | ||
|
||
void semu_timer_rebase(semu_timer_t *timer, uint64_t time) | ||
{ | ||
timer->begin = semu_timer_clocksource(timer->freq) - time; | ||
timer->begin = semu_timer_clocksource(timer) - time; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest moving
boot_complete
variable intovm_t
for a more conceptually accurate design.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we move
boot_complete
intovm_t
, all existing functions for semu_timer_t would need an additionalvm_t
parameter. For example,semu_timer_get
would change to:This change would indirectly require the areas that call this function to also pass in a
vm_t
parameter. For instance, sincesemu_timer_get
is called withinaclint_mtimer_update_interrupts
, the API ofaclint_mtimer_update_interrupts
would also need to be updated to includevm_t
.As this pattern continues, the API changes would proliferate significantly. Perhaps we could introduce a static bool pointer pointing to
boot_complete
and assign its value duringsemu_timer_init
. This way, we would only need to modify the parameters ofsemu_timer_init
.