src/hotspot/share/services/mallocTracker.hpp
author coleenp
Fri, 04 Jan 2019 15:06:01 -0500
changeset 53149 259c36ef27df
parent 49364 601146c66cad
child 53244 9807daeb47c4
permissions -rw-r--r--
8215731: Move forward class definitions out of globalDefinitions.hpp Summary: redistribute the forward declarations to the header files that need them. Reviewed-by: dholmes, lfoltan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     1
/*
48955
e22914003cf0 8194691: Cleanup unnecessary casts in Atomic/OrderAccess uses
kbarrett
parents: 47634
diff changeset
     2
 * Copyright (c) 2014, 2018, 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
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    25
#ifndef SHARE_VM_SERVICES_MALLOC_TRACKER_HPP
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    26
#define SHARE_VM_SERVICES_MALLOC_TRACKER_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"
28953
75f0bfa6ea14 8068451: Kitchensink fails with assert(_size >= sz) failed: Negative size
coleenp
parents: 27615
diff changeset
    32
#include "runtime/threadCritical.hpp"
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    33
#include "services/nmtCommon.hpp"
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    34
#include "utilities/nativeCallStack.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
/*
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    37
 * This counter class counts memory allocation and deallocation,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    38
 * records total memory allocation size and number of allocations.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    39
 * The counters are updated atomically.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    40
 */
49364
601146c66cad 8173070: Remove ValueObj class for allocation subclassing for runtime code
coleenp
parents: 48955
diff changeset
    41
