2 +----------------------------------------------------------------------+
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"
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
)) {
33 allArenas
= MALLCTL_ARENAS_ALL
;
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
) {
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
));
56 return 0; // nothing to do
61 const char *s
= prefix
.c_str();
62 return mallctlWrite
<const char*, true>("prof.dump", s
);
64 return mallctlCall
<true>("prof.dump");
70 // Frequently used mallctl mibs.
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() {
84 mallctlnametomib("epoch", g_epoch_mib
, &miblen
);
86 mallctlnametomib("arena.0.purge", g_purge_mib
, &miblen
);
88 mallctlnametomib("stats.arenas.0.pactive", g_pactive_mib
, &miblen
);
89 mallctlnametomib("stats.arenas.0.pdirty", g_pdirty_mib
, &miblen
);
92 void mallctl_epoch() {
94 mallctlbymib(g_epoch_mib
, 1, nullptr, nullptr, &epoch
, sizeof(epoch
));
97 bool purge_all(std::string
* errStr
) {
98 auto const all
= allArenas();
101 *errStr
= "arenas.narenas";
106 size_t mib
[3] = {g_purge_mib
[0], *all
, g_purge_mib
[2]};
107 if (mallctlbymib(mib
, 3, nullptr, nullptr, nullptr, 0)) {
109 *errStr
= "mallctlbymib(arena.all.purge)";
116 size_t mallctl_pactive(unsigned arenaId
) {
118 {g_pactive_mib
[0], g_pactive_mib
[1], arenaId
, g_pactive_mib
[3]};
120 size_t sz
= sizeof(pactive
);
121 if (mallctlbymib(mib
, 4, &pactive
, &sz
, nullptr, 0)) return 0;
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]};
128 size_t sz
= sizeof(pdirty
);
129 if (mallctlbymib(mib
, 4, &pdirty
, &sz
, nullptr, 0)) return 0;
133 size_t mallctl_all_pdirty() {
134 if (auto const all
= allArenas()) return mallctl_pdirty(*all
);
138 bool purge_all(std::string
* errStr
) {
141 #endif // USE_JEMALLOC