Skip to content

Commit

Permalink
Add a template worker
Browse files Browse the repository at this point in the history
A new dummy TemplateWorker for development and documentation purposes.
  • Loading branch information
erthalion committed Dec 15, 2023
1 parent 73d8499 commit 4769b14
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ pub enum Workload {
/// How often to invoke a syscall.
arrival_rate: f64,
},

/// For documentation purposes
Template {
},
}

/// Distribution for number of ports to listen on
Expand Down
9 changes: 8 additions & 1 deletion src/worker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ 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,
Expand Down Expand Up @@ -44,5 +50,6 @@ pub fn new_worker(
))
}
Workload::Syscalls { .. } => Box::new(SyscallsWorker::new(workload, cpu, process)),
Workload::Template { .. } => Box::new(TemplateWorker::new(workload, cpu, process)),
}
}
56 changes: 56 additions & 0 deletions src/worker/template.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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)
}
}

0 comments on commit 4769b14

Please sign in to comment.