| 1 | /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
| 2 | #ident "$Id$" |
| 3 | /*====== |
| 4 | This file is part of PerconaFT. |
| 5 | |
| 6 | |
| 7 | Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved. |
| 8 | |
| 9 | PerconaFT is free software: you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License, version 2, |
| 11 | as published by the Free Software Foundation. |
| 12 | |
| 13 | PerconaFT is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with PerconaFT. If not, see <http://www.gnu.org/licenses/>. |
| 20 | |
| 21 | ---------------------------------------- |
| 22 | |
| 23 | PerconaFT is free software: you can redistribute it and/or modify |
| 24 | it under the terms of the GNU Affero General Public License, version 3, |
| 25 | as published by the Free Software Foundation. |
| 26 | |
| 27 | PerconaFT is distributed in the hope that it will be useful, |
| 28 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 29 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 30 | GNU Affero General Public License for more details. |
| 31 | |
| 32 | You should have received a copy of the GNU Affero General Public License |
| 33 | along with PerconaFT. If not, see <http://www.gnu.org/licenses/>. |
| 34 | ======= */ |
| 35 | |
| 36 | #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved." |
| 37 | |
| 38 | #include <cstdint> |
| 39 | |
| 40 | #include "malloc_utils.hpp" |
| 41 | |
| 42 | #if !defined(HAVE_BITS_FUNCTEXCEPT_H) |
| 43 | |
| 44 | namespace std { |
| 45 | |
| 46 | void __throw_bad_alloc() { |
| 47 | throw bad_alloc(); |
| 48 | } |
| 49 | |
| 50 | } // namespace std |
| 51 | |
| 52 | #endif |
| 53 | |
| 54 | namespace malloc_utils { |
| 55 | |
| 56 | // How do we determine that we're using jemalloc? |
| 57 | // In the hackiest way possible. We allocate memory using malloc() and see if |
| 58 | // the per-thread counter of allocated memory increases. This makes me feel |
| 59 | // dirty inside. Also note that this requires jemalloc to have been compiled |
| 60 | // with --enable-stats. |
| 61 | bool usingJEMallocSlow() { |
| 62 | // Some platforms (*cough* OSX *cough*) require weak symbol checks to be |
| 63 | // in the form if (mallctl != nullptr). Not if (mallctl) or if (!mallctl) |
| 64 | // (!!). http://goo.gl/xpmctm |
| 65 | if (mallocx == nullptr || rallocx == nullptr || xallocx == nullptr |
| 66 | || sallocx == nullptr || dallocx == nullptr || nallocx == nullptr |
| 67 | || mallctl == nullptr) { |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | // "volatile" because gcc optimizes out the reads from *counter, because |
| 72 | // it "knows" malloc doesn't modify global state... |
| 73 | volatile uint64_t* counter; |
| 74 | size_t counterLen = sizeof(uint64_t*); |
| 75 | |
| 76 | if (mallctl("thread.allocatedp" , static_cast<void*>(&counter), &counterLen, |
| 77 | nullptr, 0) != 0) { |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | if (counterLen != sizeof(uint64_t*)) { |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | uint64_t origAllocated = *counter; |
| 86 | |
| 87 | void* ptr = malloc(1); |
| 88 | if (!ptr) { |
| 89 | // wtf, failing to allocate 1 byte |
| 90 | return false; |
| 91 | } |
| 92 | free(ptr); |
| 93 | |
| 94 | return (origAllocated != *counter); |
| 95 | } |
| 96 | |
| 97 | } // namespace malloc_utils |
| 98 | |