Skip to content

Commit

Permalink
cleanup(plugin): use bumpalo for source event batches
Browse files Browse the repository at this point in the history
Signed-off-by: Grzegorz Nosek <grzegorz.nosek@sysdig.com>
  • Loading branch information
gnosek authored and poiana committed Oct 18, 2024
1 parent 27acf2c commit 10aadf2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 48 deletions.
55 changes: 14 additions & 41 deletions falco_plugin/src/plugin/source/event_batch.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,22 @@
use falco_event::events::EventToBytes;

#[derive(Default, Debug)]
pub(crate) struct EventBatchStorage {
buf: Vec<u8>,
offsets: Vec<usize>,

raw_pointers: Vec<*const u8>,
}

impl EventBatchStorage {
pub fn start(&mut self) -> EventBatch {
self.buf.clear();
self.offsets.clear();

EventBatch {
buf: &mut self.buf,
offsets: &mut self.offsets,
}
}

pub fn get_raw_pointers(&mut self) -> (*const *const u8, usize) {
if self.offsets.is_empty() {
return (std::ptr::null(), 0);
}

self.raw_pointers.clear();
self.raw_pointers.reserve(self.offsets.len());
let base = self.buf.as_ptr();
for offset in self.offsets.iter().copied() {
self.raw_pointers.push(unsafe { base.add(offset) });
}

(self.raw_pointers.as_ptr(), self.offsets.len())
}
}

/// # An object that describes a batch of events
///
/// This is only available by reference, not by ownership, since the data needs to outlive
/// the plugin API call and is stored elsewhere (in a wrapper struct that's not exposed to
/// plugin developers)
#[derive(Debug)]
pub struct EventBatch<'a> {
buf: &'a mut Vec<u8>,
offsets: &'a mut Vec<usize>,
alloc: &'a bumpalo::Bump,
pointers: bumpalo::collections::Vec<'a, *const u8>,
}

impl EventBatch<'_> {
pub(in crate::plugin::source) fn new(alloc: &mut bumpalo::Bump) -> EventBatch {
let pointers = bumpalo::collections::Vec::new_in(alloc);
EventBatch { alloc, pointers }
}

/// # Add an event to a batch
///
/// The event can be any type, but please note that the framework may have different
Expand All @@ -57,10 +27,13 @@ impl EventBatch<'_> {
/// the [`source::SourcePluginInstance::plugin_event`](`crate::source::SourcePluginInstance::plugin_event`)
/// helper method.
pub fn add(&mut self, event: impl EventToBytes) -> std::io::Result<()> {
let pos = self.buf.len();
event.write(&mut *self.buf)?;

self.offsets.push(pos);
let mut event_buf = bumpalo::collections::Vec::new_in(self.alloc);
event.write(&mut event_buf)?;
self.pointers.push(event_buf.as_ptr());
Ok(())
}

pub(in crate::plugin::source) fn get_events(&self) -> &[*const u8] {
self.pointers.as_slice()
}
}
3 changes: 1 addition & 2 deletions falco_plugin/src/plugin/source/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::plugin::base::Plugin;
use crate::plugin::source::event_batch::EventBatchStorage;
use crate::source::{EventBatch, EventInput};
use falco_event::events::types::PPME_PLUGINEVENT_E as PluginEvent;
use falco_event::events::Event;
Expand Down Expand Up @@ -92,7 +91,7 @@ pub struct ProgressInfo<'a> {

pub(crate) struct SourcePluginInstanceWrapper<I: SourcePluginInstance> {
pub(crate) instance: I,
pub(crate) batch: EventBatchStorage,
pub(crate) batch: bumpalo::Bump,
}

/// # An open instance of a source plugin
Expand Down
11 changes: 6 additions & 5 deletions falco_plugin/src/plugin/source/wrappers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::plugin::base::PluginWrapper;
use crate::plugin::error::ffi_result::FfiResult;
use crate::plugin::source::SourcePluginInstanceWrapper;
use crate::source::{EventInput, SourcePlugin, SourcePluginInstance};
use crate::source::{EventBatch, EventInput, SourcePlugin, SourcePluginInstance};
use crate::strings::cstring_writer::WriteIntoCString;
use crate::strings::from_ptr::try_str_from_ptr;
use falco_plugin_api::plugin_api__bindgen_ty_1 as source_plugin_api;
Expand Down Expand Up @@ -183,15 +183,16 @@ pub unsafe extern "C" fn plugin_next_batch<T: SourcePlugin>(
return ss_plugin_rc_SS_PLUGIN_FAILURE;
};

let mut batch = instance.batch.start();
instance.batch.reset();
let mut batch = EventBatch::new(&mut instance.batch);
match instance
.instance
.next_batch(&mut actual_plugin.plugin, &mut batch)
{
Ok(()) => {
let (batch_evts, batch_nevts) = instance.batch.get_raw_pointers();
*nevts = batch_nevts as u32;
*evts = batch_evts as *mut *mut _;
let events = batch.get_events();
*nevts = events.len() as u32;
*evts = events as *const _ as *mut _;
ss_plugin_rc_SS_PLUGIN_SUCCESS
}
Err(e) => {
Expand Down

0 comments on commit 10aadf2

Please sign in to comment.