src/hotspot/share/services/memTracker.hpp
author coleenp
Tue, 06 Feb 2018 18:10:30 -0500
changeset 48872 c7774afc93e3
parent 48865 53427ddce0a0
child 49033 3acc342c0738
permissions -rw-r--r--
8196907: Fix minimal vm Reviewed-by: zgu, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
     1
/*
48865
53427ddce0a0 8194934: NMT: Remove MemTracker::get_virtual_memory_xxx_tracker(), create Tracker in place
zgu
parents: 47216
diff changeset
     2
 * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
     4
 *
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
     7
 * published by the Free Software Foundation.
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
     8
 *
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    13
 * accompanied this code).
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    14
 *
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    18
 *
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    21
 * questions.
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    22
 *
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    23
 */
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    24
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    25
#ifndef SHARE_VM_SERVICES_MEM_TRACKER_HPP
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    26
#define SHARE_VM_SERVICES_MEM_TRACKER_HPP
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    27
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    28
#include "services/nmtCommon.hpp"
26144
35e11b657728 8055007: NMT2: emptyStack missing in minimal build
zgu
parents: 25946
diff changeset
    29
#include "utilities/nativeCallStack.hpp"
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    30
13975
2f7431485cfa 7189254: Change makefiles for more flexibility to override defaults
jprovino
parents: 13861
diff changeset
    31
2f7431485cfa 7189254: Change makefiles for more flexibility to override defaults
jprovino
parents: 13861
diff changeset
    32
#if !INCLUDE_NMT
2f7431485cfa 7189254: Change makefiles for more flexibility to override defaults
jprovino
parents: 13861
diff changeset
    33
26144
35e11b657728 8055007: NMT2: emptyStack missing in minimal build
zgu
parents: 25946
diff changeset
    34
#define CURRENT_PC   NativeCallStack::EMPTY_STACK
35e11b657728 8055007: NMT2: emptyStack missing in minimal build
zgu
parents: 25946
diff changeset
    35
