Skip to content

Commit

Permalink
Merge pull request #103 from DLR-FT/dev/partition-id-as-key
Browse files Browse the repository at this point in the history
refactor(hypervisor): use an id to reference partitions instead of a string
  • Loading branch information
sevenautumns authored Feb 20, 2024
2 parents a737082 + 8e280f9 commit 31542a2
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 11 deletions.
5 changes: 3 additions & 2 deletions hypervisor/src/hypervisor/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ use std::net::{SocketAddr, ToSocketAddrs};
use std::path::PathBuf;
use std::time::Duration;

use a653rs::bindings::PartitionId;
use anyhow::anyhow;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -103,7 +104,7 @@ pub struct Config {
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Partition {
/// Partition id
pub id: i64,
pub id: PartitionId,

/// Partition name, used for example as prefix in the log printing
pub name: String,
Expand Down Expand Up @@ -218,7 +219,7 @@ impl Config {
ScheduledTimeframe {
start,
end: start + p.duration,
partition_name: p.name.clone(),
partition: p.id,
}
})
})
Expand Down
7 changes: 4 additions & 3 deletions hypervisor/src/hypervisor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::path::PathBuf;
use std::thread::sleep;
use std::time::{Duration, Instant};

use a653rs::bindings::PartitionId;
use anyhow::anyhow;
use once_cell::sync::OnceCell;

Expand All @@ -29,7 +30,7 @@ pub struct Hypervisor {
cg: CGroup,
major_frame: Duration,
scheduler: Scheduler,
partitions: HashMap<String, Partition>,
partitions: HashMap<PartitionId, Partition>,
sampling_channel: HashMap<String, Sampling>,
prev_cg: PathBuf,
_config: Config,
Expand Down Expand Up @@ -70,12 +71,12 @@ impl Hypervisor {
}

for p in config.partitions.iter() {
if hv.partitions.contains_key(&p.name) {
if hv.partitions.contains_key(&p.id) {
return Err(anyhow!("Partition \"{}\" already exists", p.name))
.lev_typ(SystemError::PartitionConfig, ErrorLevel::ModuleInit);
}
hv.partitions.insert(
p.name.clone(),
p.id,
Partition::new(hv.cg.get_path(), p.clone(), &hv.sampling_channel)
.lev(ErrorLevel::ModuleInit)?,
);
Expand Down
4 changes: 2 additions & 2 deletions hypervisor/src/hypervisor/partition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::process::{Command, Stdio};
use std::thread::sleep;
use std::time::{Duration, Instant};

use a653rs::bindings::PortDirection;
use a653rs::bindings::{PartitionId, PortDirection};
use a653rs::prelude::{OperatingMode, StartCondition};
use anyhow::{anyhow, bail};
use clone3::Clone3;
Expand Down Expand Up @@ -552,7 +552,7 @@ fn send_sockets(base: &Base) -> Result<IoTxRx, a653rs_linux_core::error::TypedEr
pub(crate) struct Base {
name: String,
hm: PartitionHMTable,
id: i64,
id: PartitionId,
bin: PathBuf,
mounts: Vec<(PathBuf, PathBuf)>,
cgroup: CGroup,
Expand Down
7 changes: 4 additions & 3 deletions hypervisor/src/hypervisor/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::HashMap;
use std::thread::sleep;
use std::time::Instant;

use a653rs::bindings::PartitionId;
use a653rs::prelude::OperatingMode;
use anyhow::anyhow;

Expand Down Expand Up @@ -30,7 +31,7 @@ impl Scheduler {
pub fn run_major_frame(
&mut self,
current_frame_start: Instant,
partitions_by_name: &mut HashMap<String, Partition>,
partitions: &mut HashMap<PartitionId, Partition>,
sampling_channels_by_name: &mut HashMap<String, Sampling>,
) -> LeveledResult<()> {
for timeframe in self.schedule.iter() {
Expand All @@ -41,8 +42,8 @@ impl Scheduler {
);

let timeframe_timeout = Timeout::new(current_frame_start, timeframe.end);
let partition = partitions_by_name
.get_mut(&timeframe.partition_name)
let partition = partitions
.get_mut(&timeframe.partition)
.expect("partition to exist because its name comes from `timeframe`");
PartitionTimeframeScheduler::new(partition, timeframe_timeout).run()?;

Expand Down
3 changes: 2 additions & 1 deletion hypervisor/src/hypervisor/scheduler/schedule.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::cmp::Ordering;
use std::time::Duration;

use a653rs::bindings::PartitionId;
use anyhow::bail;
use itertools::Itertools;

Expand Down Expand Up @@ -38,7 +39,7 @@ impl PartitionSchedule {
/// since the major frame's start.
#[derive(Clone, Debug)]
pub(crate) struct ScheduledTimeframe {
pub partition_name: String,
pub partition: PartitionId,
pub start: Duration,
pub end: Duration,
}
Expand Down

0 comments on commit 31542a2

Please sign in to comment.