class MemoryCounter {
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    42
 private:
46266
d5ccf56195af 8157709: NMT should use size_t version of Atomic::add
dholmes
parents: 28953
diff changeset
    43
  volatile size_t   _count;
d5ccf56195af 8157709: NMT should use size_t version of Atomic::add
dholmes
parents: 28953
diff changeset
    44
  volatile size_t   _size;
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    45
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    46
  DEBUG_ONLY(size_t   _peak_count;)
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    47
  DEBUG_ONLY(size_t   _peak_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
 public:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    50
  MemoryCounter() : _count(0), _size(0) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    51
    DEBUG_ONLY(_peak_count = 0;)
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    52
    DEBUG_ONLY(_peak_size  = 0;)
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
  inline void allocate(size_t sz) {
46993
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46489
diff changeset
    56
    Atomic::inc(&_count);
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    57
    if (sz > 0) {
46266
d5ccf56195af 8157709: NMT should use size_t version of Atomic::add
dholmes
parents: 28953
diff changeset
    58
      Atomic::add(sz, &_size);
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    59
      DEBUG_ONLY(_peak_size = MAX2(_peak_size, _size));
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    60
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    61
    DEBUG_ONLY(_peak_count = MAX2(_peak_count, _count);)
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    62
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    63
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    64
  inline void deallocate(size_t sz) {
46266
d5ccf56195af 8157709: NMT should use size_t version of Atomic::add
dholmes
parents: 28953
diff changeset
    65
    assert(_count > 0, "Nothing allocated yet");
d5ccf56195af 8157709: NMT should use size_t version of Atomic::add
dholmes
parents: 28953
diff changeset
    66
    assert(_size >= sz, "deallocation > allocated");
46993
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46489
diff changeset
    67
    Atomic::dec(&_count);
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    68
    if (sz > 0) {
47634
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47216
diff changeset
    69
      Atomic::sub(sz, &_size);
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    70
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    71
  }
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 void resize(long sz) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    74
    if (sz != 0) {
46993
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46489
diff changeset
    75
      Atomic::add(size_t(sz), &_size);
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    76
      DEBUG_ONLY(_peak_size = MAX2(_size, _peak_size);)
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
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    80
  inline size_t count() const { return _count; }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    81
  inline size_t size()  const { return _size;  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    82
  DEBUG_ONLY(inline size_t peak_count() const { return _peak_count; })
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    83
  DEBUG_ONLY(inline size_t peak_size()  const { return _peak_size; })
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
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    87
/*
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    88
 * Malloc memory used by a particular subsystem.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    89
 * It includes the memory acquired through os::malloc()
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    90
 * call and arena's backing memory.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    91
 */
49364
601146c66cad 8173070: Remove ValueObj class for allocation subclassing for runtime code
coleenp
parents: 48955
diff changeset
    92
class MallocMemory {
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    93
 private:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    94
  MemoryCounter _malloc;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    95
  MemoryCounter _arena;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    96
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    97
 public:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    98
  MallocMemory() { }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    99
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   100
  inline void record_malloc(size_t sz) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   101
    _malloc.allocate(sz);
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
  inline void record_free(size_t sz) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   105
    _malloc.deallocate(sz);
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
  inline void record_new_arena() {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   109
    _arena.allocate(0);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   110
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   111
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   112
  inline void record_arena_free() {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   113
    _arena.deallocate(0);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   114
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   115
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   116
  inline void record_arena_size_change(long sz) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   117
    _arena.resize(sz);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   118
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   119
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   120
  inline size_t malloc_size()  const { return _malloc.size(); }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   121
  inline size_t malloc_count() const { return _malloc.count();}
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   122
  inline size_t arena_size()   const { return _arena.size();  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   123
  inline size_t arena_count()  const { return _arena.count(); }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   124
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   125
  DEBUG_ONLY(inline const MemoryCounter& malloc_counter() const { return _malloc; })
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   126
  DEBUG_ONLY(inline const MemoryCounter& arena_counter()  const { return _arena;  })
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   127
};
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   128
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   129
class MallocMemorySummary;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   130
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   131
// A snapshot of malloc'd memory, includes malloc memory
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   132
// usage by types and memory used by tracking itself.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   133
class MallocMemorySnapshot : public ResourceObj {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   134
  friend class MallocMemorySummary;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   135
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   136
 private:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   137
  MallocMemory      _malloc[mt_number_of_types];
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   138
  MemoryCounter     _tracking_header;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   139
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   140
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   141
 public:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   142
  inline MallocMemory*  by_type(MEMFLAGS flags) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   143
    int index = NMTUtil::flag_to_index(flags);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   144
    return &_malloc[index];
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
  inline MallocMemory* by_index(int index) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   148
    assert(index >= 0, "Index out of bound");
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   149
    assert(index < mt_number_of_types, "Index out of bound");
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   150
    return &_malloc[index];
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   151
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   152
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   153
  inline MemoryCounter* malloc_overhead() {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   154
    return &_tracking_header;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   155
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   156
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   157
  // Total malloc'd memory amount
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   158
  size_t total() const;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   159
  // Total malloc'd memory used by arenas
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   160
  size_t total_arena() const;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   161
26288
631bf42cf7c2 8054546: NMT2 leaks memory
zgu
parents: 25946
diff changeset
   162
  inline size_t thread_count() const {
631bf42cf7c2 8054546: NMT2 leaks memory
zgu
parents: 25946
diff changeset
   163
    MallocMemorySnapshot* s = const_cast<MallocMemorySnapshot*>(this);
631bf42cf7c2 8054546: NMT2 leaks memory
zgu
parents: 25946
diff changeset
   164
    return s->by_type(mtThreadStack)->malloc_count();
25946
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
  void copy_to(MallocMemorySnapshot* s) {
28953
75f0bfa6ea14 8068451: Kitchensink fails with assert(_size >= sz) failed: Negative size
coleenp
parents: 27615
diff changeset
   168
    // Need to make sure that mtChunks don't get deallocated while the
75f0bfa6ea14 8068451: Kitchensink fails with assert(_size >= sz) failed: Negative size
coleenp
parents: 27615
diff changeset
   169
    // copy is going on, because their size is adjusted using this
75f0bfa6ea14 8068451: Kitchensink fails with assert(_size >= sz) failed: Negative size
coleenp
parents: 27615
diff changeset
   170
    // buffer in make_adjustment().
75f0bfa6ea14 8068451: Kitchensink fails with assert(_size >= sz) failed: Negative size
coleenp
parents: 27615
diff changeset
   171
    ThreadCritical tc;
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   172
    s->_tracking_header = _tracking_header;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   173
    for (int index = 0; index < mt_number_of_types; index ++) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   174
      s->_malloc[index] = _malloc[index];
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   175
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   176
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   177
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   178
  // Make adjustment by subtracting chunks used by arenas
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   179
  // from total chunks to get total free chunk size
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   180
  void make_adjustment();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   181
};
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   182
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   183
/*
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   184
 * This class is for collecting malloc statistics at summary level
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   185
 */
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   186
class MallocMemorySummary : AllStatic {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   187
 private:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   188
  // Reserve memory for placement of MallocMemorySnapshot object
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   189
  static size_t _snapshot[CALC_OBJ_SIZE_IN_TYPE(MallocMemorySnapshot, size_t)];
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   190
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   191
 public:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   192
   static void initialize();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   193
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   194
   static inline void record_malloc(size_t size, MEMFLAGS flag) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   195
     as_snapshot()->by_type(flag)->record_malloc(size);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   196
   }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   197
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   198
   static inline void record_free(size_t size, MEMFLAGS flag) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   199
     as_snapshot()->by_type(flag)->record_free(size);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   200
   }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   201
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   202
   static inline void record_new_arena(MEMFLAGS flag) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   203
     as_snapshot()->by_type(flag)->record_new_arena();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   204
   }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   205
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   206
   static inline void record_arena_free(MEMFLAGS flag) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   207
     as_snapshot()->by_type(flag)->record_arena_free();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   208
   }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   209
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   210
   static inline void record_arena_size_change(long size, MEMFLAGS flag) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   211
     as_snapshot()->by_type(flag)->record_arena_size_change(size);
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
   static void snapshot(MallocMemorySnapshot* s) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   215
     as_snapshot()->copy_to(s);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   216
     s->make_adjustment();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   217
   }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   218
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   219
   // Record memory used by malloc tracking header
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   220
   static inline void record_new_malloc_header(size_t sz) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   221
     as_snapshot()->malloc_overhead()->allocate(sz);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   222
   }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   223
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   224
   static inline void record_free_malloc_header(size_t sz) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   225
     as_snapshot()->malloc_overhead()->deallocate(sz);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   226
   }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   227
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   228
   // The memory used by malloc tracking headers
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   229
   static inline size_t tracking_overhead() {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   230
     return as_snapshot()->malloc_overhead()->size();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   231
   }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   232
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   233
  static MallocMemorySnapshot* as_snapshot() {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   234
    return (MallocMemorySnapshot*)_snapshot;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   235
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   236
};
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   237
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   238
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   239
/*
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   240
 * Malloc tracking header.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   241
 * To satisfy malloc alignment requirement, NMT uses 2 machine words for tracking purpose,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   242
 * which ensures 8-bytes alignment on 32-bit systems and 16-bytes on 64-bit systems (Product build).
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   243
 */
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   244
49364
601146c66cad 8173070: Remove ValueObj class for allocation subclassing for runtime code
coleenp
parents: 48955
diff changeset
   245
class MallocHeader {
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   246
#ifdef _LP64
27469
34008cfb8994 8058251: assert(_count > 0) failed: Negative counter when running runtime/NMT/MallocTrackingVerify.java
ctornqvi
parents: 27162
diff changeset
   247
  size_t           _size      : 64;
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   248
  size_t           _flags     : 8;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   249
  size_t           _pos_idx   : 16;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   250
  size_t           _bucket_idx: 40;
27615
d7caba003432 8062870: src/share/vm/services/mallocTracker.hpp:64 assert(_count > 0) failed: Negative counter
coleenp
parents: 27469
diff changeset
   251
#define MAX_MALLOCSITE_TABLE_SIZE right_n_bits(40)
d7caba003432 8062870: src/share/vm/services/mallocTracker.hpp:64 assert(_count > 0) failed: Negative counter
coleenp
parents: 27469
diff changeset
   252
#define MAX_BUCKET_LENGTH         right_n_bits(16)
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   253
#else
27469
34008cfb8994 8058251: assert(_count > 0) failed: Negative counter when running runtime/NMT/MallocTrackingVerify.java
ctornqvi
parents: 27162
diff changeset
   254
  size_t           _size      : 32;
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   255
  size_t           _flags     : 8;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   256
  size_t           _pos_idx   : 8;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   257
  size_t           _bucket_idx: 16;
27615
d7caba003432 8062870: src/share/vm/services/mallocTracker.hpp:64 assert(_count > 0) failed: Negative counter
coleenp
parents: 27469
diff changeset
   258
#define MAX_MALLOCSITE_TABLE_SIZE  right_n_bits(16)
d7caba003432 8062870: src/share/vm/services/mallocTracker.hpp:64 assert(_count > 0) failed: Negative counter
coleenp
parents: 27469
diff changeset
   259
#define MAX_BUCKET_LENGTH          right_n_bits(8)
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   260
#endif  // _LP64
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   261
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   262
 public:
27469
34008cfb8994 8058251: assert(_count > 0) failed: Negative counter when running runtime/NMT/MallocTrackingVerify.java
ctornqvi
parents: 27162
diff changeset
   263
  MallocHeader(size_t size, MEMFLAGS flags, const NativeCallStack& stack, NMT_TrackingLevel level) {
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   264
    assert(sizeof(MallocHeader) == sizeof(void*) * 2,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   265
      "Wrong header size");
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   266
27469
34008cfb8994 8058251: assert(_count > 0) failed: Negative counter when running runtime/NMT/MallocTrackingVerify.java
ctornqvi
parents: 27162
diff changeset
   267
    if (level == NMT_minimal) {
34008cfb8994 8058251: assert(_count > 0) failed: Negative counter when running runtime/NMT/MallocTrackingVerify.java
ctornqvi
parents: 27162
diff changeset
   268
      return;
34008cfb8994 8058251: assert(_count > 0) failed: Negative counter when running runtime/NMT/MallocTrackingVerify.java
ctornqvi
parents: 27162
diff changeset
   269
    }
34008cfb8994 8058251: assert(_count > 0) failed: Negative counter when running runtime/NMT/MallocTrackingVerify.java
ctornqvi
parents: 27162
diff changeset
   270
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   271
    _flags = flags;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   272
    set_size(size);
27469
34008cfb8994 8058251: assert(_count > 0) failed: Negative counter when running runtime/NMT/MallocTrackingVerify.java
ctornqvi
parents: 27162
diff changeset
   273
    if (level == NMT_detail) {
34008cfb8994 8058251: assert(_count > 0) failed: Negative counter when running runtime/NMT/MallocTrackingVerify.java
ctornqvi
parents: 27162
diff changeset
   274
      size_t bucket_idx;
34008cfb8994 8058251: assert(_count > 0) failed: Negative counter when running runtime/NMT/MallocTrackingVerify.java
ctornqvi
parents: 27162
diff changeset
   275
      size_t pos_idx;
46489
40abcea5a9d5 8139673: NMT stack traces in output should show mtcomponent
zgu
parents: 46266
diff changeset
   276
      if (record_malloc_site(stack, size, &bucket_idx, &pos_idx, flags)) {
27469
34008cfb8994 8058251: assert(_count > 0) failed: Negative counter when running runtime/NMT/MallocTrackingVerify.java
ctornqvi
parents: 27162
diff changeset
   277
        assert(bucket_idx <= MAX_MALLOCSITE_TABLE_SIZE, "Overflow bucket index");
34008cfb8994 8058251: assert(_count > 0) failed: Negative counter when running runtime/NMT/MallocTrackingVerify.java
ctornqvi
parents: 27162
diff changeset
   278
        assert(pos_idx <= MAX_BUCKET_LENGTH, "Overflow bucket position index");
34008cfb8994 8058251: assert(_count > 0) failed: Negative counter when running runtime/NMT/MallocTrackingVerify.java
ctornqvi
parents: 27162
diff changeset
   279
        _bucket_idx = bucket_idx;
34008cfb8994 8058251: assert(_count > 0) failed: Negative counter when running runtime/NMT/MallocTrackingVerify.java
ctornqvi
parents: 27162
diff changeset
   280
        _pos_idx = pos_idx;
34008cfb8994 8058251: assert(_count > 0) failed: Negative counter when running runtime/NMT/MallocTrackingVerify.java
ctornqvi
parents: 27162
diff changeset
   281
      }
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   282
    }
27469
34008cfb8994 8058251: assert(_count > 0) failed: Negative counter when running runtime/NMT/MallocTrackingVerify.java
ctornqvi
parents: 27162
diff changeset
   283
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   284
    MallocMemorySummary::record_malloc(size, flags);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   285
    MallocMemorySummary::record_new_malloc_header(sizeof(MallocHeader));
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   286
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   287
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   288
  inline size_t   size()  const { return _size; }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   289
  inline MEMFLAGS flags() const { return (MEMFLAGS)_flags; }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   290
  bool get_stack(NativeCallStack& stack) const;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   291
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   292
  // Cleanup tracking information before the memory is released.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   293
  void release() const;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   294
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   295
 private:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   296
  inline void set_size(size_t size) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   297
    _size = size;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   298
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   299
  bool record_malloc_site(const NativeCallStack& stack, size_t size,
46489
40abcea5a9d5 8139673: NMT stack traces in output should show mtcomponent
zgu
parents: 46266
diff changeset
   300
    size_t* bucket_idx, size_t* pos_idx, MEMFLAGS flags) const;
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   301
};
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   302
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   303
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   304
// Main class called from MemTracker to track malloc activities
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   305
class MallocTracker : AllStatic {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   306
 public:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   307
  // Initialize malloc tracker for specific tracking level
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   308
  static bool initialize(NMT_TrackingLevel level);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   309
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   310
  static bool transition(NMT_TrackingLevel from, NMT_TrackingLevel to);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   311
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   312
  // malloc tracking header size for specific tracking level
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   313
  static inline size_t malloc_header_size(NMT_TrackingLevel level) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   314
    return (level == NMT_off) ? 0 : sizeof(MallocHeader);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   315
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   316
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   317
  // Parameter name convention:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   318
  // memblock :   the beginning address for user data
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   319
  // malloc_base: the beginning address that includes malloc tracking header
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   320
  //
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   321
  // The relationship:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   322
  // memblock = (char*)malloc_base + sizeof(nmt header)
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   323
  //
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   324
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   325
  // Record  malloc on specified memory block
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   326
  static void* record_malloc(void* malloc_base, size_t size, MEMFLAGS flags,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   327
    const NativeCallStack& stack, NMT_TrackingLevel level);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   328
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   329
  // Record free on specified memory block
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   330
  static void* record_free(void* memblock);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   331
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   332
  // Offset memory address to header address
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   333
  static inline void* get_base(void* memblock);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   334
  static inline void* get_base(void* memblock, NMT_TrackingLevel level) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   335
    if (memblock == NULL || level == NMT_off) return memblock;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   336
    return (char*)memblock - malloc_header_size(level);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   337
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   338
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   339
  // Get memory size
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   340
  static inline size_t get_size(void* memblock) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   341
    MallocHeader* header = malloc_header(memblock);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   342
    return header->size();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   343
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   344
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   345
  // Get memory type
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   346
  static inline MEMFLAGS get_flags(void* memblock) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   347
    MallocHeader* header = malloc_header(memblock);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   348
    return header->flags();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   349
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   350
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   351
  // Get header size
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   352
  static inline size_t get_header_size(void* memblock) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   353
    return (memblock == NULL) ? 0 : sizeof(MallocHeader);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   354
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   355
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   356
  static inline void record_new_arena(MEMFLAGS flags) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   357
    MallocMemorySummary::record_new_arena(flags);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   358
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   359
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   360
  static inline void record_arena_free(MEMFLAGS flags) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   361
    MallocMemorySummary::record_arena_free(flags);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   362
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   363
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   364
  static inline void record_arena_size_change(int size, MEMFLAGS flags) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   365
    MallocMemorySummary::record_arena_size_change(size, flags);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   366
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   367
 private:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   368
  static inline MallocHeader* malloc_header(void *memblock) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   369
    assert(memblock != NULL, "NULL pointer");
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   370
    MallocHeader* header = (MallocHeader*)((char*)memblock - sizeof(MallocHeader));
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   371
    return header;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   372
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   373
};
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   374
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   375
#endif // INCLUDE_NMT
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   376
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   377
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   378
#endif //SHARE_VM_SERVICES_MALLOC_TRACKER_HPP