Trait libtracecmd::Handler

source ·
pub trait Handler {
    type AccumulatedData: Default;

    // Required method
    fn callback(
        input: &mut Input,
        rec: &mut Record,
        cpu: i32,
        data: &mut Self::AccumulatedData
    ) -> i32;

    // Provided methods
    fn process(input: &mut Input) -> Result<Self::AccumulatedData, i32> { ... }
    fn process_multi(inputs: &mut [Input]) -> Result<Self::AccumulatedData, i32> { ... }
}
Expand description

A trait to iterate over trace events and process them one by one.

When you use this trait, you need to implement Handler::callback and Handler::AccumulatedData. Then, you can call Handler::process or Handler::process_multi to process the given trace.dat. When Handler::process is called, the defined callback is called for each events one by one. The last argument of the callback is &mut Self::AccumulatedData.

Example

use libtracecmd::Event;
use libtracecmd::Handler;
use libtracecmd::Input;
use libtracecmd::Record;

#[derive(Default)]
struct MyData {
  // fields to accumulate data.
}

impl MyData {
  fn print_results(&self) {
    // Print accumulated data.
  }
}

struct MyStats;

impl Handler for MyStats {
  type AccumulatedData = MyData;

  fn callback(input: &mut Input, rec: &mut Record, cpu: i32, data: &mut Self::AccumulatedData) -> i32 {
    // Write your own logic to analyze `rec` and update `data`.
    0
  }
}


let mut input: Input = Input::new("trace.dat").unwrap();
let stats: MyData = MyStats::process(&mut input).unwrap();
stats.print_results();

You can find sample programs in /examples/.

Required Associated Types§

source

type AccumulatedData: Default

Type of data passed around among every call of Self::callback.

Required Methods§

source

fn callback( input: &mut Input, rec: &mut Record, cpu: i32, data: &mut Self::AccumulatedData ) -> i32

A callback that will be called for all events when Self::process or Self::process_multi is called.

Provided Methods§

source

fn process(input: &mut Input) -> Result<Self::AccumulatedData, i32>

Processes the given input by calling Self::callback for each event and returns Self::AccumulatedData returned by the last call of Self::callback.

This is a wrapper of tracecmd_iterate_events.

source

fn process_multi(inputs: &mut [Input]) -> Result<Self::AccumulatedData, i32>

Similar to Self::process, but can take multiple inputs.

This is useful when you have synchronized multiple trace.dat created by trace-cmd agent. This is a wrapper of tracecmd_iterate_events.

Object Safety§

This trait is not object safe.

Implementors§