Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
mem-tracker-test.cc
Go to the documentation of this file.
1 // Copyright 2013 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <string>
16 #include <gtest/gtest.h>
17 
18 #include "runtime/mem-tracker.h"
19 #include "util/metrics.h"
20 
21 #include "common/names.h"
22 
23 namespace impala {
24 
25 TEST(MemTestTest, SingleTrackerNoLimit) {
26  MemTracker t;
27  EXPECT_FALSE(t.has_limit());
28  t.Consume(10);
29  EXPECT_EQ(t.consumption(), 10);
30  t.Consume(10);
31  EXPECT_EQ(t.consumption(), 20);
32  t.Release(15);
33  EXPECT_EQ(t.consumption(), 5);
34  EXPECT_FALSE(t.LimitExceeded());
35 }
36 
37 TEST(MemTestTest, SingleTrackerWithLimit) {
38  MemTracker t(11);
39  EXPECT_TRUE(t.has_limit());
40  t.Consume(10);
41  EXPECT_EQ(t.consumption(), 10);
42  EXPECT_FALSE(t.LimitExceeded());
43  t.Consume(10);
44  EXPECT_EQ(t.consumption(), 20);
45  EXPECT_TRUE(t.LimitExceeded());
46  t.Release(15);
47  EXPECT_EQ(t.consumption(), 5);
48  EXPECT_FALSE(t.LimitExceeded());
49 }
50 
51 TEST(MemTestTest, ConsumptionMetric) {
52  UIntGauge metric("test", TUnit::BYTES, 0);
53  EXPECT_EQ(metric.value(), 0);
54 
55  MemTracker t(&metric, 100, -1, "");
56  EXPECT_TRUE(t.has_limit());
57  EXPECT_EQ(t.consumption(), 0);
58 
59  // Consume()/Release() arguments have no effect
60  t.Consume(150);
61  EXPECT_EQ(t.consumption(), 0);
62  EXPECT_EQ(t.peak_consumption(), 0);
63  EXPECT_FALSE(t.LimitExceeded());
64  t.Release(5);
65  EXPECT_EQ(t.consumption(), 0);
66  EXPECT_EQ(t.peak_consumption(), 0);
67  EXPECT_FALSE(t.LimitExceeded());
68 
69  metric.Increment(10);
70  // consumption_ is only updated with consumption_metric_ after calls to
71  // Consume()/Release()
72  t.Consume(0);
73  EXPECT_EQ(t.consumption(), 10);
74  EXPECT_EQ(t.peak_consumption(), 10);
75  metric.Increment(-5);
76  t.Consume(0);
77  EXPECT_EQ(t.consumption(), 5);
78  EXPECT_EQ(t.peak_consumption(), 10);
79  EXPECT_FALSE(t.LimitExceeded());
80  metric.Increment(150);
81  t.Consume(0);
82  EXPECT_EQ(t.consumption(), 155);
83  EXPECT_EQ(t.peak_consumption(), 155);
84  EXPECT_TRUE(t.LimitExceeded());
85  metric.Increment(-150);
86  t.Consume(0);
87  EXPECT_EQ(t.consumption(), 5);
88  EXPECT_EQ(t.peak_consumption(), 155);
89  EXPECT_FALSE(t.LimitExceeded());
90 }
91 
92 TEST(MemTestTest, TrackerHierarchy) {
93  MemTracker p(100);
94  MemTracker c1(80, -1, "", &p);
95  MemTracker c2(50, -1, "", &p);
96 
97  // everything below limits
98  c1.Consume(60);
99  EXPECT_EQ(c1.consumption(), 60);
100  EXPECT_FALSE(c1.LimitExceeded());
101  EXPECT_FALSE(c1.AnyLimitExceeded());
102  EXPECT_EQ(c2.consumption(), 0);
103  EXPECT_FALSE(c2.LimitExceeded());
104  EXPECT_FALSE(c2.AnyLimitExceeded());
105  EXPECT_EQ(p.consumption(), 60);
106  EXPECT_FALSE(p.LimitExceeded());
107  EXPECT_FALSE(p.AnyLimitExceeded());
108 
109  // p goes over limit
110  c2.Consume(50);
111  EXPECT_EQ(c1.consumption(), 60);
112  EXPECT_FALSE(c1.LimitExceeded());
113  EXPECT_TRUE(c1.AnyLimitExceeded());
114  EXPECT_EQ(c2.consumption(), 50);
115  EXPECT_FALSE(c2.LimitExceeded());
116  EXPECT_TRUE(c2.AnyLimitExceeded());
117  EXPECT_EQ(p.consumption(), 110);
118  EXPECT_TRUE(p.LimitExceeded());
119 
120  // c2 goes over limit, p drops below limit
121  c1.Release(20);
122  c2.Consume(10);
123  EXPECT_EQ(c1.consumption(), 40);
124  EXPECT_FALSE(c1.LimitExceeded());
125  EXPECT_FALSE(c1.AnyLimitExceeded());
126  EXPECT_EQ(c2.consumption(), 60);
127  EXPECT_TRUE(c2.LimitExceeded());
128  EXPECT_TRUE(c2.AnyLimitExceeded());
129  EXPECT_EQ(p.consumption(), 100);
130  EXPECT_FALSE(p.LimitExceeded());
131 }
132 
134  public:
135  static const int NUM_RELEASE_BYTES = 1;
136 
138 
140 
141  private:
143 };
144 
145 TEST(MemTestTest, GcFunctions) {
146  MemTracker t(10);
147  ASSERT_TRUE(t.has_limit());
148 
149  t.Consume(9);
150  EXPECT_FALSE(t.LimitExceeded());
151 
152  // Test TryConsume()
153  EXPECT_FALSE(t.TryConsume(2));
154  EXPECT_EQ(t.consumption(), 9);
155  EXPECT_FALSE(t.LimitExceeded());
156 
157  // Attach GcFunction that releases 1 byte
158  GcFunctionHelper gc_func_helper(&t);
159  t.AddGcFunction(boost::bind(&GcFunctionHelper::GcFunc, &gc_func_helper));
160  EXPECT_TRUE(t.TryConsume(2));
161  EXPECT_EQ(t.consumption(), 10);
162  EXPECT_FALSE(t.LimitExceeded());
163 
164  // GcFunction will be called even though TryConsume() fails
165  EXPECT_FALSE(t.TryConsume(2));
166  EXPECT_EQ(t.consumption(), 9);
167  EXPECT_FALSE(t.LimitExceeded());
168 
169  // GcFunction won't be called
170  EXPECT_TRUE(t.TryConsume(1));
171  EXPECT_EQ(t.consumption(), 10);
172  EXPECT_FALSE(t.LimitExceeded());
173 
174  // Test LimitExceeded()
175  t.Consume(1);
176  EXPECT_EQ(t.consumption(), 11);
177  EXPECT_FALSE(t.LimitExceeded());
178  EXPECT_EQ(t.consumption(), 10);
179 
180  // Add more GcFunctions, test that we only call them until the limit is no longer
181  // exceeded
182  GcFunctionHelper gc_func_helper2(&t);
183  t.AddGcFunction(boost::bind(&GcFunctionHelper::GcFunc, &gc_func_helper2));
184  GcFunctionHelper gc_func_helper3(&t);
185  t.AddGcFunction(boost::bind(&GcFunctionHelper::GcFunc, &gc_func_helper3));
186  t.Consume(1);
187  EXPECT_EQ(t.consumption(), 11);
188  EXPECT_FALSE(t.LimitExceeded());
189  EXPECT_EQ(t.consumption(), 10);
190 }
191 
192 }
193 
194 int main(int argc, char **argv) {
195  ::testing::InitGoogleTest(&argc, argv);
196  return RUN_ALL_TESTS();
197 }
GcFunctionHelper(MemTracker *tracker)
int64_t consumption() const
Returns the memory consumed in bytes.
Definition: mem-tracker.h:298
MemTracker tracker
bool TryConsume(int64_t bytes)
Definition: mem-tracker.h:163
TEST(AtomicTest, Basic)
Definition: atomic-test.cc:28
bool has_limit() const
Definition: mem-tracker.h:282
void AddGcFunction(GcFunction f)
Definition: mem-tracker.h:313
static const int NUM_RELEASE_BYTES
This class is thread-safe.
Definition: mem-tracker.h:61
void Release(int64_t bytes)
Decreases consumption of this tracker and its ancestors by 'bytes'.
Definition: mem-tracker.h:209
void Consume(int64_t bytes)
Increases consumption of this tracker and its ancestors by 'bytes'.
Definition: mem-tracker.h:118
int main(int argc, char **argv)