Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/gtest/jfr/test_networkUtilization.cpp
41144 views
1
/*
2
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
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 {
54
public:
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
64
typedef TimeInstant<CounterRepresentation, MockFastUnorderedElapsedCounterSource> MockJfrTicks;
65
typedef TimeInterval<CounterRepresentation, MockFastUnorderedElapsedCounterSource> MockJfrTickspan;
66
67
class MockJfrCheckpointWriter {
68
public:
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
}
81
void write_type(JfrTypeId id) {}
82
MockJfrCheckpointWriter() {}
83
void write(const char* data) {}
84
void set_context(const JfrCheckpointContext ctx) { }
85
void write_count(u4 nof_entries) { }
86
};
87
88
class MockJfrSerializer {
89
public:
90
static bool register_serializer(JfrTypeId id, bool permit_cache, MockJfrSerializer* serializer) {
91
return true;
92
}
93
virtual void on_rotation() {}
94
virtual void serialize(MockJfrCheckpointWriter& writer) {}
95
};
96
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
}
117
};
118
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
};
154
155
std::list<MockNetworkInterface> MockJfrOSInterface::_interfaces;
156
157
class MockEventNetworkUtilization : public ::EventNetworkUtilization {
158
public:
159
std::string iface;
160
s8 readRate;
161
s8 writeRate;
162
static std::vector<MockEventNetworkUtilization> committed;
163
MockJfrCheckpointWriter writer;
164
165
public:
166
MockEventNetworkUtilization(EventStartTime timing=TIMED) :
167
::EventNetworkUtilization(timing) {}
168
169
void set_networkInterface(traceid new_value) {
170
const MockNetworkInterface& entry = MockJfrOSInterface::get_interface(new_value);
171
iface = entry.name;
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
184
void set_starttime(const MockJfrTicks& time) {}
185
void set_endtime(const MockJfrTicks& time) {}
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
215
#define JfrTicks MockJfrTicks
216
#define JfrTickspan MockJfrTickspan
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
226
#undef JfrTicks
227
#undef JfrTickspan
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
static traceid id = 0;
246
247
TEST_VM_F(JfrTestNetworkUtilization, RequestFunctionBasic) {
248
249
MockNetworkInterface& eth0 = MockJfrOSInterface::add_interface("eth0", ++id);
250
JfrNetworkUtilization::send_events();
251
ASSERT_EQ(0u, MockEventNetworkUtilization::committed.size());
252
253
eth0.bytes_in += 10;
254
MockFastUnorderedElapsedCounterSource::current_ticks += 2 * NANOSECS_PER_SEC;
255
256
JfrNetworkUtilization::send_events();
257
ASSERT_EQ(1u, MockEventNetworkUtilization::committed.size());
258
MockEventNetworkUtilization& e = MockEventNetworkUtilization::committed[0];
259
EXPECT_EQ(40, e.readRate);
260
EXPECT_EQ(0, e.writeRate);
261
EXPECT_STREQ("eth0", e.iface.c_str());
262
}
263
264
TEST_VM_F(JfrTestNetworkUtilization, RequestFunctionMultiple) {
265
266
MockNetworkInterface& eth0 = MockJfrOSInterface::add_interface("eth0", ++id);
267
MockNetworkInterface& eth1 = MockJfrOSInterface::add_interface("eth1", ++id);
268
MockNetworkInterface& ppp0 = MockJfrOSInterface::add_interface("ppp0", ++id);
269
JfrNetworkUtilization::send_events();
270
ASSERT_EQ(0u, MockEventNetworkUtilization::committed.size());
271
272
eth0.bytes_in += 10;
273
eth1.bytes_in += 100;
274
ppp0.bytes_out += 50;
275
MockFastUnorderedElapsedCounterSource::current_ticks += 2 * NANOSECS_PER_SEC;
276
277
JfrNetworkUtilization::send_events();
278
ASSERT_EQ(3u, MockEventNetworkUtilization::committed.size());
279
const MockEventNetworkUtilization& eth0_event = MockEventNetworkUtilization::get_committed("eth0");
280
const MockEventNetworkUtilization& eth1_event = MockEventNetworkUtilization::get_committed("eth1");
281
const MockEventNetworkUtilization& ppp0_event = MockEventNetworkUtilization::get_committed("ppp0");
282
283
EXPECT_EQ(40, eth0_event.readRate);
284
EXPECT_EQ(0, eth0_event.writeRate);
285
EXPECT_STREQ("eth0", eth0_event.iface.c_str());
286
287
EXPECT_EQ(400, eth1_event.readRate);
288
EXPECT_EQ(0, eth1_event.writeRate);
289
EXPECT_STREQ("eth1", eth1_event.iface.c_str());
290
291
EXPECT_EQ(0, ppp0_event.readRate);
292
EXPECT_EQ(200, ppp0_event.writeRate);
293
EXPECT_STREQ("ppp0", ppp0_event.iface.c_str());
294
}
295
296
TEST_VM_F(JfrTestNetworkUtilization, InterfaceRemoved) {
297
MockNetworkInterface& eth0 = MockJfrOSInterface::add_interface("eth0", ++id);
298
MockNetworkInterface& eth1 = MockJfrOSInterface::add_interface("eth1", ++id);
299
JfrNetworkUtilization::send_events();
300
ASSERT_EQ(0u, MockEventNetworkUtilization::committed.size());
301
302
eth0.bytes_in += 10;
303
eth1.bytes_in += 20;
304
MockFastUnorderedElapsedCounterSource::current_ticks += 2 * NANOSECS_PER_SEC;
305
306
JfrNetworkUtilization::send_events();
307
ASSERT_EQ(2u, MockEventNetworkUtilization::committed.size());
308
const MockEventNetworkUtilization& eth0_event = MockEventNetworkUtilization::get_committed("eth0");
309
const MockEventNetworkUtilization& eth1_event = MockEventNetworkUtilization::get_committed("eth1");
310
311
EXPECT_EQ(40, eth0_event.readRate);
312
EXPECT_EQ(0, eth0_event.writeRate);
313
EXPECT_STREQ("eth0", eth0_event.iface.c_str());
314
315
EXPECT_EQ(80, eth1_event.readRate);
316
EXPECT_EQ(0, eth1_event.writeRate);
317
EXPECT_STREQ("eth1", eth1_event.iface.c_str());
318
319
MockJfrOSInterface::remove_interface(eth0);
320
MockEventNetworkUtilization::committed.clear();
321
322
eth1.bytes_in += 10;
323
MockFastUnorderedElapsedCounterSource::current_ticks += 2 * NANOSECS_PER_SEC;
324
JfrNetworkUtilization::send_events();
325
ASSERT_EQ(1u, MockEventNetworkUtilization::committed.size());
326
const MockEventNetworkUtilization& eth1_event_v2 = MockEventNetworkUtilization::get_committed("eth1");
327
328
EXPECT_EQ(40, eth1_event_v2.readRate);
329
EXPECT_EQ(0, eth1_event_v2.writeRate);
330
EXPECT_STREQ("eth1", eth1_event_v2.iface.c_str());
331
}
332
333
TEST_VM_F(JfrTestNetworkUtilization, InterfaceReset) {
334
MockNetworkInterface& eth0 = MockJfrOSInterface::add_interface("eth0", ++id);
335
JfrNetworkUtilization::send_events();
336
ASSERT_EQ(0u, MockEventNetworkUtilization::committed.size());
337
338
eth0.bytes_in += 10;
339
MockFastUnorderedElapsedCounterSource::current_ticks += 2 * NANOSECS_PER_SEC;
340
341
JfrNetworkUtilization::send_events();
342
ASSERT_EQ(1u, MockEventNetworkUtilization::committed.size());
343
const MockEventNetworkUtilization& event = MockEventNetworkUtilization::committed[0];
344
EXPECT_EQ(40, event.readRate);
345
EXPECT_EQ(0, event.writeRate);
346
EXPECT_STREQ("eth0", event.iface.c_str());
347
348
eth0.bytes_in = 0;
349
MockFastUnorderedElapsedCounterSource::current_ticks += 2 * NANOSECS_PER_SEC;
350
MockEventNetworkUtilization::committed.clear();
351
352
JfrNetworkUtilization::send_events();
353
ASSERT_EQ(0u, MockEventNetworkUtilization::committed.size());
354
355
eth0.bytes_in = 10;
356
MockFastUnorderedElapsedCounterSource::current_ticks += 2 * NANOSECS_PER_SEC;
357
358
JfrNetworkUtilization::send_events();
359
ASSERT_EQ(1u, MockEventNetworkUtilization::committed.size());
360
const MockEventNetworkUtilization& event_v2 = MockEventNetworkUtilization::committed[0];
361
EXPECT_EQ(40, event_v2.readRate);
362
EXPECT_EQ(0, event_v2.writeRate);
363
EXPECT_STREQ("eth0", event_v2.iface.c_str());
364
}
365
366