hotspot/src/share/vm/services/mallocSiteTable.hpp
author zgu
Thu, 07 Aug 2014 12:18:58 -0700
changeset 25946 1572c9f03fb9
child 26144 35e11b657728
permissions -rw-r--r--
8046598: Scalable Native memory tracking development Summary: Enhance scalability of native memory tracking Reviewed-by: coleenp, ctornqvi, gtriantafill
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     1
/*
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
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
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    25
#ifndef SHARE_VM_SERVICES_MALLOC_SITE_TABLE_HPP
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    26
#define SHARE_VM_SERVICES_MALLOC_SITE_TABLE_HPP
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    27
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    28
#if INCLUDE_NMT
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    29
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    30
#include "memory/allocation.hpp"
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    31
#include "runtime/atomic.hpp"
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    32
#include "services/allocationSite.hpp"
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    33
#include "services/mallocTracker.hpp"
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    34
#include "services/nmtCommon.hpp"
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    35
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    36
// MallocSite represents a code path that eventually calls
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    37
// os::malloc() to allocate memory
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    38
class MallocSite : public AllocationSite<MemoryCounter> {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    39
 public:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    40
  MallocSite() :
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    41
    AllocationSite<MemoryCounter>(emptyStack) { }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    42
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    43
  MallocSite(const NativeCallStack& stack) :
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    44
    AllocationSite<MemoryCounter>(stack) { }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    45
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    46
  void allocate(size_t size)      { data()->allocate(size);   }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    47
  void deallocate(size_t size)    { data()->deallocate(size); }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    48
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    49
  // Memory allocated from this code path
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    50
  size_t size()  const { return peek()->size(); }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    51
  // The number of calls were made
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    52
  size_t count() const { return peek()->count(); }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    53
};
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    54
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    55
// Malloc site hashtable entry
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    56
class MallocSiteHashtableEntry : public CHeapObj<mtNMT> {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    57
 private:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    58
  MallocSite                _malloc_site;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    59
  MallocSiteHashtableEntry* _next;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    60
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    61
 public:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    62
  MallocSiteHashtableEntry() : _next(NULL) { }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    63
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    64
  MallocSiteHashtableEntry(NativeCallStack stack):
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    65
    _malloc_site(stack), _next(NULL) { }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    66
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    67
  inline const MallocSiteHashtableEntry* next() const {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    68
    return _next;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    69
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    70
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    71
  // Insert an entry atomically.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    72
  // Return true if the entry is inserted successfully.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    73
  // The operation can be failed due to contention from other thread.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    74
  bool atomic_insert(const MallocSiteHashtableEntry* entry) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    75
    return (Atomic::cmpxchg_ptr((void*)entry, (volatile void*)&_next,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    76
      NULL) == NULL);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    77
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    78
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    79
  void set_callsite(const MallocSite& site) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    80
    _malloc_site = site;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    81
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    82
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    83
  inline const MallocSite* peek() const { return &_malloc_site; }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    84
  inline MallocSite* data()             { return &_malloc_site; }
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 long hash() const { return _malloc_site.hash(); }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    87
  inline bool equals(const NativeCallStack& stack) const {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    88
    return _malloc_site.equals(stack);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    89
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    90
  // Allocation/deallocation on this allocation site
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    91
  inline void allocate(size_t size)   { _malloc_site.allocate(size);   }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    92
  inline void deallocate(size_t size) { _malloc_site.deallocate(size); }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    93
  // Memory counters
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    94
  inline size_t size() const  { return _malloc_site.size();  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    95
  inline size_t count() const { return _malloc_site.count(); }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    96
};
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    97
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    98
// The walker walks every entry on MallocSiteTable
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    99
class MallocSiteWalker : public StackObj {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   100
 public:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   101
   virtual bool do_malloc_site(const MallocSite* e) { return false; }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   102
};
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   103
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   104
/*
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   105
 * Native memory tracking call site table.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   106
 * The table is only needed when detail tracking is enabled.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   107
 */
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   108
class MallocSiteTable : AllStatic {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   109
 private:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   110
  // The number of hash bucket in this hashtable. The number should
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   111
  // be tuned if malloc activities changed significantly.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   112
  // The statistics data can be obtained via Jcmd
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   113
  // jcmd <pid> VM.native_memory statistics.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   114
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   115
  // Currently, (number of buckets / number of entires) ratio is
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   116
  // about 1 / 6
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   117
  enum {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   118
    table_base_size = 128,   // The base size is calculated from statistics to give
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   119
                             // table ratio around 1:6
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   120
    table_size = (table_base_size * NMT_TrackingStackDepth - 1)
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   121
  };
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   122
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   123
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   124
  // This is a very special lock, that allows multiple shared accesses (sharedLock), but
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   125
  // once exclusive access (exclusiveLock) is requested, all shared accesses are
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   126
  // rejected forever.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   127
  class AccessLock : public StackObj {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   128
    enum LockState {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   129
      NoLock,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   130
      SharedLock,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   131
      ExclusiveLock
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   132
    };
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   133
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   134
   private:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   135
    // A very large negative number. The only possibility to "overflow"
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   136
    // this number is when there are more than -min_jint threads in
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   137
    // this process, which is not going to happen in foreseeable future.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   138
    const static int _MAGIC_ = min_jint;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   139
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   140
    LockState      _lock_state;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   141
    volatile int*  _lock;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   142
   public:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   143
    AccessLock(volatile int* lock) :
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   144
      _lock(lock), _lock_state(NoLock) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   145
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   146
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   147
    ~AccessLock() {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   148
      if (_lock_state == SharedLock) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   149
        Atomic::dec((volatile jint*)_lock);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   150
      }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   151
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   152
    // Acquire shared lock.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   153
    // Return true if shared access is granted.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   154
    inline bool sharedLock() {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   155
      jint res = Atomic::add(1, _lock);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   156
      if (res < 0) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   157
        Atomic::add(-1, _lock);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   158
        return false;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   159
      }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   160
      _lock_state = SharedLock;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   161
      return true;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   162
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   163
    // Acquire exclusive lock
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   164
    void exclusiveLock();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   165
 };
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   166
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   167
 public:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   168
  static bool initialize();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   169
  static void shutdown();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   170
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   171
  NOT_PRODUCT(static int access_peak_count() { return _peak_count; })
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   172
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   173
  // Number of hash buckets
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   174
  static inline int hash_buckets()      { return (int)table_size; }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   175
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   176
  // Access and copy a call stack from this table. Shared lock should be
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   177
  // acquired before access the entry.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   178
  static inline bool access_stack(NativeCallStack& stack, size_t bucket_idx,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   179
    size_t pos_idx) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   180
    AccessLock locker(&_access_count);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   181
    if (locker.sharedLock()) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   182
      NOT_PRODUCT(_peak_count = MAX2(_peak_count, _access_count);)
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   183
      MallocSite* site = malloc_site(bucket_idx, pos_idx);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   184
      if (site != NULL) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   185
        stack = *site->call_stack();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   186
        return true;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   187
      }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   188
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   189
    return false;
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
  // Record a new allocation from specified call path.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   193
  // Return true if the allocation is recorded successfully, bucket_idx
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   194
  // and pos_idx are also updated to indicate the entry where the allocation
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   195
  // information was recorded.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   196
  // Return false only occurs under rare scenarios:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   197
  //  1. out of memory
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   198
  //  2. overflow hash bucket
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   199
  static inline bool allocation_at(const NativeCallStack& stack, size_t size,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   200
    size_t* bucket_idx, size_t* pos_idx) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   201
    AccessLock locker(&_access_count);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   202
    if (locker.sharedLock()) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   203
      NOT_PRODUCT(_peak_count = MAX2(_peak_count, _access_count);)
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   204
      MallocSite* site = lookup_or_add(stack, bucket_idx, pos_idx);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   205
      if (site != NULL) site->allocate(size);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   206
      return site != NULL;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   207
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   208
    return false;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   209
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   210
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   211
  // Record memory deallocation. bucket_idx and pos_idx indicate where the allocation
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   212
  // information was recorded.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   213
  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
   214
    AccessLock locker(&_access_count);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   215
    if (locker.sharedLock()) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   216
      NOT_PRODUCT(_peak_count = MAX2(_peak_count, _access_count);)
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   217
      MallocSite* site = malloc_site(bucket_idx, pos_idx);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   218
      if (site != NULL) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   219
        site->deallocate(size);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   220
        return true;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   221
      }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   222
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   223
    return false;
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
  // Walk this table.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   227
  static bool walk_malloc_site(MallocSiteWalker* walker);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   228
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   229
 private:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   230
  static MallocSiteHashtableEntry* new_entry(const NativeCallStack& key);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   231
  static void reset();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   232
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   233
  // Delete a bucket linked list
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   234
  static void delete_linked_list(MallocSiteHashtableEntry* head);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   235
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   236
  static MallocSite* lookup_or_add(const NativeCallStack& key, size_t* bucket_idx, size_t* pos_idx);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   237
  static MallocSite* malloc_site(size_t bucket_idx, size_t pos_idx);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   238
  static bool walk(MallocSiteWalker* walker);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   239
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   240
  static inline int hash_to_index(int  hash) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   241
    hash = (hash > 0) ? hash : (-hash);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   242
    return (hash % table_size);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   243
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   244
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   245
  static inline const NativeCallStack* hash_entry_allocation_stack() {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   246
    return (NativeCallStack*)_hash_entry_allocation_stack;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   247
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   248
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   249
 private:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   250
  // Counter for counting concurrent access
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   251
  static volatile int                _access_count;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   252
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   253
  // The callsite hashtable. It has to be a static table,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   254
  // since malloc call can come from C runtime linker.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   255
  static MallocSiteHashtableEntry*   _table[table_size];
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   256
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   257
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   258
  // Reserve enough memory for placing the objects
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   259
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   260
  // The memory for hashtable entry allocation stack object
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   261
  static size_t _hash_entry_allocation_stack[CALC_OBJ_SIZE_IN_TYPE(NativeCallStack, size_t)];
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   262
  // The memory for hashtable entry allocation callsite object
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   263
  static size_t _hash_entry_allocation_site[CALC_OBJ_SIZE_IN_TYPE(MallocSiteHashtableEntry, size_t)];
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   264
  NOT_PRODUCT(static int     _peak_count;)
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   265
};
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   266
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   267
#endif // INCLUDE_NMT
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   268
#endif // SHARE_VM_SERVICES_MALLOC_SITE_TABLE_HPP