Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pull request to a new branch #1

Open
wants to merge 48 commits into
base: mastesr
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
3efccb2
Update README.md
hercule-karuha May 9, 2024
8c236b2
blue probe
hercule-karuha May 12, 2024
6abe977
Create LICENSE
hercule-karuha May 13, 2024
2bea3b6
add put_t and get_t
hercule-karuha May 13, 2024
c4d9c79
add ignore
hercule-karuha May 13, 2024
261a6fa
bluesim probe ilb
hercule-karuha May 13, 2024
adfb73a
rb-link
hercule-karuha May 13, 2024
17b899e
add examples
hercule-karuha May 13, 2024
a127a7f
clippy fix
hercule-karuha May 13, 2024
b9f4b30
remove warning
hercule-karuha May 13, 2024
7a362ef
add ignore
hercule-karuha May 13, 2024
feccc7a
change to server
hercule-karuha May 14, 2024
79428ef
change RProbe type
hercule-karuha May 14, 2024
dd60176
pass simple test
hercule-karuha May 16, 2024
b30ad1f
reduce nest
hercule-karuha May 16, 2024
678315f
add get by id and bycle
hercule-karuha May 16, 2024
097754b
remove debug message
hercule-karuha May 16, 2024
43bbd59
add tenstage
hercule-karuha May 16, 2024
5db36e7
fix tenstage error
hercule-karuha May 16, 2024
cc2b1ae
add simple doc and test
hercule-karuha May 17, 2024
1624a37
remove log file
hercule-karuha May 17, 2024
f87343c
clippy fix
hercule-karuha May 17, 2024
5dbcd91
add defaule
hercule-karuha May 17, 2024
06c07d6
add ci
hercule-karuha May 18, 2024
3136b8e
fmt
hercule-karuha May 18, 2024
632c964
change to deafult
hercule-karuha May 18, 2024
73a1294
remove parentheses
hercule-karuha May 19, 2024
ab365e7
remove cargo lock
hercule-karuha May 20, 2024
efe0538
bule to blue
hercule-karuha May 20, 2024
9840cf0
blue typo in ci and readme
hercule-karuha May 20, 2024
f19ea40
fmt
hercule-karuha May 20, 2024
b1eec7b
use OnceLock
hercule-karuha May 20, 2024
c54e01b
change to signle stream && remove sleep
hercule-karuha May 20, 2024
4da6837
add width defination
hercule-karuha May 20, 2024
9f15eda
change test to pass
hercule-karuha May 20, 2024
153db15
allow clippy in test
hercule-karuha May 20, 2024
37b1e0b
add fifo probe type
hercule-karuha May 21, 2024
28f49aa
add pipeline analysis
hercule-karuha May 22, 2024
d390d01
refactor add getter
hercule-karuha May 24, 2024
9aef4c1
add shut down & new_with
hercule-karuha May 25, 2024
b3ee694
add new test
hercule-karuha May 25, 2024
5837a9a
ci fix
hercule-karuha May 25, 2024
847c947
channge readme
hercule-karuha May 25, 2024
1db7f0c
add makefile
hercule-karuha May 25, 2024
56199e3
add example readme
hercule-karuha May 25, 2024
55e4b57
add Publisher & Subscriber
hercule-karuha May 29, 2024
0f8954a
test Publisher
hercule-karuha May 29, 2024
2eba386
change shutdown
hercule-karuha May 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions bluesim-rlib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! please compile this crate into an .a file and then link it to your bluesim executable.
#![warn(clippy::unwrap_used)]
use rb_link::{B2RMessage, GetPutMessage, MsgSizeType, MSG_SIZE_BYTES};
use std::env;
use std::io::{Read, Write};
use std::os::unix::net::UnixStream;
use std::sync::OnceLock;
Expand All @@ -24,7 +25,11 @@ pub unsafe extern "C" fn get(res_ptr: *mut u8, id: u32, _cycles: u32, size: u32)
panic!("cycles over flow!");
}
let mut stream = STREAM.get_or_init(|| {
UnixStream::connect(String::from("/tmp/b2rr2b")).expect("Failed to connect to socket")
let socket = match env::var("B2R_SOCKET") {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duplicated code should go into a function

Ok(path) => path,
Err(_) => "/tmp/b2rr2b".to_string(),
};
UnixStream::connect(String::from(socket)).expect("Failed to connect to socket")
});

