Skip to content

Commit

Permalink
Fix how we use the PMA (#11)
Browse files Browse the repository at this point in the history
* Fix how we use the PMA

* bump sword to fix hamt persist in PMA

* apply pma fixes to old trap-loading methods

* format
  • Loading branch information
eamsden authored Aug 27, 2024
1 parent 4f0905d commit aed66f8
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 62 deletions.
16 changes: 8 additions & 8 deletions Cargo.lock

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

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ version = "0.1.0"
edition = "2021"

[workspace.dependencies]
sword = { git = "https://github.com/zorp-corp/sword.git", rev="8bcbf4a9a8f9c870aa171577ef002c6dbfc77292" }
sword_macros = { git = "https://github.com/zorp-corp/sword.git", rev="8bcbf4a9a8f9c870aa171577ef002c6dbfc77292" }
assert_no_alloc = { git = "https://github.com/zorp-corp/sword.git", rev="8bcbf4a9a8f9c870aa171577ef002c6dbfc77292" }
sword = { git = "https://github.com/zorp-corp/sword.git", rev="eb4b5132a71302440101c5dd04528c2a875c3d0a" }
sword_macros = { git = "https://github.com/zorp-corp/sword.git", rev="eb4b5132a71302440101c5dd04528c2a875c3d0a" }
assert_no_alloc = { git = "https://github.com/zorp-corp/sword.git", rev="eb4b5132a71302440101c5dd04528c2a875c3d0a" }
anyhow = "1.0"
async-trait = "0.1"
bincode = "2.0.0-rc.3"
Expand All @@ -32,8 +32,8 @@ tracing-subscriber = { version = "0.3.18", features = ["ansi", "env-filter", "re
yaque = "0.6.6"

[profile.dev]
opt-level = 3
# opt-level = 3

[profile.dev.package."*"]
opt-level = 3
# opt-level = 3

2 changes: 1 addition & 1 deletion crown/src/kernel/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct Cli {
#[arg(
long,
help = "Start with a new data directory, removing any existing data",
default_value = "true"
default_value = "false"
)]
pub new: bool,

Expand Down
89 changes: 43 additions & 46 deletions crown/src/kernel/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use sword::jets::warm::Warm;
use sword::mem::NockStack;
use sword::mug::met3_usize;
use sword::noun::{Atom, Cell, DirectAtom, IndirectAtom, Noun, Slots, D, T};
use sword::persist::{pma_meta_get, pma_meta_set, pma_open, Persist};
use sword::persist::{pma_meta_get, pma_meta_set, pma_open, pma_sync, Persist};
use sword::trace::{path_to_cord, write_serf_trace_safe, TraceInfo};
use sword_macros::tas;

Expand Down Expand Up @@ -443,16 +443,18 @@ impl Serf {
///
/// A new `Serf` instance.
fn new(
kernel: Option<Noun>,
event_num: u64,
snapshot: Option<Snapshot>,
kernel_bytes: &[u8],
constant_hot_state: &[HotEntry],
trace_info: Option<TraceInfo>,
) -> Self {
let hot_state = [URBIT_HOT_STATE, constant_hot_state].concat();
let mut stack = NockStack::new(NOCK_STACK_SIZE, 0);
let cache = Hamt::<Noun>::new(&mut stack);
let mut cold = Cold::new(&mut stack);
let (mut cold, event_num) = snapshot.as_ref().map_or_else(
|| (Cold::new(&mut stack), 0),
|snapshot_ref| unsafe { ((*snapshot_ref.0).cold, (*snapshot_ref.0).event_num) },
);
let hot = Hot::init(&mut stack, &hot_state);
let warm = Warm::init(&mut stack, &mut cold, &hot);
let slogger = std::boxed::Box::pin(CrownSlogger {});
Expand All @@ -468,19 +470,22 @@ impl Serf {
trace_info,
};

let arvo = kernel.unwrap_or_else(|| {
let kernel_trap = Noun::cue_bytes_slice(&mut context.stack, kernel_bytes);
let fol = T(&mut context.stack, &[D(9), D(2), D(0), D(1)]);
let arvo = if context.trace_info.is_some() {
let start = Instant::now();
let arvo = interpret(&mut context, kernel_trap, fol).unwrap(); // TODO better error
write_serf_trace_safe(&mut context, "boot", start);
let arvo = snapshot.as_ref().map_or_else(
|| {
let kernel_trap = Noun::cue_bytes_slice(&mut context.stack, kernel_bytes);
let fol = T(&mut context.stack, &[D(9), D(2), D(0), D(1)]);
let arvo = if context.trace_info.is_some() {
let start = Instant::now();
let arvo = interpret(&mut context, kernel_trap, fol).unwrap(); // TODO better error
write_serf_trace_safe(&mut context, "boot", start);
arvo
} else {
interpret(&mut context, kernel_trap, fol).unwrap() // TODO better error
};
arvo
} else {
interpret(&mut context, kernel_trap, fol).unwrap() // TODO better error
};
arvo
});
},
|snapshot_ptr| unsafe { (*snapshot_ptr.0).arvo },
);

let mut serf = Self {
arvo,
Expand Down Expand Up @@ -509,16 +514,18 @@ impl Serf {
///
/// A new `Serf` instance.
fn new_form(
kernel: Option<Noun>,
event_num: u64,
snapshot: Option<Snapshot>,
form_bytes: &[u8],
constant_hot_state: &[HotEntry],
trace_info: Option<TraceInfo>,
) -> Self {
let hot_state = [URBIT_HOT_STATE, constant_hot_state].concat();
let mut stack = NockStack::new(NOCK_STACK_SIZE, 0);
let cache = Hamt::<Noun>::new(&mut stack);
let mut cold = Cold::new(&mut stack);
let (mut cold, event_num) = snapshot.as_ref().map_or_else(
|| (Cold::new(&mut stack), 0),
|snapshot_ref| unsafe { ((*snapshot_ref.0).cold, (*snapshot_ref.0).event_num) },
);
let hot = Hot::init(&mut stack, &hot_state);
let warm = Warm::init(&mut stack, &mut cold, &hot);
let slogger = std::boxed::Box::pin(CrownSlogger {});
Expand All @@ -534,18 +541,21 @@ impl Serf {
trace_info,
};

let arvo = kernel.unwrap_or_else(|| {
let kernel_form = Noun::cue_bytes_slice(&mut context.stack, form_bytes);
let arvo = if context.trace_info.is_some() {
let start = Instant::now();
let arvo = interpret(&mut context, D(0), kernel_form).unwrap(); // TODO better error
write_serf_trace_safe(&mut context, "boot", start);
let arvo = snapshot.as_ref().map_or_else(
|| {
let kernel_form = Noun::cue_bytes_slice(&mut context.stack, form_bytes);
let arvo = if context.trace_info.is_some() {
let start = Instant::now();
let arvo = interpret(&mut context, D(0), kernel_form).unwrap(); // TODO better error
write_serf_trace_safe(&mut context, "boot", start);
arvo
} else {
interpret(&mut context, D(0), kernel_form).unwrap() // TODO better error
};
arvo
} else {
interpret(&mut context, D(0), kernel_form).unwrap() // TODO better error
};
arvo
});
},
|snapshot_ptr| unsafe { (*snapshot_ptr.0).arvo },
);

let mut serf = Self {
arvo,
Expand Down Expand Up @@ -594,12 +604,6 @@ impl Serf {
_ => panic!("Unsupported snapshot version"),
};

let (arvo, event_num): (Option<Noun>, u64) =
snapshot.map_or((None, 0), |snapshot| unsafe {
let mem = snapshot.0;
(Some((*mem).arvo), (*mem).event_num)
});

let trace_info = if trace {
let file = File::create("trace.json").expect("Cannot create trace file trace.json");
let pid = std::process::id();
Expand All @@ -613,9 +617,7 @@ impl Serf {
None
};

Self::new(
arvo, event_num, kernel_bytes, constant_hot_state, trace_info,
)
Self::new(snapshot, kernel_bytes, constant_hot_state, trace_info)
}

/// Loads a Serf instance from a form (compiled Nock formula).
Expand Down Expand Up @@ -652,12 +654,6 @@ impl Serf {
_ => panic!("Unsupported snapshot version"),
};

let (arvo, event_num): (Option<Noun>, u64) =
snapshot.map_or((None, 0), |snapshot| unsafe {
let mem = snapshot.0;
(Some((*mem).arvo), (*mem).event_num)
});

let trace_info = if trace {
let file = File::create("trace.json").expect("Cannot create trace file trace.json");
let pid = std::process::id();
Expand All @@ -671,7 +667,7 @@ impl Serf {
None
};

Self::new_form(arvo, event_num, form_bytes, constant_hot_state, trace_info)
Self::new_form(snapshot, form_bytes, constant_hot_state, trace_info)
}

/// Updates the Serf's state after an event.
Expand All @@ -688,6 +684,7 @@ impl Serf {
self.arvo = new_arvo;
self.event_num = new_event_num;
self.save();
pma_sync();

self.context.cache = Hamt::new(&mut self.context.stack);
self.context.scry_stack = D(0);
Expand Down
2 changes: 1 addition & 1 deletion crown/src/kernel/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod boot;
pub mod form;
pub mod form;
2 changes: 1 addition & 1 deletion crown/src/utils/slogger.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{CrownError, Result};
use assert_no_alloc::permit_alloc;
use either::Either::*;
use std::io::{stderr, Write};
use sword::interpreter::Slogger;
use assert_no_alloc::permit_alloc;
use sword::jets::list::util::lent;
use sword::mem::NockStack;
use sword::noun::{Atom, DirectAtom, IndirectAtom, Noun, Slots};
Expand Down

0 comments on commit aed66f8

Please sign in to comment.