Skip to content

Commit 7c0fccd

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 7c0fccd

File tree

6 files changed

+194
-69
lines changed

6 files changed

+194
-69
lines changed

Makefile

+3-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ all: $(BIN) minimal.dtb
4949
OBJS := \
5050
riscv.o \
5151
ram.o \
52-
utils.o \
52+
timer.o \
5353
plic.o \
5454
uart.o \
5555
main.o \
@@ -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;

riscv.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <stdbool.h>
44
#include <stdint.h>
55

6-
#include "utils.h"
6+
#include "timer.h"
77

88
/* ERR_EXCEPTION indicates that the instruction has raised one of the
99
* exceptions defined in the specification. If this flag is set, the

timer.c

+178
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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+
}

utils.h timer.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);

utils.c

-66
This file was deleted.

0 commit comments

Comments
 (0)