|
| 1 | +// |
| 2 | +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022 |
| 3 | +// |
| 4 | +// Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | +// |
| 7 | +#include "memprof_stat.h" |
| 8 | + |
| 9 | +#include "td/utils/port/platform.h" |
| 10 | + |
| 11 | +#if (TD_DARWIN || TD_LINUX) |
| 12 | +#include <algorithm> |
| 13 | +#include <atomic> |
| 14 | +#include <cstddef> |
| 15 | +#include <cstdint> |
| 16 | +#include <cstdlib> |
| 17 | +#include <cstring> |
| 18 | +#include <functional> |
| 19 | +#include <new> |
| 20 | +#include <utility> |
| 21 | +#include <vector> |
| 22 | + |
| 23 | +#include <dlfcn.h> |
| 24 | +#include <execinfo.h> |
| 25 | + |
| 26 | +bool is_memprof_on() { |
| 27 | + return true; |
| 28 | +} |
| 29 | + |
| 30 | +#define my_assert(f) \ |
| 31 | + if (!(f)) { \ |
| 32 | + std::abort(); \ |
| 33 | + } |
| 34 | + |
| 35 | +struct malloc_info { |
| 36 | + std::int32_t magic; |
| 37 | + std::int32_t size; |
| 38 | +}; |
| 39 | + |
| 40 | +std::atomic<uint64_t> total_memory_used; |
| 41 | +void register_xalloc(malloc_info *info, std::int32_t diff) { |
| 42 | + my_assert(info->size >= 0); |
| 43 | + // TODO: this is very slow in case of several threads. |
| 44 | + // Currently this statistics is intended only for memory benchmarks. |
| 45 | + total_memory_used.fetch_add(diff * info->size, std::memory_order_relaxed); |
| 46 | +} |
| 47 | + |
| 48 | +std::size_t get_used_memory_size() { |
| 49 | + return total_memory_used.load(); |
| 50 | +} |
| 51 | + |
| 52 | +extern "C" { |
| 53 | + |
| 54 | +static constexpr std::size_t RESERVED_SIZE = 16; |
| 55 | +static constexpr std::int32_t MALLOC_INFO_MAGIC = 0x27138373; |
| 56 | + |
| 57 | +static void *do_malloc(std::size_t size) { |
| 58 | + static_assert(RESERVED_SIZE % alignof(std::max_align_t) == 0, "fail"); |
| 59 | + static_assert(RESERVED_SIZE >= sizeof(malloc_info), "fail"); |
| 60 | +#if TD_DARWIN |
| 61 | + static void *malloc_void = dlsym(RTLD_NEXT, "malloc"); |
| 62 | + static auto malloc_old = *reinterpret_cast<decltype(malloc) **>(&malloc_void); |
| 63 | +#else |
| 64 | + extern decltype(malloc) __libc_malloc; |
| 65 | + static auto malloc_old = __libc_malloc; |
| 66 | +#endif |
| 67 | + auto *info = static_cast<malloc_info *>(malloc_old(size + RESERVED_SIZE)); |
| 68 | + auto *buf = reinterpret_cast<char *>(info); |
| 69 | + |
| 70 | + info->magic = MALLOC_INFO_MAGIC; |
| 71 | + info->size = static_cast<std::int32_t>(size); |
| 72 | + |
| 73 | + register_xalloc(info, +1); |
| 74 | + |
| 75 | + void *data = buf + RESERVED_SIZE; |
| 76 | + |
| 77 | + return data; |
| 78 | +} |
| 79 | + |
| 80 | +static malloc_info *get_info(void *data_void) { |
| 81 | + auto *data = static_cast<char *>(data_void); |
| 82 | + auto *buf = data - RESERVED_SIZE; |
| 83 | + |
| 84 | + auto *info = reinterpret_cast<malloc_info *>(buf); |
| 85 | + my_assert(info->magic == MALLOC_INFO_MAGIC); |
| 86 | + return info; |
| 87 | +} |
| 88 | + |
| 89 | +void *malloc(std::size_t size) { |
| 90 | + return do_malloc(size); |
| 91 | +} |
| 92 | + |
| 93 | +void free(void *data_void) { |
| 94 | + if (data_void == nullptr) { |
| 95 | + return; |
| 96 | + } |
| 97 | + auto *info = get_info(data_void); |
| 98 | + register_xalloc(info, -1); |
| 99 | + |
| 100 | +#if TD_DARWIN |
| 101 | + static void *free_void = dlsym(RTLD_NEXT, "free"); |
| 102 | + static auto free_old = *reinterpret_cast<decltype(free) **>(&free_void); |
| 103 | +#else |
| 104 | + extern decltype(free) __libc_free; |
| 105 | + static auto free_old = __libc_free; |
| 106 | +#endif |
| 107 | + return free_old(info); |
| 108 | +} |
| 109 | +void *calloc(std::size_t size_a, std::size_t size_b) { |
| 110 | + auto size = size_a * size_b; |
| 111 | + void *res = do_malloc(size); |
| 112 | + std::memset(res, 0, size); |
| 113 | + return res; |
| 114 | +} |
| 115 | +void *realloc(void *ptr, std::size_t size) { |
| 116 | + if (ptr == nullptr) { |
| 117 | + return do_malloc(size); |
| 118 | + } |
| 119 | + auto *info = get_info(ptr); |
| 120 | + auto *new_ptr = do_malloc(size); |
| 121 | + auto to_copy = std::min(static_cast<std::int32_t>(size), info->size); |
| 122 | + std::memcpy(new_ptr, ptr, to_copy); |
| 123 | + free(ptr); |
| 124 | + return new_ptr; |
| 125 | +} |
| 126 | +void *memalign(std::size_t alignment, std::size_t size) { |
| 127 | + auto res = malloc(size); |
| 128 | + my_assert(reinterpret_cast<uint64_t>(res) % alignment == 0); |
| 129 | + return res; |
| 130 | +} |
| 131 | + |
| 132 | +int posix_memalign(void **memptr, size_t alignment, size_t size) { |
| 133 | + auto res = malloc(size); |
| 134 | + my_assert(reinterpret_cast<uint64_t>(res) % alignment == 0); |
| 135 | + *memptr = res; |
| 136 | + return 0; |
| 137 | +} |
| 138 | +} |
| 139 | + |
| 140 | +// c++14 guarantees that it is enough to override these two operators. |
| 141 | +void *operator new(std::size_t count) { |
| 142 | + return do_malloc(count); |
| 143 | +} |
| 144 | +void operator delete(void *ptr) noexcept(true) { |
| 145 | + free(ptr); |
| 146 | +} |
| 147 | +// because of gcc warning: the program should also define 'void operator delete(void*, std::size_t)' |
| 148 | +void operator delete(void *ptr, std::size_t) noexcept(true) { |
| 149 | + free(ptr); |
| 150 | +} |
| 151 | + |
| 152 | +// c++17 |
| 153 | +// void *operator new(std::size_t count, std::align_val_t al); |
| 154 | +// void operator delete(void *ptr, std::align_val_t al); |
| 155 | + |
| 156 | +#else |
| 157 | +bool is_memprof_on() { |
| 158 | + return false; |
| 159 | +} |
| 160 | +std::size_t get_used_memory_size() { |
| 161 | + return false; |
| 162 | +} |
| 163 | +#endif |
0 commit comments