let get_message = GetPutMessage::Get(id);
Expand Down Expand Up @@ -61,7 +66,11 @@ pub unsafe extern "C" fn put(id: u32, cycles: u32, data_ptr: *mut u8, size: u32)
}

let mut stream = STREAM.get_or_init(|| {
UnixStream::connect(String::from("/tmp/b2rr2b")).expect("Failed to connect to socket")
let socket = match env::var("B2R_SOCKET") {
Ok(path) => path,
Err(_) => "/tmp/b2rr2b".to_string(),
};
UnixStream::connect(String::from(socket)).expect("Failed to connect to socket")
});

let data_slice = std::slice::from_raw_parts(data_ptr, size as usize);
Expand Down
8 changes: 6 additions & 2 deletions examples/TenStage/TenStage.bsv

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think all examples should have a build script as a playground. It can be a make file or a rust build.rs. No matter which way, we should let the user run the demos using a single command line like 'make'. The example should show the users how to build bsv, how to build probe library, how to link them together, hou to run the upper app to use those probes.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import RProbe::*;
import FIFOF::*;
import Vector::*;

typedef 10 RULE_ID_START;

(* synthesize *)
module mkAdderPipeline(Empty);
function gen_fire_probes(Integer x) = mkRProbe(fromInteger(x + 10));
function gen_fire_probes(Integer x) = mkRProbe(fromInteger(x + valueOf(RULE_ID_START)));

Vector#(9, FIFOF#(Bit#(32))) fifos <- replicateM(mkSizedFIFOF(5));

Expand All @@ -19,6 +21,8 @@ module mkAdderPipeline(Empty);

RProbe#(Bit#(32), Bit#(32)) recv_probe <- mkRProbe(20);

Bit#(32) stuck_num = 32'h5c;

rule stage1;
Bit#(32) data = recv_probe.get_data;
fire_probes[0].put_data(True);
Expand Down Expand Up @@ -48,7 +52,7 @@ module mkAdderPipeline(Empty);

