diff --git a/README.md b/README.md index 1f6e5f4..669394f 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,13 @@ among available system CPU cores to fully utilize system resources. * Run linter, `cargo clippy`. +# Adding a new worker + +If your goal is to implement a new worker, take a look at `TemplateWorker`. +It's a dummy worker implementation, the sole purpose of which is to show all +parts that have to be touched to wire up a custom worker. Copy the template +over under a new name, and you'll get a decent ground for development. + \[1\]: https://en.wikipedia.org/wiki/Poisson_point_process \[2\]: "Open versus closed: A cautionary tale". Schroeder, B., Wierman, A. and diff --git a/src/lib.rs b/src/lib.rs index 22c0730..fd07124 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,6 +44,9 @@ pub enum Workload { /// How often to invoke a syscall. arrival_rate: f64, }, + + /// For documentation purposes + Template {}, } /// Distribution for number of ports to listen on diff --git a/src/worker/mod.rs b/src/worker/mod.rs index 5a3c3bd..cbc741c 100644 --- a/src/worker/mod.rs +++ b/src/worker/mod.rs @@ -4,11 +4,15 @@ use rand_distr::{Uniform, Zipf}; use crate::{Distribution, Worker, Workload, WorkloadConfig}; -use self::{endpoints::EndpointWorker, processes::ProcessesWorker, syscalls::SyscallsWorker}; +use self::{ + endpoints::EndpointWorker, processes::ProcessesWorker, syscalls::SyscallsWorker, + template::TemplateWorker, +}; pub mod endpoints; pub mod processes; pub mod syscalls; +pub mod template; pub fn new_worker( workload: WorkloadConfig, @@ -44,5 +48,6 @@ pub fn new_worker( )) } Workload::Syscalls { .. } => Box::new(SyscallsWorker::new(workload, cpu, process)), + Workload::Template { .. } => Box::new(TemplateWorker::new(workload, cpu, process)), } } diff --git a/src/worker/template.rs b/src/worker/template.rs new file mode 100644 index 0000000..68ca5a3 --- /dev/null +++ b/src/worker/template.rs @@ -0,0 +1,48 @@ +use std::fmt::Display; + +use core_affinity::CoreId; +use log::info; + +use crate::{BaseConfig, Worker, WorkerError, WorkloadConfig}; + +struct TemplateWorkload { + restart_interval: u64, +} + +pub struct TemplateWorker { + config: BaseConfig, + workload: TemplateWorkload, +} + +impl TemplateWorker { + pub fn new(workload: WorkloadConfig, cpu: CoreId, process: usize) -> Self { + let WorkloadConfig { + restart_interval, + workload: _, + } = workload; + + TemplateWorker { + config: BaseConfig { cpu, process }, + workload: TemplateWorkload { restart_interval }, + } + } +} + +impl Worker for TemplateWorker { + fn run_payload(&self) -> Result<(), WorkerError> { + info!("{self}"); + + let TemplateWorkload { restart_interval } = self.workload; + + // Do something here + info!("{restart_interval}"); + + Ok(()) + } +} + +impl Display for TemplateWorker { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.config) + } +}