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

Patch time #9

Open
wants to merge 14 commits into
base: feat/sp1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
11 changes: 11 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ members = [
"zk-keygen",
"zk-sdk",
"zk-token-sdk",
# Patches to make this code run under the SP1 prover (time support)
"patches",
]

exclude = ["programs/sbf", "svm/tests/example-programs"]
Expand Down Expand Up @@ -411,6 +413,7 @@ solana-zk-keygen = { path = "zk-keygen", version = "=2.0.5" }
solana-zk-sdk = { path = "zk-sdk", version = "=2.0.5" }
solana-zk-token-proof-program = { path = "programs/zk-token-proof", version = "=2.0.5" }
solana-zk-token-sdk = { path = "zk-token-sdk", version = "=2.0.5" }
solana-patches = { path = "patches", version = "=2.0.5" }
solana_rbpf = "=0.8.4"
spl-associated-token-account = "=4.0.0"
spl-instruction-padding = "0.2"
Expand Down
1 change: 1 addition & 0 deletions accounts-db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ solana-inline-spl = { workspace = true }
solana-measure = { workspace = true }
# solana-metrics = { workspace = true }
solana-nohash-hasher = { workspace = true }
solana-patches = { workspace = true }
solana-rayon-threadlimit = { workspace = true }
solana-sdk = { workspace = true }
solana-stake-program = { workspace = true, optional = true }
Expand Down
7 changes: 5 additions & 2 deletions accounts-db/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ use {
Arc, Condvar, Mutex,
},
thread::{sleep, Builder},
time::{Duration, Instant},
},
tempfile::TempDir,
solana_patches::time::{Duration, Instant},
};

