Skip to content
This repository has been archived by the owner on Jun 4, 2023. It is now read-only.

Commit

Permalink
prof: add bindings for vAccel profiling
Browse files Browse the repository at this point in the history
Expose a type that wraps `vaccel_prof_region` and let us use the vAccel
API to profile regions of code.

Signed-off-by: Babis Chalios <[email protected]>
  • Loading branch information
bchalios committed Sep 14, 2021
1 parent 41e2ce0 commit 9cea105
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 1 deletion.
16 changes: 16 additions & 0 deletions examples/tf_inference/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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(&[]);
Expand All @@ -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::<f32>(0) {
Ok(out) => {
Expand All @@ -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()?;

Expand Down
100 changes: 100 additions & 0 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<vaccel_prof_region>(),
40usize,
concat!("Size of: ", stringify!(vaccel_prof_region))
);
assert_eq!(
::std::mem::align_of::<vaccel_prof_region>(),
8usize,
concat!("Alignment of ", stringify!(vaccel_prof_region))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<vaccel_prof_region>())).name as *const _ as usize },
0usize,
concat!(
"Offset of field: ",
stringify!(vaccel_prof_region),
"::",
stringify!(name)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<vaccel_prof_region>())).name_owned as *const _ as usize },
8usize,
concat!(
"Offset of field: ",
stringify!(vaccel_prof_region),
"::",
stringify!(name_owned)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<vaccel_prof_region>())).nr_entries as *const _ as usize },
16usize,
concat!(
"Offset of field: ",
stringify!(vaccel_prof_region),
"::",
stringify!(nr_entries)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<vaccel_prof_region>())).samples as *const _ as usize },
24usize,
concat!(
"Offset of field: ",
stringify!(vaccel_prof_region),
"::",
stringify!(samples)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<vaccel_prof_region>())).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,
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
61 changes: 61 additions & 0 deletions src/prof/mod.rs
Original file line number Diff line number Diff line change
@@ -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<Self> {
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) };
}
}
2 changes: 1 addition & 1 deletion vaccelrt

0 comments on commit 9cea105

Please sign in to comment.