Skip to content

Commit 3c2da81

Browse files
committed
test: 增加基于 legacy console putchar 的性能测试
Signed-off-by: YdrMaster <[email protected]>
1 parent 2de250f commit 3c2da81

File tree

10 files changed

+245
-125
lines changed

10 files changed

+245
-125
lines changed

Cargo.lock

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[workspace]
2-
members = ["rustsbi-qemu", "test-kernel", "xtask"]
2+
members = ["rustsbi-qemu", "test-kernel", "legacy-console-bench", "xtask"]
33
default-members = ["xtask"]

legacy-console-bench/Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "legacy-console-bench"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
sbi-rt = { version = "0.0.2", features = ["legacy"] }
10+
riscv = "0.9.0"
11+
r0 = "1"
12+
13+
console = { git = "https://github.com/YdrMaster/rCore-Tutorial-in-single-workspace" }

legacy-console-bench/build.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
fn main() {
2+
use std::{env, fs, path::PathBuf};
3+
4+
let ld = PathBuf::from(env::var_os("OUT_DIR").unwrap()).join("linker.ld");
5+
fs::write(&ld, LINKER).unwrap();
6+
println!("cargo:rerun-if-changed=build.rs");
7+
println!("cargo:rustc-link-arg=-T{}", ld.display());
8+
}
9+
10+
const LINKER: &[u8] = b"
11+
OUTPUT_ARCH(riscv)
12+
ENTRY(_start)
13+
MEMORY {
14+
DRAM : ORIGIN = 0x80200000, LENGTH = 64M
15+
}
16+
SECTIONS {
17+
.text : ALIGN(4) {
18+
*(.text.entry)
19+
*(.text .text.*)
20+
} > DRAM
21+
.rodata : ALIGN(8) {
22+
srodata = .;
23+
*(.rodata .rodata.*)
24+
*(.srodata .srodata.*)
25+
. = ALIGN(8);
26+
erodata = .;
27+
} > DRAM
28+
.data : ALIGN(8) {
29+
sdata = .;
30+
*(.data .data.*)
31+
*(.sdata .sdata.*)
32+
. = ALIGN(8);
33+
edata = .;
34+
} > DRAM
35+
.bss (NOLOAD) : ALIGN(8) {
36+
*(.bss.uninit)
37+
sbss = .;
38+
*(.bss .bss.*)
39+
*(.sbss .sbss.*)
40+
ebss = .;
41+
} > DRAM
42+
/DISCARD/ : {
43+
*(.eh_frame)
44+
}
45+
}";

legacy-console-bench/src/main.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#![no_std]
2+
#![no_main]
3+
#![feature(naked_functions, asm_const)]
4+
#![deny(warnings)]
5+
6+
#[macro_use]
7+
extern crate console;
8+
9+
use console::log;
10+
use riscv::register::*;
11+
use sbi_rt::*;
12+
13+
#[naked]
14+
#[no_mangle]
15+
#[link_section = ".text.entry"]
16+
unsafe extern "C" fn _start(hartid: usize, device_tree_paddr: usize) -> ! {
17+
const STACK_SIZE: usize = 16384; // 16 KiB
18+
19+
#[link_section = ".bss.uninit"]
20+
static mut STACK: [u8; STACK_SIZE] = [0u8; STACK_SIZE];
21+
22+
core::arch::asm!(
23+
"la sp, {stack} + {stack_size}",
24+
"j {main}",
25+
stack_size = const STACK_SIZE,
26+
stack = sym STACK,
27+
main = sym rust_main,
28+
options(noreturn),
29+
)
30+
}
31+
32+
extern "C" fn rust_main(_hartid: usize, _dtb_pa: usize) -> ! {
33+
extern "C" {
34+
static mut sbss: u64;
35+
static mut ebss: u64;
36+
}
37+
unsafe { r0::zero_bss(&mut sbss, &mut ebss) };
38+
console::init_console(&Console);
39+
console::set_log_level(option_env!("LOG"));
40+
console::test_log();
41+
42+
let t0 = time::read();
43+
44+
for i in 0..0x2000 {
45+
print!("{i:#08x}");
46+
for _ in 0..8 {
47+
print!("{}", 8 as char);
48+
}
49+
}
50+
51+
let t1 = time::read();
52+
53+
log::info!("{}", t1 - t0);
54+
system_reset(Shutdown, NoReason);
55+
unreachable!()
56+
}
57+
58+
#[panic_handler]
59+
fn panic(info: &core::panic::PanicInfo) -> ! {
60+
log::error!("{info}");
61+
system_reset(Shutdown, SystemFailure);
62+
loop {}
63+
}
64+
65+
pub struct Console;
66+
67+
impl console::Console for Console {
68+
#[inline]
69+
fn put_char(&self, c: u8) {
70+
#[allow(deprecated)]
71+
legacy::console_putchar(c as _);
72+
}
73+
}

