Skip to content

Commit e8b6cce

Browse files
committed
python prototyping
Adjust the logics to cache python frames
1 parent 483e780 commit e8b6cce

File tree

5 files changed

+30
-21
lines changed

5 files changed

+30
-21
lines changed

app/python-tests/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FROM python:slim
2+

include/austin_symbol_lookup.hpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@ namespace ddprof {
1616
class AustinSymbolLookup {
1717
public:
1818
SymbolIdx_t get_or_insert(austin_frame_t *frame, SymbolTable &symbol_table);
19-
19+
void clear() {
20+
for (auto el : _austin_symbols) {
21+
_free_list.push_back(el);
22+
}
23+
_austin_symbols.clear();
24+
}
2025
private:
21-
using FrameKeyMap = std::unordered_map<uintptr_t, SymbolIdx_t>;
22-
FrameKeyMap _frame_key_map;
26+
std::vector<SymbolIdx_t> _austin_symbols;
27+
std::vector<SymbolIdx_t> _free_list;
2328
};
2429

2530
} // namespace ddprof

include/unwind_state.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ struct UnwindState {
4343
stack(nullptr), stack_sz(0), current_ip(0), austin_handle(nullptr) {
4444
output.clear();
4545
output.locs.reserve(DD_MAX_STACK_DEPTH);
46-
4746
}
4847

4948
ddprof::DwflHdr dwfl_hdr;

src/austin_symbol_lookup.cc

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,21 @@ namespace ddprof {
1616

1717

1818
SymbolIdx_t AustinSymbolLookup::get_or_insert(austin_frame_t * frame, SymbolTable &symbol_table) {
19-
auto iter = _frame_key_map.find(frame->key);
20-
21-
if (iter != _frame_key_map.end()) {
22-
return iter->second;
19+
SymbolIdx_t idx = -1;
20+
// reuse elements to avoid growing the table
21+
if (!_free_list.empty()) {
22+
auto el = _free_list.back();
23+
_free_list.pop_back();
24+
_austin_symbols.push_back(el);
25+
idx = el;
26+
}
27+
if (idx == -1) {
28+
idx = symbol_table.size();
29+
symbol_table.push_back(Symbol());
2330
}
24-
25-
SymbolIdx_t index = symbol_table.size();
2631
std::string symname = std::string(frame->scope);
27-
28-
symbol_table.push_back(Symbol(symname, symname, frame->line, std::string(frame->filename)));
29-
30-
_frame_key_map.emplace(frame->key, index);
31-
32-
return index;
32+
symbol_table[idx] = Symbol(symname, symname, frame->line, std::string(frame->filename));
33+
return idx;
3334
}
3435

3536
} // namespace ddprof

src/unwind_dwfl.cc

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ namespace ddprof {
105105
DDRes unwind_init_dwfl(UnwindState *us) {
106106
// Create or get the dwfl object associated to cache
107107
us->_dwfl_wrapper = &(us->dwfl_hdr.get_or_insert(us->pid));
108+
// clear at every iteration
109+
us->symbol_hdr._austin_symbol_lookup.clear();
110+
108111
if (!us->_dwfl_wrapper->_attached) {
109112
// we need to add at least one module to figure out the architecture (to
110113
// create the unwinding backend)
@@ -404,7 +407,6 @@ static DDRes add_python_frame(UnwindState *us, SymbolIdx_t symbol_idx,
404407
ElfAddress_t pc, Dwfl_Frame *dwfl_frame) {
405408
SymbolHdr &unwind_symbol_hdr = us->symbol_hdr;
406409
SymbolTable &symbol_table = unwind_symbol_hdr._symbol_table;
407-
408410
std::string symname = symbol_table.at(symbol_idx)._symname;
409411
if (us->austin_handle &&
410412
(symname.find("PyEval_EvalFrameDefault") != std::string::npos ||
@@ -413,11 +415,11 @@ static DDRes add_python_frame(UnwindState *us, SymbolIdx_t symbol_idx,
413415
unwind_symbol_hdr._austin_symbol_lookup;
414416

415417
// The register we are interested in is RSI, but it doesn't seem to be
416-
// available. So we loop over the first 64 registers and stop if we find
418+
// available. So we loop over the available registers and stop if we find
417419
// a register value that resolves correctly to a Python frame.
418-
uint64_t val;
419-
for (int i = 0; i < (int)sizeof(*dwfl_frame->regs_set) * 8; i++) {
420-
if (__libdwfl_frame_reg_get(dwfl_frame, i, &val)) {
420+
uint64_t val = 0;
421+
for (int i = 0; i < PERF_REGS_COUNT; i++) {
422+
if (__libdwfl_frame_reg_get(dwfl_frame, i, &val) && val) {
421423
austin_frame_t *frame =
422424
austin_read_frame(us->austin_handle, (void *)val);
423425
if (frame) {

0 commit comments

Comments
 (0)