Skip to content

Commit 332488b

Browse files
committed
fix some bug
1 parent fc1d00f commit 332488b

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/memray/_memray.pyx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,6 +1415,19 @@ cdef class SocketReader:
14151415
snapshot_allocations = self._impl.Py_GetSnapshotAllocationRecords(merge_threads=merge_threads)
14161416
return snapshot_allocations
14171417

1418+
def get_both_current_snapshot_and_raw_table_and_stats(self, *, bool merge_threads, int largest_num):
1419+
# for debug and compare
1420+
if self._impl is NULL:
1421+
return
1422+
1423+
snapshot_allocations, stats = self._impl.Py_GetSnapshotAllocationRecordsAndStatsData(merge_threads=merge_threads, largest_num=largest_num)
1424+
result_snaps = []
1425+
for elem in snapshot_allocations:
1426+
alloc = AllocationRecord(elem)
1427+
(<AllocationRecord> alloc)._reader = self._reader
1428+
result_snaps.append(alloc)
1429+
return result_snaps, snapshot_allocations, stats
1430+
14181431
def get_current_snapshot_raw_table_with_stats(self, *, bool merge_threads, int largest_num):
14191432
if self._impl is NULL:
14201433
return

src/memray/_memray/record_reader.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,6 @@ RecordReader::Py_GetStackFrameAndEntryInfo(
772772
if (list == nullptr) {
773773
return nullptr;
774774
}
775-
776775
while (current_index != 0 && stacks_obtained++ != max_stacks) {
777776
auto [frame_id, next_index] = d_tree.nextNode(current_index);
778777
const auto& frame = d_frame_map.at(frame_id);
@@ -872,7 +871,6 @@ RecordReader::Py_GetNativeStackFrame(FrameTree::index_t index, size_t generation
872871
if (list == nullptr) {
873872
return nullptr;
874873
}
875-
876874
while (current_index != 0 && stacks_obtained++ != max_stacks) {
877875
auto frame = d_native_frames[current_index - 1];
878876
current_index = frame.index;
@@ -898,6 +896,18 @@ RecordReader::Py_GetNativeStackFrame(FrameTree::index_t index, size_t generation
898896
return nullptr;
899897
}
900898

899+
struct hash_native_index_and_generation
900+
{
901+
std::size_t operator()(std::pair<FrameTree::index_t, size_t> data) const
902+
{
903+
// Reduce the risk of the Python frame ID and native frame ID hashing
904+
// to the same value and cancelling each other out by adding a fixed
905+
// offset to one of them. Don't worry about collisions with the TID:
906+
// it's of a fundamentally different type and collisions are unlikely.
907+
return std::hash<FrameTree::index_t>{}(data.first) xor std::hash<size_t>{}(data.second);
908+
}
909+
};
910+
901911
PyObject*
902912
RecordReader::Py_ListGetNativeStackFrame(
903913
std::vector<std::pair<FrameTree::index_t, size_t>>& index_generation_list,
@@ -907,7 +917,8 @@ RecordReader::Py_ListGetNativeStackFrame(
907917
PyErr_SetString(PyExc_RuntimeError, "Stack tracking is disabled");
908918
return NULL;
909919
}
910-
std::unordered_set<FrameTree::index_t> cached_index;
920+
using cache_t = std::pair<FrameTree::index_t, size_t>;
921+
std::unordered_set<cache_t, hash_native_index_and_generation> cached_index;
911922
PyObject* dict = PyDict_New();
912923
for (const auto& it : index_generation_list) {
913924
const auto& index = it.first;
@@ -920,10 +931,10 @@ RecordReader::Py_ListGetNativeStackFrame(
920931
return nullptr;
921932
}
922933
while (current_index != 0 && stacks_obtained++ != max_stacks) {
923-
if (cached_index.find(current_index) != cached_index.end()) {
934+
if (cached_index.find(cache_t{current_index, generation}) != cached_index.end()) {
924935
break;
925936
} else {
926-
cached_index.insert(current_index);
937+
cached_index.insert(cache_t{current_index, generation});
927938
}
928939
auto frame = d_native_frames[current_index - 1];
929940
current_index = frame.index;

0 commit comments

Comments
 (0)