Skip to content

Commit 3538990

Browse files
committed
Fix missing cgroup field in Sample record
The SAMPLE record type is supposed to have a cgroup field if specified in the sample flags. This was missed when implementing. This commit adds the cgroup field along with a test case ensuring that parsing works as expected.
1 parent 2e0fdd1 commit 3538990

2 files changed

Lines changed: 54 additions & 2 deletions

File tree

src/config.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@ impl<E> ParseConfig<E> {
138138
self
139139
}
140140

141+
#[allow(dead_code)]
142+
/// Used for testing, please open an issue if you need this.
143+
pub(crate) fn with_read_format(mut self, read_format: ReadFormat) -> Self {
144+
self.config.config_flags.set_read_format(read_format);
145+
self
146+
}
147+
141148
pub(crate) fn with_misc(mut self, misc: u16) -> Self {
142149
self.config.config_flags.set_misc(misc);
143150
self

src/records/sample.rs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ mod sample_impl {
4747
#[debug(with = crate::util::fmt::HexAddr)]
4848
pub phys_addr: u64,
4949
pub aux: Cow<'a, [u8]>,
50+
pub cgroup: u64,
5051
pub data_page_size: u64,
5152
pub code_page_size: u64
5253
}
@@ -152,6 +153,10 @@ impl<'a> Sample<'a> {
152153
self.0.aux().map(|cow| &**cow)
153154
}
154155

156+
pub fn cgroup(&self) -> Option<u64> {
157+
self.0.cgroup().copied()
158+
}
159+
155160
pub fn data_page_size(&self) -> Option<u64> {
156161
self.0.data_page_size().copied()
157162
}
@@ -246,12 +251,13 @@ impl<'p> Parse<'p> for Sample<'p> {
246251
Registers::parse_intr(p)
247252
})?;
248253
let phys_addr = p.parse_if(sty.contains(SampleFlags::PHYS_ADDR))?;
254+
let cgroup = p.parse_if(sty.contains(SampleFlags::CGROUP))?;
255+
let data_page_size = p.parse_if(sty.contains(SampleFlags::DATA_PAGE_SIZE))?;
256+
let code_page_size = p.parse_if(sty.contains(SampleFlags::CODE_PAGE_SIZE))?;
249257
let aux = p.parse_if_with(sty.contains(SampleFlags::AUX), |p| {
250258
let size = p.parse_u64()? as usize;
251259
p.parse_bytes(size)
252260
})?;
253-
let data_page_size = p.parse_if(sty.contains(SampleFlags::DATA_PAGE_SIZE))?;
254-
let code_page_size = p.parse_if(sty.contains(SampleFlags::CODE_PAGE_SIZE))?;
255261

256262
Ok(Self(sample_impl::Sample::new(
257263
ip,
@@ -276,6 +282,7 @@ impl<'p> Parse<'p> for Sample<'p> {
276282
regs_intr,
277283
phys_addr,
278284
aux,
285+
cgroup,
279286
data_page_size,
280287
code_page_size,
281288
)))
@@ -762,4 +769,42 @@ mod tests {
762769
assert_eq!(sample.cpu(), None);
763770
assert_eq!(sample.time(), None);
764771
}
772+
773+
#[test]
774+
fn parse_sample_with_cgroup() {
775+
#[rustfmt::skip]
776+
let data: &[u8] = &[
777+
0xd4, 0x08, 0x00, 0x00, 0xd4, 0x08, 0x00, 0x00,
778+
0xc9, 0x77, 0x8e, 0xa1, 0x3a, 0xa4, 0x00, 0x00,
779+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
780+
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
781+
0xd0, 0xbe, 0xc0, 0x28, 0x00, 0x00, 0x00, 0x00,
782+
0x24, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
783+
0xbd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
784+
0xac, 0x79, 0xc0, 0x28, 0x00, 0x00, 0x00, 0x00,
785+
0xbe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
786+
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
787+
];
788+
789+
let config: ParseConfig<Little> = ParseConfig::default()
790+
.with_sample_type(
791+
SampleFlags::TID
792+
| SampleFlags::CGROUP
793+
| SampleFlags::READ
794+
| SampleFlags::TIME
795+
| SampleFlags::CPU,
796+
)
797+
.with_read_format(ReadFormat::GROUP | ReadFormat::TOTAL_TIME_ENABLED);
798+
let sample: Sample = Parser::new(data, config).parse().unwrap();
799+
800+
assert_eq!(sample.pid(), Some(2260));
801+
assert_eq!(sample.tid(), Some(2260));
802+
assert_eq!(sample.time(), Some(0xA43AA18E77C9));
803+
assert_eq!(sample.cpu(), Some(0));
804+
805+
let group = sample.values().unwrap();
806+
assert_eq!(group.len(), 2);
807+
808+
assert_eq!(sample.cgroup(), Some(1));
809+
}
765810
}

0 commit comments

Comments
 (0)