#define CALLER_PC    NativeCallStack::EMPTY_STACK
13975
2f7431485cfa 7189254: Change makefiles for more flexibility to override defaults
jprovino
parents: 13861
diff changeset
    36
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    37
class Tracker : public StackObj {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    38
 public:
48872
c7774afc93e3 8196907: Fix minimal vm
coleenp
parents: 48865
diff changeset
    39
  enum TrackerType {
c7774afc93e3 8196907: Fix minimal vm
coleenp
parents: 48865
diff changeset
    40
     uncommit,
c7774afc93e3 8196907: Fix minimal vm
coleenp
parents: 48865
diff changeset
    41
     release
c7774afc93e3 8196907: Fix minimal vm
coleenp
parents: 48865
diff changeset
    42
  };
c7774afc93e3 8196907: Fix minimal vm
coleenp
parents: 48865
diff changeset
    43
  Tracker(enum TrackerType type) : _type(type) { }
c7774afc93e3 8196907: Fix minimal vm
coleenp
parents: 48865
diff changeset
    44
  void record(address addr, size_t size);
c7774afc93e3 8196907: Fix minimal vm
coleenp
parents: 48865
diff changeset
    45
 private:
c7774afc93e3 8196907: Fix minimal vm
coleenp
parents: 48865
diff changeset
    46
  enum TrackerType  _type;
13975
2f7431485cfa 7189254: Change makefiles for more flexibility to override defaults
jprovino
parents: 13861
diff changeset
    47
};
2f7431485cfa 7189254: Change makefiles for more flexibility to override defaults
jprovino
parents: 13861
diff changeset
    48
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    49
class MemTracker : AllStatic {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    50
 public:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    51
  static inline NMT_TrackingLevel tracking_level() { return NMT_off; }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    52
  static inline void shutdown() { }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    53
  static inline void init() { }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    54
  static bool check_launcher_nmt_support(const char* value) { return true; }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    55
  static bool verify_nmt_option() { return true; }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    56
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    57
  static inline void* record_malloc(void* mem_base, size_t size, MEMFLAGS flag,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    58
    const NativeCallStack& stack, NMT_TrackingLevel level) { return mem_base; }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    59
  static inline size_t malloc_header_size(NMT_TrackingLevel level) { return 0; }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    60
  static inline size_t malloc_header_size(void* memblock) { return 0; }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    61
  static inline void* malloc_base(void* memblock) { return memblock; }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    62
  static inline void* record_free(void* memblock) { return memblock; }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    63
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    64
  static inline void record_new_arena(MEMFLAGS flag) { }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    65
  static inline void record_arena_free(MEMFLAGS flag) { }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    66
  static inline void record_arena_size_change(int diff, MEMFLAGS flag) { }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    67
  static inline void record_virtual_memory_reserve(void* addr, size_t size, const NativeCallStack& stack,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    68
                       MEMFLAGS flag = mtNone) { }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    69
  static inline void record_virtual_memory_reserve_and_commit(void* addr, size_t size,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    70
    const NativeCallStack& stack, MEMFLAGS flag = mtNone) { }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    71
  static inline void record_virtual_memory_commit(void* addr, size_t size, const NativeCallStack& stack) { }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    72
  static inline void record_virtual_memory_type(void* addr, MEMFLAGS flag) { }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    73
  static inline void record_thread_stack(void* addr, size_t size) { }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    74
  static inline void release_thread_stack(void* addr, size_t size) { }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    75
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    76
  static void final_report(outputStream*) { }
27162
0a4a7276949b 8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents: 26144
diff changeset
    77
  static void error_report(outputStream*) { }
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    78
};
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    79
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    80
#else
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    81
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    82
#include "runtime/threadCritical.hpp"
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    83
#include "services/mallocTracker.hpp"
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    84
#include "services/virtualMemoryTracker.hpp"
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    85
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    86
extern volatile bool NMT_stack_walkable;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    87
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    88
#define CURRENT_PC ((MemTracker::tracking_level() == NMT_detail && NMT_stack_walkable) ? \
26144
35e11b657728 8055007: NMT2: emptyStack missing in minimal build
zgu
parents: 25946
diff changeset
    89
                    NativeCallStack(0, true) : NativeCallStack::EMPTY_STACK)
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    90
#define CALLER_PC  ((MemTracker::tracking_level() == NMT_detail && NMT_stack_walkable) ?  \
26144
35e11b657728 8055007: NMT2: emptyStack missing in minimal build
zgu
parents: 25946
diff changeset
    91
                    NativeCallStack(1, true) : NativeCallStack::EMPTY_STACK)
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    92
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    93
class MemBaseline;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    94
class Mutex;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    95
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    96
// Tracker is used for guarding 'release' semantics of virtual memory operation, to avoid
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    97
// the other thread obtains and records the same region that is just 'released' by current
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    98
// thread but before it can record the operation.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
    99
class Tracker : public StackObj {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   100
 public:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   101
  enum TrackerType {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   102
     uncommit,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   103
     release
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   104
  };
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   105
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   106
 public:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   107
  Tracker(enum TrackerType type) : _type(type) { }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   108
  void record(address addr, size_t size);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   109
 private:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   110
  enum TrackerType  _type;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   111
  // Virtual memory tracking data structures are protected by ThreadCritical lock.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   112
  ThreadCritical    _tc;
13975
2f7431485cfa 7189254: Change makefiles for more flexibility to override defaults
jprovino
parents: 13861
diff changeset
   113
};
2f7431485cfa 7189254: Change makefiles for more flexibility to override defaults
jprovino
parents: 13861
diff changeset
   114
2f7431485cfa 7189254: Change makefiles for more flexibility to override defaults
jprovino
parents: 13861
diff changeset
   115
