-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add exe and map implementation (#14)
* feat(kernel/map): add initial impl of Map * build(deps): update deps * feat(kernel): add implementation for exe and map * docs(contributing): instruct to use trace! instead of println! * feat(kernel): add more missing functions * test(kernel): add tests
- Loading branch information
1 parent
7c9aaf3
commit 872cb6a
Showing
12 changed files
with
559 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Seeds for failure cases proptest has generated in the past. It is | ||
# automatically read and these particular cases re-run before any | ||
# novel cases are generated. | ||
# | ||
# It is recommended to check this file in to source control so that | ||
# everyone who runs the test benefits from these saved cases. | ||
cc 8ee6fbaff9a6799ce9c76e546e1706ffd7862b73a9b6c632f256c942ab4cad70 # shrinks to exemaps = {ExeMap { map: Map { inner: MapInner { path: "", offset: 0, length: 12812496297095135152, runtime: Mutex { data: RuntimeStats { lnprob: 0.0, seq: 0, block: 0 } } } }, prob: 1.0 }, ExeMap { map: Map { inner: MapInner { path: "", offset: 0, length: 5634247776614416464, runtime: Mutex { data: RuntimeStats { lnprob: 0.0, seq: 0, block: 0 } } } }, prob: 1.0 }} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#![allow(clippy::mutable_key_type)] | ||
|
||
use crate::ExeMap; | ||
use educe::Educe; | ||
use std::{collections::HashSet, path::PathBuf}; | ||
|
||
#[derive(Default, Clone, Educe)] | ||
#[educe(Debug)] | ||
pub struct ExeInner { | ||
pub path: PathBuf, | ||
|
||
#[educe(Debug(ignore))] | ||
pub exemaps: HashSet<ExeMap>, | ||
|
||
pub size: usize, | ||
|
||
pub seq: u64, | ||
|
||
pub time: u64, | ||
|
||
pub update_time: Option<u64>, | ||
|
||
pub running_timestamp: Option<u64>, | ||
|
||
pub change_timestamp: u64, | ||
|
||
pub lnprob: f32, | ||
} | ||
|
||
impl ExeInner { | ||
pub fn new(path: impl Into<PathBuf>) -> Self { | ||
Self { | ||
path: path.into(), | ||
..Default::default() | ||
} | ||
} | ||
|
||
pub fn with_change_timestamp(&mut self, change_timestamp: u64) -> &mut Self { | ||
self.change_timestamp = change_timestamp; | ||
self | ||
} | ||
|
||
pub fn with_running(&mut self, last_running_timestamp: u64) -> &mut Self { | ||
self.update_time.replace(last_running_timestamp); | ||
self.running_timestamp.replace(last_running_timestamp); | ||
self | ||
} | ||
|
||
pub fn with_exemaps(&mut self, exemaps: HashSet<ExeMap>) -> &mut Self { | ||
self.exemaps = exemaps; | ||
let size: usize = self | ||
.exemaps | ||
.iter() | ||
.map(|map| map.map.length()) | ||
.fold(0usize, |acc, x| acc.wrapping_add(x)); | ||
self.size = self.size.wrapping_add(size); | ||
self | ||
} | ||
|
||
pub const fn is_running(&self, last_running_timestamp: u64) -> bool { | ||
if let Some(running_timestamp) = self.running_timestamp { | ||
running_timestamp >= last_running_timestamp | ||
} else { | ||
0 == last_running_timestamp | ||
} | ||
} | ||
|
||
pub fn bid_in_maps(&self, last_running_timestamp: u64) { | ||
if self.is_running(last_running_timestamp) { | ||
self.exemaps.iter().for_each(|v| v.map.increase_lnprob(1.)); | ||
} else { | ||
self.exemaps | ||
.iter() | ||
.for_each(|v| v.map.set_lnprob(self.lnprob)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,111 @@ | ||
pub struct Exe {} | ||
#![allow(clippy::mutable_key_type)] | ||
|
||
mod inner; | ||
|
||
use crate::ExeMap; | ||
use inner::ExeInner; | ||
use parking_lot::Mutex; | ||
use std::{collections::HashSet, path::PathBuf, sync::Arc}; | ||
|
||
#[derive(Debug, Default, Clone)] | ||
pub struct Exe(Arc<Mutex<ExeInner>>); | ||
|
||
impl Exe { | ||
pub fn new(path: impl Into<PathBuf>) -> Self { | ||
Self(Arc::new(Mutex::new(ExeInner::new(path)))) | ||
} | ||
pub fn with_change_timestamp(self, change_timestamp: u64) -> Self { | ||
self.0.lock().with_change_timestamp(change_timestamp); | ||
self | ||
} | ||
|
||
pub fn with_running(self, last_running_timestamp: u64) -> Self { | ||
self.0.lock().with_running(last_running_timestamp); | ||
self | ||
} | ||
|
||
pub fn with_exemaps(self, exemaps: HashSet<ExeMap>) -> Self { | ||
self.0.lock().with_exemaps(exemaps); | ||
self | ||
} | ||
|
||
pub fn path(&self) -> PathBuf { | ||
self.0.lock().path.clone() | ||
} | ||
|
||
pub fn lnprob(&self) -> f32 { | ||
self.0.lock().lnprob | ||
} | ||
|
||
pub fn zero_lnprob(&self) { | ||
self.0.lock().lnprob = 0.0; | ||
} | ||
|
||
pub fn size(&self) -> usize { | ||
self.0.lock().size | ||
} | ||
|
||
pub fn is_running(&self, last_running_timestamp: u64) -> bool { | ||
self.0.lock().is_running(last_running_timestamp) | ||
} | ||
|
||
pub fn update_running_timestamp(&self, running_timestamp: u64) { | ||
self.0.lock().running_timestamp.replace(running_timestamp); | ||
} | ||
|
||
pub fn update_change_timestamp(&self, change_timestamp: u64) { | ||
self.0.lock().change_timestamp = change_timestamp; | ||
} | ||
|
||
pub fn update_time(&self, time: u64) { | ||
self.0.lock().time = time; | ||
} | ||
|
||
pub fn set_seq(&self, seq: u64) { | ||
self.0.lock().seq = seq; | ||
} | ||
|
||
pub fn bid_in_maps(&self, last_running_timestamp: u64) { | ||
self.0.lock().bid_in_maps(last_running_timestamp); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::{ExeMap, Map}; | ||
use pretty_assertions::assert_eq; | ||
use prop::collection::hash_set; | ||
use proptest::prelude::*; | ||
|
||
prop_compose! { | ||
fn arbitrary_map()( | ||
path in ".*", | ||
offset in 0..usize::MAX, | ||
length in 0..usize::MAX, | ||
) -> Map { | ||
Map::new(path, offset, length) | ||
} | ||
} | ||
|
||
prop_compose! { | ||
// create arbitrary ExeMap from arbitrary Map | ||
fn arbitrary_exemap()(map in arbitrary_map()) -> ExeMap { | ||
ExeMap::new(map) | ||
} | ||
} | ||
|
||
proptest! { | ||
#[test] | ||
fn exe_sums_map_sizes(exemaps in hash_set(arbitrary_exemap(), 0..2000)) { | ||
let map_sizes: usize = exemaps | ||
.iter() | ||
.map(|m| m.map.length()) | ||
.fold(0usize, |acc, x| acc.wrapping_add(x)); | ||
let exe = Exe::new("foo").with_exemaps(exemaps); | ||
let exe_size = exe.size(); | ||
|
||
assert_eq!(exe_size, map_sizes); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use crate::Map; | ||
use educe::Educe; | ||
|
||
#[derive(Debug, Default, Clone, Educe)] | ||
#[educe(Eq, PartialEq, Ord, PartialOrd, Hash)] | ||
pub struct ExeMap { | ||
pub map: Map, | ||
|
||
#[educe(Eq(ignore), Ord(ignore), Hash(ignore))] | ||
pub prob: f32, | ||
} | ||
|
||
impl ExeMap { | ||
pub fn new(map: Map) -> Self { | ||
Self { map, prob: 1.0 } | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn exemap_prob_always_1() { | ||
let map = Map::new("test", 0, 0); | ||
let exe_map = ExeMap::new(map); | ||
assert_eq!(exe_map.prob, 1.0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
mod error; | ||
pub mod exe; | ||
pub mod state; | ||
mod exe; | ||
mod exemap; | ||
mod map; | ||
mod state; | ||
pub mod utils; | ||
|
||
pub use error::Error; | ||
pub use exe::Exe; | ||
pub use exemap::ExeMap; | ||
pub use map::{Map, RuntimeStats}; | ||
pub use state::State; |
Oops, something went wrong.