Skip to content

Commit fc1d00f

Browse files
committed
fix get status and get total size total cnt
1 parent 61d47c1 commit fc1d00f

File tree

6 files changed

+90
-8
lines changed

6 files changed

+90
-8
lines changed

src/memray/_memray.pyx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,7 +1402,11 @@ cdef class SocketReader:
14021402
(<AllocationRecord> alloc)._reader = self._reader
14031403
yield alloc
14041404

1405+
def get_main_thread_id(self):
1406+
return self._reader.get().getMainThreadTid()
14051407

1408+
def get_skipped_frames_on_main_thread(self):
1409+
return self._reader.get().getSkippedFramesOnMainThread()
14061410

14071411
def get_current_snapshot_raw_table(self, *, bool merge_threads):
14081412
if self._impl is NULL:

src/memray/_memray/record_reader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ RecordReader::Py_GetTraceInfo(std::vector<std::pair<FrameTree::index_t, size_t>>
820820
}
821821
for (const auto& kv : d_frame_map) {
822822
PyObject* pkey = PyLong_FromUnsignedLong(kv.first);
823-
PyObject* pvalue = kv.second.toPythonObject(d_pystring_cache);
823+
PyObject* pvalue = kv.second.toPythonObjectWithEntry(d_pystring_cache);
824824
PyDict_SetItem(py_d_frame_map, pkey, pvalue);
825825
Py_XDECREF(pkey);
826826
Py_XDECREF(pvalue);

src/memray/_memray/records.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,38 @@ Frame::toPythonObject(python_helpers::PyUnicode_Cache& pystring_cache) const
112112
PyTuple_SET_ITEM(tuple, 2, pylineno);
113113
return tuple;
114114
}
115+
116+
PyObject*
117+
Frame::toPythonObjectWithEntry(python_helpers::PyUnicode_Cache& pystring_cache) const
118+
{
119+
PyObject* pyfunction_name = pystring_cache.getUnicodeObject(function_name);
120+
if (pyfunction_name == nullptr) {
121+
return nullptr;
122+
}
123+
PyObject* pyfilename = pystring_cache.getUnicodeObject(filename);
124+
if (pyfilename == nullptr) {
125+
return nullptr;
126+
}
127+
PyObject* pylineno = PyLong_FromLong(this->lineno);
128+
if (pylineno == nullptr) {
129+
return nullptr;
130+
}
131+
PyObject* pyisentry = PyBool_FromLong(this->is_entry_frame);
132+
if (pyisentry == nullptr) {
133+
return nullptr;
134+
}
135+
PyObject* tuple = PyTuple_New(4);
136+
if (tuple == nullptr) {
137+
Py_DECREF(pylineno);
138+
Py_XDECREF(pyisentry);
139+
return nullptr;
140+
}
141+
Py_INCREF(pyfunction_name);
142+
Py_INCREF(pyfilename);
143+
PyTuple_SET_ITEM(tuple, 0, pyfunction_name);
144+
PyTuple_SET_ITEM(tuple, 1, pyfilename);
145+
PyTuple_SET_ITEM(tuple, 2, pylineno);
146+
PyTuple_SET_ITEM(tuple, 3, pyisentry);
147+
return tuple;
148+
}
115149
} // namespace memray::tracking_api

src/memray/_memray/records.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ struct Frame
246246

247247
PyObject* toPythonObject(python_helpers::PyUnicode_Cache& pystring_cache) const;
248248