class MemTracker : AllStatic {
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   116
 public:
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   117
  static inline NMT_TrackingLevel tracking_level() {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   118
    if (_tracking_level == NMT_unknown) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   119
      // No fencing is needed here, since JVM is in single-threaded
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   120
      // mode.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   121
      _tracking_level = init_tracking_level();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   122
      _cmdline_tracking_level = _tracking_level;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   123
    }
13861
619cff4bfb8b 7188594: Print statistic collected by NMT with VM flag
zgu
parents: 13859
diff changeset
   124
    return _tracking_level;
619cff4bfb8b 7188594: Print statistic collected by NMT with VM flag
zgu
parents: 13859
diff changeset
   125
  }
619cff4bfb8b 7188594: Print statistic collected by NMT with VM flag
zgu
parents: 13859
diff changeset
   126
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   127
  // A late initialization, for the stuff(s) can not be
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   128
  // done in init_tracking_level(), which can NOT malloc
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   129
  // any memory.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   130
  static void init();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   131
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   132
  // Shutdown native memory tracking
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   133
  static void shutdown();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   134
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   135
  // Verify native memory tracking command line option.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   136
  // This check allows JVM to detect if compatible launcher
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   137
  // is used.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   138
  // If an incompatible launcher is used, NMT may not be
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   139
  // able to start, even it is enabled by command line option.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   140
  // A warning message should be given if it is encountered.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   141
  static bool check_launcher_nmt_support(const char* value);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   142
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   143
  // This method checks native memory tracking environment
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   144
  // variable value passed by launcher.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   145
  // Launcher only obligated to pass native memory tracking
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   146
  // option value, but not obligated to validate the value,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   147
  // and launcher has option to discard native memory tracking
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   148
  // option from the command line once it sets up the environment
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   149
  // variable, so NMT has to catch the bad value here.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   150
  static bool verify_nmt_option();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   151
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   152
  // Transition the tracking level to specified level
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   153
  static bool transition_to(NMT_TrackingLevel level);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   154
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   155
  static inline void* record_malloc(void* mem_base, size_t size, MEMFLAGS flag,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   156
    const NativeCallStack& stack, NMT_TrackingLevel level) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   157
    return MallocTracker::record_malloc(mem_base, size, flag, stack, level);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   158
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   159
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   160
  static inline size_t malloc_header_size(NMT_TrackingLevel level) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   161
    return MallocTracker::malloc_header_size(level);
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   162
  }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   163
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   164
  static size_t malloc_header_size(void* memblock) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   165
    if (tracking_level() != NMT_off) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   166
      return MallocTracker::get_header_size(memblock);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   167
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   168
    return 0;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   169
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   170
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   171
  // To malloc base address, which is the starting address
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   172
  // of malloc tracking header if tracking is enabled.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   173
  // Otherwise, it returns the same address.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   174
  static void* malloc_base(void* memblock);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   175
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   176
  // Record malloc free and return malloc base address
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   177
  static inline void* record_free(void* memblock) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   178
    return MallocTracker::record_free(memblock);
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   179
  }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   180
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   181
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   182
  // Record creation of an arena
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   183
  static inline void record_new_arena(MEMFLAGS flag) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   184
    if (tracking_level() < NMT_summary) return;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   185
    MallocTracker::record_new_arena(flag);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   186
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   187
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   188
  // Record destruction of an arena
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   189
  static inline void record_arena_free(MEMFLAGS flag) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   190
    if (tracking_level() < NMT_summary) return;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   191
    MallocTracker::record_arena_free(flag);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   192
  }
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   193
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   194
  // Record arena size change. Arena size is the size of all arena
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   195
  // chuncks that backing up the arena.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   196
  static inline void record_arena_size_change(int diff, MEMFLAGS flag) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   197
    if (tracking_level() < NMT_summary) return;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   198
    MallocTracker::record_arena_size_change(diff, flag);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   199
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   200
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   201
  static inline void record_virtual_memory_reserve(void* addr, size_t size, const NativeCallStack& stack,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   202
    MEMFLAGS flag = mtNone) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   203
    if (tracking_level() < NMT_summary) return;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   204
    if (addr != NULL) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   205
      ThreadCritical tc;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   206
      // Recheck to avoid potential racing during NMT shutdown
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   207
      if (tracking_level() < NMT_summary) return;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   208
      VirtualMemoryTracker::add_reserved_region((address)addr, size, stack, flag);
