hotspot/src/share/vm/services/mallocTracker.hpp
author zgu
Thu, 07 Aug 2014 12:18:58 -0700
changeset 25946 1572c9f03fb9
child 26288 631bf42cf7c2
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_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"
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    32
#include "services/nmtCommon.hpp"
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    33
#include "utilities/nativeCallStack.hpp"
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    34
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    35
/*
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    36
 * This counter class counts memory allocation and deallocation,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    37
 * records total memory allocation size and number of allocations.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    38
 * The counters are updated atomically.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    39
 */
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    40
class MemoryCounter VALUE_OBJ_CLASS_SPEC {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    41
 private:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    42
  size_t   _count;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    43
  size_t   _size;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    44
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    45
  DEBUG_ONLY(size_t   _peak_count;)
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    46
  DEBUG_ONLY(size_t   _peak_size; )
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    47
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    48
 public:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    49
  MemoryCounter() : _count(0), _size(0) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    50
    DEBUG_ONLY(_peak_count = 0;)
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    51
    DEBUG_ONLY(_peak_size  = 0;)
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    52
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    53
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    54
  // Reset counters
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    55
  void reset() {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    56
    _size  = 0;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    57
    _count = 0;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    58
    DEBUG_ONLY(_peak_size = 0;)
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    59
    DEBUG_ONLY(_peak_count = 0;)
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    60
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    61
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    62
  inline void allocate(size_t sz) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    63
    Atomic::add(1, (volatile MemoryCounterType*)&_count);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    64
    if (sz > 0) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    65
      Atomic::add((MemoryCounterType)sz, (volatile MemoryCounterType*)&_size);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    66
      DEBUG_ONLY(_peak_size = MAX2(_peak_size, _size));
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    67
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    68
    DEBUG_ONLY(_peak_count = MAX2(_peak_count, _count);)
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
  inline void deallocate(size_t sz) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    72
    assert(_count > 0, "Negative counter");
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    73
    assert(_size >= sz, "Negative size");
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    74
    Atomic::add(-1, (volatile MemoryCounterType*)&_count);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    75
    if (sz > 0) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    76
      Atomic::add(-(MemoryCounterType)sz, (volatile MemoryCounterType*)&_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 void resize(long sz) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    81
    if (sz != 0) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    82
      Atomic::add((MemoryCounterType)sz, (volatile MemoryCounterType*)&_size);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    83
      DEBUG_ONLY(_peak_size = MAX2(_size, _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
  inline size_t count() const { return _count; }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    88
  inline size_t size()  const { return _size;  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    89
  DEBUG_ONLY(inline size_t peak_count() const { return _peak_count; })
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    90
  DEBUG_ONLY(inline size_t peak_size()  const { return _peak_size; })
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    91
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    92
};
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    93
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    94
/*
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    95
 * Malloc memory used by a particular subsystem.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    96
 * It includes the memory acquired through os::malloc()
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    97
 * call and arena's backing memory.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    98
 */
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    99
class MallocMemory VALUE_OBJ_CLASS_SPEC {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   100
 private:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   101
  MemoryCounter _malloc;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   102
  MemoryCounter _arena;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   103
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   104
 public:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   105
  MallocMemory() { }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   106
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   107
  inline void record_malloc(size_t sz) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   108
    _malloc.allocate(sz);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   109
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   110
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   111
  inline void record_free(size_t sz) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   112
    _malloc.deallocate(sz);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   113
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   114
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   115
  inline void record_new_arena() {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   116
    _arena.allocate(0);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   117
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   118
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   119
  inline void record_arena_free() {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   120
    _arena.deallocate(0);
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
  inline void record_arena_size_change(long sz) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   124
    _arena.resize(sz);
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
  void reset() {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   128
    _malloc.reset();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   129
    _arena.reset();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   130
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   131
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   132
  inline size_t malloc_size()  const { return _malloc.size(); }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   133
  inline size_t malloc_count() const { return _malloc.count();}
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   134
  inline size_t arena_size()   const { return _arena.size();  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   135
  inline size_t arena_count()  const { return _arena.count(); }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   136
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   137
  DEBUG_ONLY(inline const MemoryCounter& malloc_counter() const { return _malloc; })
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   138
  DEBUG_ONLY(inline const MemoryCounter& arena_counter()  const { return _arena;  })
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
class MallocMemorySummary;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   142
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   143
// A snapshot of malloc'd memory, includes malloc memory
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   144
// usage by types and memory used by tracking itself.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   145
class MallocMemorySnapshot : public ResourceObj {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   146
  friend class MallocMemorySummary;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   147
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   148
 private:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   149
  MallocMemory      _malloc[mt_number_of_types];
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   150
  MemoryCounter     _tracking_header;
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
 public:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   154
  inline MallocMemory*  by_type(MEMFLAGS flags) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   155
    int index = NMTUtil::flag_to_index(flags);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   156
    return &_malloc[index];
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   157
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   158
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   159
  inline MallocMemory* by_index(int index) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   160
    assert(index >= 0, "Index out of bound");
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   161
    assert(index < mt_number_of_types, "Index out of bound");
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   162
    return &_malloc[index];
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   163
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   164
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   165
  inline MemoryCounter* malloc_overhead() {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   166
    return &_tracking_header;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   167
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   168
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   169
  // Total malloc'd memory amount
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   170
  size_t total() const;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   171
  // Total malloc'd memory used by arenas
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   172
  size_t total_arena() const;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   173
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   174
  inline size_t thread_count() {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   175
    return by_type(mtThreadStack)->malloc_count();
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
  void reset();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   179
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   180
  void copy_to(MallocMemorySnapshot* s) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   181
    s->_tracking_header = _tracking_header;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   182
    for (int index = 0; index < mt_number_of_types; index ++) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   183
      s->_malloc[index] = _malloc[index];
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   184
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   185
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   186
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   187
  // Make adjustment by subtracting chunks used by arenas
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   188
  // from total chunks to get total free chunk size
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   189
  void make_adjustment();
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
/*
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   193
 * This class is for collecting malloc statistics at summary level
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   194
 */
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   195
class MallocMemorySummary : AllStatic {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   196
 private:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   197
  // Reserve memory for placement of MallocMemorySnapshot object
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   198
  static size_t _snapshot[CALC_OBJ_SIZE_IN_TYPE(MallocMemorySnapshot, size_t)];
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   199
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   200
 public:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   201
   static void initialize();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   202
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   203
   static inline void record_malloc(size_t size, MEMFLAGS flag) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   204
     as_snapshot()->by_type(flag)->record_malloc(size);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   205
   }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   206
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   207
   static inline void record_free(size_t size, MEMFLAGS flag) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   208
     as_snapshot()->by_type(flag)->record_free(size);
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
   static inline void record_new_arena(MEMFLAGS flag) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   212
     as_snapshot()->by_type(flag)->record_new_arena();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   213
   }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   214
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   215
   static inline void record_arena_free(MEMFLAGS flag) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   216
     as_snapshot()->by_type(flag)->record_arena_free();
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
   static inline void record_arena_size_change(long size, MEMFLAGS flag) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   220
     as_snapshot()->by_type(flag)->record_arena_size_change(size);
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
   static void snapshot(MallocMemorySnapshot* s) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   224
     as_snapshot()->copy_to(s);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   225
     s->make_adjustment();
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
   // Record memory used by malloc tracking header
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   229
   static inline void record_new_malloc_header(size_t sz) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   230
     as_snapshot()->malloc_overhead()->allocate(sz);
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 inline void record_free_malloc_header(size_t sz) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   234
     as_snapshot()->malloc_overhead()->deallocate(sz);
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
   // The memory used by malloc tracking headers
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   238
   static inline size_t tracking_overhead() {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   239
     return as_snapshot()->malloc_overhead()->size();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   240
   }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   241
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   242
   // Reset all counters to zero
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   243
   static void reset() {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   244
     as_snapshot()->reset();
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 MallocMemorySnapshot* as_snapshot() {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   248
    return (MallocMemorySnapshot*)_snapshot;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   249
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   250
};
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   251
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   252
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   253
/*
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   254
 * Malloc tracking header.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   255
 * To satisfy malloc alignment requirement, NMT uses 2 machine words for tracking purpose,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   256
 * 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
   257
 */
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   258
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   259
class MallocHeader VALUE_OBJ_CLASS_SPEC {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   260
#ifdef _LP64
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   261
  size_t           _size      : 62;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   262
  size_t           _level     : 2;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   263
  size_t           _flags     : 8;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   264
  size_t           _pos_idx   : 16;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   265
  size_t           _bucket_idx: 40;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   266
#define MAX_MALLOCSITE_TABLE_SIZE ((size_t)1 << 40)
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   267
#define MAX_BUCKET_LENGTH         ((size_t)(1 << 16))
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   268
#define MAX_MALLOC_SIZE           (((size_t)1 << 62) - 1)
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   269
#else
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   270
  size_t           _size      : 30;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   271
  size_t           _level     : 2;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   272
  size_t           _flags     : 8;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   273
  size_t           _pos_idx   : 8;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   274
  size_t           _bucket_idx: 16;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   275
#define MAX_MALLOCSITE_TABLE_SIZE  ((size_t)(1 << 16))
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   276
#define MAX_BUCKET_LENGTH          ((size_t)(1 << 8))
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   277
// Max malloc size = 1GB - 1 on 32 bit system, such has total 4GB memory
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   278
#define MAX_MALLOC_SIZE            ((size_t)(1 << 30) - 1)
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   279
#endif  // _LP64
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   280
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   281
 public:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   282
  // Summary tracking header
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   283
  MallocHeader(size_t size, MEMFLAGS flags) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   284
    assert(sizeof(MallocHeader) == sizeof(void*) * 2,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   285
      "Wrong header size");
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   286
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   287
    _level = NMT_summary;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   288
    _flags = flags;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   289
    set_size(size);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   290
    MallocMemorySummary::record_malloc(size, flags);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   291
    MallocMemorySummary::record_new_malloc_header(sizeof(MallocHeader));
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   292
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   293
  // Detail tracking header
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   294
  MallocHeader(size_t size, MEMFLAGS flags, const NativeCallStack& stack) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   295
    assert(sizeof(MallocHeader) == sizeof(void*) * 2,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   296
      "Wrong header size");
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   297
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   298
    _level = NMT_detail;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   299
    _flags = flags;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   300
    set_size(size);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   301
    size_t bucket_idx;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   302
    size_t pos_idx;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   303
    if (record_malloc_site(stack, size, &bucket_idx, &pos_idx)) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   304
      assert(bucket_idx <= MAX_MALLOCSITE_TABLE_SIZE, "Overflow bucket index");
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   305
      assert(pos_idx <= MAX_BUCKET_LENGTH, "Overflow bucket position index");
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   306
      _bucket_idx = bucket_idx;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   307
      _pos_idx = pos_idx;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   308
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   309
    MallocMemorySummary::record_malloc(size, flags);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   310
    MallocMemorySummary::record_new_malloc_header(sizeof(MallocHeader));
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   311
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   312
  // Minimal tracking header
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   313
  MallocHeader() {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   314
    assert(sizeof(MallocHeader) == sizeof(void*) * 2,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   315
      "Wrong header size");
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   316
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   317
    _level = (unsigned short)NMT_minimal;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   318
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   319
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   320
  inline NMT_TrackingLevel tracking_level() const {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   321
    return (NMT_TrackingLevel)_level;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   322
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   323
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   324
  inline size_t   size()  const { return _size; }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   325
  inline MEMFLAGS flags() const { return (MEMFLAGS)_flags; }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   326
  bool get_stack(NativeCallStack& stack) const;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   327
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   328
  // Cleanup tracking information before the memory is released.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   329
  void release() const;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   330
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   331
 private:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   332
  inline void set_size(size_t size) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   333
    assert(size <= MAX_MALLOC_SIZE, "Malloc size too large, should use virtual memory?");
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   334
    _size = size;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   335
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   336
  bool record_malloc_site(const NativeCallStack& stack, size_t size,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   337
    size_t* bucket_idx, size_t* pos_idx) const;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   338
};
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   339
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   340
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   341
// Main class called from MemTracker to track malloc activities
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   342
class MallocTracker : AllStatic {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   343
 public:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   344
  // Initialize malloc tracker for specific tracking level
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   345
  static bool initialize(NMT_TrackingLevel level);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   346
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   347
  static bool transition(NMT_TrackingLevel from, NMT_TrackingLevel to);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   348
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   349
  // malloc tracking header size for specific tracking level
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   350
  static inline size_t malloc_header_size(NMT_TrackingLevel level) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   351
    return (level == NMT_off) ? 0 : sizeof(MallocHeader);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   352
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   353
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   354
  // Parameter name convention:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   355
  // memblock :   the beginning address for user data
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   356
  // malloc_base: the beginning address that includes malloc tracking header
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   357
  //
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   358
  // The relationship:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   359
  // memblock = (char*)malloc_base + sizeof(nmt header)
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   360
  //
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   361
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   362
  // Record  malloc on specified memory block
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   363
  static void* record_malloc(void* malloc_base, size_t size, MEMFLAGS flags,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   364
    const NativeCallStack& stack, NMT_TrackingLevel level);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   365
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   366
  // Record free on specified memory block
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   367
  static void* record_free(void* memblock);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   368
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   369
  // Get tracking level of specified memory block
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   370
  static inline NMT_TrackingLevel get_memory_tracking_level(void* memblock);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   371
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   372
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   373
  // Offset memory address to header address
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   374
  static inline void* get_base(void* memblock);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   375
  static inline void* get_base(void* memblock, NMT_TrackingLevel level) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   376
    if (memblock == NULL || level == NMT_off) return memblock;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   377
    return (char*)memblock - malloc_header_size(level);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   378
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   379
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   380
  // Get memory size
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   381
  static inline size_t get_size(void* memblock) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   382
    MallocHeader* header = malloc_header(memblock);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   383
    assert(header->tracking_level() >= NMT_summary,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   384
      "Wrong tracking level");
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   385
    return header->size();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   386
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   387
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   388
  // Get memory type
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   389
  static inline MEMFLAGS get_flags(void* memblock) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   390
    MallocHeader* header = malloc_header(memblock);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   391
    assert(header->tracking_level() >= NMT_summary,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   392
      "Wrong tracking level");
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   393
    return header->flags();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   394
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   395
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   396
  // Get header size
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   397
  static inline size_t get_header_size(void* memblock) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   398
    return (memblock == NULL) ? 0 : sizeof(MallocHeader);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   399
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   400
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   401
  static inline void record_new_arena(MEMFLAGS flags) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   402
    MallocMemorySummary::record_new_arena(flags);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   403
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   404
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   405
  static inline void record_arena_free(MEMFLAGS flags) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   406
    MallocMemorySummary::record_arena_free(flags);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   407
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   408
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   409
  static inline void record_arena_size_change(int size, MEMFLAGS flags) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   410
    MallocMemorySummary::record_arena_size_change(size, flags);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   411
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   412
 private:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   413
  static inline MallocHeader* malloc_header(void *memblock) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   414
    assert(memblock != NULL, "NULL pointer");
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   415
    MallocHeader* header = (MallocHeader*)((char*)memblock - sizeof(MallocHeader));
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   416
    assert(header->tracking_level() >= NMT_minimal, "Bad header");
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   417
    return header;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   418
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   419
};
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   420
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   421
#endif // INCLUDE_NMT
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   422
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   423
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   424
#endif //SHARE_VM_SERVICES_MALLOC_TRACKER_HPP