src/hotspot/os/windows/iphlp_interface.cpp
author naoto
Tue, 09 Jul 2019 08:05:38 -0700
changeset 55627 9c1885fb2a42
parent 50879 d90c3cbf13df
permissions -rw-r--r--
8227127: Era designator not displayed correctly using the COMPAT provider Reviewed-by: rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50879
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
     1
/*
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
     4
 *
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
     8
 *
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    13
 * accompanied this code).
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    14
 *
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    18
 *
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    21
 * questions.
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    22
 *
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    23
 */
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    24
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    25
#include "precompiled.hpp"
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    26
#include "iphlp_interface.hpp"
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    27
#include "runtime/os.hpp"
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    28
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    29
// IPHLP API
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    30
typedef DWORD(WINAPI *GetIfTable2_Fn)(PMIB_IF_TABLE2*);
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    31
typedef DWORD(WINAPI *FreeMibTable_Fn)(PVOID);
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    32
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    33
// IPHLP statics
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    34
GetIfTable2_Fn IphlpDll::_GetIfTable2 = NULL;
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    35
FreeMibTable_Fn IphlpDll::_FreeMibTable = NULL;
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    36
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    37
LONG IphlpDll::_critical_section = 0;
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    38
LONG IphlpDll::_initialized = 0;
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    39
LONG IphlpDll::_iphlp_reference_count = 0;
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    40
HMODULE IphlpDll::_hModule = NULL;
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    41
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    42
void IphlpDll::initialize(void) {
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    43
  _hModule = os::win32::load_Windows_dll("iphlpapi.dll", NULL, 0);
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    44
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    45
  if (NULL == _hModule) {
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    46
    return;
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    47
  }
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    48
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    49
  // The 'A' at the end means the ANSI (not the UNICODE) vesions of the methods
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    50
  _GetIfTable2 = (GetIfTable2_Fn)::GetProcAddress(_hModule, "GetIfTable2");
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    51
  _FreeMibTable = (FreeMibTable_Fn)::GetProcAddress(_hModule, "FreeMibTable");
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    52
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    53
  // interlock is used for fencing
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    54
  InterlockedExchange(&_initialized, 1);
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    55
}
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    56
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    57
bool IphlpDll::IphlpDetach(void) {
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    58
  LONG prev_ref_count = InterlockedExchangeAdd(&_iphlp_reference_count, -1);
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    59
  BOOL ret = false;
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    60
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    61
  if (1 == prev_ref_count) {
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    62
    if (_initialized && _hModule != NULL) {
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    63
      ret = FreeLibrary(_hModule);
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    64
      if (ret) {
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    65
        _hModule = NULL;
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    66
        _GetIfTable2 = NULL;
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    67
        _FreeMibTable = NULL;
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    68
        InterlockedExchange(&_initialized, 0);
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    69
      }
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    70
    }
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    71
  }
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    72
  return ret != 0;
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    73
}
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    74
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    75
bool IphlpDll::IphlpAttach(void) {
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    76
  InterlockedExchangeAdd(&_iphlp_reference_count, 1);
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    77
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    78
  if (1 == _initialized) {
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    79
    return true;
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    80
  }
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    81
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    82
  while (InterlockedCompareExchange(&_critical_section, 1, 0) == 1);
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    83
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    84
  if (0 == _initialized) {
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    85
    initialize();
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    86
  }
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    87
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    88
  while (InterlockedCompareExchange(&_critical_section, 0, 1) == 0);
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    89
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    90
  return (_GetIfTable2 != NULL && _FreeMibTable != NULL);
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    91
}
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    92
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    93
DWORD IphlpDll::GetIfTable2(PMIB_IF_TABLE2* Table) {
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    94
  assert(_initialized && _GetIfTable2 != NULL,
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    95
         "IphlpAttach() not yet called");
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    96
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    97
  return _GetIfTable2(Table);
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    98
}
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
    99
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
   100
DWORD IphlpDll::FreeMibTable(PVOID Memory) {
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
   101
  assert(_initialized && _FreeMibTable != NULL,
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
   102
         "IphlpAttach() not yet called");
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
   103
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
   104
  return _FreeMibTable(Memory);
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents:
diff changeset
   105
}