16436
6b67e51e3cfb 8009298: NMT: Special version of class loading/unloading with runThese stresses out NMT
zgu
parents: 15452
diff changeset
   209
    }
6b67e51e3cfb 8009298: NMT: Special version of class loading/unloading with runThese stresses out NMT
zgu
parents: 15452
diff changeset
   210
  }
6b67e51e3cfb 8009298: NMT: Special version of class loading/unloading with runThese stresses out NMT
zgu
parents: 15452
diff changeset
   211
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   212
  static inline void record_virtual_memory_reserve_and_commit(void* addr, size_t size,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   213
    const NativeCallStack& stack, MEMFLAGS flag = mtNone) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   214
    if (tracking_level() < NMT_summary) return;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   215
    if (addr != NULL) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   216
      ThreadCritical tc;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   217
      if (tracking_level() < NMT_summary) return;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   218
      VirtualMemoryTracker::add_reserved_region((address)addr, size,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   219
        stack, flag, true);
46976
6d4058ac744f 8186748: NMT: memTracker::record_virtual_memory_reserve_and_commit() does not tag the memory as committed
zgu
parents: 40655
diff changeset
   220
      VirtualMemoryTracker::add_committed_region((address)addr, size, stack);
19546
f6b7c9e96ea3 8007074: SIGSEGV at ParMarkBitMap::verify_clear()
stefank
parents: 18681
diff changeset
   221
    }
f6b7c9e96ea3 8007074: SIGSEGV at ParMarkBitMap::verify_clear()
stefank
parents: 18681
diff changeset
   222
  }
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   223
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   224
  static inline void record_virtual_memory_commit(void* addr, size_t size,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   225
    const NativeCallStack& stack) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   226
    if (tracking_level() < NMT_summary) return;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   227
    if (addr != NULL) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   228
      ThreadCritical tc;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   229
      if (tracking_level() < NMT_summary) return;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   230
      VirtualMemoryTracker::add_committed_region((address)addr, size, stack);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   231
    }
18086
f44cf213a775 8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents: 16991
diff changeset
   232
  }
f44cf213a775 8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents: 16991
diff changeset
   233
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   234
  static inline void record_virtual_memory_type(void* addr, MEMFLAGS flag) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   235
    if (tracking_level() < NMT_summary) return;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   236
    if (addr != NULL) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   237
      ThreadCritical tc;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   238
      if (tracking_level() < NMT_summary) return;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   239
      VirtualMemoryTracker::set_reserved_region_type((address)addr, flag);
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   240
    }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   241
  }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   242
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   243
  static inline void record_thread_stack(void* addr, size_t size) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   244
    if (tracking_level() < NMT_summary) return;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   245
    if (addr != NULL) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   246
      // uses thread stack malloc slot for book keeping number of threads
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   247
      MallocMemorySummary::record_malloc(0, mtThreadStack);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   248
      record_virtual_memory_reserve_and_commit(addr, size, CALLER_PC, mtThreadStack);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   249
    }
18086
f44cf213a775 8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents: 16991
diff changeset
   250
  }
f44cf213a775 8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents: 16991
diff changeset
   251
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   252
  static inline void release_thread_stack(void* addr, size_t size) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   253
    if (tracking_level() < NMT_summary) return;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   254
    if (addr != NULL) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   255
      // uses thread stack malloc slot for book keeping number of threads
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   256
      MallocMemorySummary::record_free(0, mtThreadStack);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   257
      ThreadCritical tc;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   258
      if (tracking_level() < NMT_summary) return;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   259
      VirtualMemoryTracker::remove_released_region((address)addr, size);
