Skip to content

Commit ed2eedd

Browse files
authored
Merge pull request rustsbi#41 from YdrMaster/main
增加性能测试
2 parents 2de250f + fef526e commit ed2eedd

File tree

18 files changed

+380
-307
lines changed

18 files changed

+380
-307
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22

33
All notable changes to this project will be documented in this file.
44

5-
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres
6-
to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
76

87
## [Unreleased]
98

109
### Added
1110

1211
- Hint spin loop in hart state monitor module
12+
- Add bench-kernel crate to workspace for sbi call bench
1313

1414
### Modified
1515

1616
- Use instance based RustSBI interface, with separate functions for legacy stdio
1717
- Update sbi-testing to version 0.0.1
18+
- Use crate rcore-console version 0.0.0 in rustsbi-qemu and test-kernel for `print!` and `println!`
1819
- Use crate os-xtask-utils version 0.0.0 in xtask builder
1920

2021
### Fixed

Cargo.lock

Lines changed: 24 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", "bench-kernel", "xtask"]
33
default-members = ["xtask"]

bench-kernel/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "bench-kernel"
3+
version = "0.0.1"
4+
edition = "2021"
5+
authors = ["YdrMaster <[email protected]>"]
6+
publish = false
7+
8+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
9+
10+
[dependencies]
11+
sbi-rt = "0.0.2"
12+
riscv = "0.9.0"
13+
r0 = "1"
14+
uart_16550 = "0.2"
15+
rcore-console = "0.0.0"

bench-kernel/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+
}";

bench-kernel/src/main.rs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#![no_std]
2+
#![no_main]
3+
#![feature(naked_functions, asm_const)]
4+
#![deny(warnings)]
5+
6+
use core::mem::MaybeUninit;
7+
use rcore_console::log;
8+
use riscv::register::*;
9+
use sbi_rt::*;
10+
use uart_16550::MmioSerialPort;
11+
12+
#[naked]
13+
#[no_mangle]
14+
#[link_section = ".text.entry"]
15+
unsafe extern "C" fn _start(hartid: usize, device_tree_paddr: usize) -> ! {
16+
const STACK_SIZE: usize = 16384; // 16 KiB
17+
18+
#[link_section = ".bss.uninit"]
19+
static mut STACK: [u8; STACK_SIZE] = [0u8; STACK_SIZE];
20+
21+
core::arch::asm!(
22+
"la sp, {stack} + {stack_size}",
23+
"j {main}",
24+
stack_size = const STACK_SIZE,
25+
stack = sym STACK,
26+
main = sym rust_main,
27+
options(noreturn),
28+
)
29+
}
30+
31+
extern "C" fn rust_main(hartid: usize, _dtb_pa: usize) -> ! {
32+
extern "C" {
33+
static mut sbss: u64;
34+
static mut ebss: u64;
35+
}
36+
unsafe { r0::zero_bss(&mut sbss, &mut ebss) };
37+
// 初始化打印
38+
unsafe { UART = MaybeUninit::new(MmioSerialPort::new(0x1000_0000)) };
39+
rcore_console::init_console(&Console);
40+
rcore_console::set_log_level(option_env!("LOG"));
41+
rcore_console::test_log();
42+
// 打开软中断
43+
unsafe {
44+
sie::set_ssoft();
45+
sstatus::set_sie();
46+
};
47+
// 测试调用延迟
48+
let t0 = time::read();
49+
50+
for _ in 0..0xffff {
51+
let _ = sbi_rt::get_marchid();
52+
}
53+
54+
let t1 = time::read();
55+
log::info!("marchid duration = {}", t1 - t0);
56+
// 测试中断响应延迟
57+
let t0 = time::read();
58+
59+
for _ in 0..0xffff {
60+
unsafe {
61+
core::arch::asm!(
62+
" la {0}, 1f
63+
csrw stvec, {0}
64+
ecall
65+
wfi
66+
.align 2
67+
1:
68+
",
69+
out(reg) _,
70+
in("a7") 0x735049,
71+
in("a6") 0,
72+
in("a0") 1 << hartid,
73+
in("a1") 0,
74+
options(nomem),
75+
);
76+
}
77+
}
78+
79+
let t1 = time::read();
80+
log::info!("ipi duration = {}", t1 - t0);
81+
82+
system_reset(Shutdown, NoReason);
83+
unreachable!()
84+
}
85+
86+
#[panic_handler]
87+
fn panic(info: &core::panic::PanicInfo) -> ! {
88+
log::error!("{info}");
89+
system_reset(Shutdown, SystemFailure);
90+
loop {}
91+
}
92+
93+
struct Console;
94+
static mut UART: MaybeUninit<MmioSerialPort> = MaybeUninit::uninit();
95+
96+
impl rcore_console::Console for Console {
97+
#[inline]
98+
fn put_char(&self, c: u8) {
99+
unsafe { UART.assume_init_mut() }.send(c);
100+
}
101+
}

rustsbi-qemu/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ riscv = "0.9.0"
2323
spin = "0.9"
2424
r0 = "1"
2525
uart_16550 = "0.2"
26+
rcore-console = "0.0.0"
2627
dtb-walker = "=0.2.0-alpha.3"
2728
qemu-exit = "3.0"

rustsbi-qemu/src/console.rs

Lines changed: 0 additions & 30 deletions
This file was deleted.

rustsbi-qemu/src/execute.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,11 @@ impl<'a> Environment<'a> {
5959
use rustsbi::spec::{binary::*, hsm::*, srst::*};
6060
if self.ctx.sbi_extension() == sbi_spec::legacy::LEGACY_CONSOLE_PUTCHAR {
6161
let ch = self.ctx.a(0);
62-
crate::console::STDOUT.wait().putchar((ch & 0xFF) as u8);
62+
print!("{:}", ch as u8 as char);
6363
self.ctx.mepc = self.ctx.mepc.wrapping_add(4);
6464
return None;
6565
} else if self.ctx.sbi_extension() == sbi_spec::legacy::LEGACY_CONSOLE_GETCHAR {
66-
let ch = crate::console::STDOUT.wait().getchar();
67-
*self.ctx.a_mut(0) = ch as usize;
66+
*self.ctx.a_mut(0) = unsafe { crate::UART.lock().assume_init_mut().receive() } as usize;
6867
self.ctx.mepc = self.ctx.mepc.wrapping_add(4);
6968
return None;
7069
}

0 commit comments

Comments
 (0)