Skip to content

Commit 92a329a

Browse files
authored
feat(script): remove the code that suspend and resume the scheduler via a fully suspended state (#5262)
### What problem does this PR solve? Remove some dead code. Currently, the validator does not suspend and resume the scheduler through a fully suspended state, so attempting to remove it. ### Related changes - PR to update `owner/repo`: - Need to cherry-pick to the release branch ### Check List <!--REMOVE the items that are not applicable--> Tests <!-- At least one of them must be included. --> - Unit test - Integration test - Manual test (add detailed scripts or steps below) - No code Side effects - Performance regression - Breaking backward compatibility
1 parent a7bbe48 commit 92a329a

8 files changed

Lines changed: 36 additions & 1573 deletions

File tree

script/src/scheduler.rs

Lines changed: 3 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use crate::syscalls::{
55
};
66

77
use crate::types::{
8-
DataLocation, DataPieceId, FIRST_FD_SLOT, FIRST_VM_ID, Fd, FdArgs, FullSuspendedState,
9-
IterationResult, Message, ReadState, RunMode, SgData, SyscallGenerator, TerminatedResult,
10-
VmArgs, VmContext, VmId, VmState, WriteState,
8+
DataLocation, DataPieceId, FIRST_FD_SLOT, FIRST_VM_ID, Fd, FdArgs, IterationResult, Message,
9+
ReadState, RunMode, SgData, SyscallGenerator, TerminatedResult, VmArgs, VmContext, VmId,
10+
VmState, WriteState,
1111
};
1212
use ckb_traits::{CellDataProvider, ExtensionProvider, HeaderProvider};
1313
use ckb_types::core::Cycle;
@@ -201,80 +201,6 @@ where
201201
}
202202
}
203203

204-
/// Resume a previously suspended scheduler state
205-
pub fn resume(
206-
sg_data: SgData<DL>,
207-
syscall_generator: SyscallGenerator<DL, V, M::Inner>,
208-
syscall_context: V,
209-
full: FullSuspendedState,
210-
) -> Self {
211-
let mut scheduler = Self {
212-
sg_data,
213-
syscall_generator,
214-
syscall_context,
215-
total_cycles: Arc::new(AtomicU64::new(full.total_cycles)),
216-
iteration_cycles: full.iteration_cycles,
217-
next_vm_id: full.next_vm_id,
218-
next_fd_slot: full.next_fd_slot,
219-
states: full
220-
.vms
221-
.iter()
222-
.map(|(id, state, _)| (*id, state.clone()))
223-
.collect(),
224-
fds: full.fds.into_iter().collect(),
225-
inherited_fd: full.inherited_fd.into_iter().collect(),
226-
instantiated: BTreeMap::default(),
227-
suspended: full
228-
.vms
229-
.into_iter()
230-
.map(|(id, _, snapshot)| (id, snapshot))
231-
.collect(),
232-
message_box: Arc::new(Mutex::new(Vec::new())),
233-
terminated_vms: full.terminated_vms.into_iter().collect(),
234-
root_vm_args: Vec::new(),
235-
};
236-
scheduler
237-
.ensure_vms_instantiated(&full.instantiated_ids)
238-
.unwrap();
239-
// NOTE: suspending/resuming a scheduler is part of CKB's implementation
240-
// details. It is not part of execution consensue. We should not charge
241-
// cycles for them.
242-
scheduler.iteration_cycles = 0;
243-
scheduler
244-
}
245-
246-
/// Suspend current scheduler into a serializable full state
247-
pub fn suspend(mut self) -> Result<FullSuspendedState, Error> {
248-
assert!(self.message_box.lock().expect("lock").is_empty());
249-
let mut vms = Vec::with_capacity(self.states.len());
250-
let instantiated_ids: Vec<_> = self.instantiated.keys().cloned().collect();
251-
for id in &instantiated_ids {
252-
self.suspend_vm(id)?;
253-
}
254-
for (id, state) in self.states {
255-
let snapshot = self
256-
.suspended
257-
.remove(&id)
258-
.ok_or_else(|| Error::Unexpected("Unable to find VM Id".to_string()))?;
259-
vms.push((id, state, snapshot));
260-
}
261-
Ok(FullSuspendedState {
262-
// NOTE: suspending a scheduler is actually part of CKB's
263-
// internal execution logic, it does not belong to VM execution
264-
// consensus. We are not charging cycles for suspending
265-
// a VM in the process of suspending the whole scheduler.
266-
total_cycles: self.total_cycles.load(Ordering::Acquire),
267-
iteration_cycles: self.iteration_cycles,
268-
next_vm_id: self.next_vm_id,
269-
next_fd_slot: self.next_fd_slot,
270-
vms,
271-
fds: self.fds.into_iter().collect(),
272-
inherited_fd: self.inherited_fd.into_iter().collect(),
273-
terminated_vms: self.terminated_vms.into_iter().collect(),
274-
instantiated_ids,
275-
})
276-
}
277-
278204
/// This is the only entrypoint for running the scheduler,
279205
/// both newly created instance and resumed instance are supported.
280206
/// It accepts 2 run mode, one can either limit the cycles to execute,

script/src/types.rs

