| 1 | #pragma once |
| 2 | #ifndef MEMCPYSAMPLER_HPP |
| 3 | #define MEMCPYSAMPLER_HPP |
| 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" |
| 13 | #if !defined(_WIN32) |
| 14 | #include <unistd.h> // for getpid() |
| 15 | #endif |
| 16 | |
| 17 | #include "printf.h" |
| 18 | #include "pywhere.hpp" |
| 19 | #include "sampler.hpp" |
| 20 | |
| 21 | #if !defined(__APPLE__) && !defined(_WIN32) |
| 22 | #include <endian.h> |
| 23 | #endif |
| 24 | |
| 25 | void *memcpy_musl(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__ |
| 30 | |
| 31 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 32 | #define LS >> |
| 33 | #define RS << |
| 34 | #else |
| 35 | #define LS << |
| 36 | #define RS >> |
| 37 | #endif |
| 38 | |
| 39 | typedef uint32_t __attribute__((__may_alias__)) u32; |
| 40 | uint32_t w, x; |
| 41 | |
| 42 | for (; (uintptr_t)s % 4 && n; n--) *d++ = *s++; |
| 43 | |
| 44 | if ((uintptr_t)d % 4 == 0) { |
| 45 | for (; n >= 16; s += 16, d += 16, n -= 16) { |
| 46 | *(u32 *)(d + 0) = *(u32 *)(s + 0); |
| 47 | *(u32 *)(d + 4) = *(u32 *)(s + 4); |
| 48 | *(u32 *)(d + 8) = *(u32 *)(s + 8); |
| 49 | *(u32 *)(d + 12) = *(u32 *)(s + 12); |
| 50 | } |
| 51 | if (n & 8) { |
| 52 | *(u32 *)(d + 0) = *(u32 *)(s + 0); |
| 53 | *(u32 *)(d + 4) = *(u32 *)(s + 4); |
| 54 | d += 8; |
| 55 | s += 8; |
| 56 | } |
| 57 | if (n & 4) { |
| 58 | *(u32 *)(d + 0) = *(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_t)d % 4) { |
| 73 | case 1: |
| 74 | w = *(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 = *(u32 *)(s + 1); |
| 81 | *(u32 *)(d + 0) = (w LS 24) | (x RS 8); |
| 82 | w = *(u32 *)(s + 5); |
| 83 | *(u32 *)(d + 4) = (x LS 24) | (w RS 8); |
| 84 | x = *(u32 *)(s + 9); |
| 85 | *(u32 *)(d + 8) = (w LS 24) | (x RS 8); |
| 86 | w = *(u32 *)(s + 13); |
| 87 | *(u32 *)(d + 12) = (x LS 24) | (w RS 8); |
| 88 | } |
| 89 | break; |
| 90 | case 2: |
| 91 | w = *(u32 *)s; |
| 92 | *d++ = *s++; |
| 93 | *d++ = *s++; |
| 94 | n -= 2; |
| 95 | for (; n >= 18; s += 16, d += 16, n -= 16) { |
| 96 | x = *(u32 *)(s + 2); |
| 97 | *(u32 *)(d + 0) = (w LS 16) | (x RS 16); |
| 98 | w = *(u32 *)(s + 6); |
| 99 | *(u32 *)(d + 4) = (x LS 16) | (w RS 16); |
| 100 | x = *(u32 *)(s + 10); |
| 101 | *(u32 *)(d + 8) = (w LS 16) | (x RS 16); |
| 102 | w = *(u32 *)(s + 14); |
| 103 | *(u32 *)(d + 12) = (x LS 16) | (w RS 16); |
| 104 | } |
| 105 | break; |
| 106 | case 3: |
| 107 | w = *(u32 *)s; |
| 108 | *d++ = *s++; |
| 109 | n -= 1; |
| 110 | for (; n >= 19; s += 16, d += 16, n -= 16) { |
| 111 | x = *(u32 *)(s + 3); |
| 112 | *(u32 *)(d + 0) = (w LS 8) | (x RS 24); |
| 113 | w = *(u32 *)(s + 7); |
| 114 | *(u32 *)(d + 4) = (x LS 8) | (w RS 24); |
| 115 | x = *(u32 *)(s + 11); |
| 116 | *(u32 *)(d + 8) = (w LS 8) | (x RS 24); |
| 117 | w = *(u32 *)(s + 15); |
| 118 | *(u32 *)(d + 12) = (x LS 8) | (w RS 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" |
| 171 | |
| 172 | template <uint64_t MemcpySamplingRateBytes> |
| 173 | class MemcpySampler { |
| 174 | enum { MemcpySignal = SIGPROF }; |
| 175 | static constexpr auto flags = |
| 176 | O_WRONLY | O_CREAT | O_SYNC | O_APPEND; // O_TRUNC; |
| 177 | static constexpr auto perms = S_IRUSR | S_IWUSR; |
| 178 | static constexpr auto fname = "/tmp/scalene-memcpy-signal%d"; |
| 179 | |
| 180 | public: |
| 181 | MemcpySampler() |
| 182 | : _samplefile("/tmp/scalene-memcpy-signal%d", |
| 183 | "/tmp/scalene-memcpy-lock%d", "/tmp/scalene-memcpy-init%d"), |
| 184 | _interval(MemcpySamplingRateBytes), |
| 185 | _memcpyOps(0), |
| 186 | _memcpyTriggered(0) { |
| 187 | static HL::PosixLock init_lock; |
| 188 | init_lock.lock(); |
| 189 | auto old_sig = signal(MemcpySignal, SIG_IGN); |
| 190 | if (old_sig != SIG_DFL) signal(MemcpySignal, old_sig); |
| 191 | init_lock.unlock(); |
| 192 | auto pid = getpid(); |
| 193 | snprintf_((char *)scalene_memcpy_signal_filename, |
| 194 | sizeof(scalene_memcpy_signal_filename), fname, pid); |
| 195 | // printf("initialized (%s)\n", scalene_memcpy_signal_filename); |
| 196 | } |
| 197 | |
| 198 | int local_strlen(const char *str) { |
| 199 | int len = 0; |
| 200 | while (*str != '\0') { |
| 201 | len++; |
| 202 | str++; |
| 203 | } |
| 204 | return len; |
| 205 | } |
| 206 | |
| 207 | ~MemcpySampler() { unlink(scalene_memcpy_signal_filename); } |
| 208 | |
| 209 | ATTRIBUTE_ALWAYS_INLINE inline void *memcpy(void *dst, const void *src, |
| 210 | size_t n) { |
| 211 | MallocRecursionGuard g; |
| 212 | auto result = local_memcpy(dst, src, n); |
| 213 | if (pythonDetected() && !g.wasInMalloc()) incrementMemoryOps(n); |
| 214 | return result; // always dst |
| 215 | } |
| 216 | |
| 217 | ATTRIBUTE_ALWAYS_INLINE inline void *memmove(void *dst, const void *src, |
| 218 | size_t n) { |
| 219 | MallocRecursionGuard g; |
| 220 | auto result = local_memmove(dst, src, n); |
| 221 | if (pythonDetected() && !g.wasInMalloc()) incrementMemoryOps(n); |
| 222 | return result; // always dst |
| 223 | } |
| 224 | |
| 225 | ATTRIBUTE_ALWAYS_INLINE inline char *strcpy(char *dst, const char *src) { |
| 226 | MallocRecursionGuard g; |
| 227 | auto n = ::strlen(src); |
| 228 | auto result = local_strcpy(dst, src); |
| 229 | if (pythonDetected() && !g.wasInMalloc()) incrementMemoryOps(n); |
| 230 | return result; // always dst |
| 231 | } |
| 232 | |
| 233 | private: |
| 234 | //// local implementations of memcpy and friends. |
| 235 | Sampler<MemcpySamplingRateBytes> _memcpySampler; |
| 236 | SampleFile _samplefile; |
| 237 | ATTRIBUTE_ALWAYS_INLINE inline void *local_memcpy(void *dst, const void *src, |
| 238 | size_t n) { |
| 239 | #if defined(__APPLE__) |
| 240 | return ::memcpy(dst, src, n); |
| 241 | #else |
| 242 | return memcpy_musl(dst, src, n); |
| 243 | #endif |
| 244 | } |
| 245 | |
| 246 | void *local_memmove(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_memcpy(buf, src, n); |
| 253 | local_memcpy(dst, buf, n); |
| 254 | free(buf); |
| 255 | return dst; |
| 256 | #endif |
| 257 | } |
| 258 | |
| 259 | char *local_strcpy(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 incrementMemoryOps(int n) { |
| 269 | if (pythonDetected()) { |
| 270 | _memcpyOps += n; |
| 271 | auto sampleMemop = _memcpySampler.sample(n); |
| 272 | if (unlikely(sampleMemop)) { |
| 273 | writeCount(); |
| 274 | _memcpyTriggered++; |
| 275 | _memcpyOps = 0; |
| 276 | #if !SCALENE_DISABLE_SIGNALS |
| 277 | raise(MemcpySignal); |
| 278 | #endif |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | uint64_t _interval; |
| 284 | uint64_t _memcpyOps; |
| 285 | unsigned long long _memcpyTriggered; |
| 286 | static constexpr int BUFFER_LENGTH = 1024; |
| 287 | char scalene_memcpy_signal_filename[BUFFER_LENGTH]; |
| 288 | |
| 289 | void writeCount() { |
| 290 | #if 1 |
| 291 | std::string filename; |
| 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_LENGTH]; |
| 300 | snprintf_(buf, sizeof(buf), "%d,%d,%d,%s,%d,%d\n\n", _memcpyTriggered, |
| 301 | _memcpyOps, getpid(), filename.c_str(), lineno, bytei); |
| 302 | _samplefile.writeToFile(buf); |
| 303 | } |
| 304 | }; |
| 305 | |
| 306 | #endif |