1/*
2 * Copyright 2016-present Facebook, Inc.
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
17#include <folly/memory/MallctlHelper.h>
18#include <folly/CPortability.h>
19#include <folly/init/Init.h>
20#include <folly/memory/Malloc.h>
21#include <folly/portability/GTest.h>
22
23#if defined(FOLLY_USE_JEMALLOC) && !FOLLY_SANITIZE
24#include <jemalloc/jemalloc.h>
25#endif
26
27using namespace folly;
28
29#if JEMALLOC_VERSION_MAJOR > 4
30static constexpr char const* kDecayCmd = "arena.0.dirty_decay_ms";
31const char* malloc_conf = "dirty_decay_ms:10";
32#else
33static constexpr char const* kDecayCmd = "arena.0.decay_time";
34const char* malloc_conf = "purge:decay,decay_time:10";
35#endif
36
37class MallctlHelperTest : public ::testing::Test {
38 protected:
39 void TearDown() override {
40 // Reset decay_time of arena 0 to 10 seconds.
41 ssize_t decayTime = 10;
42 EXPECT_NO_THROW(mallctlWrite(kDecayCmd, decayTime));
43 }
44
45 static ssize_t readArena0DecayTime() {
46 ssize_t decayTime = 0;
47 EXPECT_NO_THROW(mallctlRead(kDecayCmd, &decayTime));
48 return decayTime;
49 }
50};
51
52TEST_F(MallctlHelperTest, valid_read) {
53 ssize_t decayTime = 0;
54 EXPECT_NO_THROW(mallctlRead(kDecayCmd, &decayTime));
55 EXPECT_EQ(10, decayTime);
56}
57
58TEST_F(MallctlHelperTest, invalid_read) {
59 ssize_t decayTime = 0;
60 EXPECT_THROW(mallctlRead("invalid", &decayTime), std::runtime_error);
61 EXPECT_EQ(0, decayTime);
62}
63
64TEST_F(MallctlHelperTest, valid_write) {
65 ssize_t decayTime = 20;
66 EXPECT_NO_THROW(mallctlWrite(kDecayCmd, decayTime));
67 EXPECT_EQ(20, readArena0DecayTime());
68}
69
70TEST_F(MallctlHelperTest, invalid_write) {
71 ssize_t decayTime = 20;
72 EXPECT_THROW(mallctlWrite("invalid", decayTime), std::runtime_error);
73 EXPECT_EQ(10, readArena0DecayTime());
74}
75
76TEST_F(MallctlHelperTest, valid_read_write) {
77 ssize_t oldDecayTime = 0;
78 ssize_t newDecayTime = 20;
79 EXPECT_NO_THROW(mallctlReadWrite(kDecayCmd, &oldDecayTime, newDecayTime));
80 EXPECT_EQ(10, oldDecayTime);
81 EXPECT_EQ(20, readArena0DecayTime());
82}
83
84TEST_F(MallctlHelperTest, invalid_read_write) {
85 ssize_t oldDecayTime = 0;
86 ssize_t newDecayTime = 20;
87 EXPECT_THROW(
88 mallctlReadWrite("invalid", &oldDecayTime, newDecayTime),
89 std::runtime_error);
90 EXPECT_EQ(0, oldDecayTime);
91 EXPECT_EQ(10, readArena0DecayTime());
92}
93
94TEST_F(MallctlHelperTest, valid_call) {
95 EXPECT_NO_THROW(mallctlCall("arena.0.decay"));
96}
97
98TEST_F(MallctlHelperTest, invalid_call) {
99 EXPECT_THROW(mallctlCall("invalid"), std::runtime_error);
100}
101
102int main(int argc, char** argv) {
103 ::testing::InitGoogleTest(&argc, argv);
104 init(&argc, &argv);
105 return usingJEMalloc() ? RUN_ALL_TESTS() : 0;
106}
107