diff -r 2c3cc4b01880 -r c16ac7a2eba4 test/hotspot/gtest/jfr/test_networkUtilization.cpp --- a/test/hotspot/gtest/jfr/test_networkUtilization.cpp Wed Oct 30 16:14:56 2019 +0100 +++ b/test/hotspot/gtest/jfr/test_networkUtilization.cpp Wed Oct 30 19:43:52 2019 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -51,7 +51,7 @@ namespace { class MockFastUnorderedElapsedCounterSource : public ::FastUnorderedElapsedCounterSource { - public: + public: static jlong current_ticks; static Type now() { return current_ticks; @@ -65,7 +65,7 @@ typedef TimeInterval MockJfrTickspan; class MockJfrCheckpointWriter { - public: + public: traceid current; std::map ids; @@ -78,44 +78,97 @@ void write_key(traceid id) { current = id; } - void write(const char* data) { - ids[current] = data; - } + void write_type(JfrTypeId id) {} + MockJfrCheckpointWriter() {} + void write(const char* data) {} void set_context(const JfrCheckpointContext ctx) { } - void write_count(u4 nof_entries, jlong offset) { } + void write_count(u4 nof_entries) { } }; class MockJfrSerializer { - public: - static MockJfrSerializer* current; - - static bool register_serializer(JfrTypeId id, bool require_safepoint, bool permit_cache, MockJfrSerializer* serializer) { - current = serializer; + public: + static bool register_serializer(JfrTypeId id, bool permit_cache, MockJfrSerializer* serializer) { return true; } + virtual void on_rotation() {} + virtual void serialize(MockJfrCheckpointWriter& writer) {} + }; - virtual void serialize(MockJfrCheckpointWriter& writer) = 0; + struct MockNetworkInterface { + std::string name; + uint64_t bytes_in; + uint64_t bytes_out; + traceid id; + MockNetworkInterface(std::string name, uint64_t bytes_in, uint64_t bytes_out, traceid id) : + name(name), bytes_in(bytes_in), bytes_out(bytes_out), id(id) {} + + bool operator==(const MockNetworkInterface& rhs) const { + return name == rhs.name; + } + }; + + class NetworkInterface : public ::NetworkInterface { + public: + NetworkInterface(const char* name, uint64_t bytes_in, uint64_t bytes_out, NetworkInterface* next) : + ::NetworkInterface(name, bytes_in, bytes_out, next) {} + NetworkInterface* next(void) const { + return reinterpret_cast(::NetworkInterface::next()); + } }; - MockJfrSerializer* MockJfrSerializer::current; + class MockJfrOSInterface { + static std::list _interfaces; + public: + MockJfrOSInterface() {} + static int network_utilization(NetworkInterface** network_interfaces) { + *network_interfaces = NULL; + for (std::list::const_iterator i = _interfaces.begin(); + i != _interfaces.end(); + ++i) { + NetworkInterface* cur = new NetworkInterface(i->name.c_str(), i->bytes_in, i->bytes_out, *network_interfaces); + *network_interfaces = cur; + } + return OS_OK; + } + static MockNetworkInterface& add_interface(const std::string& name, traceid id) { + MockNetworkInterface iface(name, 0, 0, id); + _interfaces.push_front(iface); + return _interfaces.front(); + } + static void remove_interface(const MockNetworkInterface& iface) { + _interfaces.remove(iface); + } + static void clear_interfaces() { + _interfaces.clear(); + } + static const MockNetworkInterface& get_interface(traceid id) { + std::list::const_iterator i = _interfaces.begin(); + for (; i != _interfaces.end(); ++i) { + if (i->id == id) { + break; + } + } + return *i; + } + }; - class MockEventNetworkUtilization : public ::EventNetworkUtilization - { - public: + std::list MockJfrOSInterface::_interfaces; + + class MockEventNetworkUtilization : public ::EventNetworkUtilization { + public: std::string iface; s8 readRate; s8 writeRate; static std::vector committed; MockJfrCheckpointWriter writer; - public: + public: MockEventNetworkUtilization(EventStartTime timing=TIMED) : - ::EventNetworkUtilization(timing) { - } + ::EventNetworkUtilization(timing) {} void set_networkInterface(traceid new_value) { - MockJfrSerializer::current->serialize(writer); - iface = writer.ids[new_value]; + const MockNetworkInterface& entry = MockJfrOSInterface::get_interface(new_value); + iface = entry.name; } void set_readRate(s8 new_value) { readRate = new_value; @@ -129,7 +182,6 @@ } void set_starttime(const MockJfrTicks& time) {} - void set_endtime(const MockJfrTicks& time) {} static const MockEventNetworkUtilization& get_committed(const std::string& name) { @@ -149,62 +201,6 @@ jlong MockFastUnorderedElapsedCounterSource::current_ticks; - struct MockNetworkInterface { - std::string name; - uint64_t bytes_in; - uint64_t bytes_out; - MockNetworkInterface(std::string name, uint64_t bytes_in, uint64_t bytes_out) - : name(name), - bytes_in(bytes_in), - bytes_out(bytes_out) { - - } - bool operator==(const MockNetworkInterface& rhs) const { - return name == rhs.name; - } - }; - - class NetworkInterface : public ::NetworkInterface { - public: - NetworkInterface(const char* name, uint64_t bytes_in, uint64_t bytes_out, NetworkInterface* next) - : ::NetworkInterface(name, bytes_in, bytes_out, next) { - } - NetworkInterface* next(void) const { - return reinterpret_cast(::NetworkInterface::next()); - } - }; - - class MockJfrOSInterface { - static std::list _interfaces; - - public: - MockJfrOSInterface() { - } - static int network_utilization(NetworkInterface** network_interfaces) { - *network_interfaces = NULL; - for (std::list::const_iterator i = _interfaces.begin(); - i != _interfaces.end(); - ++i) { - NetworkInterface* cur = new NetworkInterface(i->name.c_str(), i->bytes_in, i->bytes_out, *network_interfaces); - *network_interfaces = cur; - } - return OS_OK; - } - static MockNetworkInterface& add_interface(const std::string& name) { - MockNetworkInterface iface(name, 0, 0); - _interfaces.push_back(iface); - return _interfaces.back(); - } - static void remove_interface(const MockNetworkInterface& iface) { - _interfaces.remove(iface); - } - static void clear_interfaces() { - _interfaces.clear(); - } - }; - - std::list MockJfrOSInterface::_interfaces; - // Reincluding source files in the anonymous namespace unfortunately seems to // behave strangely with precompiled headers (only when using gcc though) #ifndef DONT_USE_PRECOMPILED_HEADER @@ -248,7 +244,7 @@ TEST_VM_F(JfrTestNetworkUtilization, RequestFunctionBasic) { - MockNetworkInterface& eth0 = MockJfrOSInterface::add_interface("eth0"); + MockNetworkInterface& eth0 = MockJfrOSInterface::add_interface("eth0", 1); JfrNetworkUtilization::send_events(); ASSERT_EQ(0u, MockEventNetworkUtilization::committed.size()); @@ -265,9 +261,9 @@ TEST_VM_F(JfrTestNetworkUtilization, RequestFunctionMultiple) { - MockNetworkInterface& eth0 = MockJfrOSInterface::add_interface("eth0"); - MockNetworkInterface& eth1 = MockJfrOSInterface::add_interface("eth1"); - MockNetworkInterface& ppp0 = MockJfrOSInterface::add_interface("ppp0"); + MockNetworkInterface& eth0 = MockJfrOSInterface::add_interface("eth0", 2); + MockNetworkInterface& eth1 = MockJfrOSInterface::add_interface("eth1", 3); + MockNetworkInterface& ppp0 = MockJfrOSInterface::add_interface("ppp0", 4); JfrNetworkUtilization::send_events(); ASSERT_EQ(0u, MockEventNetworkUtilization::committed.size()); @@ -296,8 +292,8 @@ } TEST_VM_F(JfrTestNetworkUtilization, InterfaceRemoved) { - MockNetworkInterface& eth0 = MockJfrOSInterface::add_interface("eth0"); - MockNetworkInterface& eth1 = MockJfrOSInterface::add_interface("eth1"); + MockNetworkInterface& eth0 = MockJfrOSInterface::add_interface("eth0", 5); + MockNetworkInterface& eth1 = MockJfrOSInterface::add_interface("eth1", 6); JfrNetworkUtilization::send_events(); ASSERT_EQ(0u, MockEventNetworkUtilization::committed.size()); @@ -333,7 +329,7 @@ } TEST_VM_F(JfrTestNetworkUtilization, InterfaceReset) { - MockNetworkInterface& eth0 = MockJfrOSInterface::add_interface("eth0"); + MockNetworkInterface& eth0 = MockJfrOSInterface::add_interface("eth0", 7); JfrNetworkUtilization::send_events(); ASSERT_EQ(0u, MockEventNetworkUtilization::committed.size());