Debian Codegraph @

src/include/sampleheap.hpp

raw symbols SCIP
1 #cxx . . $ `<file>/src/include/sampleheap.hpp`/pragma once
2
3 #ifndef SAMPLEHEAP_H
4 #define SAMPLEHEAP_Hcxx . . $ `src/include/sampleheap.hpp:4:9`!
5
6 #include <assert.h>
7 #include <dlfcn.h>
8 #include <fcntl.h>
9 #include <signal.h>
10 #include <stdlib.h>
11 #include <sys/errno.h>
12 #include <sys/mman.h>
13 #include <sys/stat.h>
14 #include <sys/syscall.h>
15 #include <sys/types.h>
16 #include <unistd.h> // for getpid()
17
18 #include <atomic>
19 #include <random>
20
21 // We're unable to use the limited API because, for example,
22 // there doesn't seem to be a function returning co_filename
23 // #define Py_LIMITED_API 0x03070000
24
25 #include "common.hpp"cxx . . $ `<file>/src/include/common.hpp`/
26 #include "mallocrecursionguard.hpp"cxx . . $ `<file>/src/include/mallocrecursionguard.hpp`/
27 #include "poissonsampler.hpp"cxx . . $ `<file>/src/include/poissonsampler.hpp`/
28 #include "printf.h"cxx . . $ `<file>/vendor/printf/printf.h`/
29 #include "pywhere.hpp"cxx . . $ `<file>/src/include/pywhere.hpp`/
30 #include "samplefile.hpp"cxx . . $ `<file>/src/include/samplefile.hpp`/
31 #include "scaleneheader.hpp"cxx . . $ `<file>/src/include/scaleneheader.hpp`/
32 #include "thresholdsampler.hpp"cxx . . $ `<file>/src/include/thresholdsampler.hpp`/
33
34 static SampleFilecxx . . $ SampleFile#& getSampleFilecxx . . $ getSampleFile(7d94a2118539d826).() {
35 static SampleFilecxx . . $ SampleFile# mallocSampleFilecxx . . $ SampleFile#SampleFile(facfdab5246d4be7).("/tmp/scalene-malloc-signal%d",
36 "/tmp/scalene-malloc-lock%d",
37 "/tmp/scalene-malloc-init%d");
38
39 return mallocSampleFile;
40 }
41
42 #define USE_ATOMICScxx . . $ `src/include/sampleheap.hpp:42:9`! 0
43 #if USE_ATOMICScxx . . $ `src/include/sampleheap.hpp:42:9`!
44 typedef std::atomic<uint64_t> counterType;
45 #else
46 typedef uint64_tcxx . . $ uint64_t# counterTypecxx . . $ counterType#;
47 #endif
48
49 template <uint64_tcxx . . $ uint64_t# DefaultAllocationSamplingRateBytes, class SuperHeap>
50 class SampleHeapcxx . . $ SampleHeap# : public SuperHeap {
51 constexpr static auto sampling_window_envnamecxx . . $ SampleHeap#sampling_window_envname. =
52 "SCALENE_ALLOCATION_SAMPLING_WINDOW";
53
54 public:
55 enumcxx . . $ SampleHeap#$anonymous_type_0# { Alignmentcxx . . $ SampleHeap#Alignment. = SuperHeap::Alignment };
56 enum AllocSignalcxx . . $ SampleHeap#AllocSignal# { MallocSignalcxx . . $ SampleHeap#MallocSignal. = SIGXCPUcxx . . $ `<external>/1457673850468744829/signum-arch.h:46:9`!, FreeSignalcxx . . $ SampleHeap#FreeSignal. = SIGXFSZcxx . . $ `<external>/1457673850468744829/signum-arch.h:45:9`! };
57 static constexpr uint64_tcxx . . $ uint64_t# NEWLINEcxx . . $ SampleHeap#NEWLINE. =
58 98821; // Sentinel value denoting a new line has executed
59
60 SampleHeapcxx . . $ SampleHeap#`SampleHeap<DefaultAllocationSamplingRateBytes, SuperHeap>`(49f6e7a06ebc5aa8).()
61 : _lastMallocTriggercxx . . $ SampleHeap#_lastMallocTrigger.(nullptr),
62 _freedLastMallocTriggercxx . . $ SampleHeap#_freedLastMallocTrigger.(false),
63 _allocationSamplercxx . . $ SampleHeap#_allocationSampler.(getenvcxx . . $ getenv(a8b8e32347182212).(sampling_window_envnamecxx . . $ SampleHeap#sampling_window_envname.)
64 ? atolcxx . . $ atol(3c95bd14d5d5d286).(getenvcxx . . $ getenv(a8b8e32347182212).(sampling_window_envnamecxx . . $ SampleHeap#sampling_window_envname.))
65 : DefaultAllocationSamplingRateBytes) {
66 getSampleFilecxx . . $ getSampleFile(7d94a2118539d826).(); // invoked here so the file gets initialized before python
67 // attempts to read from it
68
69 get_signal_init_lockcxx . . $ SampleHeap#get_signal_init_lock(a13e1f97ec5ca3b0).().lockcxx . . $ HL/PosixLockType#lock(49f6e7a06ebc5aa8).();
70 auto old_malloc = signal(MallocSignalcxx . . $ SampleHeap#MallocSignal., SIG_IGNcxx . . $ `<external>/11773118241360591791/signum-generic.h:30:9`!);
71 if (old_malloc !=cxx . . $ std/`operator!=`(39b98f4fe21ae589). SIG_DFLcxx . . $ `<external>/11773118241360591791/signum-generic.h:29:9`!) {
72 signal(MallocSignalcxx . . $ SampleHeap#MallocSignal., old_malloc);
73 }
74 auto old_free = signal(FreeSignalcxx . . $ SampleHeap#FreeSignal., SIG_IGNcxx . . $ `<external>/11773118241360591791/signum-generic.h:30:9`!);
75 if (old_free !=cxx . . $ std/`operator!=`(39b98f4fe21ae589). SIG_DFLcxx . . $ `<external>/11773118241360591791/signum-generic.h:29:9`!) {
76 signal(FreeSignalcxx . . $ SampleHeap#FreeSignal., old_free);
77 }
78 get_signal_init_lockcxx . . $ SampleHeap#get_signal_init_lock(a13e1f97ec5ca3b0).().unlockcxx . . $ HL/PosixLockType#unlock(49f6e7a06ebc5aa8).();
79 }
80
81 ATTRIBUTE_ALWAYS_INLINEcxx . . $ `src/include/common.hpp:12:9`! inline void* malloccxx . . $ SampleHeap#malloc(bfcfa4d6b7f7ef64).(size_t sz) {
82 MallocRecursionGuardcxx . . $ MallocRecursionGuard# gcxx . . $ MallocRecursionGuard#MallocRecursionGuard(49f6e7a06ebc5aa8).;
83 auto ptr = SuperHeap::malloc(sz);
84 if (unlikelycxx . . $ `src/include/common.hpp:8:9`!(ptr == nullptr)) {
85 return nullptr;
86 }
87 if (pythonDetectedcxx . . $ pythonDetected(c9857a73d68e59ba).() && !g.wasInMalloccxx . . $ MallocRecursionGuard#wasInMalloc(50ce9a9e25b4a850).()) {
88 // All standard Python allocations pass through
89 // MakeLocalAllocator in libscalene.cpp .
90 // We don't want to double-count these allocations--
91 // if we're in here and g.wasInMalloc is true, then
92 // we passed through MakeLocalAllocator and
93 // this allocation has already been counted
94 //
95 // We only want to count allocations here
96 // if `malloc` itself was called by client code.
97 auto realSize = SuperHeap::getSize(ptr);
98 if (realSize >cxx . . $ std/`operator>`(39b98f4fe21ae589). 0) {
99 if (sz == NEWLINEcxx . . $ SampleHeap#NEWLINE. + sizeof(ScaleneHeadercxx . . $ ScaleneHeader#)) {
100 // FIXME
101 //
102 // If we ourselves are allocating a NEWLINE, we're doing
103 // it through the Python allocator, so if it's an actual newline
104 // I don't think we should ever get here. I think our original intention
105 // is that we shouldn't count NEWLINE records that we already counted,
106 // but I think if we get to this line here, we didn't actually create a NEWLINE
107 // and should count it.
108 return ptr;
109 }
110 register_malloccxx . . $ SampleHeap#register_malloc(48ecb54ea0393171).(realSize, ptr, false); // false -> invoked from C/C++
111 }
112 }
113 return ptr;
114 }
115 ATTRIBUTE_ALWAYS_INLINEcxx . . $ `src/include/common.hpp:12:9`! inline void* realloccxx . . $ SampleHeap#realloc(417ab160f6baadc6).(void* ptr, size_t sz) {
116 MallocRecursionGuardcxx . . $ MallocRecursionGuard# gcxx . . $ MallocRecursionGuard#MallocRecursionGuard(49f6e7a06ebc5aa8).;
117 if (!ptr) {
118 ptr = SuperHeap::malloc(sz);
119 return ptr;
120 }
121 if (sz == 0) {
122 SuperHeap::free(ptr);
123 #if defined(__APPLE__)
124 // 0 size = free. We return a small object. This behavior is
125 // apparently required under Mac OS X and optional under POSIX.
126 return SuperHeap::malloc(1);
127 #else
128 // For POSIX, don't return anything.
129 return nullptr;
130 #endif
131 }
132 size_t objSize = SuperHeap::getSize(ptr);
133
134 void* buf = SuperHeap::malloc(sz);
135 size_t buf_size = buf ? SuperHeap::getSize(buf) : 0;
136 if (buf) {
137 if (objSize == buf_size) {
138 // The objects are the same actual size.
139 // Free the new object and return the original.
140 SuperHeap::free(buf);
141 return ptr;
142 }
143 // Copy the contents of the original object
144 // up to the size of the new block.
145 size_t minSize = (objSize < sz) ? objSize : sz;
146 memcpy(buf, ptr, minSize);
147 }
148
149 // Free the old block.
150 SuperHeap::free(ptr);
151 if (buf) {
152 if (sz < buf_size) {
153 register_malloccxx . . $ SampleHeap#register_malloc(48ecb54ea0393171).(buf_size - sz, buf,
154 false); // false -> invoked from C/C++
155 } else if (sz > buf_size) {
156 register_freecxx . . $ SampleHeap#register_free(3bdecba87b0fe3d5).(sz - buf_size, ptr);
157 }
158 }
159 // Return a pointer to the new one.
160 return buf;
161 }
162 inline void register_malloccxx . . $ SampleHeap#register_malloc(48ecb54ea0393171).(size_t realSize, void* ptr,
163 bool inPythonAllocator = true) {
164 if (p_scalene_done) return;
165 assertcxx . . $ `/usr/include/assert.h:50:10`!(realSize);
166 // If this is the special NEWLINE value, trigger an update.
167 if (unlikelycxx . . $ `src/include/common.hpp:8:9`!(realSize == NEWLINE)) {
168 stdcxx . . $ std/::stringcxx . . $ std/string# filenamecxx . . $ std/__cxx11/basic_string#basic_string(49f6e7a06ebc5aa8).;
169 // Originally, we had the following check around this line:
170 //
171 // ```
172 // if (where != nullptr && where(filename, lineno, bytei))
173 // ```
174 //
175 // This was to prevent a NEWLINE record from being accidentally triggered by
176 // non-Scalene code.
177 //
178 // However, by definition, we trigger a NEWLINE _after_ the line has
179 // been executed, specifically on a `PyTrace_Line` event.
180 //
181 // If the absolute last line of a program makes an allocation,
182 // the next PyTrace_Line will occur inside `scalene_profiler.py` and not any client
183 // code, since the line after the last line of the program is when Scalene starts its
184 // teardown.
185 //
186 // In this case. the `whereInPython` function will return 0, since whereInPython checks
187 // if the current frame is in client code and the Scalene profiler teardown code is by definition
188 // not.
189 //
190 // This risks letting allocations of length NEWLINE_TRIGGER_LENGTH that are not true NEWLINEs
191 // create a NEWLINE record, but we view this as incredibly improbable.
192 writeCountcxx . . $ SampleHeap#writeCount(e021c2df541e98e9).(MallocSignalcxx . . $ SampleHeap#MallocSignal., realSize, ptr, filename, -1, -1);
193 mallocTriggeredcxx . . $ SampleHeap#mallocTriggered(ff59cb7d091451ff).()++;
194 return;
195 }
196 size_t sampleMallocSize;
197 auto sampleMalloc =
198 _allocationSamplercxx . . $ SampleHeap#_allocationSampler..incrementcxx . . $ ThresholdSampler#increment(9031ac04415d63cc).(realSize, ptr, sampleMallocSize);
199 if (inPythonAllocator) {
200 _pythonCountcxx . . $ SampleHeap#_pythonCount. += realSize;
201 } else {
202 _cCountcxx . . $ SampleHeap#_cCount. += realSize;
203 }
204 if (unlikelycxx . . $ `src/include/common.hpp:8:9`!(sampleMalloc)) {
205 process_malloccxx . . $ SampleHeap#process_malloc(3bdecba87b0fe3d5).(sampleMallocSize, ptr);
206 }
207 }
208
209 private:
210 void process_malloccxx . . $ SampleHeap#process_malloc(3bdecba87b0fe3d5).(size_t sampleMalloc, void* ptr) {
211 stdcxx . . $ std/::stringcxx . . $ std/string# filenamecxx . . $ std/__cxx11/basic_string#basic_string(49f6e7a06ebc5aa8).;
212 int lineno;
213 int bytei;
214
215 decltype(whereInPython)* where = p_whereInPython;
216 if (where != nullptr && where(filename, lineno, bytei)) {
217 writeCountcxx . . $ SampleHeap#writeCount(e021c2df541e98e9).(MallocSignalcxx . . $ SampleHeap#MallocSignal., sampleMalloc, ptr, filename, lineno, bytei);
218 #if !SCALENE_DISABLE_SIGNALScxx . . $ `src/source/libscalene.cpp:1:9`!
219 raise(MallocSignalcxx . . $ SampleHeap#MallocSignal.);
220 #endif
221 _lastMallocTriggercxx . . $ SampleHeap#_lastMallocTrigger. = ptr;
222 _freedLastMallocTriggercxx . . $ SampleHeap#_freedLastMallocTrigger. = false;
223 _pythonCountcxx . . $ SampleHeap#_pythonCount. = 0;
224 _cCountcxx . . $ SampleHeap#_cCount. = 0;
225 mallocTriggeredcxx . . $ SampleHeap#mallocTriggered(ff59cb7d091451ff).()++;
226 }
227 }
228
229 public:
230 ATTRIBUTE_ALWAYS_INLINEcxx . . $ `src/include/common.hpp:12:9`! inline void freecxx . . $ SampleHeap#free(5d22bdacc48458e8).(void* ptr) {
231 MallocRecursionGuardcxx . . $ MallocRecursionGuard# gcxx . . $ MallocRecursionGuard#MallocRecursionGuard(49f6e7a06ebc5aa8).;
232 if (unlikelycxx . . $ `src/include/common.hpp:8:9`!(ptr == nullptr)) {
233 return;
234 }
235 auto realSize = SuperHeap::getSize(ptr);
236 SuperHeap::free(ptr);
237 if (pythonDetectedcxx . . $ pythonDetected(c9857a73d68e59ba).() && !g.wasInMalloccxx . . $ MallocRecursionGuard#wasInMalloc(50ce9a9e25b4a850).()) {
238 register_freecxx . . $ SampleHeap#register_free(3bdecba87b0fe3d5).(realSize, ptr);
239 }
240 }
241
242 inline void register_freecxx . . $ SampleHeap#register_free(3bdecba87b0fe3d5).(size_t realSize, void* ptr) {
243 if (p_scalene_done) return;
244 size_t sampleFreeSize;
245 auto sampleFree =
246 _allocationSamplercxx . . $ SampleHeap#_allocationSampler..decrementcxx . . $ ThresholdSampler#decrement(9031ac04415d63cc).(realSize, ptr, sampleFreeSize);
247
248 if (unlikelycxx . . $ `src/include/common.hpp:8:9`!(ptr && (ptr == _lastMallocTrigger))) {
249 _freedLastMallocTriggercxx . . $ SampleHeap#_freedLastMallocTrigger. = true;
250 }
251 if (unlikelycxx . . $ `src/include/common.hpp:8:9`!(sampleFree)) {
252 process_freecxx . . $ SampleHeap#process_free(2c44cd906ae4e829).(sampleFreeSize);
253 }
254 }
255
256 private:
257 void process_freecxx . . $ SampleHeap#process_free(2c44cd906ae4e829).(size_t sampleFree) {
258 stdcxx . . $ std/::stringcxx . . $ std/string# filenamecxx . . $ std/__cxx11/basic_string#basic_string(49f6e7a06ebc5aa8).;
259 int lineno = 1;
260 int bytei = 0;
261
262 #if 1
263 decltype(whereInPython)* where = p_whereInPython;
264 if (where != nullptr && where(filename, lineno, bytei)) {
265 ;
266 }
267 #endif
268
269 writeCountcxx . . $ SampleHeap#writeCount(e021c2df541e98e9).(FreeSignalcxx . . $ SampleHeap#FreeSignal., sampleFree, nullptr, filename, lineno, bytei);
270 #if !SCALENE_DISABLE_SIGNALScxx . . $ `src/source/libscalene.cpp:1:9`!
271 raise(FreeSignalcxx . . $ SampleHeap#FreeSignal.);
272 #endif
273 freeTriggeredcxx . . $ SampleHeap#freeTriggered(ff59cb7d091451ff).()++;
274 }
275
276 public:
277 void* memaligncxx . . $ SampleHeap#memalign(63a7bf2941bfcd7a).(size_t alignment, size_t sz) {
278 MallocRecursionGuardcxx . . $ MallocRecursionGuard# gcxx . . $ MallocRecursionGuard#MallocRecursionGuard(49f6e7a06ebc5aa8).;
279 auto ptr = SuperHeap::memalign(alignment, sz);
280 if (unlikelycxx . . $ `src/include/common.hpp:8:9`!(ptr == nullptr)) {
281 return nullptr;
282 }
283 if (pythonDetectedcxx . . $ pythonDetected(c9857a73d68e59ba).() && !g.wasInMalloccxx . . $ MallocRecursionGuard#wasInMalloc(50ce9a9e25b4a850).()) {
284 auto realSize = SuperHeap::getSize(ptr);
285 assertcxx . . $ `/usr/include/assert.h:50:10`!(realSize >= sz);
286 // EDB 4 June 2023, disabled below, possibly spurious assertion
287 // assert((sz < 16) || (realSize <= 2 * sz));
288 register_malloccxx . . $ SampleHeap#register_malloc(48ecb54ea0393171).(realSize, ptr, false); // false -> invoked from C/C++
289 }
290 return ptr;
291 }
292
293 private:
294 // Prevent copying and assignment.
295 SampleHeapcxx . . $ SampleHeap#`SampleHeap<DefaultAllocationSamplingRateBytes, SuperHeap>`(4a7ab03cafd3daa4).(const SampleHeap&) = delete;
296 SampleHeap& operatorcxx . . $ SampleHeap#`operator=`(d3c4837e015b2b97).=(const SampleHeap&) = delete;
297
298 static auto& mallocTriggeredcxx . . $ SampleHeap#mallocTriggered(ff59cb7d091451ff).() {
299 static stdcxx . . $ std/::atomiccxx . . $ std/atomic#<uint64_tcxx . . $ uint64_t#> _mallocTriggeredcxx . . $ std/atomic#atomic(2c44cd906ae4e829).{0};
300 return _mallocTriggered;
301 }
302 static auto& freeTriggeredcxx . . $ SampleHeap#freeTriggered(ff59cb7d091451ff).() {
303 static stdcxx . . $ std/::atomiccxx . . $ std/atomic#<uint64_tcxx . . $ uint64_t#> _freeTriggeredcxx . . $ std/atomic#atomic(2c44cd906ae4e829).{0};
304 return _freeTriggered;
305 }
306
307 counterTypecxx . . $ counterType# _pythonCountcxx . . $ SampleHeap#_pythonCount.{0};
308 counterTypecxx . . $ counterType# _cCountcxx . . $ SampleHeap#_cCount.{0};
309
310 void* _lastMallocTriggercxx . . $ SampleHeap#_lastMallocTrigger.;
311 bool _freedLastMallocTriggercxx . . $ SampleHeap#_freedLastMallocTrigger.;
312 #if 0
313 typedef PoissonSampler Sampler;
314 #warning "Experimental use only: Poisson sampler"
315 #else
316 typedef ThresholdSamplercxx . . $ ThresholdSampler# Samplercxx . . $ SampleHeap#Sampler#;
317 #endif
318
319 Samplercxx . . $ SampleHeap#Sampler# _allocationSamplercxx . . $ SampleHeap#_allocationSampler.;
320
321 static constexpr auto flagscxx . . $ SampleHeap#flags. = O_RDWRcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:45:9`! | O_CREATcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:47:10`!;
322 static constexpr auto permscxx . . $ SampleHeap#perms. = S_IRUSRcxx . . $ `<external>/14710646456994434043/fcntl.h:105:10`! | S_IWUSRcxx . . $ `<external>/14710646456994434043/fcntl.h:106:10`!;
323
324 void writeCountcxx . . $ SampleHeap#writeCount(e021c2df541e98e9).(AllocSignalcxx . . $ SampleHeap#AllocSignal# sig, uint64_tcxx . . $ uint64_t# count, void* ptr,
325 const stdcxx . . $ std/::stringcxx . . $ std/string#& filename, int lineno, int bytei) {
326 char buf[SampleFilecxx . . $ SampleFile#::MAX_BUFSIZEcxx . . $ SampleFile#MAX_BUFSIZE.];
327 if (_pythonCountcxx . . $ SampleHeap#_pythonCount. == 0) {
328 _pythonCountcxx . . $ SampleHeap#_pythonCount. = 1; // prevent 0/0
329 }
330 snprintf_(
331 buf, sizeof(buf),
332 #if defined(__APPLE__)
333 "%c,%llu,%llu,%f,%d,%p,%s,%d,%d\n\n",
334 #else
335 "%c,%lu,%lu,%f,%d,%p,%s,%d,%d\n\n",
336 #endif
337 ((sig ==cxx . . $ std/`operator==`(39b98f4fe21ae589). MallocSignalcxx . . $ SampleHeap#MallocSignal.) ? 'M' : ((_freedLastMallocTriggercxx . . $ SampleHeap#_freedLastMallocTrigger.) ? 'f' : 'F')),
338 mallocTriggeredcxx . . $ SampleHeap#mallocTriggered(ff59cb7d091451ff).() +cxx . . $ std/operator+(933aeee3a62e2e30). freeTriggeredcxx . . $ SampleHeap#freeTriggered(ff59cb7d091451ff).(), count,
339 (float)_pythonCountcxx . . $ SampleHeap#_pythonCount. / (_pythonCountcxx . . $ SampleHeap#_pythonCount. + _cCountcxx . . $ SampleHeap#_cCount.), getpid(),
340 _freedLastMallocTriggercxx . . $ SampleHeap#_freedLastMallocTrigger. ? _lastMallocTriggercxx . . $ SampleHeap#_lastMallocTrigger. : ptr, filename.c_strcxx . . $ std/__cxx11/basic_string#c_str(1016ee5406d91ab9).(),
341 lineno, bytei);
342 // Ensure we don't report last-malloc-freed multiple times.
343 _freedLastMallocTriggercxx . . $ SampleHeap#_freedLastMallocTrigger. = false;
344 getSampleFilecxx . . $ getSampleFile(7d94a2118539d826).().writeToFilecxx . . $ SampleFile#writeToFile(fda57c8427c60377).(buf);
345 }
346
347 static HLcxx . . $ HL/::PosixLockcxx . . $ HL/PosixLock#& get_signal_init_lockcxx . . $ SampleHeap#get_signal_init_lock(a13e1f97ec5ca3b0).() {
348 static HLcxx . . $ HL/::PosixLockcxx . . $ HL/PosixLock# signal_init_lockcxx . . $ HL/PosixLockType#PosixLockType(49f6e7a06ebc5aa8).;
349 return signal_init_lock;
350 }
351 };
352
353 #endif