2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/memory/MallctlHelper.h>
18 #include <folly/Malloc.h>
19 #include <folly/init/Init.h>
20 #include <folly/portability/GTest.h>
22 #ifdef FOLLY_HAVE_LIBJEMALLOC
23 #include <jemalloc/jemalloc.h>
26 using namespace folly;
28 #if JEMALLOC_VERSION_MAJOR > 4
29 static constexpr char const* kDecayCmd = "arena.0.dirty_decay_ms";
30 const char* malloc_conf = "dirty_decay_ms:10";
32 static constexpr char const* kDecayCmd = "arena.0.decay_time";
33 const char* malloc_conf = "purge:decay,decay_time:10";
36 class MallctlHelperTest : public ::testing::Test {
38 void TearDown() override {
39 // Reset decay_time of arena 0 to 10 seconds.
40 ssize_t decayTime = 10;
41 EXPECT_NO_THROW(mallctlWrite(kDecayCmd, decayTime));
44 static ssize_t readArena0DecayTime() {
45 ssize_t decayTime = 0;
46 EXPECT_NO_THROW(mallctlRead(kDecayCmd, &decayTime));
51 TEST_F(MallctlHelperTest, valid_read) {
52 ssize_t decayTime = 0;
53 EXPECT_NO_THROW(mallctlRead(kDecayCmd, &decayTime));
54 EXPECT_EQ(10, decayTime);
57 TEST_F(MallctlHelperTest, invalid_read) {
58 ssize_t decayTime = 0;
59 EXPECT_THROW(mallctlRead("invalid", &decayTime), std::runtime_error);
60 EXPECT_EQ(0, decayTime);
63 TEST_F(MallctlHelperTest, valid_write) {
64 ssize_t decayTime = 20;
65 EXPECT_NO_THROW(mallctlWrite(kDecayCmd, decayTime));
66 EXPECT_EQ(20, readArena0DecayTime());
69 TEST_F(MallctlHelperTest, invalid_write) {
70 ssize_t decayTime = 20;
71 EXPECT_THROW(mallctlWrite("invalid", decayTime), std::runtime_error);
72 EXPECT_EQ(10, readArena0DecayTime());
75 TEST_F(MallctlHelperTest, valid_read_write) {
76 ssize_t oldDecayTime = 0;
77 ssize_t newDecayTime = 20;
78 EXPECT_NO_THROW(mallctlReadWrite(kDecayCmd, &oldDecayTime, newDecayTime));
79 EXPECT_EQ(10, oldDecayTime);
80 EXPECT_EQ(20, readArena0DecayTime());
83 TEST_F(MallctlHelperTest, invalid_read_write) {
84 ssize_t oldDecayTime = 0;
85 ssize_t newDecayTime = 20;
87 mallctlReadWrite("invalid", &oldDecayTime, newDecayTime),
89 EXPECT_EQ(0, oldDecayTime);
90 EXPECT_EQ(10, readArena0DecayTime());
93 TEST_F(MallctlHelperTest, valid_call) {
94 EXPECT_NO_THROW(mallctlCall("arena.0.decay"));
97 TEST_F(MallctlHelperTest, invalid_call) {
98 EXPECT_THROW(mallctlCall("invalid"), std::runtime_error);
101 int main(int argc, char** argv) {
102 ::testing::InitGoogleTest(&argc, argv);
104 return usingJEMalloc() ? RUN_ALL_TESTS() : 0;