Skip to content

Commit

Permalink
agent: Make tokio-uring as optional feature
Browse files Browse the repository at this point in the history
Signed-off-by: Daiki Ueno <[email protected]>
  • Loading branch information
ueno committed Jun 28, 2023
1 parent 6dc3022 commit a5acc5e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
5 changes: 4 additions & 1 deletion agent/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ edition = "2021"
license = "GPL-3.0-or-later"
authors = ["The crypto-auditing developers"]

[features]
default = ["tokio-uring"]

[dependencies]
anyhow = "1.0"
bytes = "1.2"
Expand All @@ -21,7 +24,7 @@ serde = "1.0"
serde_cbor = "0.10"
time = { version = "0.3", features=["formatting", "local-offset", "macros"] }
tokio = { version = "1.23", features=["full"] }
tokio-uring = "0.4"
tokio-uring = { version = "0.4", optional = true }
toml = "0.6"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features=["env-filter"] }
Expand Down
17 changes: 15 additions & 2 deletions agent/src/log_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ use serde_cbor::{ser::IoWrite, Serializer};
use std::path::PathBuf;
use time::{macros::format_description, OffsetDateTime};
use tokio::time::{Duration, Instant};
#[cfg(not(feature = "tokio-uring"))]
use tokio::{
fs::{rename, File},
io::AsyncWriteExt,
};
#[cfg(feature = "tokio-uring")]
use tokio_uring::fs::{rename, File};

pub struct LogWriter {
Expand Down Expand Up @@ -97,6 +103,7 @@ impl LogWriter {
.await
.with_context(|| format!("unable to sync file `{}`", self.config.log_file.display()))?;

#[cfg(feature = "tokio-uring")]
file.close().await.with_context(|| {
format!("unable to close file `{}`", self.config.log_file.display())
})?;
Expand Down Expand Up @@ -151,8 +158,14 @@ impl LogWriter {
config::Format::Packed => serde_cbor::ser::to_vec_packed(&group)?,
config::Format::Minimal => to_vec_minimal(&group)?,
};
let (res, _) = match self.file {
Some(ref file) => file.write_at(v, self.offset).await,
#[cfg(feature = "tokio-uring")]
let res = match self.file {
Some(ref file) => file.write_at(v, self.offset).await.0,
_ => bail!("log file is not opened"),
};
#[cfg(not(feature = "tokio-uring"))]
let res = match self.file {
Some(ref mut file) => file.write(&v).await,
_ => bail!("log file is not opened"),
};
let n = res?;
Expand Down
17 changes: 16 additions & 1 deletion agent/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use anyhow::{bail, Context as _, Result};
use bytes::BytesMut;
use core::future::Future;
use crypto_auditing::types::{ContextID, EventGroup};
use openssl::{
rand::rand_bytes,
Expand Down Expand Up @@ -71,6 +72,20 @@ impl Tracer {
}
}

#[cfg(feature = "tokio-uring")]
fn start<F: Future>(future: F) -> F::Output {
tokio_uring::start(future)
}

#[cfg(not(feature = "tokio-uring"))]
fn start<F: Future>(future: F) -> F::Output {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(future)
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = config::Config::new()?;

Expand Down Expand Up @@ -136,7 +151,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut encryption_key = vec![0; cipher.key_len()];
rand_bytes(&mut encryption_key)?;

tokio_uring::start(async {
start(async {
let mut rb = ringbuf::RingBuffer::new(skel.obj.map_mut("ringbuf").unwrap());

if let Some((ref user, ref group)) = config.user {
Expand Down

0 comments on commit a5acc5e

Please sign in to comment.