Skip to content

Commit 112253a

Browse files
committed
fixed parse_smap_entries algorithm
removed a redundant field
1 parent 5c13bcc commit 112253a

File tree

1 file changed

+80
-13
lines changed

1 file changed

+80
-13
lines changed

src/uu/pmap/src/smaps_format_parser.rs

Lines changed: 80 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use std::io::{Error, ErrorKind};
1010
#[derive(Debug, Clone, Default, PartialEq)]
1111
pub struct SmapEntry {
1212
pub map_line: MapLine,
13-
pub size_in_kb: u64,
1413
pub kernel_page_size_in_kb: u64,
1514
pub mmu_page_size_in_kb: u64,
1615
pub rss_in_kb: u64,
@@ -46,34 +45,35 @@ pub struct SmapEntry {
4645
pub fn parse_smap_entries(contents: &str) -> Result<Vec<SmapEntry>, Error> {
4746
let mut smap_entries = Vec::new();
4847
let mut smap_entry = SmapEntry::default();
49-
let mut is_entry_modified = false;
5048

51-
for line in contents.lines() {
49+
for (i, line) in contents.lines().enumerate() {
5250
let map_line = parse_map_line(line);
5351
if let Ok(map_line) = map_line {
52+
if i > 0 {
53+
smap_entries.push(smap_entry.clone());
54+
smap_entry = SmapEntry::default();
55+
}
5456
smap_entry.map_line = map_line;
55-
is_entry_modified = true;
5657
} else {
5758
let (key, val) = line
5859
.split_once(':')
5960
.ok_or_else(|| Error::from(ErrorKind::InvalidData))?;
6061
let val = val.trim();
6162

6263
match key {
63-
"VmFlags" => {
64-
smap_entry.vmflags = val.into();
65-
smap_entries.push(smap_entry.clone());
66-
smap_entry = SmapEntry::default();
67-
is_entry_modified = false;
68-
}
64+
"VmFlags" => smap_entry.vmflags = val.into(),
6965
"THPeligible" => smap_entry.thp_eligible = get_smap_item_value(val)?,
7066
_ => {
7167
let val = val
7268
.strip_suffix(" kB")
7369
.ok_or_else(|| Error::from(ErrorKind::InvalidData))?;
7470
let val = get_smap_item_value(val)?;
7571
match key {
76-
"Size" => smap_entry.size_in_kb = val,
72+
"Size" => {
73+
if smap_entry.map_line.size_in_kb != val {
74+
return Err(Error::from(ErrorKind::InvalidData));
75+
}
76+
}
7777
"KernelPageSize" => smap_entry.kernel_page_size_in_kb = val,
7878
"MMUPageSize" => smap_entry.mmu_page_size_in_kb = val,
7979
"Rss" => smap_entry.rss_in_kb = val,
@@ -102,7 +102,7 @@ pub fn parse_smap_entries(contents: &str) -> Result<Vec<SmapEntry>, Error> {
102102
}
103103
}
104104

105-
if is_entry_modified {
105+
if !contents.is_empty() {
106106
smap_entries.push(smap_entry);
107107
}
108108

@@ -161,7 +161,6 @@ mod test {
161161
inode,
162162
mapping: mapping.to_string(),
163163
},
164-
size_in_kb,
165164
kernel_page_size_in_kb,
166165
mmu_page_size_in_kb,
167166
rss_in_kb,
@@ -478,6 +477,74 @@ mod test {
478477
"THPeligible: 0\n",
479478
"VmFlags: rd mr mw me sd \n")
480479
),
480+
(
481+
vec![
482+
create_smap_entry(
483+
"000071af8c9e6000", Perms::from("rw-s"), "0000000105830000", "000:00010", 1075, " [ anon ]",
484+
16, 4, 4, 16, 16, 16, 0, 0, 0, 16, 16, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
485+
0, "rd wr mr mw me ac sd"),
486+
create_smap_entry(
487+
"0000560880413000", Perms::from("r--p"), "0000000000000000", "008:00008", 10813151, "konsole",
488+
180, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
489+
0, ""),
490+
create_smap_entry(
491+
"000071af6cf0c000", Perms::from("rw-s"), "0000000000000000", "000:00001", 256481, "memfd:wayland-shm (deleted)",
492+
3560, 4, 4, 532, 108, 0, 524, 0, 8, 0, 532, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
493+
0, "rd mr mw me sd"),
494+
],
495+
concat!(
496+
"71af8c9e6000-71af8c9ea000 rw-s 105830000 00:10 1075 anon_inode:i915.gem\n",
497+
"Size: 16 kB\n",
498+
"KernelPageSize: 4 kB\n",
499+
"MMUPageSize: 4 kB\n",
500+
"Rss: 16 kB\n",
501+
"Pss: 16 kB\n",
502+
"Pss_Dirty: 16 kB\n",
503+
"Shared_Clean: 0 kB\n",
504+
"Shared_Dirty: 0 kB\n",
505+
"Private_Clean: 0 kB\n",
506+
"Private_Dirty: 16 kB\n",
507+
"Referenced: 16 kB\n",
508+
"Anonymous: 16 kB\n",
509+
"KSM: 0 kB\n",
510+
"LazyFree: 0 kB\n",
511+
"AnonHugePages: 0 kB\n",
512+
"ShmemPmdMapped: 0 kB\n",
513+
"FilePmdMapped: 0 kB\n",
514+
"Shared_Hugetlb: 0 kB\n",
515+
"Private_Hugetlb: 0 kB\n",
516+
"Swap: 0 kB\n",
517+
"SwapPss: 0 kB\n",
518+
"Locked: 0 kB\n",
519+
"THPeligible: 0\n",
520+
"VmFlags: rd wr mr mw me ac sd\n",
521+
"560880413000-560880440000 r--p 00000000 08:08 10813151 /usr/bin/konsole\n",
522+
"71af6cf0c000-71af6d286000 rw-s 00000000 00:01 256481 /memfd:wayland-shm (deleted)\n",
523+
"Size: 3560 kB\n",
524+
"KernelPageSize: 4 kB\n",
525+
"MMUPageSize: 4 kB\n",
526+
"Rss: 532 kB\n",
527+
"Pss: 108 kB\n",
528+
"Pss_Dirty: 0 kB\n",
529+
"Shared_Clean: 524 kB\n",
530+
"Shared_Dirty: 0 kB\n",
531+
"Private_Clean: 8 kB\n",
532+
"Private_Dirty: 0 kB\n",
533+
"Referenced: 532 kB\n",
534+
"Anonymous: 0 kB\n",
535+
"KSM: 0 kB\n",
536+
"LazyFree: 0 kB\n",
537+
"AnonHugePages: 0 kB\n",
538+
"ShmemPmdMapped: 0 kB\n",
539+
"FilePmdMapped: 0 kB\n",
540+
"Shared_Hugetlb: 0 kB\n",
541+
"Private_Hugetlb: 0 kB\n",
542+
"Swap: 0 kB\n",
543+
"SwapPss: 0 kB\n",
544+
"Locked: 0 kB\n",
545+
"THPeligible: 0\n",
546+
"VmFlags: rd mr mw me sd \n")
547+
),
481548
];
482549

483550
for (expected_smap_entries, entries) in data {

0 commit comments

Comments
 (0)