Skip to content

Commit

Permalink
Merge pull request #14 from stackrox/feature/per-core-mode
Browse files Browse the repository at this point in the history
Add per-core mode
  • Loading branch information
erthalion authored Apr 3, 2024
2 parents 74de174 + cb06061 commit e44836b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
23 changes: 23 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,29 @@ pub struct WorkloadConfig {
/// An amount of time for workload payload to run before restarting.
pub restart_interval: u64,

/// Controls per-core mode to handle number of workers. If per-core mode
/// is enabled, `workers` will be treated as a number of workers per CPU
/// core. Otherwise it will be treated as a total number of workers.
#[serde(default = "default_per_core")]
pub per_core: bool,

/// How many workers to spin, depending on `per_core` in either per-core
/// or total mode.
#[serde(default = "default_workers")]
pub workers: usize,

/// Custom workload configuration.
pub workload: Workload,
}

fn default_workers() -> usize {
1
}

fn default_per_core() -> bool {
true
}

/// Workload specific configuration, contains one enum value for each
/// workload type.
#[derive(Debug, Copy, Clone, Deserialize)]
Expand Down Expand Up @@ -114,6 +133,7 @@ mod tests {
let WorkloadConfig {
restart_interval,
workload,
..
} = config;
assert_eq!(restart_interval, 10);
if let Workload::Processes {
Expand Down Expand Up @@ -152,6 +172,7 @@ mod tests {
let WorkloadConfig {
restart_interval,
workload,
..
} = config;
assert_eq!(restart_interval, 10);

Expand Down Expand Up @@ -189,6 +210,7 @@ mod tests {
let WorkloadConfig {
restart_interval,
workload,
..
} = config;
assert_eq!(restart_interval, 10);

Expand Down Expand Up @@ -224,6 +246,7 @@ mod tests {
let WorkloadConfig {
restart_interval,
workload,
..
} = config;
assert_eq!(restart_interval, 10);
if let Workload::Syscalls { arrival_rate } = workload {
Expand Down
25 changes: 16 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ extern crate log;
extern crate core_affinity;

use config::Config;
use core_affinity::CoreId;
use fork::{fork, Fork};
use itertools::iproduct;
use nix::sys::wait::waitpid;
Expand All @@ -26,8 +27,6 @@ use nix::unistd::Pid;
use berserker::{worker::new_worker, WorkloadConfig};

fn main() {
// Retrieve the IDs of all active CPU cores.
let core_ids = core_affinity::get_core_ids().unwrap();
let config = Config::builder()
// Add in `./Settings.toml`
.add_source(config::File::with_name("/etc/berserker/workload.toml").required(false))
Expand All @@ -49,8 +48,16 @@ fn main() {

env_logger::init();

// Create processes for each active CPU core.
let handles: Vec<_> = iproduct!(core_ids.into_iter(), 0..9)
info!("Config: {:?}", config);

let core_ids: Vec<CoreId> = if config.per_core {
// Retrieve the IDs of all active CPU cores.
core_affinity::get_core_ids().unwrap()
} else {
vec![CoreId { id: 0 }]
};

let handles: Vec<_> = iproduct!(core_ids.into_iter(), 0..config.workers)
.map(|(cpu, process)| {
let worker = new_worker(config, cpu, process, &mut lower, &mut upper);

Expand All @@ -60,13 +67,13 @@ fn main() {
Some(child)
}
Ok(Fork::Child) => {
if core_affinity::set_for_current(cpu) {
loop {
worker.run_payload().unwrap();
}
if config.per_core {
core_affinity::set_for_current(cpu);
}

None
loop {
worker.run_payload().unwrap();
}
}
Err(e) => {
warn!("Failed: {e:?}");
Expand Down
2 changes: 2 additions & 0 deletions src/worker/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ impl EndpointWorker {
let WorkloadConfig {
restart_interval,
workload: _,
per_core: _,
workers: _,
} = workload;

EndpointWorker {
Expand Down

0 comments on commit e44836b

Please sign in to comment.