Skip to content
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

Add a template worker #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/worker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)),
}
}
48 changes: 48 additions & 0 deletions src/worker/template.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}