Debian Codegraph @

src/include/memcpysampler.hpp

raw symbols SCIP
1 #cxx . . $ `<file>/src/include/memcpysampler.hpp`/pragma once
2 #ifndef MEMCPYSAMPLER_HPP
3 #define MEMCPYSAMPLER_HPPcxx . . $ `src/include/memcpysampler.hpp:3:9`!
4
5 #include <fcntl.h>
6 #include <signal.h>
7 #include <stdint.h>
8 #include <string.h>
9 #include <sys/stat.h>
10 #include <sys/types.h>
11
12 #include "mallocrecursionguard.hpp"cxx . . $ `<file>/src/include/mallocrecursionguard.hpp`/
13 #if !defined(_WIN32)
14 #include <unistd.h> // for getpid()
15 #endif
16
17 #include "printf.h"cxx . . $ `<file>/vendor/printf/printf.h`/
18 #include "pywhere.hpp"cxx . . $ `<file>/src/include/pywhere.hpp`/
19 #include "sampler.hpp"cxx . . $ `<file>/src/include/sampler.hpp`/
20
21 #if !defined(__APPLE__) && !defined(_WIN32)
22 #include <endian.h>
23 #endif
24
25 void *memcpy_muslcxx . . $ memcpy_musl(cc817cb175f282d).(void *dest, const void *src, size_t n) {
26 unsigned char *d = reinterpret_cast<unsigned char *>(dest);
27 const unsigned char *s = reinterpret_cast<const unsigned char *>(src);
28
29 #ifdef __GNUC__cxx . . $ `<built-in>:8:9`!
30
31 #if __BYTE_ORDERcxx . . $ `<external>/1758940242695212560/endianness.h:9:9`! == __LITTLE_ENDIANcxx . . $ `<external>/15579718094795539545/endian.h:30:9`!
32 #define LScxx . . $ `src/include/memcpysampler.hpp:32:9`! >>
33 #define RScxx . . $ `src/include/memcpysampler.hpp:33:9`! <<
34 #else
35 #define LS <<
36 #define RS >>
37 #endif
38
39 typedef uint32_tcxx . . $ uint32_t# __attribute__((__may_alias__)) u32cxx . . $ memcpy_musl(cc817cb175f282d).u32#;
40 uint32_tcxx . . $ uint32_t# w, x;
41
42 for (; (uintptr_tcxx . . $ uintptr_t#)s % 4 && n; n--) *d++ = *s++;
43
44 if ((uintptr_tcxx . . $ uintptr_t#)d % 4 == 0) {
45 for (; n >= 16; s += 16, d += 16, n -= 16) {
46 *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(d + 0) = *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(s + 0);
47 *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(d + 4) = *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(s + 4);
48 *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(d + 8) = *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(s + 8);
49 *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(d + 12) = *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(s + 12);
50 }
51 if (n & 8) {
52 *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(d + 0) = *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(s + 0);
53 *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(d + 4) = *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(s + 4);
54 d += 8;
55 s += 8;
56 }
57 if (n & 4) {
58 *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(d + 0) = *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(s + 0);
59 d += 4;
60 s += 4;
61 }
62 if (n & 2) {
63 *d++ = *s++;
64 *d++ = *s++;
65 }
66 if (n & 1) {
67 *d = *s;
68 }
69 return dest;
70 }
71
72 if (n >= 32) switch ((uintptr_tcxx . . $ uintptr_t#)d % 4) {
73 case 1:
74 w = *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)s;
75 *d++ = *s++;
76 *d++ = *s++;
77 *d++ = *s++;
78 n -= 3;
79 for (; n >= 17; s += 16, d += 16, n -= 16) {
80 x = *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(s + 1);
81 *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(d + 0) = (w LScxx . . $ `src/include/memcpysampler.hpp:32:9`! 24) | (x RScxx . . $ `src/include/memcpysampler.hpp:33:9`! 8);
82 w = *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(s + 5);
83 *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(d + 4) = (x LScxx . . $ `src/include/memcpysampler.hpp:32:9`! 24) | (w RScxx . . $ `src/include/memcpysampler.hpp:33:9`! 8);
84 x = *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(s + 9);
85 *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(d + 8) = (w LScxx . . $ `src/include/memcpysampler.hpp:32:9`! 24) | (x RScxx . . $ `src/include/memcpysampler.hpp:33:9`! 8);
86 w = *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(s + 13);
87 *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(d + 12) = (x LScxx . . $ `src/include/memcpysampler.hpp:32:9`! 24) | (w RScxx . . $ `src/include/memcpysampler.hpp:33:9`! 8);
88 }
89 break;
90 case 2:
91 w = *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)s;
92 *d++ = *s++;
93 *d++ = *s++;
94 n -= 2;
95 for (; n >= 18; s += 16, d += 16, n -= 16) {
96 x = *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(s + 2);
97 *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(d + 0) = (w LScxx . . $ `src/include/memcpysampler.hpp:32:9`! 16) | (x RScxx . . $ `src/include/memcpysampler.hpp:33:9`! 16);
98 w = *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(s + 6);
99 *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(d + 4) = (x LScxx . . $ `src/include/memcpysampler.hpp:32:9`! 16) | (w RScxx . . $ `src/include/memcpysampler.hpp:33:9`! 16);
100 x = *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(s + 10);
101 *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(d + 8) = (w LScxx . . $ `src/include/memcpysampler.hpp:32:9`! 16) | (x RScxx . . $ `src/include/memcpysampler.hpp:33:9`! 16);
102 w = *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(s + 14);
103 *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(d + 12) = (x LScxx . . $ `src/include/memcpysampler.hpp:32:9`! 16) | (w RScxx . . $ `src/include/memcpysampler.hpp:33:9`! 16);
104 }
105 break;
106 case 3:
107 w = *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)s;
108 *d++ = *s++;
109 n -= 1;
110 for (; n >= 19; s += 16, d += 16, n -= 16) {
111 x = *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(s + 3);
112 *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(d + 0) = (w LScxx . . $ `src/include/memcpysampler.hpp:32:9`! 8) | (x RScxx . . $ `src/include/memcpysampler.hpp:33:9`! 24);
113 w = *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(s + 7);
114 *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(d + 4) = (x LScxx . . $ `src/include/memcpysampler.hpp:32:9`! 8) | (w RScxx . . $ `src/include/memcpysampler.hpp:33:9`! 24);
115 x = *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(s + 11);
116 *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(d + 8) = (w LScxx . . $ `src/include/memcpysampler.hpp:32:9`! 8) | (x RScxx . . $ `src/include/memcpysampler.hpp:33:9`! 24);
117 w = *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(s + 15);
118 *(u32cxx . . $ memcpy_musl(cc817cb175f282d).u32# *)(d + 12) = (x LScxx . . $ `src/include/memcpysampler.hpp:32:9`! 8) | (w RScxx . . $ `src/include/memcpysampler.hpp:33:9`! 24);
119 }
120 break;
121 }
122 if (n & 16) {
123 *d++ = *s++;
124 *d++ = *s++;
125 *d++ = *s++;
126 *d++ = *s++;
127 *d++ = *s++;
128 *d++ = *s++;
129 *d++ = *s++;
130 *d++ = *s++;
131 *d++ = *s++;
132 *d++ = *s++;
133 *d++ = *s++;
134 *d++ = *s++;
135 *d++ = *s++;
136 *d++ = *s++;
137 *d++ = *s++;
138 *d++ = *s++;
139 }
140 if (n & 8) {
141 *d++ = *s++;
142 *d++ = *s++;
143 *d++ = *s++;
144 *d++ = *s++;
145 *d++ = *s++;
146 *d++ = *s++;
147 *d++ = *s++;
148 *d++ = *s++;
149 }
150 if (n & 4) {
151 *d++ = *s++;
152 *d++ = *s++;
153 *d++ = *s++;
154 *d++ = *s++;
155 }
156 if (n & 2) {
157 *d++ = *s++;
158 *d++ = *s++;
159 }
160 if (n & 1) {
161 *d = *s;
162 }
163 return dest;
164 #endif
165
166 for (; n; n--) *d++ = *s++;
167 return dest;
168 }
169
170 #include "samplefile.hpp"cxx . . $ `<file>/src/include/samplefile.hpp`/
171
172 template <uint64_tcxx . . $ uint64_t# MemcpySamplingRateBytes>
173 class MemcpySamplercxx . . $ MemcpySampler# {
174 enumcxx . . $ MemcpySampler#$anonymous_type_0# { MemcpySignalcxx . . $ MemcpySampler#MemcpySignal. = SIGPROFcxx . . $ `<external>/1457673850468744829/signum-arch.h:48:9`! };
175 static constexpr auto flagscxx . . $ MemcpySampler#flags. =
176 O_WRONLYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:44:9`! | O_CREATcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:47:10`! | O_SYNCcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:68:10`! | O_APPENDcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:59:10`!; // O_TRUNC;
177 static constexpr auto permscxx . . $ MemcpySampler#perms. = S_IRUSRcxx . . $ `<external>/14710646456994434043/fcntl.h:105:10`! | S_IWUSRcxx . . $ `<external>/14710646456994434043/fcntl.h:106:10`!;
178 static constexpr auto fnamecxx . . $ MemcpySampler#fname. = "/tmp/scalene-memcpy-signal%d";
179
180 public:
181 MemcpySamplercxx . . $ MemcpySampler#`MemcpySampler<MemcpySamplingRateBytes>`(49f6e7a06ebc5aa8).()
182 : _samplefilecxx . . $ MemcpySampler#_samplefile.("/tmp/scalene-memcpy-signal%d",
183 "/tmp/scalene-memcpy-lock%d", "/tmp/scalene-memcpy-init%d"),
184 _intervalcxx . . $ MemcpySampler#_interval.(MemcpySamplingRateBytes),
185 _memcpyOpscxx . . $ MemcpySampler#_memcpyOps.(0),
186 _memcpyTriggeredcxx . . $ MemcpySampler#_memcpyTriggered.(0) {
187 static HLcxx . . $ HL/::PosixLockcxx . . $ HL/PosixLock# init_lockcxx . . $ HL/PosixLockType#PosixLockType(49f6e7a06ebc5aa8).;
188 init_lock.lockcxx . . $ HL/PosixLockType#lock(49f6e7a06ebc5aa8).();
189 auto old_sig = signal(MemcpySignalcxx . . $ MemcpySampler#MemcpySignal., SIG_IGNcxx . . $ `<external>/11773118241360591791/signum-generic.h:30:9`!);
190 if (old_sig !=cxx . . $ std/`operator!=`(39b98f4fe21ae589). SIG_DFLcxx . . $ `<external>/11773118241360591791/signum-generic.h:29:9`!) signal(MemcpySignalcxx . . $ MemcpySampler#MemcpySignal., old_sig);
191 init_lock.unlockcxx . . $ HL/PosixLockType#unlock(49f6e7a06ebc5aa8).();
192 auto pid = getpid();
193 snprintf_((char *)scalene_memcpy_signal_filenamecxx . . $ MemcpySampler#scalene_memcpy_signal_filename.,
194 sizeof(scalene_memcpy_signal_filenamecxx . . $ MemcpySampler#scalene_memcpy_signal_filename.), fnamecxx . . $ MemcpySampler#fname., pid);
195 // printf("initialized (%s)\n", scalene_memcpy_signal_filename);
196 }
197
198 int local_strlencxx . . $ MemcpySampler#local_strlen(5c4306d8553b3696).(const char *str) {
199 int len = 0;
200 while (*str != '\0') {
201 len++;
202 str++;
203 }
204 return len;
205 }
206
207 ~cxx . . $ MemcpySampler#`~MemcpySampler<MemcpySamplingRateBytes>`(49f6e7a06ebc5aa8).MemcpySampler() { unlink(scalene_memcpy_signal_filenamecxx . . $ MemcpySampler#scalene_memcpy_signal_filename.); }
208
209 ATTRIBUTE_ALWAYS_INLINEcxx . . $ `src/include/common.hpp:12:9`! inline void *memcpycxx . . $ MemcpySampler#memcpy(cc817cb175f282d).(void *dst, const void *src,
210 size_t n) {
211 MallocRecursionGuardcxx . . $ MallocRecursionGuard# gcxx . . $ MallocRecursionGuard#MallocRecursionGuard(49f6e7a06ebc5aa8).;
212 auto result = local_memcpycxx . . $ MemcpySampler#local_memcpy(cc817cb175f282d).(dst, src, n);
213 if (pythonDetectedcxx . . $ pythonDetected(c9857a73d68e59ba).() && !g.wasInMalloccxx . . $ MallocRecursionGuard#wasInMalloc(50ce9a9e25b4a850).()) incrementMemoryOpscxx . . $ MemcpySampler#incrementMemoryOps(d4f767463ce0a6b3).(n);
214 return result; // always dst
215 }
216
217 ATTRIBUTE_ALWAYS_INLINEcxx . . $ `src/include/common.hpp:12:9`! inline void *memmovecxx . . $ MemcpySampler#memmove(cc817cb175f282d).(void *dst, const void *src,
218 size_t n) {
219 MallocRecursionGuardcxx . . $ MallocRecursionGuard# gcxx . . $ MallocRecursionGuard#MallocRecursionGuard(49f6e7a06ebc5aa8).;
220 auto result = local_memmovecxx . . $ MemcpySampler#local_memmove(cc817cb175f282d).(dst, src, n);
221 if (pythonDetectedcxx . . $ pythonDetected(c9857a73d68e59ba).() && !g.wasInMalloccxx . . $ MallocRecursionGuard#wasInMalloc(50ce9a9e25b4a850).()) incrementMemoryOpscxx . . $ MemcpySampler#incrementMemoryOps(d4f767463ce0a6b3).(n);
222 return result; // always dst
223 }
224
225 ATTRIBUTE_ALWAYS_INLINEcxx . . $ `src/include/common.hpp:12:9`! inline char *strcpycxx . . $ MemcpySampler#strcpy(b7b3a83273496486).(char *dst, const char *src) {
226 MallocRecursionGuardcxx . . $ MallocRecursionGuard# gcxx . . $ MallocRecursionGuard#MallocRecursionGuard(49f6e7a06ebc5aa8).;
227 auto n = ::strlen(src);
228 auto result = local_strcpycxx . . $ MemcpySampler#local_strcpy(b7b3a83273496486).(dst, src);
229 if (pythonDetectedcxx . . $ pythonDetected(c9857a73d68e59ba).() && !g.wasInMalloccxx . . $ MallocRecursionGuard#wasInMalloc(50ce9a9e25b4a850).()) incrementMemoryOpscxx . . $ MemcpySampler#incrementMemoryOps(d4f767463ce0a6b3).(n);
230 return result; // always dst
231 }
232
233 private:
234 //// local implementations of memcpy and friends.
235 Samplercxx . . $ Sampler#<MemcpySamplingRateBytes> _memcpySamplercxx . . $ MemcpySampler#_memcpySampler.;
236 SampleFilecxx . . $ SampleFile# _samplefilecxx . . $ MemcpySampler#_samplefile.;
237 ATTRIBUTE_ALWAYS_INLINEcxx . . $ `src/include/common.hpp:12:9`! inline void *local_memcpycxx . . $ MemcpySampler#local_memcpy(cc817cb175f282d).(void *dst, const void *src,
238 size_t n) {
239 #if defined(__APPLE__)
240 return ::memcpy(dst, src, n);
241 #else
242 return memcpy_muslcxx . . $ memcpy_musl(cc817cb175f282d).(dst, src, n);
243 #endif
244 }
245
246 void *local_memmovecxx . . $ MemcpySampler#local_memmove(cc817cb175f282d).(void *dst, const void *src, size_t n) {
247 #if defined(__APPLE__)
248 return ::memmove(dst, src, n);
249 #else
250 // TODO: optimize if these areas don't overlap.
251 void *buf = malloc(n);
252 local_memcpycxx . . $ MemcpySampler#local_memcpy(cc817cb175f282d).(buf, src, n);
253 local_memcpycxx . . $ MemcpySampler#local_memcpy(cc817cb175f282d).(dst, buf, n);
254 free(buf);
255 return dst;
256 #endif
257 }
258
259 char *local_strcpycxx . . $ MemcpySampler#local_strcpy(b7b3a83273496486).(char *dst, const char *src) {
260 char *orig_dst = dst;
261 while (*src != '\0') {
262 *dst++ = *src++;
263 }
264 *dst = '\0';
265 return orig_dst;
266 }
267
268 void incrementMemoryOpscxx . . $ MemcpySampler#incrementMemoryOps(d4f767463ce0a6b3).(int n) {
269 if (pythonDetectedcxx . . $ pythonDetected(c9857a73d68e59ba).()) {
270 _memcpyOpscxx . . $ MemcpySampler#_memcpyOps. += n;
271 auto sampleMemop = _memcpySamplercxx . . $ MemcpySampler#_memcpySampler..samplecxx . . $ Sampler#sample(ecdaa1632f49f591).(n);
272 if (unlikelycxx . . $ `src/include/common.hpp:8:9`!(sampleMemop)) {
273 writeCountcxx . . $ MemcpySampler#writeCount(49f6e7a06ebc5aa8).();
274 _memcpyTriggeredcxx . . $ MemcpySampler#_memcpyTriggered.++;
275 _memcpyOpscxx . . $ MemcpySampler#_memcpyOps. = 0;
276 #if !SCALENE_DISABLE_SIGNALScxx . . $ `src/source/libscalene.cpp:1:9`!
277 raise(MemcpySignalcxx . . $ MemcpySampler#MemcpySignal.);
278 #endif
279 }
280 }
281 }
282
283 uint64_tcxx . . $ uint64_t# _intervalcxx . . $ MemcpySampler#_interval.;
284 uint64_tcxx . . $ uint64_t# _memcpyOpscxx . . $ MemcpySampler#_memcpyOps.;
285 unsigned long long _memcpyTriggeredcxx . . $ MemcpySampler#_memcpyTriggered.;
286 static constexpr int BUFFER_LENGTHcxx . . $ MemcpySampler#BUFFER_LENGTH. = 1024;
287 char scalene_memcpy_signal_filenamecxx . . $ MemcpySampler#scalene_memcpy_signal_filename.[BUFFER_LENGTHcxx . . $ MemcpySampler#BUFFER_LENGTH.];
288
289 void writeCountcxx . . $ MemcpySampler#writeCount(49f6e7a06ebc5aa8).() {
290 #if 1
291 stdcxx . . $ std/::stringcxx . . $ std/string# filenamecxx . . $ std/__cxx11/basic_string#basic_string(49f6e7a06ebc5aa8).;
292 int lineno = 1;
293 int bytei = 0;
294 decltype(whereInPython) *where = p_whereInPython;
295 if (where != nullptr && where(filename, lineno, bytei)) {
296 ;
297 }
298 #endif
299 char buf[BUFFER_LENGTHcxx . . $ MemcpySampler#BUFFER_LENGTH.];
300 snprintf_(buf, sizeof(buf), "%d,%d,%d,%s,%d,%d\n\n", _memcpyTriggeredcxx . . $ MemcpySampler#_memcpyTriggered.,
301 _memcpyOpscxx . . $ MemcpySampler#_memcpyOps., getpid(), filename.c_strcxx . . $ std/__cxx11/basic_string#c_str(1016ee5406d91ab9).(), lineno, bytei);
302 _samplefilecxx . . $ MemcpySampler#_samplefile..writeToFilecxx . . $ SampleFile#writeToFile(fda57c8427c60377).(buf);
303 }
304 };
305
306 #endif