-
Notifications
You must be signed in to change notification settings - Fork 161
underhill_core: specify which CPUs have outstanding IO, not just if they have interrupts #2512
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
Open
mattkur
wants to merge
4
commits into
microsoft:main
Choose a base branch
from
mattkur:finer-grained-cpu-heuristics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
220 changes: 220 additions & 0 deletions
220
openhcl/underhill_core/src/nvme_manager/save_restore_helpers.rs
This file contains hidden or 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,220 @@ | ||||||
| // Copyright (c) Microsoft Corporation. | ||||||
| // Licensed under the MIT License. | ||||||
|
|
||||||
| use crate::nvme_manager::save_restore::NvmeManagerSavedState; | ||||||
| use std::collections::BTreeMap; | ||||||
| use std::collections::btree_map::Entry; | ||||||
|
|
||||||
| /// Useful state about how the VM's vCPUs interacted with NVMe device interrupts at the time of save. | ||||||
| /// | ||||||
| /// This information is used to make heuristic decisions during restore, such as whether to | ||||||
| /// disable sidecar for VMs with active device interrupts. | ||||||
| pub struct VPInterruptState { | ||||||
| /// List of vCPUs with any mapped device interrupts, sorted by CPU ID. | ||||||
| /// This excludes vCPUs that also had outstanding I/O at the time of save, | ||||||
| /// which are counted in `vps_with_outstanding_io`. | ||||||
| pub vps_with_mapped_interrupts_no_io: Vec<u32>, | ||||||
|
|
||||||
| /// List of vCPUs with outstanding I/O at the time of save, sorted by CPU ID. | ||||||
| pub vps_with_outstanding_io: Vec<u32>, | ||||||
| } | ||||||
|
|
||||||
| /// Analyzes the saved NVMe manager state to determine which vCPUs had mapped device interrupts | ||||||
| /// and which had outstanding I/O at the time of save. | ||||||
| /// | ||||||
| /// See [`VPInterruptState`] for more details. | ||||||
| pub fn nvme_interrupt_state(state: Option<&NvmeManagerSavedState>) -> VPInterruptState { | ||||||
| let mut vp_state = BTreeMap::new(); | ||||||
|
|
||||||
| if let Some(state) = state { | ||||||
| for disk in &state.nvme_disks { | ||||||
| for q in &disk.driver_state.worker_data.io { | ||||||
| match vp_state.entry(q.cpu) { | ||||||
| Entry::Vacant(e) => { | ||||||
| e.insert(!q.queue_data.handler_data.pending_cmds.commands.is_empty()); | ||||||
| } | ||||||
| Entry::Occupied(mut e) => { | ||||||
| *e.get_mut() |= !q.queue_data.handler_data.pending_cmds.commands.is_empty(); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| let (vps_with_outstanding_io, vps_with_mapped_interrupts_no_io): (Vec<_>, Vec<_>) = vp_state | ||||||
| .iter() | ||||||
| .map(|(&vp, &has_outstanding_io)| (vp, has_outstanding_io)) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| .partition(|&(_, has_outstanding_io)| has_outstanding_io); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
You can dedup the mappings done below here i think. |
||||||
|
|
||||||
| VPInterruptState { | ||||||
| vps_with_mapped_interrupts_no_io: vps_with_mapped_interrupts_no_io | ||||||
| .into_iter() | ||||||
| .map(|(vp, _)| vp) | ||||||
| .collect(), | ||||||
| vps_with_outstanding_io: vps_with_outstanding_io | ||||||
| .into_iter() | ||||||
| .map(|(vp, _)| vp) | ||||||
| .collect(), | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| #[cfg(test)] | ||||||
| mod tests { | ||||||
| use super::*; | ||||||
| use crate::nvme_manager::save_restore::{NvmeManagerSavedState, NvmeSavedDiskConfig}; | ||||||
| use nvme_driver::NvmeDriverSavedState; | ||||||
| use nvme_driver::save_restore::{ | ||||||
| CompletionQueueSavedState, IoQueueSavedState, NvmeDriverWorkerSavedState, | ||||||
| PendingCommandSavedState, PendingCommandsSavedState, QueueHandlerSavedState, | ||||||
| QueuePairSavedState, SubmissionQueueSavedState, | ||||||
| }; | ||||||
| use nvme_spec as spec; | ||||||
| use zerocopy::FromZeros; | ||||||
|
|
||||||
| #[test] | ||||||
| fn returns_empty_when_state_absent() { | ||||||
| let result = nvme_interrupt_state(None); | ||||||
| assert!(result.vps_with_mapped_interrupts_no_io.is_empty()); | ||||||
| assert!(result.vps_with_outstanding_io.is_empty()); | ||||||
| } | ||||||
|
|
||||||
| #[test] | ||||||
| fn collects_unique_sorted_vps_and_outstanding_subset() { | ||||||
| let state = build_state(vec![ | ||||||
| vec![QueueSpec::new(2, false), QueueSpec::new(1, true)], | ||||||
| vec![QueueSpec::new(1, false), QueueSpec::new(3, true)], | ||||||
| vec![QueueSpec::new(5, false), QueueSpec::new(2, false)], | ||||||
| ]); | ||||||
|
|
||||||
| let result = nvme_interrupt_state(Some(&state)); | ||||||
|
|
||||||
| assert_eq!(result.vps_with_mapped_interrupts_no_io, vec![2, 5]); | ||||||
| assert_eq!(result.vps_with_outstanding_io, vec![1, 3]); | ||||||
| } | ||||||
|
|
||||||
| #[test] | ||||||
| fn reports_outstanding_if_any_queue_pending_for_vp() { | ||||||
| let state = build_state(vec![vec![ | ||||||
| QueueSpec::new(4, false), | ||||||
| QueueSpec::new(4, true), | ||||||
| ]]); | ||||||
|
|
||||||
| let result = nvme_interrupt_state(Some(&state)); | ||||||
|
|
||||||
| assert_eq!( | ||||||
| result.vps_with_mapped_interrupts_no_io, | ||||||
| Vec::<u32>::from_iter([]) | ||||||
| ); | ||||||
| assert_eq!(result.vps_with_outstanding_io, vec![4]); | ||||||
| } | ||||||
|
|
||||||
| #[test] | ||||||
| fn handles_state_with_no_disks() { | ||||||
| let state = NvmeManagerSavedState { | ||||||
| cpu_count: 0, | ||||||
| nvme_disks: Vec::new(), | ||||||
| }; | ||||||
|
|
||||||
| let result = nvme_interrupt_state(Some(&state)); | ||||||
|
|
||||||
| assert!(result.vps_with_mapped_interrupts_no_io.is_empty()); | ||||||
| assert!(result.vps_with_outstanding_io.is_empty()); | ||||||
| } | ||||||
|
|
||||||
| struct QueueSpec { | ||||||
| cpu: u32, | ||||||
| has_outstanding_io: bool, | ||||||
| } | ||||||
|
|
||||||
| impl QueueSpec { | ||||||
| const fn new(cpu: u32, has_outstanding_io: bool) -> Self { | ||||||
| Self { | ||||||
| cpu, | ||||||
| has_outstanding_io, | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // Helper to fabricate NVMe manager save-state snapshots with specific CPU/IO mappings. | ||||||
| fn build_state(disk_queue_specs: Vec<Vec<QueueSpec>>) -> NvmeManagerSavedState { | ||||||
| NvmeManagerSavedState { | ||||||
| cpu_count: 0, // Not relevant for these tests. | ||||||
| nvme_disks: disk_queue_specs | ||||||
| .into_iter() | ||||||
| .enumerate() | ||||||
| .map(|(disk_index, queues)| NvmeSavedDiskConfig { | ||||||
| pci_id: format!("0000:{disk_index:02x}.0"), | ||||||
| driver_state: NvmeDriverSavedState { | ||||||
| identify_ctrl: spec::IdentifyController::new_zeroed(), | ||||||
| device_id: format!("disk{disk_index}"), | ||||||
| namespaces: Vec::new(), | ||||||
| worker_data: NvmeDriverWorkerSavedState { | ||||||
| admin: None, | ||||||
| io: queues | ||||||
| .into_iter() | ||||||
| .enumerate() | ||||||
| .map(|(queue_index, spec)| { | ||||||
| // Tests only care about per-disk affinity, so queue IDs can | ||||||
| // restart from zero for each disk without losing coverage. | ||||||
| build_io_queue( | ||||||
| queue_index as u16, | ||||||
| spec.cpu, | ||||||
| spec.has_outstanding_io, | ||||||
| ) | ||||||
| }) | ||||||
| .collect(), | ||||||
| qsize: 0, | ||||||
| max_io_queues: 0, | ||||||
| }, | ||||||
| }, | ||||||
| }) | ||||||
| .collect(), | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| fn build_io_queue(qid: u16, cpu: u32, outstanding: bool) -> IoQueueSavedState { | ||||||
| IoQueueSavedState { | ||||||
| cpu, | ||||||
| iv: qid as u32, | ||||||
| queue_data: QueuePairSavedState { | ||||||
| mem_len: 0, | ||||||
| base_pfn: 0, | ||||||
| qid, | ||||||
| sq_entries: 1, | ||||||
| cq_entries: 1, | ||||||
| handler_data: QueueHandlerSavedState { | ||||||
| sq_state: SubmissionQueueSavedState { | ||||||
| sqid: qid, | ||||||
| head: 0, | ||||||
| tail: 0, | ||||||
| committed_tail: 0, | ||||||
| len: 1, | ||||||
| }, | ||||||
| cq_state: CompletionQueueSavedState { | ||||||
| cqid: qid, | ||||||
| head: 0, | ||||||
| committed_head: 0, | ||||||
| len: 1, | ||||||
| phase: false, | ||||||
| }, | ||||||
| pending_cmds: build_pending_cmds(outstanding), | ||||||
| aer_handler: None, | ||||||
| }, | ||||||
| }, | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| fn build_pending_cmds(outstanding: bool) -> PendingCommandsSavedState { | ||||||
| PendingCommandsSavedState { | ||||||
| commands: if outstanding { | ||||||
| vec![PendingCommandSavedState { | ||||||
| command: spec::Command::new_zeroed(), | ||||||
| }] | ||||||
| } else { | ||||||
| Vec::new() | ||||||
| }, | ||||||
| next_cid_high_bits: 0, | ||||||
| cid_key_bits: 0, | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: consider destructuring the interrupt state so that new fields won't get forgotten here?