rule stage5;
Bit#(32) data = fifos[3].first;
if(data != 32'h5c) begin
if(data != stuck_num) begin
fifos[3].deq;
fire_probes[4].put_data(True);
fifos[4].enq(data + 1);
Expand Down
1 change: 1 addition & 0 deletions examples/adder-pipeline/AdderPipeline.bsv
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module mkAdderPipeline(Empty);
probe.put_data(data + 1);
put_times <= put_times + 1;
if(put_times == 9) begin
probe.shut_down_server();
$finish;
end
endrule
Expand Down
7 changes: 4 additions & 3 deletions examples/pipeline-analyzer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use std::thread;
use std::time::Duration;

fn main() {
let mut server = B2RServer::new();
let mut server = B2RServer::new_with("/tmp/adder");
let mut id_getter = IDGetter::new(&server);
for i in 0..10 {
let num: u32 = i;
server.put(0, num.to_le_bytes().to_vec())
Expand All @@ -13,15 +14,15 @@ fn main() {

thread::sleep(Duration::from_secs(3));

let msg_vec = server.get_id_all(0);
let msg_vec = id_getter.get_id_all(0);
for msg in msg_vec {
println!(
"get from blue id:{}, cycle:{}, data:{}",
msg.id,
msg.cycles,
u32::from_le_bytes([
msg.message[0],
msg.message[1],
msg.message[1],
msg.message[2],
msg.message[3]
])
Expand Down
21 changes: 12 additions & 9 deletions examples/ten_stage_analysis/src/main.rs
hercule-karuha marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ use std::time::Duration;
fn main() {
hercule-karuha marked this conversation as resolved.
Show resolved Hide resolved
let input_data: Vec<u32> = (1..200).collect();

let mut server = B2RServer::new();
let mut server = B2RServer::new_with("/tmp/ten_stage");
let mut pipe_getter = PipeLineGetter::new(&server);

for i in input_data {
server.put(20, i.to_le_bytes().to_vec())
}
// mark the probe as fifo or fired
for i in 0..9 {
server.give_type(ProbeType::Fifo, i as u32);
let id: u32 = i;
pipe_getter.add_fifo_probe(id);
}
for i in 10..19 {
server.give_type(ProbeType::Fired, i as u32);
let id: u32 = i;
pipe_getter.add_rule_probe(id);
}

for i in input_data {
server.put(20, i.to_le_bytes().to_vec())
}

let handlle = server.serve();
Expand All @@ -24,7 +27,7 @@ fn main() {

let mut fired: u32 = 0;
loop {
let state = server.get_pipeline_state();
let state = pipe_getter.get_pipeline_state();
if state.fire_rules.len() as u32 > fired {
fired = state.fire_rules.len() as u32;
} else if state.empty_fifos.len() + state.full_fifos.len() == 9 { // if all fifos is empty or full the pipeline is stuck
Expand All @@ -39,7 +42,7 @@ fn main() {
}
print!("\nfired rules:");
for id in state.fire_rules {
print!(" {} ", id)
print!(" {} ", id)
}
print!("\n");
break;
Expand Down
8 changes: 8 additions & 0 deletions probe-blue/RProbe.bsv
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ import FIFOF::*;
interface RProbe#(type get_t, type put_t);
method get_t get_data();
method Action put_data(put_t data);
// should be called when there is no more message to send
method Action shut_down_server();
endinterface

module mkRProbe#(Bit#(WORD_WIDTH) id)(RProbe#(get_t, put_t)) provisos(Bits#(get_t, wid_get), Bits#(put_t, wid_put));
Bit#(WORD_WIDTH) shut_down_id = 32'hFFFFFFFF;
Bit#(WORD_WIDTH) shut_down_msg = 32'h0;
Bit#(WORD_WIDTH) get_size = fromInteger(valueOf(TDiv#(wid_get, BYTE_WIDTH)));
Bit#(WORD_WIDTH) put_size = fromInteger(valueOf(TDiv#(wid_put, BYTE_WIDTH)));

Expand All @@ -30,6 +34,10 @@ module mkRProbe#(Bit#(WORD_WIDTH) id)(RProbe#(get_t, put_t)) provisos(Bits#(get_
let bvec = pack(data);
put(id, cycles, bvec, put_size);
endmethod

method Action shut_down_server();
put(shut_down_id, cycles, shut_down_msg, put_size);
endmethod
endmodule

module mkFIFOFProbe#(Bit#(WORD_WIDTH) id, FIFOF#(t) fifo)(Empty);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since some id is kept for internal use(e.g., control shutdown), you should check if the user supplied id is conflict with those reserved ids.

Expand Down
1 change: 1 addition & 0 deletions rb_link/src/config.rs
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We must treat naming seriously. For the name config, it means something that can be tuned. But thoes two things here seems not. Maybe this file should be called consts.rs or something else?

Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub type MsgSizeType = u32;
pub const MSG_SIZE_BYTES: usize = std::mem::size_of::<MsgSizeType>();
pub const SHUT_DOWN_ID:u32 = 0xFFFFFFFF;
2 changes: 1 addition & 1 deletion rb_link/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod test;
mod test;

mod config;
mod server;
Expand Down
33 changes: 23 additions & 10 deletions rb_link/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub enum GetPutMessage {

#[derive(Serialize, Deserialize)]
struct R2BMessage {
id: u32,
id: u32,
message: Vec<u8>,
}

Expand All @@ -36,41 +36,50 @@ pub struct B2RMessage {
/// A server for interacting with Bluesim.
/// Cache bidirectional data and send data upon receiving requests.
pub struct B2RServer {
socket_path: String,
b2r_cache: Arc<Mutex<HashMap<u32, VecDeque<B2RMessage>>>>,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the simulation run a long time, will this buffer overflow or lead to OOM?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The getter poped from the cache, i think the message shouldn't wait too long to be processed in debug program.
So I think it won't lead to OOM.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use a channel to do cross thread communication? What you did here is almost build a channel by hand and make code verbose.

r2b_cache: Arc<Mutex<HashMap<u32, VecDeque<R2BMessage>>>>,
}




impl Default for B2RServer {
fn default() -> Self {
Self::new()
}
}

impl B2RServer {
/// make a new server
/// make a new server with socket: /tmp/b2rr2b
pub fn new() -> Self {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this API should be removed

B2RServer {
socket_path: "/tmp/b2rr2b".to_string(),
b2r_cache: Arc::new(Mutex::new(HashMap::new())),
r2b_cache: Arc::new(Mutex::new(HashMap::new())),
}
}


/// make a new server with a socket path
/// you need to specify the B2R_SOCKET environment variable when start the bluesim
pub fn new_with(path: &str) -> Self {
B2RServer {
socket_path: path.to_string(),
b2r_cache: Arc::new(Mutex::new(HashMap::new())),
r2b_cache: Arc::new(Mutex::new(HashMap::new())),
}
}

/// Start a thread to run the server.
/// Create a UnixListener at "/tmp/b2rr2b".

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is outdated

/// Return the JoinHandle of that thread.
/// This function needs to be called before running your Bluesim program.
/// the server thread returns when bluesim called shut_down_server()
pub fn serve(&mut self) -> JoinHandle<()> {
// let probe_infos = self.probe_infos.clone();
let b2r_cache = self.b2r_cache.clone();
let r2b_cache = self.r2b_cache.clone();
let socket_path = self.socket_path.clone();
thread::spawn(move || {
let _ = fs::remove_file("/tmp/b2rr2b");
let listener = UnixListener::bind("/tmp/b2rr2b").expect("Failed to bind Unix listener");
let _ = fs::remove_file(socket_path.as_str());
let listener = UnixListener::bind(socket_path).expect("Failed to bind Unix listener");
let mut stream = match listener.incoming().next() {
Some(stream_res) => stream_res.expect("Fail to connect to bluesim"),
None => panic!("listener returns a None"),
Expand All @@ -96,6 +105,10 @@ impl B2RServer {
}
GetPutMessage::Put(b2r_message) => {
// println!("receive put to id {}", b2r_message.id);
// return if reveive message with SHUT_DOWN_ID
if b2r_message.id == SHUT_DOWN_ID {
return;
}
let mut b2r_cache = b2r_cache.lock().expect("Fail to lock b2r_cache");
let queue = b2r_cache.entry(b2r_message.id).or_default();
queue.push_back(b2r_message);
Expand All @@ -105,7 +118,6 @@ impl B2RServer {
})
}


/// Send a message to the probe with ID "id".
/// Please ensure that message.len() == ceil(get_t_width/8),
/// where get_t_width is the width of get_t defined in your BSV code.
Expand All @@ -115,7 +127,6 @@ impl B2RServer {
let queue = r2b_cache.entry(id).or_default();
queue.push_back(r2b_message);
}

}

fn get_msg_size(bytes: Vec<u8>) -> u32 {
Expand All @@ -124,6 +135,8 @@ fn get_msg_size(bytes: Vec<u8>) -> u32 {

fn receive_getput(stream: &mut UnixStream) -> Result<GetPutMessage, Box<bincode::ErrorKind>> {
// println!("connect comeing");
// the first MSG_SIZE_BYTES bytes is the size of message
// use to initialize the buffer
let mut sz_buf: Vec<u8> = vec![0; MSG_SIZE_BYTES];
stream
.read_exact(&mut sz_buf)
Expand Down
Empty file removed rb_link/src/server/server.rs
Empty file.
Loading