src/hotspot/share/services/mallocSiteTable.hpp
author phh
Sat, 30 Nov 2019 14:33:05 -0800
changeset 59330 5b96c12f909d
parent 59290 97d13893ec3c
permissions -rw-r--r--
8234541: C1 emits an empty message when it inlines successfully Summary: Use "inline" as the message when successfull Reviewed-by: thartmann, mdoerr Contributed-by: navy.xliu@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     1
/*
53244
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 51334
diff changeset
     2
 * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     4
 *
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     7
 * published by the Free Software Foundation.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     8
 *
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    13
 * accompanied this code).
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    14
 *
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    18
 *
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    21
 * questions.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    22
 *
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    23
 */
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    24
53244
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 51334
diff changeset
    25
#ifndef SHARE_SERVICES_MALLOCSITETABLE_HPP
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 51334
diff changeset
    26
#define SHARE_SERVICES_MALLOCSITETABLE_HPP
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    27
59290
97d13893ec3c 8234748: Clean up atomic and orderAccess includes
stefank
parents: 59249
diff changeset
    28
#include "utilities/macros.hpp"
97d13893ec3c 8234748: Clean up atomic and orderAccess includes
stefank
parents: 59249
diff changeset
    29
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    30
#if INCLUDE_NMT
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    31
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    32
#include "memory/allocation.hpp"
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    33
#include "runtime/atomic.hpp"
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    34
#include "services/allocationSite.hpp"
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    35
#include "services/mallocTracker.hpp"
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    36
#include "services/nmtCommon.hpp"
26144
35e11b657728 8055007: NMT2: emptyStack missing in minimal build
zgu
parents: 25946
diff changeset
    37