const PAGE_SIZE: u64 = 4 * 1024;
Expand Down Expand Up @@ -5330,6 +5330,7 @@ impl AccountsDb {

#[cfg(test)]
{
use std::time::Duration;
Yiwen-Gao marked this conversation as resolved.
Show resolved Hide resolved
// Give some time for cache flushing to occur here for unit tests
sleep(Duration::from_millis(self.load_delay));
}
Expand Down Expand Up @@ -6597,6 +6598,7 @@ impl AccountsDb {
let flush_stats = self.accounts_cache.slot_cache(slot).map(|slot_cache| {
#[cfg(test)]
{
use std::time::Duration;
// Give some time for cache flushing to occur here for unit tests
sleep(Duration::from_millis(self.load_delay));
}
Expand Down Expand Up @@ -9245,7 +9247,7 @@ impl AccountsDb {
{
// 10 ms is long enough to allow some flushing to occur before insertion is resumed.
// callers of this are typically run in parallel, so many threads will be sleeping at different starting intervals, waiting to resume insertion.
sleep(Duration::from_millis(10));
// sleep(Duration::from_millis(10));
}
}

Expand Down Expand Up @@ -9756,6 +9758,7 @@ pub mod tests {
str::FromStr,
sync::{atomic::AtomicBool, RwLock},
thread::{self, Builder, JoinHandle},
time::Duration,
},
test_case::test_case,
};
Expand Down
4 changes: 2 additions & 2 deletions accounts-db/src/bucket_map_holder_stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ impl BucketMapHolderStats {
let mut thread_time_elapsed_ms = elapsed_ms * storage.threads as u64;
if disk.is_some() {
// if was_startup {
// these stats only apply at startup
// these stats only apply at startup
// datapoint_info!(
// "accounts_index_startup",
// (
Expand Down Expand Up @@ -546,7 +546,7 @@ impl BucketMapHolderStats {
// i64
// ),
// );
// } else {
// } else {
// datapoint_info!(
// if startup || was_startup {
// thread_time_elapsed_ms *= 2; // more threads are allocated during startup
Expand Down
1 change: 1 addition & 0 deletions measure/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ edition = { workspace = true }
[dependencies]
log = { workspace = true }
solana-sdk = { workspace = true }
solana-patches = { workspace = true }

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
2 changes: 1 addition & 1 deletion measure/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ macro_rules! measure {
#[macro_export]
macro_rules! measure_us {
($val:expr) => {{
let start = std::time::Instant::now();
let start = solana_patches::time::Instant::now();
let result = $val;
(result, solana_sdk::timing::duration_as_us(&start.elapsed()))
}};
Expand Down
22 changes: 10 additions & 12 deletions measure/src/measure.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use {
solana_patches::time::{Duration, Instant},
solana_sdk::timing::{duration_as_ms, duration_as_ns, duration_as_s, duration_as_us},
std::{
fmt,
time::{Duration, Instant},
},
std::fmt,
};

#[derive(Debug)]
Expand Down Expand Up @@ -87,14 +85,14 @@ impl fmt::Display for Measure {
mod tests {
use {super::*, std::thread::sleep};

#[test]
fn test_measure() {
let test_duration = Duration::from_millis(100);
let mut measure = Measure::start("test");
sleep(test_duration);
measure.stop();
assert!(measure.as_duration() >= test_duration);
}
// #[test]
// fn test_measure() {
// let test_duration = Duration::from_millis(100);
// let mut measure = Measure::start("test");
// sleep(test_duration);
// measure.stop();
// assert!(measure.as_duration() >= test_duration);
// }

#[test]
fn test_measure_as() {
Expand Down
16 changes: 16 additions & 0 deletions patches/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "solana-patches"
description = "Patches to work around SP1 time and other util restrictions"
documentation = ""
version = { workspace = true }
authors = { workspace = true }
repository = { workspace = true }
homepage = { workspace = true }
license = { workspace = true }
edition = { workspace = true }

[dependencies]
serde = { version = "1.0", features = ["derive"] }

[lib]
name = "solana_patches"
1 change: 1 addition & 0 deletions patches/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod time;
109 changes: 109 additions & 0 deletions patches/src/time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use std::ops::{Add, AddAssign, Div, Sub};

#[derive(Clone, Copy, Default, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Instant {}

impl Instant {
pub fn now() -> Self {
Self::default()
}

pub fn elapsed(&self) -> Duration {
Duration::default()
}

pub fn duration_since(&self, _time: Instant) -> Duration {
Duration::default()
}
}

impl Sub for Instant {
type Output = Duration;

fn sub(self, _other: Self) -> Duration {
Duration::default()
}
}

#[derive(
Clone, Copy, Default, Debug, PartialEq, Eq, PartialOrd, Ord, serde::Serialize, serde::Deserialize,
)]
pub struct Duration {}

impl Duration {
pub const fn new(_secs: u64, _nanos: u32) -> Duration {
Duration {}
}

pub const fn from_nanos(_nanos: u64) -> Duration {
Self::new(0, 0)
}

pub const fn from_micros(_micros: u64) -> Duration {
Self::new(0, 0)
}

pub const fn from_millis(_millis: u64) -> Duration {
Self::new(0, 0)
}

pub const fn from_secs(_secs: u64) -> Duration {
Self::new(0, 0)
}

pub const fn as_secs(&self) -> u64 {
0
}

pub const fn as_nanos(&self) -> u128 {
0
}

pub const fn as_micros(&self) -> u128 {
0
}

pub const fn subsec_nanos(&self) -> u32 {
0
}

pub const ZERO: Duration = Duration::from_nanos(0);
pub const MAX: Duration = Duration::new(u64::MAX, 1);
}

impl Add for Duration {
type Output = Self;
fn add(self, _other: Self) -> Self {
self
}
}

impl AddAssign for Duration {
fn add_assign(&mut self, _other: Self) {
}
}

impl Div<u32> for Duration {
type Output = Self;
fn div(self, _other: u32) -> Self {
self
}
}

#[derive(Default)]
pub struct SystemTime {}

impl SystemTime {
pub fn now() -> SystemTime {
Self::default()
}

pub fn duration_since(
&self,
_time: SystemTime,
) -> Result<Duration, Box<dyn std::error::Error>> {
Ok(Duration::default())
}
}

pub const UNIX_EPOCH: SystemTime = SystemTime {};
1 change: 1 addition & 0 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ solana-inline-spl = { workspace = true }
solana-loader-v4-program = { workspace = true }
solana-measure = { workspace = true }
# solana-perf = { workspace = true }
solana-patches = { workspace = true }
solana-program-runtime = { workspace = true }
solana-rayon-threadlimit = { workspace = true }
solana-sdk = { workspace = true }
Expand Down
4 changes: 2 additions & 2 deletions runtime/src/accounts_background_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use {
accounts_db::CalcAccountsHashDataSource, accounts_hash::CalcAccountsHashConfig,
},
solana_measure::{measure::Measure, measure_us},
solana_patches::time::Instant,
Yiwen-Gao marked this conversation as resolved.
Show resolved Hide resolved
solana_sdk::clock::{BankId, Slot},
stats::StatsManager,
std::{
Expand All @@ -32,7 +33,6 @@ use {
Arc, RwLock,
},
thread::{self, sleep, Builder, JoinHandle},
time::{Duration, Instant},
},
};

Expand Down Expand Up @@ -694,7 +694,7 @@ impl AccountsBackgroundService {
}
}
stats.record_and_maybe_submit(start_time.elapsed());
sleep(Duration::from_millis(INTERVAL_MS));
// sleep(Duration::from_millis(INTERVAL_MS));
}
info!("AccountsBackgroundService has stopped");
})
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/accounts_background_service/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use {
// solana_metrics::datapoint_info,
std::time::{Duration, Instant},
solana_patches::time::{Duration, Instant},
};

const SUBMIT_INTERVAL: Duration = Duration::from_secs(60);
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/bank_forks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use {
crossbeam_channel::SendError,
log::*,
solana_measure::measure::Measure,
solana_patches::time::Instant,
solana_program_runtime::loaded_programs::{BlockRelation, ForkGraph},
solana_sdk::{
clock::{Epoch, Slot},
Expand All @@ -25,7 +26,6 @@ use {
atomic::{AtomicBool, AtomicU64, Ordering},
Arc, RwLock,
},
time::Instant,
},
thiserror::Error,
};
Expand Down
1 change: 1 addition & 0 deletions sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ sha3 = { workspace = true, optional = true }
siphasher = { workspace = true }
solana-frozen-abi = { workspace = true, optional = true }
solana-frozen-abi-macro = { workspace = true, optional = true }
solana-patches = { workspace = true }
solana-program = { workspace = true }
solana-sdk-macro = { workspace = true }
thiserror = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/genesis_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ use {
bincode::{deserialize, serialize},
chrono::{TimeZone, Utc},
memmap2::Mmap,
solana_patches::time::{SystemTime, UNIX_EPOCH},
Copy link
Author

Choose a reason for hiding this comment

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

These are only used to create defaults for GensisConfig, so it should be fine to have dummy values.

std::{
collections::BTreeMap,
fmt,
fs::{File, OpenOptions},
io::Write,
path::{Path, PathBuf},
str::FromStr,
time::{SystemTime, UNIX_EPOCH},
},
};

Expand Down
Loading