author | mgronlun |
Wed, 30 Oct 2019 19:43:52 +0100 | |
changeset 58863 | c16ac7a2eba4 |
parent 53026 | 7d4397b43fa3 |
permissions | -rw-r--r-- |
50879 | 1 |
/* |
58863 | 2 |
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. |
50879 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
* |
|
23 |
*/ |
|
24 |
||
25 |
#include "precompiled.hpp" |
|
26 |
||
27 |
// This test performs mocking of certain JVM functionality. This works by |
|
28 |
// including the source file under test inside an anonymous namespace (which |
|
29 |
// prevents linking conflicts) with the mocked symbols redefined. |
|
30 |
||
31 |
// The include list should mirror the one found in the included source file - |
|
32 |
// with the ones that should pick up the mocks removed. Those should be included |
|
33 |
// later after the mocks have been defined. |
|
34 |
||
35 |
#include "logging/log.hpp" |
|
36 |
#include "jfr/jfrEvents.hpp" |
|
37 |
#include "jfr/metadata/jfrSerializer.hpp" |
|
38 |
#include "jfr/periodic/jfrOSInterface.hpp" |
|
39 |
#include "jfr/utilities/jfrTime.hpp" |
|
40 |
#include "jfr/utilities/jfrTypes.hpp" |
|
41 |
#include "runtime/os_perf.hpp" |
|
42 |
#include "utilities/globalDefinitions.hpp" |
|
43 |
#include "utilities/growableArray.hpp" |
|
44 |
||
45 |
#include "unittest.hpp" |
|
46 |
||
47 |
#include <vector> |
|
48 |
#include <list> |
|
49 |
#include <map> |
|
50 |
||
51 |
namespace { |
|
52 |
||
53 |
class MockFastUnorderedElapsedCounterSource : public ::FastUnorderedElapsedCounterSource { |
|
58863 | 54 |
public: |
50879 | 55 |
static jlong current_ticks; |
56 |
static Type now() { |
|
57 |
return current_ticks; |
|
58 |
} |
|
59 |
static uint64_t nanoseconds(Type value) { |
|
60 |
return value; |
|
61 |
} |
|
62 |
}; |
|
63 |
||
50894 | 64 |
typedef TimeInstant<CounterRepresentation, MockFastUnorderedElapsedCounterSource> MockJfrTicks; |
65 |
typedef TimeInterval<CounterRepresentation, MockFastUnorderedElapsedCounterSource> MockJfrTickspan; |
|
50879 | 66 |
|
67 |
class MockJfrCheckpointWriter { |
|
58863 | 68 |
public: |
50879 | 69 |
traceid current; |
70 |
std::map<traceid, std::string> ids; |
|
71 |
||
72 |
const JfrCheckpointContext context() const { |
|
73 |
return JfrCheckpointContext(); |
|
74 |
} |
|
75 |
intptr_t reserve(size_t size) { |
|
76 |
return 0; |
|
77 |
} |
|
78 |
void write_key(traceid id) { |
|
79 |
current = id; |
|
80 |
} |
|
58863 | 81 |
void write_type(JfrTypeId id) {} |
82 |
MockJfrCheckpointWriter() {} |
|
83 |
void write(const char* data) {} |
|
50879 | 84 |
void set_context(const JfrCheckpointContext ctx) { } |
58863 | 85 |
void write_count(u4 nof_entries) { } |
50879 | 86 |
}; |
87 |
||
88 |
class MockJfrSerializer { |
|
58863 | 89 |
public: |
90 |
static bool register_serializer(JfrTypeId id, bool permit_cache, MockJfrSerializer* serializer) { |
|
50879 | 91 |
return true; |
92 |
} |
|
58863 | 93 |
virtual void on_rotation() {} |
94 |
virtual void serialize(MockJfrCheckpointWriter& writer) {} |
|
95 |
}; |
|
50879 | 96 |
|
58863 | 97 |
struct MockNetworkInterface { |
98 |
std::string name; |
|
99 |
uint64_t bytes_in; |
|
100 |
uint64_t bytes_out; |
|
101 |
traceid id; |
|
102 |
MockNetworkInterface(std::string name, uint64_t bytes_in, uint64_t bytes_out, traceid id) : |
|
103 |
name(name), bytes_in(bytes_in), bytes_out(bytes_out), id(id) {} |
|
104 |
||
105 |
bool operator==(const MockNetworkInterface& rhs) const { |
|
106 |
return name == rhs.name; |
|
107 |
} |
|
108 |
}; |
|
109 |
||
110 |
class NetworkInterface : public ::NetworkInterface { |
|
111 |
public: |
|
112 |
NetworkInterface(const char* name, uint64_t bytes_in, uint64_t bytes_out, NetworkInterface* next) : |
|
113 |
::NetworkInterface(name, bytes_in, bytes_out, next) {} |
|
114 |
NetworkInterface* next(void) const { |
|
115 |
return reinterpret_cast<NetworkInterface*>(::NetworkInterface::next()); |
|
116 |
} |
|
50879 | 117 |
}; |
118 |
||
58863 | 119 |
class MockJfrOSInterface { |
120 |
static std::list<MockNetworkInterface> _interfaces; |
|
121 |
public: |
|
122 |
MockJfrOSInterface() {} |
|
123 |
static int network_utilization(NetworkInterface** network_interfaces) { |
|
124 |
*network_interfaces = NULL; |
|
125 |
for (std::list<MockNetworkInterface>::const_iterator i = _interfaces.begin(); |
|
126 |
i != _interfaces.end(); |
|
127 |
++i) { |
|
128 |
NetworkInterface* cur = new NetworkInterface(i->name.c_str(), i->bytes_in, i->bytes_out, *network_interfaces); |
|
129 |
*network_interfaces = cur; |
|
130 |
} |
|
131 |
return OS_OK; |
|
132 |
} |
|
133 |
static MockNetworkInterface& add_interface(const std::string& name, traceid id) { |
|
134 |
MockNetworkInterface iface(name, 0, 0, id); |
|
135 |
_interfaces.push_front(iface); |
|
136 |
return _interfaces.front(); |
|
137 |
} |
|
138 |
static void remove_interface(const MockNetworkInterface& iface) { |
|
139 |
_interfaces.remove(iface); |
|
140 |
} |
|
141 |
static void clear_interfaces() { |
|
142 |
_interfaces.clear(); |
|
143 |
} |
|
144 |
static const MockNetworkInterface& get_interface(traceid id) { |
|
145 |
std::list<MockNetworkInterface>::const_iterator i = _interfaces.begin(); |
|
146 |
for (; i != _interfaces.end(); ++i) { |
|
147 |
if (i->id == id) { |
|
148 |
break; |
|
149 |
} |
|
150 |
} |
|
151 |
return *i; |
|
152 |
} |
|
153 |
}; |
|
50879 | 154 |
|
58863 | 155 |
std::list<MockNetworkInterface> MockJfrOSInterface::_interfaces; |
156 |
||
157 |
class MockEventNetworkUtilization : public ::EventNetworkUtilization { |
|
158 |
public: |
|
50879 | 159 |
std::string iface; |
160 |
s8 readRate; |
|
161 |
s8 writeRate; |
|
162 |
static std::vector<MockEventNetworkUtilization> committed; |
|
163 |
MockJfrCheckpointWriter writer; |
|
164 |
||
58863 | 165 |
public: |
50879 | 166 |
MockEventNetworkUtilization(EventStartTime timing=TIMED) : |
58863 | 167 |
::EventNetworkUtilization(timing) {} |
50879 | 168 |
|
169 |
void set_networkInterface(traceid new_value) { |
|
58863 | 170 |
const MockNetworkInterface& entry = MockJfrOSInterface::get_interface(new_value); |
171 |
iface = entry.name; |
|
50879 | 172 |
} |
173 |
void set_readRate(s8 new_value) { |
|
174 |
readRate = new_value; |
|
175 |
} |
|
176 |
void set_writeRate(s8 new_value) { |
|
177 |
writeRate = new_value; |
|
178 |
} |
|
179 |
||
180 |
void commit() { |
|
181 |
committed.push_back(*this); |
|
182 |
} |
|
183 |
||
50894 | 184 |
void set_starttime(const MockJfrTicks& time) {} |
185 |
void set_endtime(const MockJfrTicks& time) {} |
|
50879 | 186 |
|
187 |
static const MockEventNetworkUtilization& get_committed(const std::string& name) { |
|
188 |
static MockEventNetworkUtilization placeholder; |
|
189 |
for (std::vector<MockEventNetworkUtilization>::const_iterator i = committed.begin(); |
|
190 |
i != committed.end(); |
|
191 |
++i) { |
|
192 |
if (name == i->iface) { |
|
193 |
return *i; |
|
194 |
} |
|
195 |
} |
|
196 |
return placeholder; |
|
197 |
} |
|
198 |
}; |
|
199 |
||
200 |
std::vector<MockEventNetworkUtilization> MockEventNetworkUtilization::committed; |
|
201 |
||
202 |
jlong MockFastUnorderedElapsedCounterSource::current_ticks; |
|
203 |
||
204 |
// Reincluding source files in the anonymous namespace unfortunately seems to |
|
205 |
// behave strangely with precompiled headers (only when using gcc though) |
|
206 |
#ifndef DONT_USE_PRECOMPILED_HEADER |
|
207 |
#define DONT_USE_PRECOMPILED_HEADER |
|
208 |
#endif |
|
209 |
||
210 |
#define EventNetworkUtilization MockEventNetworkUtilization |
|
211 |
#define FastUnorderedElapsedCounterSource MockFastUnorderedElapsedCounterSource |
|
212 |
#define JfrOSInterface MockJfrOSInterface |
|
213 |
#define JfrSerializer MockJfrSerializer |
|
214 |
#define JfrCheckpointWriter MockJfrCheckpointWriter |
|
50894 | 215 |
#define JfrTicks MockJfrTicks |
216 |
#define JfrTickspan MockJfrTickspan |
|
50879 | 217 |
|
218 |
#include "jfr/periodic/jfrNetworkUtilization.hpp" |
|
219 |
#include "jfr/periodic/jfrNetworkUtilization.cpp" |
|
220 |
||
221 |
#undef EventNetworkUtilization |
|
222 |
#undef FastUnorderedElapsedCounterSource |
|
223 |
#undef JfrOSInterface |
|
224 |
#undef JfrSerializer |
|
225 |
#undef JfrCheckpointWriter |
|
50894 | 226 |
#undef JfrTicks |
227 |
#undef JfrTickspan |
|
50879 | 228 |
|
229 |
} // anonymous namespace |
|
230 |
||
231 |
class JfrTestNetworkUtilization : public ::testing::Test { |
|
232 |
protected: |
|
233 |
void SetUp() { |
|
234 |
MockEventNetworkUtilization::committed.clear(); |
|
235 |
MockJfrOSInterface::clear_interfaces(); |
|
236 |
// Ensure that tests are separated in time |
|
237 |
MockFastUnorderedElapsedCounterSource::current_ticks += 1 * NANOSECS_PER_SEC; |
|
238 |
} |
|
239 |
||
240 |
void TearDown() { |
|
241 |
JfrNetworkUtilization::destroy(); |
|
242 |
} |
|
243 |
}; |
|
244 |
||
245 |
TEST_VM_F(JfrTestNetworkUtilization, RequestFunctionBasic) { |
|
246 |
||
58863 | 247 |
MockNetworkInterface& eth0 = MockJfrOSInterface::add_interface("eth0", 1); |
50879 | 248 |
JfrNetworkUtilization::send_events(); |
249 |
ASSERT_EQ(0u, MockEventNetworkUtilization::committed.size()); |
|
250 |
||
251 |
eth0.bytes_in += 10; |
|
252 |
MockFastUnorderedElapsedCounterSource::current_ticks += 2 * NANOSECS_PER_SEC; |
|
253 |
||
254 |
JfrNetworkUtilization::send_events(); |
|
255 |
ASSERT_EQ(1u, MockEventNetworkUtilization::committed.size()); |
|
256 |
MockEventNetworkUtilization& e = MockEventNetworkUtilization::committed[0]; |
|
53026
7d4397b43fa3
8215362: JFR GTest JfrTestNetworkUtilization fails
egahlin
parents:
50894
diff
changeset
|
257 |
EXPECT_EQ(40, e.readRate); |
50879 | 258 |
EXPECT_EQ(0, e.writeRate); |
259 |
EXPECT_STREQ("eth0", e.iface.c_str()); |
|
260 |
} |
|
261 |
||
262 |
TEST_VM_F(JfrTestNetworkUtilization, RequestFunctionMultiple) { |
|
263 |
||
58863 | 264 |
MockNetworkInterface& eth0 = MockJfrOSInterface::add_interface("eth0", 2); |
265 |
MockNetworkInterface& eth1 = MockJfrOSInterface::add_interface("eth1", 3); |
|
266 |
MockNetworkInterface& ppp0 = MockJfrOSInterface::add_interface("ppp0", 4); |
|
50879 | 267 |
JfrNetworkUtilization::send_events(); |
268 |
ASSERT_EQ(0u, MockEventNetworkUtilization::committed.size()); |
|
269 |
||
270 |
eth0.bytes_in += 10; |
|
271 |
eth1.bytes_in += 100; |
|
272 |
ppp0.bytes_out += 50; |
|
273 |
MockFastUnorderedElapsedCounterSource::current_ticks += 2 * NANOSECS_PER_SEC; |
|
274 |
||
275 |
JfrNetworkUtilization::send_events(); |
|
276 |
ASSERT_EQ(3u, MockEventNetworkUtilization::committed.size()); |
|
277 |
const MockEventNetworkUtilization& eth0_event = MockEventNetworkUtilization::get_committed("eth0"); |
|
278 |
const MockEventNetworkUtilization& eth1_event = MockEventNetworkUtilization::get_committed("eth1"); |
|
279 |
const MockEventNetworkUtilization& ppp0_event = MockEventNetworkUtilization::get_committed("ppp0"); |
|
280 |
||
53026
7d4397b43fa3
8215362: JFR GTest JfrTestNetworkUtilization fails
egahlin
parents:
50894
diff
changeset
|
281 |
EXPECT_EQ(40, eth0_event.readRate); |
50879 | 282 |
EXPECT_EQ(0, eth0_event.writeRate); |
283 |
EXPECT_STREQ("eth0", eth0_event.iface.c_str()); |
|
284 |
||
53026
7d4397b43fa3
8215362: JFR GTest JfrTestNetworkUtilization fails
egahlin
parents:
50894
diff
changeset
|
285 |
EXPECT_EQ(400, eth1_event.readRate); |
50879 | 286 |
EXPECT_EQ(0, eth1_event.writeRate); |
287 |
EXPECT_STREQ("eth1", eth1_event.iface.c_str()); |
|
288 |
||
289 |
EXPECT_EQ(0, ppp0_event.readRate); |
|
53026
7d4397b43fa3
8215362: JFR GTest JfrTestNetworkUtilization fails
egahlin
parents:
50894
diff
changeset
|
290 |
EXPECT_EQ(200, ppp0_event.writeRate); |
50879 | 291 |
EXPECT_STREQ("ppp0", ppp0_event.iface.c_str()); |
292 |
} |
|
293 |
||
294 |
TEST_VM_F(JfrTestNetworkUtilization, InterfaceRemoved) { |
|
58863 | 295 |
MockNetworkInterface& eth0 = MockJfrOSInterface::add_interface("eth0", 5); |
296 |
MockNetworkInterface& eth1 = MockJfrOSInterface::add_interface("eth1", 6); |
|
50879 | 297 |
JfrNetworkUtilization::send_events(); |
298 |
ASSERT_EQ(0u, MockEventNetworkUtilization::committed.size()); |
|
299 |
||
300 |
eth0.bytes_in += 10; |
|
301 |
eth1.bytes_in += 20; |
|
302 |
MockFastUnorderedElapsedCounterSource::current_ticks += 2 * NANOSECS_PER_SEC; |
|
303 |
||
304 |
JfrNetworkUtilization::send_events(); |
|
305 |
ASSERT_EQ(2u, MockEventNetworkUtilization::committed.size()); |
|
306 |
const MockEventNetworkUtilization& eth0_event = MockEventNetworkUtilization::get_committed("eth0"); |
|
307 |
const MockEventNetworkUtilization& eth1_event = MockEventNetworkUtilization::get_committed("eth1"); |
|
308 |
||
53026
7d4397b43fa3
8215362: JFR GTest JfrTestNetworkUtilization fails
egahlin
parents:
50894
diff
changeset
|
309 |
EXPECT_EQ(40, eth0_event.readRate); |
50879 | 310 |
EXPECT_EQ(0, eth0_event.writeRate); |
311 |
EXPECT_STREQ("eth0", eth0_event.iface.c_str()); |
|
312 |
||
53026
7d4397b43fa3
8215362: JFR GTest JfrTestNetworkUtilization fails
egahlin
parents:
50894
diff
changeset
|
313 |
EXPECT_EQ(80, eth1_event.readRate); |
50879 | 314 |
EXPECT_EQ(0, eth1_event.writeRate); |
315 |
EXPECT_STREQ("eth1", eth1_event.iface.c_str()); |
|
316 |
||
317 |
MockJfrOSInterface::remove_interface(eth0); |
|
318 |
MockEventNetworkUtilization::committed.clear(); |
|
319 |
||
320 |
eth1.bytes_in += 10; |
|
321 |
MockFastUnorderedElapsedCounterSource::current_ticks += 2 * NANOSECS_PER_SEC; |
|
322 |
JfrNetworkUtilization::send_events(); |
|
323 |
ASSERT_EQ(1u, MockEventNetworkUtilization::committed.size()); |
|
324 |
const MockEventNetworkUtilization& eth1_event_v2 = MockEventNetworkUtilization::get_committed("eth1"); |
|
325 |
||
53026
7d4397b43fa3
8215362: JFR GTest JfrTestNetworkUtilization fails
egahlin
parents:
50894
diff
changeset
|
326 |
EXPECT_EQ(40, eth1_event_v2.readRate); |
50879 | 327 |
EXPECT_EQ(0, eth1_event_v2.writeRate); |
328 |
EXPECT_STREQ("eth1", eth1_event_v2.iface.c_str()); |
|
329 |
} |
|
330 |
||
331 |
TEST_VM_F(JfrTestNetworkUtilization, InterfaceReset) { |
|
58863 | 332 |
MockNetworkInterface& eth0 = MockJfrOSInterface::add_interface("eth0", 7); |
50879 | 333 |
JfrNetworkUtilization::send_events(); |
334 |
ASSERT_EQ(0u, MockEventNetworkUtilization::committed.size()); |
|
335 |
||
336 |
eth0.bytes_in += 10; |
|
337 |
MockFastUnorderedElapsedCounterSource::current_ticks += 2 * NANOSECS_PER_SEC; |
|
338 |
||
339 |
JfrNetworkUtilization::send_events(); |
|
340 |
ASSERT_EQ(1u, MockEventNetworkUtilization::committed.size()); |
|
341 |
const MockEventNetworkUtilization& event = MockEventNetworkUtilization::committed[0]; |
|
53026
7d4397b43fa3
8215362: JFR GTest JfrTestNetworkUtilization fails
egahlin
parents:
50894
diff
changeset
|
342 |
EXPECT_EQ(40, event.readRate); |
50879 | 343 |
EXPECT_EQ(0, event.writeRate); |
344 |
EXPECT_STREQ("eth0", event.iface.c_str()); |
|
345 |
||
346 |
eth0.bytes_in = 0; |
|
347 |
MockFastUnorderedElapsedCounterSource::current_ticks += 2 * NANOSECS_PER_SEC; |
|
348 |
MockEventNetworkUtilization::committed.clear(); |
|
349 |
||
350 |
JfrNetworkUtilization::send_events(); |
|
351 |
ASSERT_EQ(0u, MockEventNetworkUtilization::committed.size()); |
|
352 |
||
353 |
eth0.bytes_in = 10; |
|
354 |
MockFastUnorderedElapsedCounterSource::current_ticks += 2 * NANOSECS_PER_SEC; |
|
355 |
||
356 |
JfrNetworkUtilization::send_events(); |
|
357 |
ASSERT_EQ(1u, MockEventNetworkUtilization::committed.size()); |
|
358 |
const MockEventNetworkUtilization& event_v2 = MockEventNetworkUtilization::committed[0]; |
|
53026
7d4397b43fa3
8215362: JFR GTest JfrTestNetworkUtilization fails
egahlin
parents:
50894
diff
changeset
|
359 |
EXPECT_EQ(40, event_v2.readRate); |
50879 | 360 |
EXPECT_EQ(0, event_v2.writeRate); |
361 |
EXPECT_STREQ("eth0", event_v2.iface.c_str()); |
|
362 |
} |