#include "utilities/nativeCallStack.hpp"
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    38
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    39
// MallocSite represents a code path that eventually calls
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    40
// os::malloc() to allocate memory
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    41
class MallocSite : public AllocationSite<MemoryCounter> {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    42
 public:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    43
  MallocSite() :
53685
df83034c9275 8218558: NMT stack traces in output should show mt component for virtual memory allocations
zgu
parents: 53244
diff changeset
    44
    AllocationSite<MemoryCounter>(NativeCallStack::empty_stack(), mtNone) {}
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    45
46489
40abcea5a9d5 8139673: NMT stack traces in output should show mtcomponent
zgu
parents: 46264
diff changeset
    46
  MallocSite(const NativeCallStack& stack, MEMFLAGS flags) :
53685
df83034c9275 8218558: NMT stack traces in output should show mt component for virtual memory allocations
zgu
parents: 53244
diff changeset
    47
    AllocationSite<MemoryCounter>(stack, flags) {}
46489
40abcea5a9d5 8139673: NMT stack traces in output should show mtcomponent
zgu
parents: 46264
diff changeset
    48
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    49
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    50
  void allocate(size_t size)      { data()->allocate(size);   }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    51
  void deallocate(size_t size)    { data()->deallocate(size); }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    52
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    53
  // Memory allocated from this code path
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    54
  size_t size()  const { return peek()->size(); }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    55
  // The number of calls were made
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    56
  size_t count() const { return peek()->count(); }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    57
};
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    58
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    59
// Malloc site hashtable entry
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    60
class MallocSiteHashtableEntry : public CHeapObj<mtNMT> {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    61
 private:
47634
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47216
diff changeset
    62
  MallocSite                         _malloc_site;
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47216
diff changeset
    63
  MallocSiteHashtableEntry* volatile _next;
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    64
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    65
 public:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    66
  MallocSiteHashtableEntry() : _next(NULL) { }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    67
46489
40abcea5a9d5 8139673: NMT stack traces in output should show mtcomponent
zgu
parents: 46264
diff changeset
    68
  MallocSiteHashtableEntry(NativeCallStack stack, MEMFLAGS flags):
40abcea5a9d5 8139673: NMT stack traces in output should show mtcomponent
zgu
parents: 46264
diff changeset
    69
    _malloc_site(stack, flags), _next(NULL) {
40abcea5a9d5 8139673: NMT stack traces in output should show mtcomponent
zgu
parents: 46264
diff changeset
    70
    assert(flags != mtNone, "Expect a real memory type");
40abcea5a9d5 8139673: NMT stack traces in output should show mtcomponent
zgu
parents: 46264
diff changeset
    71
  }
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    72
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    73
  inline const MallocSiteHashtableEntry* next() const {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    74
    return _next;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    75
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    76
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    77
  // Insert an entry atomically.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    78
  // Return true if the entry is inserted successfully.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    79
  // The operation can be failed due to contention from other thread.
47634
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47216
diff changeset
    80
  bool atomic_insert(MallocSiteHashtableEntry* entry);
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    81
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    82
  void set_callsite(const MallocSite& site) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    83
    _malloc_site = site;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    84
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    85
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    86
  inline const MallocSite* peek() const { return &_malloc_site; }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    87
  inline MallocSite* data()             { return &_malloc_site; }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    88
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    89
  inline long hash() const { return _malloc_site.hash(); }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    90
  inline bool equals(const NativeCallStack& stack) const {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    91
    return _malloc_site.equals(stack);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    92
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    93
  // Allocation/deallocation on this allocation site
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    94
  inline void allocate(size_t size)   { _malloc_site.allocate(size);   }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    95
  inline void deallocate(size_t size) { _malloc_site.deallocate(size); }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    96
  // Memory counters
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    97
  inline size_t size() const  { return _malloc_site.size();  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    98
  inline size_t count() const { return _malloc_site.count(); }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    99
};
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   100
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   101
// The walker walks every entry on MallocSiteTable
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   102
class MallocSiteWalker : public StackObj {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   103
 public:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   104
   virtual bool do_malloc_site(const MallocSite* e) { return false; }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   105
};
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   106
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   107
/*
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   108
 * Native memory tracking call site table.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   109
 * The table is only needed when detail tracking is enabled.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   110
 */
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   111
class MallocSiteTable : AllStatic {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   112
 private:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   113
  // The number of hash bucket in this hashtable. The number should
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   114
  // be tuned if malloc activities changed significantly.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   115
  // The statistics data can be obtained via Jcmd
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   116
  // jcmd <pid> VM.native_memory statistics.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   117
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   118
  // Currently, (number of buckets / number of entires) ratio is
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   119
  // about 1 / 6
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   120
  enum {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   121
    table_base_size = 128,   // The base size is calculated from statistics to give
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   122
                             // table ratio around 1:6
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   123
    table_size = (table_base_size * NMT_TrackingStackDepth - 1)
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   124
  };
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   125
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   126
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   127
  // This is a very special lock, that allows multiple shared accesses (sharedLock), but
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   128
  // once exclusive access (exclusiveLock) is requested, all shared accesses are
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   129
  // rejected forever.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   130
  class AccessLock : public StackObj {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   131
    enum LockState {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   132
      NoLock,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   133
      SharedLock,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   134
      ExclusiveLock
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   135
    };
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   136
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   137
   private:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   138
    // A very large negative number. The only possibility to "overflow"
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   139
    // this number is when there are more than -min_jint threads in
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   140
    // this process, which is not going to happen in foreseeable future.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   141
    const static int _MAGIC_ = min_jint;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   142
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   143
    LockState      _lock_state;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   144
    volatile int*  _lock;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   145
   public:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   146
    AccessLock(volatile int* lock) :
51334
cc2c79d22508 8208671: Runtime, JFR, Serviceability changes to allow enabling -Wreorder
tschatzl
parents: 50965
diff changeset
   147
      _lock_state(NoLock), _lock(lock) {
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   148
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   149
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   150
    ~AccessLock() {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   151
      if (_lock_state == SharedLock) {
48955
e22914003cf0 8194691: Cleanup unnecessary casts in Atomic/OrderAccess uses
kbarrett
parents: 47634
diff changeset
   152
        Atomic::dec(_lock);
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   153
      }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   154
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   155
    // Acquire shared lock.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   156
    // Return true if shared access is granted.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   157
    inline bool sharedLock() {
59249
29b0d0b61615 8234737: Harmonize parameter order in Atomic - add
stefank
parents: 53685
diff changeset
   158
      jint res = Atomic::add(_lock, 1);
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   159
      if (res < 0) {
48955
e22914003cf0 8194691: Cleanup unnecessary casts in Atomic/OrderAccess uses
kbarrett
parents: 47634
diff changeset
   160
        Atomic::dec(_lock);
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   161
        return false;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   162
      }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   163
      _lock_state = SharedLock;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   164
      return true;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   165
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   166
    // Acquire exclusive lock
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   167
    void exclusiveLock();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   168
 };
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   169
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   170
 public:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   171
  static bool initialize();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   172
  static void shutdown();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   173
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   174
  NOT_PRODUCT(static int access_peak_count() { return _peak_count; })
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   175
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   176
  // Number of hash buckets
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   177
  static inline int hash_buckets()      { return (int)table_size; }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   178
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   179
  // Access and copy a call stack from this table. Shared lock should be
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   180
  // acquired before access the entry.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   181
  static inline bool access_stack(NativeCallStack& stack, size_t bucket_idx,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   182
    size_t pos_idx) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   183
    AccessLock locker(&_access_count);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   184
    if (locker.sharedLock()) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   185
      NOT_PRODUCT(_peak_count = MAX2(_peak_count, _access_count);)
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   186
      MallocSite* site = malloc_site(bucket_idx, pos_idx);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   187
      if (site != NULL) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   188
        stack = *site->call_stack();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   189
        return true;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   190
      }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   191
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   192
    return false;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   193
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   194
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   195
  // Record a new allocation from specified call path.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   196
  // Return true if the allocation is recorded successfully, bucket_idx
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   197
  // and pos_idx are also updated to indicate the entry where the allocation
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   198
  // information was recorded.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   199
  // Return false only occurs under rare scenarios:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   200
  //  1. out of memory
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   201
  //  2. overflow hash bucket
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   202
  static inline bool allocation_at(const NativeCallStack& stack, size_t size,
46489
40abcea5a9d5 8139673: NMT stack traces in output should show mtcomponent
zgu
parents: 46264
diff changeset
   203
    size_t* bucket_idx, size_t* pos_idx, MEMFLAGS flags) {
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   204
    AccessLock locker(&_access_count);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   205
    if (locker.sharedLock()) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   206
      NOT_PRODUCT(_peak_count = MAX2(_peak_count, _access_count);)
46489
40abcea5a9d5 8139673: NMT stack traces in output should show mtcomponent
zgu
parents: 46264
diff changeset
   207
      MallocSite* site = lookup_or_add(stack, bucket_idx, pos_idx, flags);
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   208
      if (site != NULL) site->allocate(size);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   209
      return site != NULL;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   210
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   211
    return false;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   212
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   213
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   214
  // Record memory deallocation. bucket_idx and pos_idx indicate where the allocation
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   215
  // information was recorded.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   216
  static inline bool deallocation_at(size_t size, size_t bucket_idx, size_t pos_idx) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   217
    AccessLock locker(&_access_count);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   218
    if (locker.sharedLock()) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   219
      NOT_PRODUCT(_peak_count = MAX2(_peak_count, _access_count);)
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   220
      MallocSite* site = malloc_site(bucket_idx, pos_idx);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   221
      if (site != NULL) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   222
        site->deallocate(size);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   223
        return true;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   224
      }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   225
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   226
    return false;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   227
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   228
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   229
  // Walk this table.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   230
  static bool walk_malloc_site(MallocSiteWalker* walker);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   231
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   232
 private:
46489
40abcea5a9d5 8139673: NMT stack traces in output should show mtcomponent
zgu
parents: 46264
diff changeset
   233
  static MallocSiteHashtableEntry* new_entry(const NativeCallStack& key, MEMFLAGS flags);
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   234
  static void reset();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   235
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   236
  // Delete a bucket linked list
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   237
  static void delete_linked_list(MallocSiteHashtableEntry* head);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   238
46489
40abcea5a9d5 8139673: NMT stack traces in output should show mtcomponent
zgu
parents: 46264
diff changeset
   239
  static MallocSite* lookup_or_add(const NativeCallStack& key, size_t* bucket_idx, size_t* pos_idx, MEMFLAGS flags);
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   240
  static MallocSite* malloc_site(size_t bucket_idx, size_t pos_idx);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   241
  static bool walk(MallocSiteWalker* walker);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   242
29462
f2e2922222a4 8069124: runtime/NMT/MallocSiteHashOverflow.java failing in nightlies
ctornqvi
parents: 26144
diff changeset
   243
  static inline unsigned int hash_to_index(unsigned int hash) {
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   244
    return (hash % table_size);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   245
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   246
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   247
  static inline const NativeCallStack* hash_entry_allocation_stack() {
50965
29eaf3feab30 8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents: 50904
diff changeset
   248
    assert(_hash_entry_allocation_stack != NULL, "Must be set");
29eaf3feab30 8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents: 50904
diff changeset
   249
    return _hash_entry_allocation_stack;
29eaf3feab30 8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents: 50904
diff changeset
   250
  }
29eaf3feab30 8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents: 50904
diff changeset
   251
29eaf3feab30 8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents: 50904
diff changeset
   252
  static inline const MallocSiteHashtableEntry* hash_entry_allocation_site() {
29eaf3feab30 8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents: 50904
diff changeset
   253
    assert(_hash_entry_allocation_site != NULL, "Must be set");
29eaf3feab30 8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents: 50904
diff changeset
   254
    return _hash_entry_allocation_site;
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   255
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   256
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   257
 private:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   258
  // Counter for counting concurrent access
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   259
  static volatile int                _access_count;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   260
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   261
  // The callsite hashtable. It has to be a static table,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   262
  // since malloc call can come from C runtime linker.
50965
29eaf3feab30 8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents: 50904
diff changeset
   263
  static MallocSiteHashtableEntry*        _table[table_size];
29eaf3feab30 8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents: 50904
diff changeset
   264
  static const NativeCallStack*           _hash_entry_allocation_stack;
29eaf3feab30 8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents: 50904
diff changeset
   265
  static const MallocSiteHashtableEntry*  _hash_entry_allocation_site;
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   266
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   267
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   268
  NOT_PRODUCT(static int     _peak_count;)
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   269
};
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   270
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   271
#endif // INCLUDE_NMT
53244
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 51334
diff changeset
   272
#endif // SHARE_SERVICES_MALLOCSITETABLE_HPP