Skip to content

Commit

Permalink
ulib: make axstd API closer to rust std
Browse files Browse the repository at this point in the history
- now apps can be built with both std and ArceOS without modification
- TODO: net, fs
  • Loading branch information
equation314 committed Jul 23, 2023
1 parent 5f3c683 commit 5ba6043
Show file tree
Hide file tree
Showing 63 changed files with 883 additions and 622 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,31 @@ jobs:
run: make ARCH=${{ matrix.arch }} A=apps/c/udpserver
- name: Build c/iperf
run: make ARCH=${{ matrix.arch }} A=apps/c/iperf

build-apps-for-std:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
arch: [x86_64]
steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ env.rust-toolchain }}
- name: Build helloworld
run: cargo build -p arceos-helloworld
- name: Build memtest
run: cargo build -p arceos-memtest
- name: Build exception
run: cargo build -p arceos-exception
- name: Build task/yield
run: cargo build -p arceos-yield
- name: Build task/parallel
run: cargo build -p arceos-parallel
- name: Build task/sleep
run: cargo build -p arceos-sleep
- name: Build task/priority
run: cargo build -p arceos-priority
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apps/display/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ authors = ["Shiping Yuan <[email protected]>"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
axstd = { path = "../../ulib/axstd", features = ["display"] }
axstd = { path = "../../ulib/axstd", features = ["display"], optional = true }
embedded-graphics = "0.8"
2 changes: 1 addition & 1 deletion apps/display/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use embedded_graphics::pixelcolor::Rgb888;
use embedded_graphics::prelude::{RgbColor, Size};
use embedded_graphics::{draw_target::DrawTarget, prelude::OriginDimensions};

pub use axstd::display::{framebuffer_flush, framebuffer_info, DisplayInfo};
pub use axstd::os::arceos::axdisplay::{framebuffer_flush, framebuffer_info, DisplayInfo};

pub struct Display {
size: Size,
Expand Down
11 changes: 6 additions & 5 deletions apps/display/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#![no_std]
#![no_main]
#![cfg_attr(feature = "axstd", no_std)]
#![cfg_attr(feature = "axstd", no_main)]

extern crate axstd;
mod display;
#[cfg(feature = "axstd")]
extern crate axstd as std;

mod display;
use display::*;

use embedded_graphics::{
Expand Down Expand Up @@ -77,7 +78,7 @@ fn test_gpu() -> i32 {
0
}

#[no_mangle]
#[cfg_attr(feature = "axstd", no_mangle)]
fn main() -> ! {
test_gpu();
loop {
Expand Down
2 changes: 1 addition & 1 deletion apps/exception/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ authors = ["Yuekai Jia <[email protected]>"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
axstd = { path = "../../ulib/axstd", features = ["paging"] }
axstd = { path = "../../ulib/axstd", optional = true }
2 changes: 0 additions & 2 deletions apps/exception/expect_debug_aarch64.out
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ Found physcial memory regions:
boot stack (READ | WRITE | RESERVED)
.bss (READ | WRITE | RESERVED)
free memory (READ | WRITE | FREE)
Initialize global memory allocator...
Initialize kernel page table...
Initialize platform devices...
Primary CPU 0 init OK.
Running exception tests...
Expand Down
2 changes: 0 additions & 2 deletions apps/exception/expect_debug_riscv64.out
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ Found physcial memory regions:
boot stack (READ | WRITE | RESERVED)
.bss (READ | WRITE | RESERVED)
free memory (READ | WRITE | FREE)
Initialize global memory allocator...
Initialize kernel page table...
Initialize platform devices...
Primary CPU 0 init OK.
Running exception tests...
Expand Down
3 changes: 1 addition & 2 deletions apps/exception/expect_debug_x86_64.out
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ Found physcial memory regions:
boot stack (READ | WRITE | RESERVED)
.bss (READ | WRITE | RESERVED)
free memory (READ | WRITE | FREE)
Initialize global memory allocator...
Initialize kernel page table...
Initialize platform devices...
Primary CPU 0 init OK.
Running exception tests...
#BP @ 0x[0-9a-f]\{16\}
Expand Down
13 changes: 8 additions & 5 deletions apps/exception/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#![no_std]
#![no_main]
#![cfg_attr(feature = "axstd", no_std)]
#![cfg_attr(feature = "axstd", no_main)]

use axstd::println;
use core::arch::asm;
#[cfg(feature = "axstd")]
extern crate axstd as std;

use std::arch::asm;
use std::println;

fn raise_break_exception() {
unsafe {
Expand All @@ -15,7 +18,7 @@ fn raise_break_exception() {
}
}

#[no_mangle]
#[cfg_attr(feature = "axstd", no_mangle)]
fn main() {
println!("Running exception tests...");
raise_break_exception();
Expand Down
2 changes: 1 addition & 1 deletion apps/helloworld/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ authors = ["Yuekai Jia <[email protected]>"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
axstd = { path = "../../ulib/axstd" }
axstd = { path = "../../ulib/axstd", optional = true }
11 changes: 7 additions & 4 deletions apps/helloworld/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#![no_std]
#![no_main]
#![cfg_attr(feature = "axstd", no_std)]
#![cfg_attr(feature = "axstd", no_main)]

#[no_mangle]
#[cfg(feature = "axstd")]
use axstd::println;

#[cfg_attr(feature = "axstd", no_mangle)]
fn main() {
axstd::println!("Hello, world!");
println!("Hello, world!");
}
3 changes: 2 additions & 1 deletion apps/memtest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ authors = ["Yuekai Jia <[email protected]>"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
axstd = { path = "../../ulib/axstd", features = ["alloc", "paging"] }
rand = { version = "0.8", default-features = false, features = ["small_rng"] }
axstd = { path = "../../ulib/axstd", features = ["alloc"], optional = true }
33 changes: 18 additions & 15 deletions apps/memtest/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#![no_std]
#![no_main]
#![cfg_attr(feature = "axstd", no_std)]
#![cfg_attr(feature = "axstd", no_main)]

#[macro_use]
extern crate axstd;
extern crate alloc;
#[cfg(feature = "axstd")]
extern crate axstd as std;

use alloc::collections::BTreeMap;
use alloc::vec::Vec;
use axstd::rand;
use rand::{rngs::SmallRng, RngCore, SeedableRng};
use std::collections::BTreeMap;
use std::vec::Vec;

fn test_vec() {
fn test_vec(rng: &mut impl RngCore) {
const N: usize = 1_000_000;
let mut v = Vec::with_capacity(N);
for _ in 0..N {
v.push(rand::rand_u32());
v.push(rng.next_u32());
}
v.sort();
for i in 0..N - 1 {
Expand All @@ -22,12 +22,12 @@ fn test_vec() {
println!("test_vec() OK!");
}

fn test_btree_map() {
fn test_btree_map(rng: &mut impl RngCore) {
const N: usize = 10_000;
let mut m = BTreeMap::new();
for _ in 0..N {
let value = rand::rand_u32();
let key = alloc::format!("key_{value}");
let value = rng.next_u32();
let key = format!("key_{value}");
m.insert(key, value);
}
for (k, v) in m.iter() {
Expand All @@ -38,10 +38,13 @@ fn test_btree_map() {
println!("test_btree_map() OK!");
}

#[no_mangle]
#[cfg_attr(feature = "axstd", no_mangle)]
fn main() {
println!("Running memory tests...");
test_vec();
test_btree_map();

let mut rng = SmallRng::seed_from_u64(0xdead_beef);
test_vec(&mut rng);
test_btree_map(&mut rng);

println!("Memory tests run OK!");
}
2 changes: 1 addition & 1 deletion apps/memtest/test_cmd
Original file line number Diff line number Diff line change
@@ -1 +1 @@
test_one "LOG=trace" "expect_trace.out"
test_one "LOG=trace APP_FEATURES=axstd/paging" "expect_trace.out"
4 changes: 2 additions & 2 deletions apps/net/echoserver/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn reverse(buf: &[u8]) -> Vec<u8> {
lines.join(&b'\n')
}

fn echo_server(mut stream: TcpStream) -> io::Result {
fn echo_server(mut stream: TcpStream) -> io::Result<()> {
let mut buf = [0u8; 1024];
loop {
let n = stream.read(&mut buf)?;
Expand All @@ -37,7 +37,7 @@ fn echo_server(mut stream: TcpStream) -> io::Result {
}
}

fn accept_loop() -> io::Result {
fn accept_loop() -> io::Result<()> {
let (addr, port) = (IpAddr::from_str(LOCAL_IP).unwrap(), LOCAL_PORT);
let listener = TcpListener::bind((addr, port).into())?;
println!("listen on: {}", listener.local_addr().unwrap());
Expand Down
2 changes: 1 addition & 1 deletion apps/net/httpclient/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn get_addr() -> SocketAddr {
addr_iter[0]
}

fn client() -> io::Result {
fn client() -> io::Result<()> {
let mut stream = TcpStream::connect(get_addr())?;
stream.write(REQUEST.as_bytes())?;
let mut buf = [0; 2048];
Expand Down
4 changes: 2 additions & 2 deletions apps/net/httpserver/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const CONTENT: &str = r#"<html>
</html>
"#;

fn http_server(mut stream: TcpStream) -> io::Result {
fn http_server(mut stream: TcpStream) -> io::Result<()> {
let mut buf = [0u8; 1024];
stream.read(&mut buf)?;

Expand All @@ -60,7 +60,7 @@ fn http_server(mut stream: TcpStream) -> io::Result {
Ok(())
}

fn accept_loop() -> io::Result {
fn accept_loop() -> io::Result<()> {
let (addr, port) = (IpAddr::from_str(LOCAL_IP).unwrap(), LOCAL_PORT);
let listener = TcpListener::bind((addr, port).into())?;
println!("listen on: http://{}/", listener.local_addr().unwrap());
Expand Down
2 changes: 1 addition & 1 deletion apps/net/udpserver/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use axstd::net::{IpAddr, UdpSocket};
const LOCAL_IP: &str = "0.0.0.0";
const LOCAL_PORT: u16 = 5555;

fn receive_loop() -> io::Result {
fn receive_loop() -> io::Result<()> {
let (addr, port) = (IpAddr::from_str(LOCAL_IP).unwrap(), LOCAL_PORT);
let socket = UdpSocket::bind((addr, port).into())?;
println!("listen on: {}", socket.local_addr().unwrap());
Expand Down
3 changes: 2 additions & 1 deletion apps/task/parallel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ sched_rr = ["axstd/sched_rr"]
sched_cfs = ["axstd/sched_cfs"]

[dependencies]
axstd = { path = "../../../ulib/axstd", features = ["alloc", "paging", "multitask", "irq"] }
rand = { version = "0.8", default-features = false, features = ["small_rng"] }
axstd = { path = "../../../ulib/axstd", features = ["alloc", "multitask", "irq"], optional = true }
37 changes: 18 additions & 19 deletions apps/task/parallel/expect_info_smp1_fifo.out
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,28 @@ Found physcial memory regions:
.bss (READ | WRITE | RESERVED)
free memory (READ | WRITE | FREE)
Initialize global memory allocator...
Initialize kernel page table...
Initialize platform devices...
Initialize scheduling...
use FIFO scheduler.
Initialize interrupt handlers...
Primary CPU 0 init OK.
part 0: TaskId(4) \[0, 125000)
part 1: TaskId(5) \[125000, 250000)
part 2: TaskId(6) \[250000, 375000)
part 3: TaskId(7) \[375000, 500000)
part 4: TaskId(8) \[500000, 625000)
part 5: TaskId(9) \[625000, 750000)
part 6: TaskId(10) \[750000, 875000)
part 7: TaskId(11) \[875000, 1000000)
part 8: TaskId(12) \[1000000, 1125000)
part 9: TaskId(13) \[1125000, 1250000)
part 10: TaskId(14) \[1250000, 1375000)
part 11: TaskId(15) \[1375000, 1500000)
part 12: TaskId(16) \[1500000, 1625000)
part 13: TaskId(17) \[1625000, 1750000)
part 14: TaskId(18) \[1750000, 1875000)
part 15: TaskId(19) \[1875000, 2000000)
part 15: TaskId(19) finished
sum = 61783189038
part 0: ThreadId(4) \[0, 125000)
part 1: ThreadId(5) \[125000, 250000)
part 2: ThreadId(6) \[250000, 375000)
part 3: ThreadId(7) \[375000, 500000)
part 4: ThreadId(8) \[500000, 625000)
part 5: ThreadId(9) \[625000, 750000)
part 6: ThreadId(10) \[750000, 875000)
part 7: ThreadId(11) \[875000, 1000000)
part 8: ThreadId(12) \[1000000, 1125000)
part 9: ThreadId(13) \[1125000, 1250000)
part 10: ThreadId(14) \[1250000, 1375000)
part 11: ThreadId(15) \[1375000, 1500000)
part 12: ThreadId(16) \[1500000, 1625000)
part 13: ThreadId(17) \[1625000, 1750000)
part 14: ThreadId(18) \[1750000, 1875000)
part 15: ThreadId(19) \[1875000, 2000000)
part 15: ThreadId(19) finished
sum = 87362923216
Parallel summation tests run OK!
Shutting down...
Loading

0 comments on commit 5ba6043

Please sign in to comment.