Skip to content

Commit 9395054

Browse files
committed
Scale frequency to suppress RCU CPU stall warning
Since the emulator currently operates using sequential emulation, the execution time for the boot process is relatively long, which can result in the generation of RCU CPU stall warnings. To address this issue, there are several potential solutions: 1. Scale the frequency to slow down emulator time during the boot process, thereby eliminating RCU CPU stall warnings. 2. During the boot process, avoid using 'clock_gettime' to update ticks and instead manage the tick increment relationship manually. 3. Implement multi-threaded emulation to accelerate the emulator's execution speed. For the third point, while implementing multi-threaded emulation can significantly accelerate the emulator's execution speed, it cannot guarantee that this issue will not reappear as the number of cores increases in the future. Therefore, a better approach is to use methods 1 and 2 to allow the emulator to set an expected time for completing the boot process. The advantages and disadvantages of the scale method are as follows: Advantages: - Simple implementation - Effectively sets the expected boot process completion time - Results have strong interpretability - Emulator time can be easily mapped back to real time Disadvantages: - Slower execution speed The advantages and disadvantages of the increment ticks method are as follows: Advantages: - Faster execution speed - Effectively sets the expected boot process completion time Disadvantages: - More complex implementation - Some results are difficult to interpret - Emulator time is difficult to map back to real time Based on practical tests, the second method provides limited acceleration but introduces some significant drawbacks, such as difficulty in interpreting results and the complexity of managing the increment relationship. Therefore, this commit opts for the scale frequency method to address this issue. This commit divides time into emulator time and real time. During the boot process, the timer uses scale frequency to slow down the growth of emulator time, eliminating RCU CPU stall warnings. After the boot process is complete, the growth of emulator time aligns with real time. To configure the scale frequency parameter, three pieces of information are required: 1. The expected completion time of the boot process 2. A reference point for estimating the boot process completion time 3. The relationship between the reference point and the number of SMPs According to the Linux kernel documentation: https://docs.kernel.org/RCU/stallwarn.html#config-rcu-cpu-stall-timeout The grace period for RCU CPU stalls is typically set to 21 seconds. By dividing this value by two as the expected completion time, we can provide a sufficient buffer to reduce the impact of errors and avoid RCU CPU stall warnings. Using 'gprof' for basic statistical analysis, it was found that 'semu_timer_clocksource' accounts for approximately 10% of the boot process execution time. Since the logic within 'semu_timer_clocksource' is relatively simple, its execution time can be assumed to be nearly equal to 'clock_gettime'. Furthermore, by adding a counter to 'semu_timer_clocksource', it was observed that each time the number of SMPs increases by 1, the execution count of 'semu_timer_clocksource' increases by approximately '2 * 10^8' With this information, we can estimate the boot process completion time as 'sec_per_call * SMPs * 2 * 10^8 * (100% / 10%)' seconds, and thereby calculate the scale frequency parameter. For instance, if the estimated time is 200 seconds and the target time is 10 seconds, the scaling factor would be '10 / 200'.
1 parent 36fc1b2 commit 9395054

File tree

4 files changed

+139
-14
lines changed

4 files changed

+139
-14
lines changed

Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ E :=
7878
S := $E $E
7979

8080
SMP ?= 1
81+
CFLAGS += -D SEMU_SMP=$(SMP)
82+
CFLAGS += -D SEMU_BOOT_TARGET_TIME=10
8183
.PHONY: riscv-harts.dtsi
8284
riscv-harts.dtsi:
8385
$(Q)python3 scripts/gen-hart-dts.py $@ $(SMP) $(CLOCK_FREQ)

riscv.c

+8
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,14 @@ static void op_sret(hart_t *vm)
382382
vm->s_mode = vm->sstatus_spp;
383383
vm->sstatus_sie = vm->sstatus_spie;
384384

385+
/* After the booting process is complete, initrd will be loaded. At this
386+
* point, the sytstem will switch to U mode for the first time. Therefore,
387+
* by checking whether the switch to U mode has already occurred, we can
388+
* determine if the boot process has been completed.
389+
*/
390+
if (!boot_complete && !vm->s_mode)
391+
boot_complete = true;
392+
385393
/* Reset stack */
386394
vm->sstatus_spp = false;
387395
vm->sstatus_spie = true;

utils.c

+125-13
Original file line numberDiff line numberDiff line change
@@ -19,48 +19,160 @@
1919
#endif
2020
#endif
2121

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+
2233
/* Calculate "x * n / d" without unnecessary overflow or loss of precision.
2334
*
2435
* Reference:
2536
* https://elixir.bootlin.com/linux/v6.10.7/source/include/linux/math.h#L121
2637
*/
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)
2839
{
2940
const uint64_t q = x / d;
3041
const uint64_t r = x % d;
3142

3243
return q * n + r * n / d;
3344
}
3445

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+
3589
void semu_timer_init(semu_timer_t *timer, uint64_t freq)
3690
{
91+
measure_bogomips_ns(freq); /* Measure the time taken by 'clock_gettime' */
92+
3793
timer->freq = freq;
3894
semu_timer_rebase(timer, 0);
3995
}
4096

41-
static uint64_t semu_timer_clocksource(uint64_t freq)
97+
static uint64_t semu_timer_clocksource(semu_timer_t *timer)
4298
{
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+
43108
#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+
}
47127
#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+
}
53149
#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+
}
55167
#endif
56168
}
57169

58170
uint64_t semu_timer_get(semu_timer_t *timer)
59171
{
60-
return semu_timer_clocksource(timer->freq) - timer->begin;
172+
return semu_timer_clocksource(timer) - timer->begin;
61173
}
62174

63175
void semu_timer_rebase(semu_timer_t *timer, uint64_t time)
64176
{
65-
timer->begin = semu_timer_clocksource(timer->freq) - time;
177+
timer->begin = semu_timer_clocksource(timer) - time;
66178
}

utils.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <stdbool.h>
34
#include <stdint.h>
45

56
/* TIMER */
@@ -8,6 +9,8 @@ typedef struct {
89
uint64_t freq;
910
} semu_timer_t;
1011

12+
extern bool boot_complete; /* complete boot process and get in initrd */
13+
1114
void semu_timer_init(semu_timer_t *timer, uint64_t freq);
1215
uint64_t semu_timer_get(semu_timer_t *timer);
13-
void semu_timer_rebase(semu_timer_t *timer, uint64_t time);
16+
void semu_timer_rebase(semu_timer_t *timer, uint64_t time);

0 commit comments

Comments
 (0)