18086
f44cf213a775 8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents: 16991
diff changeset
   260
    }
f44cf213a775 8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents: 16991
diff changeset
   261
  }
f44cf213a775 8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents: 16991
diff changeset
   262
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   263
  // Query lock is used to synchronize the access to tracking data.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   264
  // So far, it is only used by JCmd query, but it may be used by
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   265
  // other tools.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   266
  static inline Mutex* query_lock() { return _query_lock; }
18086
f44cf213a775 8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents: 16991
diff changeset
   267
27162
0a4a7276949b 8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents: 26144
diff changeset
   268
  // Make a final report or report for hs_err file.
0a4a7276949b 8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents: 26144
diff changeset
   269
  static void error_report(outputStream* output) {
0a4a7276949b 8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents: 26144
diff changeset
   270
    if (tracking_level() >= NMT_summary) {
0a4a7276949b 8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents: 26144
diff changeset
   271
      report(true, output);  // just print summary for error case.
0a4a7276949b 8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents: 26144
diff changeset
   272
    }
0a4a7276949b 8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents: 26144
diff changeset
   273
   }
0a4a7276949b 8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents: 26144
diff changeset
   274
0a4a7276949b 8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents: 26144
diff changeset
   275
  static void final_report(outputStream* output) {
0a4a7276949b 8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents: 26144
diff changeset
   276
    NMT_TrackingLevel level = tracking_level();
0a4a7276949b 8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents: 26144
diff changeset
   277
    if (level >= NMT_summary) {
0a4a7276949b 8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents: 26144
diff changeset
   278
      report(level == NMT_summary, output);
0a4a7276949b 8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents: 26144
diff changeset
   279
    }
0a4a7276949b 8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents: 26144
diff changeset
   280
  }
0a4a7276949b 8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents: 26144
diff changeset
   281
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   282
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   283
  // Stored baseline
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   284
  static inline MemBaseline& get_baseline() {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   285
    return _baseline;
18086
f44cf213a775 8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents: 16991
diff changeset
   286
  }
f44cf213a775 8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents: 16991
diff changeset
   287
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   288
  static NMT_TrackingLevel cmdline_tracking_level() {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   289
    return _cmdline_tracking_level;
15452
3bfde2dea09d 8005012: Add WB APIs to better support NMT testing
ctornqvi
parents: 15104
diff changeset
   290
  }
3bfde2dea09d 8005012: Add WB APIs to better support NMT testing
ctornqvi
parents: 15104
diff changeset
   291
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   292
  static void tuning_statistics(outputStream* out);
15452
3bfde2dea09d 8005012: Add WB APIs to better support NMT testing
ctornqvi
parents: 15104
diff changeset
   293
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   294
 private:
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   295
  static NMT_TrackingLevel init_tracking_level();
27162
0a4a7276949b 8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents: 26144
diff changeset
   296
  static void report(bool summary_only, outputStream* output);
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   297
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   298
 private:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   299
  // Tracking level
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   300
  static volatile NMT_TrackingLevel   _tracking_level;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   301
  // If NMT option value passed by launcher through environment
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   302
  // variable is valid
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   303
  static bool                         _is_nmt_env_valid;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   304
  // command line tracking level
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   305
  static NMT_TrackingLevel            _cmdline_tracking_level;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   306
  // Stored baseline
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   307
  static MemBaseline      _baseline;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   308
  // Query lock
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   309
  static Mutex*           _query_lock;
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   310
};
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   311
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   312
#endif // INCLUDE_NMT
13975
2f7431485cfa 7189254: Change makefiles for more flexibility to override defaults
jprovino
parents: 13861
diff changeset
   313
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   314
#endif // SHARE_VM_SERVICES_MEM_TRACKER_HPP
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents: 25477
diff changeset
   315