Skip to content

Commit bdc5782

Browse files
committed
common: MemoryModel: speed-up and clean get_mapped_heap()
Main points: - using charconv as a faster alternative to strtoll functions; - only parsing the addresses for relevant lines; - lines ending with '[heap]' are OK Signed-off-by: Ronen Friedman <[email protected]>
1 parent cf62ba8 commit bdc5782

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

src/common/MemoryModel.cc

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,39 +54,59 @@ tl::expected<int64_t, std::string> MemoryModel::get_mapped_heap()
5454
string line;
5555
getline(proc_maps, line);
5656

57-
const char *start = line.c_str();
58-
const char *dash = start;
57+
if (line.length() < 48) {
58+
// a malformed line. We expect at least
59+
// '560c03f8d000-560c03fae000 rw-p 00000000 00:00 0'
60+
continue;
61+
}
62+
63+
const char* start = line.c_str();
64+
const char* dash = start;
5965
while (*dash && *dash != '-')
6066
dash++;
6167
if (!*dash)
6268
continue;
63-
const char *end = dash + 1;
69+
const char* end = dash + 1;
6470
while (*end && *end != ' ')
6571
end++;
6672
if (!*end)
6773
continue;
68-
unsigned long long as = strtoll(start, 0, 16);
69-
unsigned long long ae = strtoll(dash + 1, 0, 16);
7074

75+
auto addr_end = end;
7176
end++;
72-
const char *mode = end;
73-
74-
int skip = 4;
75-
while (skip--) {
76-
end++;
77-
while (*end && *end != ' ')
78-
end++;
79-
}
80-
if (*end)
81-
end++;
82-
83-
long size = ae - as;
77+
const char* mode = end;
8478

8579
/*
8680
* anything 'rw' and anon is assumed to be heap.
81+
* But we should count lines with inode '0' and '[heap]' as well
8782
*/
88-
if (mode[0] == 'r' && mode[1] == 'w' && !*end)
83+
if (mode[0] != 'r' || mode[1] != 'w') {
84+
continue;
85+
}
86+
87+
auto the_rest = line.substr(5 + end - start);
88+
if (!the_rest.starts_with("00000000 00:00 0")) {
89+
continue;
90+
}
91+
92+
if (the_rest.ends_with("[stack]")) {
93+
// should we really exclude the stack?
94+
continue;
95+
}
96+
97+
std::string_view final_token{the_rest.begin() + sizeof("00000000 00:00 0") - 1,
98+
the_rest.end()};
99+
if (final_token.size() < 3 ||
100+
final_token.ends_with("[heap]") || final_token.ends_with("[stack]")) {
101+
// calculate and sum the size of the heap segment
102+
uint64_t as{0ull};
103+
from_chars(start, dash, as, 16);
104+
uint64_t ae{0ull};
105+
from_chars(dash + 1, addr_end, ae, 16);
106+
// fmt::print("\t\tas:{:x} ae:{:x} -> {}\n", as, ae, ((ae - as) >> 10));
107+
long size = ae - as;
89108
heap += size;
109+
}
90110
}
91111

92112
return heap;

0 commit comments

Comments
 (0)