Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 31ae86c

Browse files
authored
Add Record::parse_sample_id to parse a SampleId from a Record (#60)
* Add Record::parse_sample_id to parse a SampleId from a Record * Address clippy warning
1 parent 7276719 commit 31ae86c

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

perf-event/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## Unreleased
9+
### Added
10+
- Added `Record::parse_sample_id` to allow accessing a `SampleId` when
11+
`Builder::sample_id_all` is enabled.
912

1013
## 0.7.4 - 2024-05-30
1114
### Added

perf-event/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ bitflags = "2.1"
2727
c-enum = "0.2.0"
2828
libc = "0.2"
2929
memmap2 = "0.9"
30-
perf-event-data = "0.1.1"
30+
perf-event-data = "0.1.8"
3131
perf-event-open-sys2 = "5.0.4"
3232

3333
[dev-dependencies]
34+
ctrlc = "3.4.5"
3435
nix = { version = "0.29", features = ["process", "feature"] }
3536
anyhow = "1.0"
37+
chrono = "0.4.39"

perf-event/src/sampler.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,32 @@ impl<'s> Record<'s> {
604604
let mut parser = Parser::new(self.data, self.sampler.config().clone());
605605
data::Record::parse_with_header(&mut parser, self.header)
606606
}
607+
608+
/// Parse the sample id for the record.
609+
///
610+
/// This will only be non-empty if the [`sample_id_all`] was set when
611+
/// building the counter. In addition, `MMAP` records never have a sample id
612+
/// set. If you want sample ids and `MMAP` records you will need to request
613+
/// `MMAP2` records instead.
614+
///
615+
/// [`sample_id_all`]: crate::Builder::sample_id_all
616+
pub fn parse_sample_id(&self) -> ParseResult<data::SampleId> {
617+
use perf_event_open_sys::bindings;
618+
619+
let config = self.sampler.config();
620+
let mut parser = Parser::new(self.data, config.clone());
621+
622+
let (mut parser, metadata) = parser.parse_metadata_with_header(self.header)?;
623+
624+
// All other records either already parsed the sample id or don't have it.
625+
// With SAMPLE records, we can construct the sample id struct directly.
626+
if self.ty() != bindings::PERF_RECORD_SAMPLE {
627+
return Ok(*metadata.sample_id());
628+
}
629+
630+
let record = parser.parse::<data::Sample>()?;
631+
Ok(data::SampleId::from_sample(&record))
632+
}
607633
}
608634

609635
impl<'s> Drop for Record<'s> {

0 commit comments

Comments
 (0)