249+
PyObject* toPythonObjectWithEntry(python_helpers::PyUnicode_Cache& pystring_cache) const;
250+
249251
auto operator==(const Frame& other) const -> bool
250252
{
251253
return (function_name == other.function_name && filename == other.filename

src/memray/_memray/socket_reader_thread.cpp

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,21 @@ BackgroundSocketReader::Py_GetSnapshotAllocationRecordsAndStatsData(bool merge_t
9393
std::unordered_map<int, uint64_t> cnt_by_alloc;
9494
std::vector<std::pair<uint64_t, std::optional<memray::tracking_api::frame_id_t>>> top_size;
9595
std::vector<std::pair<uint64_t, std::optional<memray::tracking_api::frame_id_t>>> top_cnt;
96+
std::uint64_t total_size;
97+
std::uint64_t total_cnt;
9698
{
9799
std::lock_guard<std::mutex> lock(d_mutex);
98100
stack_to_allocation = d_aggregator.getSnapshotAllocations(merge_threads);
99101
cnt_by_size = stats_aggregator.allocationCountBySize();
100102
cnt_by_alloc = stats_aggregator.allocationCountByAllocator();
101103
top_size = stats_aggregator.topLocationsBySize(largest_num);
102104
top_cnt = stats_aggregator.topLocationsByCount(largest_num);
105+
total_cnt = stats_aggregator.totalAllocations();
106+
total_size = stats_aggregator.totalBytesAllocated();
103107
}
104108
PyObject* snaps = api::Py_ListFromSnapshotAllocationRecords(stack_to_allocation);
105-
PyObject* stats = Py_GetStatsData(cnt_by_size, cnt_by_alloc, top_size, top_cnt);
109+
PyObject* stats =
110+
Py_GetStatsData(cnt_by_size, cnt_by_alloc, top_size, top_cnt, total_size, total_cnt);
106111
PyObject* result = PyTuple_Pack(2, snaps, stats);
107112
Py_XDECREF(snaps);
108113
Py_XDECREF(stats);
@@ -120,7 +125,9 @@ BackgroundSocketReader::Py_GetStatsData(
120125
const std::unordered_map<size_t, uint64_t>& cnt_by_size,
121126
const std::unordered_map<int, uint64_t>& cnt_by_alloc,
122127
std::vector<std::pair<uint64_t, std::optional<memray::tracking_api::frame_id_t>>>& top_size,
123-
std::vector<std::pair<uint64_t, std::optional<memray::tracking_api::frame_id_t>>>& top_cnt)
128+
std::vector<std::pair<uint64_t, std::optional<memray::tracking_api::frame_id_t>>>& top_cnt,
129+
std::uint64_t total_size,
130+
std::uint64_t total_cnt)
124131
{
125132
PyObject* result = PyList_New(0);
126133
if (result == nullptr) {
@@ -163,8 +170,21 @@ BackgroundSocketReader::Py_GetStatsData(
163170
return nullptr;
164171
}
165172
for (const auto& it : top_size) {
166-
PyObject* pk = PyLong_FromSize_t(it.first);
167-
PyObject* pv = PyLong_FromSize_t(it.second.value_or(-1));
173+
// PyObject* pk = PyLong_FromSize_t(it.second.value_or(0));
174+
// PyObject * pk = d_record_reader ->Py_GetFrame(it.second.value_or(0));
175+
PyObject* pk;
176+
try { // todo: optimize
177+
pk = d_record_reader->Py_GetFrame(it.second.value_or(0));
178+
} catch (std::exception& e) {
179+
PyObject* function = PyUnicode_FromString("");
180+
PyObject* file = PyUnicode_FromString("");
181+
PyObject* line = PyLong_FromLong(0);
182+
pk = PyTuple_Pack(3, function, file, line);
183+
Py_XDECREF(function);
184+
Py_XDECREF(file);
185+
Py_XDECREF(line);
186+
};
187+
PyObject* pv = PyLong_FromSize_t(it.first);
168188
PyObject* pair = PyTuple_Pack(2, pk, pv);
169189
PyList_Append(py_top_size, pair);
170190
Py_XDECREF(pk);
@@ -180,8 +200,21 @@ BackgroundSocketReader::Py_GetStatsData(
180200
return nullptr;
181201
}
182202
for (const auto& it : top_cnt) {
183-
PyObject* pk = PyLong_FromSize_t(it.first);
184-
PyObject* pv = PyLong_FromSize_t(it.second.value_or(-1));
203+
// PyObject* pk = PyLong_FromSize_t(it.second.value_or(0));
204+
// PyObject * pk = d_record_reader ->Py_GetFrame(it.second.value_or(0));
205+
PyObject* pk;
206+
try { // todo: optimize
207+
pk = d_record_reader->Py_GetFrame(it.second.value_or(0));
208+
} catch (std::exception& e) {
209+
PyObject* function = PyUnicode_FromString("");
210+
PyObject* file = PyUnicode_FromString("");
211+
PyObject* line = PyLong_FromLong(0);
212+
pk = PyTuple_Pack(3, function, file, line);
213+
Py_XDECREF(function);
214+
Py_XDECREF(file);
215+
Py_XDECREF(line);
216+
};
217+
PyObject* pv = PyLong_FromSize_t(it.first);
185218
PyObject* pair = PyTuple_Pack(2, pk, pv);
186219
PyList_Append(py_top_cnt, pair);
187220
Py_XDECREF(pk);
@@ -191,6 +224,13 @@ BackgroundSocketReader::Py_GetStatsData(
191224
PyList_Append(result, py_top_cnt);
192225
Py_XDECREF(py_top_cnt);
193226

227+
PyObject* py_total_size = PyLong_FromUnsignedLong(total_size);
228+
PyObject* py_total_cnt = PyLong_FromUnsignedLong(total_cnt);
229+
PyList_Append(result, py_total_size);
230+
PyList_Append(result, py_total_cnt);
231+
Py_XDECREF(py_total_size);
232+
Py_XDECREF(py_total_cnt);
233+
194234
return result;
195235
}
196236

src/memray/_memray/socket_reader_thread.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ class BackgroundSocketReader
4545
const std::unordered_map<size_t, uint64_t>& cnt_by_size,
4646
const std::unordered_map<int, uint64_t>& cnt_by_alloc,
4747
std::vector<std::pair<uint64_t, std::optional<memray::tracking_api::frame_id_t>>>& top_size,
48-
std::vector<std::pair<uint64_t, std::optional<memray::tracking_api::frame_id_t>>>& top_cnt);
48+
std::vector<std::pair<uint64_t, std::optional<memray::tracking_api::frame_id_t>>>& top_cnt,
49+
std::uint64_t total_size,
50+
std::uint64_t total_cnt);
4951
PyObject* Py_GetSnapshotAllocationRecordsAndStatsData(bool merge_threads, int largest_num);
5052
};
5153

0 commit comments

Comments
 (0)