diff --git a/examples/tf_inference/main.rs b/examples/tf_inference/main.rs index 33d5a1f..9b10286 100644 --- a/examples/tf_inference/main.rs +++ b/examples/tf_inference/main.rs @@ -23,6 +23,9 @@ fn main() -> utilities::Result<()> { sess.register(&mut model)?; info!("Registered model {} with session {}", model.id(), sess.id()); + let mut load_region = vaccel::prof::ProfRegion::new("model_load")?; + + load_region.enter()?; // Load model graph if let Err(err) = model.session_load(&mut sess) { error!("Could not load graph for model {}: {}", model.id(), err); @@ -33,6 +36,7 @@ fn main() -> utilities::Result<()> { return Err(utilities::Error::Vaccel(err)); } + load_region.exit()?; // Prepare data for inference let run_options = tf::Buffer::new(&[]); @@ -45,7 +49,11 @@ fn main() -> utilities::Result<()> { sess_args.add_input(&in_node, &in_tensor); sess_args.request_output(&out_node); + let mut run_region = vaccel::prof::ProfRegion::new("session_run")?; + + run_region.enter()?; let result = model.session_run(&mut sess, &mut sess_args)?; + run_region.exit()?; match result.get_output::(0) { Ok(out) => { @@ -66,7 +74,15 @@ fn main() -> utilities::Result<()> { Err(err) => println!("Inference failed: '{}'", err), } + let mut delete_region = vaccel::prof::ProfRegion::new("session_delete")?; + + delete_region.enter()?; model.session_delete(&mut sess)?; + delete_region.exit()?; + + load_region.print()?; + run_region.print()?; + delete_region.print()?; sess.close()?; diff --git a/src/ffi.rs b/src/ffi.rs index ee2ad52..10125d5 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -5726,6 +5726,106 @@ extern "C" { pub fn vaccel_tf_saved_model_id(model: *const vaccel_tf_saved_model) -> vaccel_id_t; } #[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct prof_sample { + _unused: [u8; 0], +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct vaccel_prof_region { + pub name: *const ::std::os::raw::c_char, + pub name_owned: bool, + pub nr_entries: size_t, + pub samples: *mut prof_sample, + pub size: size_t, +} +#[test] +fn bindgen_test_layout_vaccel_prof_region() { + assert_eq!( + ::std::mem::size_of::(), + 40usize, + concat!("Size of: ", stringify!(vaccel_prof_region)) + ); + assert_eq!( + ::std::mem::align_of::(), + 8usize, + concat!("Alignment of ", stringify!(vaccel_prof_region)) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).name as *const _ as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(vaccel_prof_region), + "::", + stringify!(name) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).name_owned as *const _ as usize }, + 8usize, + concat!( + "Offset of field: ", + stringify!(vaccel_prof_region), + "::", + stringify!(name_owned) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).nr_entries as *const _ as usize }, + 16usize, + concat!( + "Offset of field: ", + stringify!(vaccel_prof_region), + "::", + stringify!(nr_entries) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).samples as *const _ as usize }, + 24usize, + concat!( + "Offset of field: ", + stringify!(vaccel_prof_region), + "::", + stringify!(samples) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).size as *const _ as usize }, + 32usize, + concat!( + "Offset of field: ", + stringify!(vaccel_prof_region), + "::", + stringify!(size) + ) + ); +} +impl Default for vaccel_prof_region { + fn default() -> Self { + unsafe { ::std::mem::zeroed() } + } +} +extern "C" { + pub fn vaccel_prof_region_start(region: *mut vaccel_prof_region) -> ::std::os::raw::c_int; +} +extern "C" { + pub fn vaccel_prof_region_stop(region: *const vaccel_prof_region) -> ::std::os::raw::c_int; +} +extern "C" { + pub fn vaccel_prof_region_print(region: *const vaccel_prof_region) -> ::std::os::raw::c_int; +} +extern "C" { + pub fn vaccel_prof_region_init( + region: *mut vaccel_prof_region, + name: *const ::std::os::raw::c_char, + ) -> ::std::os::raw::c_int; +} +extern "C" { + pub fn vaccel_prof_region_destroy(region: *mut vaccel_prof_region) -> ::std::os::raw::c_int; +} +#[repr(C)] #[derive(Debug, Default, Copy, Clone)] pub struct __locale_data { pub _address: u8, diff --git a/src/lib.rs b/src/lib.rs index 26fae07..1018b0c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,7 @@ use std::fmt; pub mod ffi; pub mod ops; +pub mod prof; pub mod resource; pub mod session; pub mod tensorflow; diff --git a/src/prof/mod.rs b/src/prof/mod.rs new file mode 100644 index 0000000..81bd9f0 --- /dev/null +++ b/src/prof/mod.rs @@ -0,0 +1,61 @@ +use crate::ffi; +use crate::{Error, Result}; + +use std::ffi::CString; + +/// A vAccel profile region +/// +/// This describes a region in a vAccel application +/// for which we can gather stats +#[derive(Debug)] +pub struct ProfRegion { + inner: ffi::vaccel_prof_region, +} + +impl ProfRegion { + /// Create a new profile region + /// + /// This will allocate and initialize a profile region + /// + /// # Arguments + /// + /// * `name` - The name of the region + pub fn new(name: &str) -> Result { + let c_name = CString::new(name).map_err(|_| Error::InvalidArgument)?; + + let mut inner = ffi::vaccel_prof_region::default(); + + match unsafe { ffi::vaccel_prof_region_init(&mut inner, c_name.as_c_str().as_ptr()) as u32 } + { + ffi::VACCEL_OK => Ok(ProfRegion { inner }), + err => Err(Error::Runtime(err)), + } + } + + pub fn enter(&mut self) -> Result<()> { + match unsafe { ffi::vaccel_prof_region_start(&mut self.inner) as u32 } { + ffi::VACCEL_OK => Ok(()), + err => Err(Error::Runtime(err)), + } + } + + pub fn exit(&mut self) -> Result<()> { + match unsafe { ffi::vaccel_prof_region_stop(&mut self.inner) as u32 } { + ffi::VACCEL_OK => Ok(()), + err => Err(Error::Runtime(err)), + } + } + + pub fn print(&self) -> Result<()> { + match unsafe { ffi::vaccel_prof_region_print(&self.inner) as u32 } { + ffi::VACCEL_OK => Ok(()), + err => Err(Error::Runtime(err)), + } + } +} + +impl Drop for ProfRegion { + fn drop(&mut self) { + unsafe { ffi::vaccel_prof_region_destroy(&mut self.inner) }; + } +} diff --git a/vaccelrt b/vaccelrt index dd65060..a31df3e 160000 --- a/vaccelrt +++ b/vaccelrt @@ -1 +1 @@ -Subproject commit dd65060b1964d21ae355b5d79c7ff3bd812e192b +Subproject commit a31df3e738bc1ce6698aa8b7d444bf36198beb24