Skip to content

Commit 544156c

Browse files
committed
WIP: correct percentage calculation
1 parent f548bda commit 544156c

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

utils.c

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ static double scale_factor;
2727
uint64_t count = 0;
2828
struct timespec boot_begin, boot_end;
2929
double TEST_ns_per_call, TEST_predict_sec;
30-
static uint64_t local_start; // or use a local var
31-
static uint64_t total_clocksource_ns = 0;
3230
static int G_n_harts = 0;
3331

3432
/* Calculate "x * n / d" without unnecessary overflow or loss of precision.
@@ -76,13 +74,6 @@ static inline uint64_t host_time_ns()
7674
#endif
7775
}
7876

79-
/* for testing */
80-
static inline void semu_timer_clocksource_exit(void)
81-
{
82-
uint64_t end = host_time_ns();
83-
total_clocksource_ns += end - local_start;
84-
}
85-
8677
/* BogoMips is a rough measurement of CPU speed, typically calculated by
8778
* executing a counting loop to estimate the CPU's performance.
8879
*
@@ -183,6 +174,12 @@ static uint64_t semu_timer_clocksource(semu_timer_t *timer)
183174
static int64_t offset = 0;
184175
static bool first_switch = true;
185176

177+
/* for testing */
178+
static volatile uint64_t local_total_ns = 0;
179+
static volatile uint64_t local_start;
180+
static volatile uint64_t local_end;
181+
static volatile uint64_t total_clocksource_ns = 0;
182+
186183
#if defined(HAVE_POSIX_TIMER) || defined(HAVE_MACH_TIMER)
187184
uint64_t now_ns = host_time_ns();
188185
local_start = now_ns;
@@ -200,18 +197,23 @@ static uint64_t semu_timer_clocksource(semu_timer_t *timer)
200197
uint64_t scaled_ticks = real_ticks * 0.001;
201198

202199
if (!boot_complete) {
203-
semu_timer_clocksource_exit();
200+
local_end = host_time_ns();
201+
total_clocksource_ns += local_end - local_start;
202+
local_total_ns += local_end - local_start;
204203
char filename[50];
205204
snprintf(filename, sizeof(filename), "./time_log/time_log_%d.txt",
206205
G_n_harts);
207206

208207
if (cnt % 1000000 == 0) {
209-
cnt = 0;
210-
start_count = false;
211208
time_2 = now_ns;
212209
time_log_file = fopen(filename, "a");
213-
fprintf(time_log_file, "diff: %lu\n", time_2 - time_1);
210+
fprintf(time_log_file, "diff: %lu, total: %lu\n", (time_2 - time_1),
211+
local_total_ns);
214212
fclose(time_log_file);
213+
214+
cnt = 0;
215+
start_count = false;
216+
local_total_ns = 0;
215217
}
216218
return scaled_ticks; /* Return scaled ticks in the boot phase. */
217219
}
@@ -222,7 +224,8 @@ static uint64_t semu_timer_clocksource(semu_timer_t *timer)
222224
offset = (int64_t) (real_ticks - scaled_ticks);
223225

224226
/* for testing */
225-
semu_timer_clocksource_exit();
227+
local_end = host_time_ns();
228+
total_clocksource_ns += local_end - local_start;
226229
clock_gettime(CLOCK_REALTIME, &boot_end);
227230

228231
double boot_time = (boot_end.tv_sec - boot_begin.tv_sec) +
@@ -241,9 +244,10 @@ static uint64_t semu_timer_clocksource(semu_timer_t *timer)
241244
"\033[1;31m[SEMU LOG]: total_clocksource_ns = %lu, "
242245
"percentage = %.5f\033[0m\n",
243246
total_clocksource_ns,
244-
((double) total_clocksource_ns) /
245-
((boot_end.tv_sec - boot_begin.tv_sec) * 1e9 +
246-
boot_end.tv_nsec - boot_begin.tv_nsec));
247+
((double) total_clocksource_ns / 2) /
248+
(((boot_end.tv_sec - boot_begin.tv_sec) * 1e9 +
249+
boot_end.tv_nsec - boot_begin.tv_nsec) -
250+
(total_clocksource_ns / 2)));
247251

248252
printf(
249253
"\033[1;31m[SEMU LOG]: real_ns_per_call = %.5f, diff_ns_per_call = "

0 commit comments

Comments
 (0)