Use iterator adapters in decl_class_parents
[hiphop-php.git] / hphp / util / jemalloc-util.cpp
blobdbe0c20d635ad6c76c89e8e7c57509ef8dd7a00f
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #include "hphp/util/jemalloc-util.h"
19 #include "hphp/util/optional.h"
21 namespace HPHP {
23 namespace {
24 #ifdef USE_JEMALLOC
25 Optional<unsigned> allArenas() {
26 assert(mallctlnametomib && mallctlbymib);
27 unsigned allArenas = 0;
28 #ifndef MALLCTL_ARENAS_ALL
29 if (mallctlRead<unsigned, true>("arenas.narenas", &allArenas)) {
30 return std::nullopt;
32 #else
33 allArenas = MALLCTL_ARENAS_ALL;
34 #endif
35 return allArenas;
37 #endif // USE_JEMALLOC
40 int jemalloc_pprof_enable() {
41 return mallctlWrite<bool, true>("prof.active", true);
44 int jemalloc_pprof_disable() {
45 return mallctlWrite<bool, true>("prof.active", false);
48 int jemalloc_pprof_dump(const std::string& prefix, bool force) {
49 if (!force) {
50 bool enabled = false;
51 bool active = false;
52 // Check if profiling is active before trying to dump.
53 int err = mallctlRead<bool, true>("opt.prof", &enabled) ||
54 (enabled && mallctlRead<bool, true>("prof.active", &active));
55 if (err || !active) {
56 return 0; // nothing to do
60 if (prefix != "") {
61 const char *s = prefix.c_str();
62 return mallctlWrite<const char*, true>("prof.dump", s);
63 } else {
64 return mallctlCall<true>("prof.dump");
68 #ifdef USE_JEMALLOC
70 // Frequently used mallctl mibs.
72 namespace {
73 // If a value is passed in, refresh the data from which the mallctl*() functions
74 // report values, and increment the epoch. Return the current epoch. This is
75 // useful for detecting whether another thread caused a refresh.
76 size_t g_epoch_mib[1]; // "epoch"
77 size_t g_purge_mib[3]; // "arena.<i>.purge"
78 size_t g_pactive_mib[4]; // "stats.arenas.<i>.pactive"
79 size_t g_pdirty_mib[4]; // "stats.arenas.<i>.pdirty"
82 void init_mallctl_mibs() {
83 size_t miblen = 1;
84 mallctlnametomib("epoch", g_epoch_mib, &miblen);
85 miblen = 3;
86 mallctlnametomib("arena.0.purge", g_purge_mib, &miblen);
87 miblen = 4;
88 mallctlnametomib("stats.arenas.0.pactive", g_pactive_mib, &miblen);
89 mallctlnametomib("stats.arenas.0.pdirty", g_pdirty_mib, &miblen);
92 void mallctl_epoch() {
93 uint64_t epoch = 1;
94 mallctlbymib(g_epoch_mib, 1, nullptr, nullptr, &epoch, sizeof(epoch));
97 bool purge_all(std::string* errStr) {
98 auto const all = allArenas();
99 if (!all) {
100 if (errStr) {
101 *errStr = "arenas.narenas";
103 return false;
106 size_t mib[3] = {g_purge_mib[0], *all, g_purge_mib[2]};
107 if (mallctlbymib(mib, 3, nullptr, nullptr, nullptr, 0)) {
108 if (errStr) {
109 *errStr = "mallctlbymib(arena.all.purge)";
111 return false;
113 return true;
116 size_t mallctl_pactive(unsigned arenaId) {
117 size_t mib[4] =
118 {g_pactive_mib[0], g_pactive_mib[1], arenaId, g_pactive_mib[3]};
119 size_t pactive = 0;
120 size_t sz = sizeof(pactive);
121 if (mallctlbymib(mib, 4, &pactive, &sz, nullptr, 0)) return 0;
122 return pactive;
125 size_t mallctl_pdirty(unsigned arenaId) {
126 size_t mib[4] = {g_pdirty_mib[0], g_pdirty_mib[1], arenaId, g_pdirty_mib[3]};
127 size_t pdirty = 0;
128 size_t sz = sizeof(pdirty);
129 if (mallctlbymib(mib, 4, &pdirty, &sz, nullptr, 0)) return 0;
130 return pdirty;
133 size_t mallctl_all_pdirty() {
134 if (auto const all = allArenas()) return mallctl_pdirty(*all);
135 return 0;
137 #else
138 bool purge_all(std::string* errStr) {
139 return false;
141 #endif // USE_JEMALLOC