| Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2014 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
| Christopher Ferris | a22f5d5 | 2019-03-01 16:40:59 -0800 | [diff] [blame] | 17 | #include <errno.h> | 
| Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 18 | #include <malloc.h> | 
| Christopher Ferris | 03eebcb | 2014-06-13 13:57:51 -0700 | [diff] [blame] | 19 | #include <sys/param.h> | 
| Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 20 | #include <unistd.h> | 
|  | 21 |  | 
| Christopher Ferris | e9a7b81 | 2023-05-11 15:36:27 -0700 | [diff] [blame] | 22 | #include <async_safe/log.h> | 
| Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 23 | #include <private/MallocXmlElem.h> | 
|  | 24 |  | 
| Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 25 | #include "jemalloc.h" | 
|  | 26 |  | 
| Christopher Ferris | e9a7b81 | 2023-05-11 15:36:27 -0700 | [diff] [blame] | 27 | __BEGIN_DECLS | 
|  | 28 |  | 
|  | 29 | size_t je_mallinfo_narenas(); | 
|  | 30 | size_t je_mallinfo_nbins(); | 
|  | 31 | struct mallinfo je_mallinfo_arena_info(size_t); | 
|  | 32 | struct mallinfo je_mallinfo_bin_info(size_t, size_t); | 
|  | 33 |  | 
|  | 34 | __END_DECLS | 
|  | 35 |  | 
| Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 36 | void* je_pvalloc(size_t bytes) { | 
| Elliott Hughes | 91570ce | 2014-07-10 12:34:23 -0700 | [diff] [blame] | 37 | size_t pagesize = getpagesize(); | 
| Dan Albert | a613d0d | 2017-10-05 16:39:33 -0700 | [diff] [blame] | 38 | size_t size = __BIONIC_ALIGN(bytes, pagesize); | 
| Christopher Ferris | 03eebcb | 2014-06-13 13:57:51 -0700 | [diff] [blame] | 39 | if (size < bytes) { | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 40 | return nullptr; | 
| Christopher Ferris | 03eebcb | 2014-06-13 13:57:51 -0700 | [diff] [blame] | 41 | } | 
|  | 42 | return je_memalign(pagesize, size); | 
| Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 43 | } | 
|  | 44 |  | 
|  | 45 | #ifdef je_memalign | 
|  | 46 | #undef je_memalign | 
|  | 47 | #endif | 
|  | 48 |  | 
|  | 49 | // The man page for memalign says it fails if boundary is not a power of 2, | 
|  | 50 | // but this is not true. Both glibc and dlmalloc round up to the next power | 
|  | 51 | // of 2, so we'll do the same. | 
|  | 52 | void* je_memalign_round_up_boundary(size_t boundary, size_t size) { | 
| Christopher Ferris | 03eebcb | 2014-06-13 13:57:51 -0700 | [diff] [blame] | 53 | if (boundary != 0) { | 
|  | 54 | if (!powerof2(boundary)) { | 
|  | 55 | boundary = BIONIC_ROUND_UP_POWER_OF_2(boundary); | 
| Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 56 | } | 
|  | 57 | } else { | 
|  | 58 | boundary = 1; | 
|  | 59 | } | 
|  | 60 | return je_memalign(boundary, size); | 
|  | 61 | } | 
| Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 62 |  | 
| Christopher Ferris | a22f5d5 | 2019-03-01 16:40:59 -0800 | [diff] [blame] | 63 | #ifdef je_aligned_alloc | 
|  | 64 | #undef je_aligned_alloc | 
|  | 65 | #endif | 
|  | 66 |  | 
|  | 67 | // The aligned_alloc function requires that size is a multiple of alignment. | 
|  | 68 | // jemalloc doesn't enforce this, so add enforcement here. | 
|  | 69 | void* je_aligned_alloc_wrapper(size_t alignment, size_t size) { | 
|  | 70 | if ((size % alignment) != 0) { | 
|  | 71 | errno = EINVAL; | 
|  | 72 | return nullptr; | 
|  | 73 | } | 
|  | 74 | return je_aligned_alloc(alignment, size); | 
|  | 75 | } | 
|  | 76 |  | 
| Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 77 | int je_mallopt(int param, int value) { | 
|  | 78 | // The only parameter we currently understand is M_DECAY_TIME. | 
|  | 79 | if (param == M_DECAY_TIME) { | 
| Chia-hung Duan | 6abb406 | 2024-04-17 19:08:48 -0700 | [diff] [blame] | 80 | // Only support setting the value to -1 or 0 or 1. | 
| Christopher Ferris | d73a49e | 2018-10-19 16:03:44 -0700 | [diff] [blame] | 81 | ssize_t decay_time_ms; | 
| Chia-hung Duan | 6abb406 | 2024-04-17 19:08:48 -0700 | [diff] [blame] | 82 | if (value < 0) { | 
|  | 83 | // Given that SSIZE_MAX may not be supported in jemalloc, set this to a | 
|  | 84 | // sufficiently large number that essentially disables the decay timer. | 
|  | 85 | decay_time_ms = 10000000; | 
|  | 86 | } else if (value) { | 
| Christopher Ferris | d73a49e | 2018-10-19 16:03:44 -0700 | [diff] [blame] | 87 | decay_time_ms = 1000; | 
| Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 88 | } else { | 
| Christopher Ferris | d73a49e | 2018-10-19 16:03:44 -0700 | [diff] [blame] | 89 | decay_time_ms = 0; | 
| Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 90 | } | 
|  | 91 | // First get the total number of arenas. | 
|  | 92 | unsigned narenas; | 
|  | 93 | size_t sz = sizeof(unsigned); | 
|  | 94 | if (je_mallctl("arenas.narenas", &narenas, &sz, nullptr, 0) != 0) { | 
|  | 95 | return 0; | 
|  | 96 | } | 
|  | 97 |  | 
|  | 98 | // Set the decay time for any arenas that will be created in the future. | 
| Christopher Ferris | d73a49e | 2018-10-19 16:03:44 -0700 | [diff] [blame] | 99 | if (je_mallctl("arenas.dirty_decay_ms", nullptr, nullptr, &decay_time_ms, sizeof(decay_time_ms)) != 0) { | 
|  | 100 | return 0; | 
|  | 101 | } | 
|  | 102 | if (je_mallctl("arenas.muzzy_decay_ms", nullptr, nullptr, &decay_time_ms, sizeof(decay_time_ms)) != 0) { | 
| Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 103 | return 0; | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | // Change the decay on the already existing arenas. | 
|  | 107 | char buffer[100]; | 
|  | 108 | for (unsigned i = 0; i < narenas; i++) { | 
| Christopher Ferris | d73a49e | 2018-10-19 16:03:44 -0700 | [diff] [blame] | 109 | snprintf(buffer, sizeof(buffer), "arena.%d.dirty_decay_ms", i); | 
|  | 110 | if (je_mallctl(buffer, nullptr, nullptr, &decay_time_ms, sizeof(decay_time_ms)) != 0) { | 
|  | 111 | break; | 
|  | 112 | } | 
|  | 113 | snprintf(buffer, sizeof(buffer), "arena.%d.muzzy_decay_ms", i); | 
|  | 114 | if (je_mallctl(buffer, nullptr, nullptr, &decay_time_ms, sizeof(decay_time_ms)) != 0) { | 
| Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 115 | break; | 
|  | 116 | } | 
|  | 117 | } | 
|  | 118 | return 1; | 
| Christopher Ferris | d86eb86 | 2023-02-28 12:45:54 -0800 | [diff] [blame] | 119 | } else if (param == M_PURGE || param == M_PURGE_ALL) { | 
| Christopher Ferris | 0f710fd | 2019-05-01 13:26:46 -0700 | [diff] [blame] | 120 | // Only clear the current thread cache since there is no easy way to | 
|  | 121 | // clear the caches of other threads. | 
|  | 122 | // This must be done first so that cleared allocations get purged | 
|  | 123 | // in the next calls. | 
| Christopher Ferris | 3d0bafb | 2019-07-08 14:54:58 -0700 | [diff] [blame] | 124 | // Ignore the return call since this will fail if the tcache is disabled. | 
|  | 125 | je_mallctl("thread.tcache.flush", nullptr, nullptr, nullptr, 0); | 
| Christopher Ferris | 0f710fd | 2019-05-01 13:26:46 -0700 | [diff] [blame] | 126 |  | 
| Tim Murray | ac578f2 | 2018-10-15 16:26:56 -0700 | [diff] [blame] | 127 | unsigned narenas; | 
|  | 128 | size_t sz = sizeof(unsigned); | 
|  | 129 | if (je_mallctl("arenas.narenas", &narenas, &sz, nullptr, 0) != 0) { | 
|  | 130 | return 0; | 
|  | 131 | } | 
|  | 132 | char buffer[100]; | 
|  | 133 | snprintf(buffer, sizeof(buffer), "arena.%u.purge", narenas); | 
|  | 134 | if (je_mallctl(buffer, nullptr, nullptr, nullptr, 0) != 0) { | 
|  | 135 | return 0; | 
|  | 136 | } | 
|  | 137 | return 1; | 
| Christopher Ferris | e9a7b81 | 2023-05-11 15:36:27 -0700 | [diff] [blame] | 138 | } else if (param == M_LOG_STATS) { | 
|  | 139 | for (size_t i = 0; i < je_mallinfo_narenas(); i++) { | 
|  | 140 | struct mallinfo mi = je_mallinfo_arena_info(i); | 
|  | 141 | if (mi.hblkhd != 0) { | 
|  | 142 | async_safe_format_log(ANDROID_LOG_INFO, "jemalloc", | 
|  | 143 | "Arena %zu: large bytes %zu huge bytes %zu bin bytes %zu", i, | 
|  | 144 | mi.ordblks, mi.uordblks, mi.fsmblks); | 
|  | 145 |  | 
|  | 146 | for (size_t j = 0; j < je_mallinfo_nbins(); j++) { | 
|  | 147 | struct mallinfo mi = je_mallinfo_bin_info(i, j); | 
|  | 148 | if (mi.ordblks != 0) { | 
|  | 149 | size_t total_allocs = 1; | 
|  | 150 | if (mi.uordblks > mi.fordblks) { | 
|  | 151 | total_allocs = mi.uordblks - mi.fordblks; | 
|  | 152 | } | 
|  | 153 | size_t bin_size = mi.ordblks / total_allocs; | 
|  | 154 | async_safe_format_log( | 
|  | 155 | ANDROID_LOG_INFO, "jemalloc", | 
|  | 156 | "  Bin %zu (%zu bytes): allocated bytes %zu nmalloc %zu ndalloc %zu", j, bin_size, | 
|  | 157 | mi.ordblks, mi.uordblks, mi.fordblks); | 
|  | 158 | } | 
|  | 159 | } | 
|  | 160 | } | 
|  | 161 | } | 
|  | 162 | return 1; | 
| Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 163 | } | 
| Christopher Ferris | e9a7b81 | 2023-05-11 15:36:27 -0700 | [diff] [blame] | 164 |  | 
| Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 165 | return 0; | 
|  | 166 | } | 
| Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 167 |  | 
| Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 168 | int je_malloc_info(int options, FILE* fp) { | 
|  | 169 | if (options != 0) { | 
|  | 170 | errno = EINVAL; | 
|  | 171 | return -1; | 
|  | 172 | } | 
|  | 173 |  | 
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 174 | fflush(fp); | 
|  | 175 | int fd = fileno(fp); | 
|  | 176 | MallocXmlElem root(fd, "malloc", "version=\"jemalloc-1\""); | 
| Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 177 |  | 
|  | 178 | // Dump all of the large allocations in the arenas. | 
| Christopher Ferris | db9706a | 2019-05-02 18:33:11 -0700 | [diff] [blame] | 179 | for (size_t i = 0; i < je_mallinfo_narenas(); i++) { | 
|  | 180 | struct mallinfo mi = je_mallinfo_arena_info(i); | 
| Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 181 | if (mi.hblkhd != 0) { | 
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 182 | MallocXmlElem arena_elem(fd, "heap", "nr=\"%d\"", i); | 
| Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 183 | { | 
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 184 | MallocXmlElem(fd, "allocated-large").Contents("%zu", mi.ordblks); | 
|  | 185 | MallocXmlElem(fd, "allocated-huge").Contents("%zu", mi.uordblks); | 
|  | 186 | MallocXmlElem(fd, "allocated-bins").Contents("%zu", mi.fsmblks); | 
| Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 187 |  | 
|  | 188 | size_t total = 0; | 
| Christopher Ferris | db9706a | 2019-05-02 18:33:11 -0700 | [diff] [blame] | 189 | for (size_t j = 0; j < je_mallinfo_nbins(); j++) { | 
|  | 190 | struct mallinfo mi = je_mallinfo_bin_info(i, j); | 
| Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 191 | if (mi.ordblks != 0) { | 
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 192 | MallocXmlElem bin_elem(fd, "bin", "nr=\"%d\"", j); | 
|  | 193 | MallocXmlElem(fd, "allocated").Contents("%zu", mi.ordblks); | 
|  | 194 | MallocXmlElem(fd, "nmalloc").Contents("%zu", mi.uordblks); | 
|  | 195 | MallocXmlElem(fd, "ndalloc").Contents("%zu", mi.fordblks); | 
| Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 196 | total += mi.ordblks; | 
|  | 197 | } | 
|  | 198 | } | 
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 199 | MallocXmlElem(fd, "bins-total").Contents("%zu", total); | 
| Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 200 | } | 
|  | 201 | } | 
|  | 202 | } | 
|  | 203 |  | 
|  | 204 | return 0; | 
|  | 205 | } |