rustsbi-qemu/src/main.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,15 @@ type FixedRustSBI<'a> = RustSBI<
109109

110110
/// rust 入口。
111111
extern "C" fn rust_main(_hartid: usize, opaque: usize) -> Operation {
112-
#[link_section = ".bss.uninit"] // 以免清零
113-
static GENESIS: AtomicBool = AtomicBool::new(false);
114-
112+
static GENESIS: AtomicBool = AtomicBool::new(true);
113+
static SERIAL: Once<ns16550a::Ns16550a> = Once::new();
115114
static BOARD_INFO: Once<BoardInfo> = Once::new();
116115
static CSR_PRINT: AtomicBool = AtomicBool::new(false);
117116

118117
// static RUSTSBI: Once<RustSBI<>> = Once::new();
119118

120119
// 全局初始化过程
121-
if !GENESIS.swap(true, AcqRel) {
120+
if GENESIS.swap(false, AcqRel) {
122121
// 清零 bss 段
123122
zero_bss();
124123
// 解析设备树

test-kernel/build.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
fn main() {
22
use std::{env, fs, path::PathBuf};
33

4-
let ld = &PathBuf::from(env::var_os("OUT_DIR").unwrap()).join("linker.ld");
5-
fs::write(ld, LINKER).unwrap();
4+
let ld = PathBuf::from(env::var_os("OUT_DIR").unwrap()).join("linker.ld");
5+
fs::write(&ld, LINKER).unwrap();
66
println!("cargo:rerun-if-changed=build.rs");
77
println!("cargo:rustc-link-arg=-T{}", ld.display());
88
}
@@ -11,7 +11,7 @@ const LINKER: &[u8] = b"
1111
OUTPUT_ARCH(riscv)
1212
ENTRY(_start)
1313
MEMORY {
14-
DRAM : ORIGIN = 0x80200000, LENGTH = 512M
14+
DRAM : ORIGIN = 0x80200000, LENGTH = 64M
1515
}
1616
SECTIONS {
1717
.text : ALIGN(4) {

test-kernel/src/main.rs

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,21 @@ unsafe extern "C" fn _start(hartid: usize, device_tree_paddr: usize) -> ! {
2424
static mut STACK: [u8; STACK_SIZE] = [0u8; STACK_SIZE];
2525

2626
asm!(
27-
" csrw sie, zero
28-
la sp, {stack}
29-
li t0, {stack_size}
30-
add sp, sp, t0
31-
j {main}
32-
",
27+
"la sp, {stack} + {stack_size}",
28+
"j {main}",
3329
stack_size = const STACK_SIZE,
3430
stack = sym STACK,
35-
main = sym primary_rust_main,
31+
main = sym rust_main,
3632
options(noreturn),
3733
)
3834
}
3935

40-
extern "C" fn primary_rust_main(hartid: usize, dtb_pa: usize) -> ! {
41-
zero_bss();
36+
extern "C" fn rust_main(hartid: usize, dtb_pa: usize) -> ! {
37+
extern "C" {
38+
static mut sbss: u64;
39+
static mut ebss: u64;
40+
}
41+
unsafe { r0::zero_bss(&mut sbss, &mut ebss) };
4242
let BoardInfo {
4343
smp,
4444
frequency,
@@ -70,20 +70,6 @@ extern "C" fn primary_rust_main(hartid: usize, dtb_pa: usize) -> ! {
7070
unreachable!()
7171
}
7272

73-
/// 清零 bss 段。
74-
#[inline(always)]
75-
fn zero_bss() {
76-
#[cfg(target_pointer_width = "32")]
77-
type Word = u32;
78-
#[cfg(target_pointer_width = "64")]
79-
type Word = u64;
80-
extern "C" {
81-
static mut sbss: Word;
82-
static mut ebss: Word;
83-
}
84-
unsafe { r0::zero_bss(&mut sbss, &mut ebss) };
85-
}
86-
8773
#[cfg_attr(not(test), panic_handler)]
8874
fn panic(info: &core::panic::PanicInfo) -> ! {
8975
let (hart_id, pc): (usize, usize);

xtask/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ publish = false
1111
[dependencies]
1212
clap = { version = "4.0", features = ["derive"] }
1313
os-xtask-utils = "0.0.0"
14+
once_cell = "1.16"

0 commit comments

Comments
 (0)