proxygen
MallctlHelperTest.cpp
Go to the documentation of this file.
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 
18 #include <folly/CPortability.h>
19 #include <folly/init/Init.h>
20 #include <folly/memory/Malloc.h>
22 
23 #if defined(FOLLY_USE_JEMALLOC) && !FOLLY_SANITIZE
24 #include <jemalloc/jemalloc.h>
25 #endif
26 
27 using namespace folly;
28 
29 #if JEMALLOC_VERSION_MAJOR > 4
30 static constexpr char const* kDecayCmd = "arena.0.dirty_decay_ms";
31 const char* malloc_conf = "dirty_decay_ms:10";
32 #else
33 static constexpr char const* kDecayCmd = "arena.0.decay_time";
34 const char* malloc_conf = "purge:decay,decay_time:10";
35 #endif
36 
38  protected:
39  void TearDown() override {
40  // Reset decay_time of arena 0 to 10 seconds.
41  ssize_t decayTime = 10;
43  }
44 
45  static ssize_t readArena0DecayTime() {
46  ssize_t decayTime = 0;
48  return decayTime;
49  }
50 };
51 
52 TEST_F(MallctlHelperTest, valid_read) {
53  ssize_t decayTime = 0;
55  EXPECT_EQ(10, decayTime);
56 }
57 
58 TEST_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 
64 TEST_F(MallctlHelperTest, valid_write) {
65  ssize_t decayTime = 20;
67  EXPECT_EQ(20, readArena0DecayTime());
68 }
69 
70 TEST_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 
76 TEST_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 
84 TEST_F(MallctlHelperTest, invalid_read_write) {
85  ssize_t oldDecayTime = 0;
86  ssize_t newDecayTime = 20;
88  mallctlReadWrite("invalid", &oldDecayTime, newDecayTime),
89  std::runtime_error);
90  EXPECT_EQ(0, oldDecayTime);
91  EXPECT_EQ(10, readArena0DecayTime());
92 }
93 
94 TEST_F(MallctlHelperTest, valid_call) {
95  EXPECT_NO_THROW(mallctlCall("arena.0.decay"));
96 }
97 
98 TEST_F(MallctlHelperTest, invalid_call) {
99  EXPECT_THROW(mallctlCall("invalid"), std::runtime_error);
100 }
101 
102 int main(int argc, char** argv) {
103  ::testing::InitGoogleTest(&argc, argv);
104  init(&argc, &argv);
105  return usingJEMalloc() ? RUN_ALL_TESTS() : 0;
106 }
#define EXPECT_NO_THROW(statement)
Definition: gtest.h:1845
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2232
bool usingJEMalloc() noexcept
Definition: Malloc.h:147
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void mallctlCall(const char *cmd)
Definition: MallctlHelper.h:62
static constexpr char const * kDecayCmd
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
void TearDown() override
void mallctlReadWrite(const char *cmd, T *out, T in)
Definition: MallctlHelper.h:58
void mallctlRead(const char *cmd, T *out)
Definition: MallctlHelper.h:48
void init(int *argc, char ***argv, bool removeFlags)
Definition: Init.cpp:34
char ** argv
const char * malloc_conf
TEST_F(AsyncSSLSocketWriteTest, write_coalescing1)
void mallctlWrite(const char *cmd, T in)
Definition: MallctlHelper.h:53
static ssize_t readArena0DecayTime()
int main(int argc, char **argv)
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: gtest.cc:5370