Skip to content

Commit

Permalink
Add Record::parse_sample_id to parse a SampleId from a Record (#60)
Browse files Browse the repository at this point in the history
* Add Record::parse_sample_id to parse a SampleId from a Record
* Address clippy warning
  • Loading branch information
Phantomical authored Jan 21, 2025
1 parent 7276719 commit 31ae86c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
3 changes: 3 additions & 0 deletions perf-event/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
### Added
- Added `Record::parse_sample_id` to allow accessing a `SampleId` when
`Builder::sample_id_all` is enabled.

## 0.7.4 - 2024-05-30
### Added
Expand Down
4 changes: 3 additions & 1 deletion perf-event/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ bitflags = "2.1"
c-enum = "0.2.0"
libc = "0.2"
memmap2 = "0.9"
perf-event-data = "0.1.1"
perf-event-data = "0.1.8"
perf-event-open-sys2 = "5.0.4"

[dev-dependencies]
ctrlc = "3.4.5"
nix = { version = "0.29", features = ["process", "feature"] }
anyhow = "1.0"
chrono = "0.4.39"
26 changes: 26 additions & 0 deletions perf-event/src/sampler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,32 @@ impl<'s> Record<'s> {
let mut parser = Parser::new(self.data, self.sampler.config().clone());
data::Record::parse_with_header(&mut parser, self.header)
}

/// Parse the sample id for the record.
///
/// This will only be non-empty if the [`sample_id_all`] was set when
/// building the counter. In addition, `MMAP` records never have a sample id
/// set. If you want sample ids and `MMAP` records you will need to request
/// `MMAP2` records instead.
///
/// [`sample_id_all`]: crate::Builder::sample_id_all
pub fn parse_sample_id(&self) -> ParseResult<data::SampleId> {
use perf_event_open_sys::bindings;

let config = self.sampler.config();
let mut parser = Parser::new(self.data, config.clone());

let (mut parser, metadata) = parser.parse_metadata_with_header(self.header)?;

// All other records either already parsed the sample id or don't have it.
// With SAMPLE records, we can construct the sample id struct directly.
if self.ty() != bindings::PERF_RECORD_SAMPLE {
return Ok(*metadata.sample_id());
}

let record = parser.parse::<data::Sample>()?;
Ok(data::SampleId::from_sample(&record))
}
}

impl<'s> Drop for Record<'s> {
Expand Down

0 comments on commit 31ae86c

Please sign in to comment.