@@ -93,16 +93,21 @@ BackgroundSocketReader::Py_GetSnapshotAllocationRecordsAndStatsData(bool merge_t
93
93
std::unordered_map<int , uint64_t > cnt_by_alloc;
94
94
std::vector<std::pair<uint64_t , std::optional<memray::tracking_api::frame_id_t >>> top_size;
95
95
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;
96
98
{
97
99
std::lock_guard<std::mutex> lock (d_mutex);
98
100
stack_to_allocation = d_aggregator.getSnapshotAllocations (merge_threads);
99
101
cnt_by_size = stats_aggregator.allocationCountBySize ();
100
102
cnt_by_alloc = stats_aggregator.allocationCountByAllocator ();
101
103
top_size = stats_aggregator.topLocationsBySize (largest_num);
102
104
top_cnt = stats_aggregator.topLocationsByCount (largest_num);
105
+ total_cnt = stats_aggregator.totalAllocations ();
106
+ total_size = stats_aggregator.totalBytesAllocated ();
103
107
}
104
108
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);
106
111
PyObject* result = PyTuple_Pack (2 , snaps, stats);
107
112
Py_XDECREF (snaps);
108
113
Py_XDECREF (stats);
@@ -120,7 +125,9 @@ BackgroundSocketReader::Py_GetStatsData(
120
125
const std::unordered_map<size_t , uint64_t >& cnt_by_size,
121
126
const std::unordered_map<int , uint64_t >& cnt_by_alloc,
122
127
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)
124
131
{
125
132
PyObject* result = PyList_New (0 );
126
133
if (result == nullptr ) {
@@ -163,8 +170,21 @@ BackgroundSocketReader::Py_GetStatsData(
163
170
return nullptr ;
164
171
}
165
172
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 );
168
188
PyObject* pair = PyTuple_Pack (2 , pk, pv);
169
189
PyList_Append (py_top_size, pair);
170
190
Py_XDECREF (pk);
@@ -180,8 +200,21 @@ BackgroundSocketReader::Py_GetStatsData(
180
200
return nullptr ;
181
201
}
182
202
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 );
185
218
PyObject* pair = PyTuple_Pack (2 , pk, pv);
186
219
PyList_Append (py_top_cnt, pair);
187
220
Py_XDECREF (pk);
@@ -191,6 +224,13 @@ BackgroundSocketReader::Py_GetStatsData(
191
224
PyList_Append (result, py_top_cnt);
192
225
Py_XDECREF (py_top_cnt);
193
226
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
+
194
234
return result;
195
235
}
196
236
0 commit comments