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 | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 17 | #include <malloc.h> |
Christopher Ferris | 03eebcb | 2014-06-13 13:57:51 -0700 | [diff] [blame] | 18 | #include <sys/param.h> |
Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 19 | #include <unistd.h> |
| 20 | |
| 21 | #include "jemalloc.h" |
Christopher Ferris | 03eebcb | 2014-06-13 13:57:51 -0700 | [diff] [blame] | 22 | #include "private/bionic_macros.h" |
Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 23 | |
| 24 | void* je_pvalloc(size_t bytes) { |
Elliott Hughes | 91570ce | 2014-07-10 12:34:23 -0700 | [diff] [blame] | 25 | size_t pagesize = getpagesize(); |
Dan Albert | a613d0d | 2017-10-05 16:39:33 -0700 | [diff] [blame] | 26 | size_t size = __BIONIC_ALIGN(bytes, pagesize); |
Christopher Ferris | 03eebcb | 2014-06-13 13:57:51 -0700 | [diff] [blame] | 27 | if (size < bytes) { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 28 | return nullptr; |
Christopher Ferris | 03eebcb | 2014-06-13 13:57:51 -0700 | [diff] [blame] | 29 | } |
| 30 | return je_memalign(pagesize, size); |
Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | #ifdef je_memalign |
| 34 | #undef je_memalign |
| 35 | #endif |
| 36 | |
| 37 | // The man page for memalign says it fails if boundary is not a power of 2, |
| 38 | // but this is not true. Both glibc and dlmalloc round up to the next power |
| 39 | // of 2, so we'll do the same. |
| 40 | void* je_memalign_round_up_boundary(size_t boundary, size_t size) { |
Christopher Ferris | 03eebcb | 2014-06-13 13:57:51 -0700 | [diff] [blame] | 41 | if (boundary != 0) { |
| 42 | if (!powerof2(boundary)) { |
| 43 | boundary = BIONIC_ROUND_UP_POWER_OF_2(boundary); |
Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 44 | } |
| 45 | } else { |
| 46 | boundary = 1; |
| 47 | } |
| 48 | return je_memalign(boundary, size); |
| 49 | } |
Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 50 | |
| 51 | int je_mallopt(int param, int value) { |
| 52 | // The only parameter we currently understand is M_DECAY_TIME. |
| 53 | if (param == M_DECAY_TIME) { |
| 54 | // Only support setting the value to 1 or 0. |
Christopher Ferris | d73a49e | 2018-10-19 16:03:44 -0700 | [diff] [blame^] | 55 | ssize_t decay_time_ms; |
Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 56 | if (value) { |
Christopher Ferris | d73a49e | 2018-10-19 16:03:44 -0700 | [diff] [blame^] | 57 | decay_time_ms = 1000; |
Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 58 | } else { |
Christopher Ferris | d73a49e | 2018-10-19 16:03:44 -0700 | [diff] [blame^] | 59 | decay_time_ms = 0; |
Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 60 | } |
| 61 | // First get the total number of arenas. |
| 62 | unsigned narenas; |
| 63 | size_t sz = sizeof(unsigned); |
| 64 | if (je_mallctl("arenas.narenas", &narenas, &sz, nullptr, 0) != 0) { |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | // 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^] | 69 | if (je_mallctl("arenas.dirty_decay_ms", nullptr, nullptr, &decay_time_ms, sizeof(decay_time_ms)) != 0) { |
| 70 | return 0; |
| 71 | } |
| 72 | 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] | 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | // Change the decay on the already existing arenas. |
| 77 | char buffer[100]; |
| 78 | for (unsigned i = 0; i < narenas; i++) { |
Christopher Ferris | d73a49e | 2018-10-19 16:03:44 -0700 | [diff] [blame^] | 79 | snprintf(buffer, sizeof(buffer), "arena.%d.dirty_decay_ms", i); |
| 80 | if (je_mallctl(buffer, nullptr, nullptr, &decay_time_ms, sizeof(decay_time_ms)) != 0) { |
| 81 | break; |
| 82 | } |
| 83 | snprintf(buffer, sizeof(buffer), "arena.%d.muzzy_decay_ms", i); |
| 84 | 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] | 85 | break; |
| 86 | } |
| 87 | } |
| 88 | return 1; |
Tim Murray | ac578f2 | 2018-10-15 16:26:56 -0700 | [diff] [blame] | 89 | } else if (param == M_PURGE) { |
| 90 | unsigned narenas; |
| 91 | size_t sz = sizeof(unsigned); |
| 92 | if (je_mallctl("arenas.narenas", &narenas, &sz, nullptr, 0) != 0) { |
| 93 | return 0; |
| 94 | } |
| 95 | char buffer[100]; |
| 96 | snprintf(buffer, sizeof(buffer), "arena.%u.purge", narenas); |
| 97 | if (je_mallctl(buffer, nullptr, nullptr, nullptr, 0) != 0) { |
| 98 | return 0; |
| 99 | } |
| 100 | return 1; |
Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 101 | } |
| 102 | return 0; |
| 103 | } |