Lines changed: 2 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,8 @@ use ckb_traits::CellDataProvider;
2626
use ckb_vm::snapshot2::Snapshot2Context;
2727

2828
use ckb_vm::{
29-
DefaultMachineRunner, RISCV_GENERAL_REGISTER_NUMBER, SupportMachine,
30-
bytes::Bytes,
31-
machine::Pause,
32-
snapshot2::{DataSource, Snapshot2},
29+
DefaultMachineRunner, SupportMachine, bytes::Bytes, machine::Pause, snapshot2::DataSource,
3330
};
34-
use std::mem::size_of;
3531

3632
/// The type of CKB-VM ISA.
3733
pub type VmIsa = u8;
@@ -197,13 +193,10 @@ impl fmt::Display for ScriptGroupType {
197193
}
198194

199195
/// Struct specifies which script has verified so far.
200-
/// State is lifetime free, but capture snapshot need heavy memory copy
201196
#[derive(Clone)]
202197
pub struct TransactionState {
203198
/// current suspended script index
204199
pub current: usize,
205-
/// vm scheduler suspend state
206-
pub state: Option<FullSuspendedState>,
207200
/// current consumed cycle
208201
pub current_cycles: Cycle,
209202
/// limit cycles
@@ -212,15 +205,9 @@ pub struct TransactionState {
212205

213206
impl TransactionState {
214207
/// Creates a new TransactionState struct
215-
pub fn new(
216-
state: Option<FullSuspendedState>,
217-
current: usize,
218-
current_cycles: Cycle,
219-
limit_cycles: Cycle,
220-
) -> Self {
208+
pub fn new(current: usize, current_cycles: Cycle, limit_cycles: Cycle) -> Self {
221209
TransactionState {
222210
current,
223-
state,
224211
current_cycles,
225212
limit_cycles,
226213
}
@@ -488,67 +475,6 @@ impl TryFrom<(u64, u64, u64)> for DataPieceId {
488475
}
489476
}
490477

491-
/// Full state representing all VM instances from verifying a CKB script.
492-
/// It should be serializable to binary formats, while also be able to
493-
/// fully recover the running environment with the full transaction environment.
494-
#[derive(Clone, Debug)]
495-
pub struct FullSuspendedState {
496-
/// Total executed cycles
497-
pub total_cycles: Cycle,
498-
/// Iteration cycles. Due to an implementation bug in Meepo hardfork,
499-
/// this value will not always be zero at visible execution boundaries.
500-
/// We will have to preserve this value.
501-
pub iteration_cycles: Cycle,
502-
/// Next available VM ID
503-
pub next_vm_id: VmId,
504-
/// Next available file descriptor
505-
pub next_fd_slot: u64,
506-
/// Suspended VMs
507-
pub vms: Vec<(VmId, VmState, Snapshot2<DataPieceId>)>,
508-
/// Opened file descriptors with owners
509-
pub fds: Vec<(Fd, VmId)>,
510-
/// Inherited file descriptors for each spawned process
511-
pub inherited_fd: Vec<(VmId, Vec<Fd>)>,
512-
/// Terminated VMs with exit codes
513-
pub terminated_vms: Vec<(VmId, i8)>,
514-
/// Currently instantiated VMs. Upon resumption, those VMs will
515-
/// be instantiated.
516-
pub instantiated_ids: Vec<VmId>,
517-
}
518-
519-
impl FullSuspendedState {
520-
/// Calculates the size of current suspended state, should be used
521-
/// to derive cycles charged for suspending / resuming.
522-
pub fn size(&self) -> u64 {
523-
(size_of::<Cycle>()
524-
+ size_of::<VmId>()
525-
+ size_of::<u64>()
526-
+ size_of::<u64>()
527-
+ self.vms.iter().fold(0, |mut acc, (_, _, snapshot)| {
528-
acc += size_of::<VmId>() + size_of::<VmState>();
529-
acc += snapshot.pages_from_source.len()
530-
* (size_of::<u64>()
531-
+ size_of::<u8>()
532-
+ size_of::<DataPieceId>()
533-
+ size_of::<u64>()
534-
+ size_of::<u64>());
535-
for dirty_page in &snapshot.dirty_pages {
536-
acc += size_of::<u64>() + size_of::<u8>() + dirty_page.2.len();
537-
}
538-
acc += size_of::<u32>()
539-
+ RISCV_GENERAL_REGISTER_NUMBER * size_of::<u64>()
540-
+ size_of::<u64>()
541-
+ size_of::<u64>()
542-
+ size_of::<u64>();
543-
acc
544-
})
545-
+ (self.fds.len() * (size_of::<Fd>() + size_of::<VmId>()))) as u64
546-
+ (self.inherited_fd.len() * (size_of::<Fd>())) as u64
547-
+ (self.terminated_vms.len() * (size_of::<VmId>() + size_of::<i8>())) as u64
548-
+ (self.instantiated_ids.len() * size_of::<VmId>()) as u64
549-
}
550-
}
551-
552478
/// A cell that is either loaded, or not yet loaded.
553479
#[derive(Debug, PartialEq, Eq, Clone)]
554480
pub enum DataGuard {

0 commit comments

Comments
 (0)