src/share/vm/memory/allocation.hpp

Thu, 28 Jun 2012 17:03:16 -0400

author
zgu
date
Thu, 28 Jun 2012 17:03:16 -0400
changeset 3900
d2a62e0f25eb
parent 3138
f6f3bb0ee072
child 4037
da91efe96a93
permissions
-rw-r--r--

6995781: Native Memory Tracking (Phase 1)
7151532: DCmd for hotspot native memory tracking
Summary: Implementation of native memory tracking phase 1, which tracks VM native memory usage, and related DCmd
Reviewed-by: acorn, coleenp, fparain

     1 /*
     2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  *
    23  */
    25 #ifndef SHARE_VM_MEMORY_ALLOCATION_HPP
    26 #define SHARE_VM_MEMORY_ALLOCATION_HPP
    28 #include "runtime/globals.hpp"
    29 #include "utilities/globalDefinitions.hpp"
    30 #ifdef COMPILER1
    31 #include "c1/c1_globals.hpp"
    32 #endif
    33 #ifdef COMPILER2
    34 #include "opto/c2_globals.hpp"
    35 #endif
    37 #include <new>
    39 #define ARENA_ALIGN_M1 (((size_t)(ARENA_AMALLOC_ALIGNMENT)) - 1)
    40 #define ARENA_ALIGN_MASK (~((size_t)ARENA_ALIGN_M1))
    41 #define ARENA_ALIGN(x) ((((size_t)(x)) + ARENA_ALIGN_M1) & ARENA_ALIGN_MASK)
    44 // noinline attribute
    45 #ifdef _WINDOWS
    46   #define _NOINLINE_  __declspec(noinline)
    47 #else
    48   #if __GNUC__ < 3    // gcc 2.x does not support noinline attribute
    49     #define _NOINLINE_
    50   #else
    51     #define _NOINLINE_ __attribute__ ((noinline))
    52   #endif
    53 #endif
    55 // All classes in the virtual machine must be subclassed
    56 // by one of the following allocation classes:
    57 //
    58 // For objects allocated in the resource area (see resourceArea.hpp).
    59 // - ResourceObj
    60 //
    61 // For objects allocated in the C-heap (managed by: free & malloc).
    62 // - CHeapObj
    63 //
    64 // For objects allocated on the stack.
    65 // - StackObj
    66 //
    67 // For embedded objects.
    68 // - ValueObj
    69 //
    70 // For classes used as name spaces.
    71 // - AllStatic
    72 //
    73 // The printable subclasses are used for debugging and define virtual
    74 // member functions for printing. Classes that avoid allocating the
    75 // vtbl entries in the objects should therefore not be the printable
    76 // subclasses.
    77 //
    78 // The following macros and function should be used to allocate memory
    79 // directly in the resource area or in the C-heap:
    80 //
    81 //   NEW_RESOURCE_ARRAY(type,size)
    82 //   NEW_RESOURCE_OBJ(type)
    83 //   NEW_C_HEAP_ARRAY(type,size)
    84 //   NEW_C_HEAP_OBJ(type)
    85 //   char* AllocateHeap(size_t size, const char* name);
    86 //   void  FreeHeap(void* p);
    87 //
    88 // C-heap allocation can be traced using +PrintHeapAllocation.
    89 // malloc and free should therefore never called directly.
    91 // Base class for objects allocated in the C-heap.
    93 // In non product mode we introduce a super class for all allocation classes
    94 // that supports printing.
    95 // We avoid the superclass in product mode since some C++ compilers add
    96 // a word overhead for empty super classes.
    98 #ifdef PRODUCT
    99 #define ALLOCATION_SUPER_CLASS_SPEC
   100 #else
   101 #define ALLOCATION_SUPER_CLASS_SPEC : public AllocatedObj
   102 class AllocatedObj {
   103  public:
   104   // Printing support
   105   void print() const;
   106   void print_value() const;
   108   virtual void print_on(outputStream* st) const;
   109   virtual void print_value_on(outputStream* st) const;
   110 };
   111 #endif
   114 /*
   115  * MemoryType bitmap layout:
   116  * | 16 15 14 13 12 11 10 09 | 08 07 06 05 | 04 03 02 01 |
   117  * |      memory type        |   object    | reserved    |
   118  * |                         |     type    |             |
   119  */
   120 enum MemoryType {
   121   // Memory type by sub systems. It occupies lower byte.
   122   mtNone              = 0x0000,  // undefined
   123   mtClass             = 0x0100,  // memory class for Java classes
   124   mtThread            = 0x0200,  // memory for thread objects
   125   mtThreadStack       = 0x0300,
   126   mtCode              = 0x0400,  // memory for generated code
   127   mtGC                = 0x0500,  // memory for GC
   128   mtCompiler          = 0x0600,  // memory for compiler
   129   mtInternal          = 0x0700,  // memory used by VM, but does not belong to
   130                                  // any of above categories, and not used for
   131                                  // native memory tracking
   132   mtOther             = 0x0800,  // memory not used by VM
   133   mtSymbol            = 0x0900,  // symbol
   134   mtNMT               = 0x0A00,  // memory used by native memory tracking
   135   mtChunk             = 0x0B00,  // chunk that holds content of arenas
   136   mtJavaHeap          = 0x0C00,  // Java heap
   137   mtDontTrack         = 0x0D00,  // memory we donot or cannot track
   138   mt_number_of_types  = 0x000C,  // number of memory types
   139   mt_masks            = 0x7F00,
   141   // object type mask
   142   otArena             = 0x0010, // an arena object
   143   otNMTRecorder       = 0x0020, // memory recorder object
   144   ot_masks            = 0x00F0
   145 };
   147 #define IS_MEMORY_TYPE(flags, type) ((flags & mt_masks) == type)
   148 #define HAS_VALID_MEMORY_TYPE(flags)((flags & mt_masks) != mtNone)
   149 #define FLAGS_TO_MEMORY_TYPE(flags) (flags & mt_masks)
   151 #define IS_ARENA_OBJ(flags)         ((flags & ot_masks) == otArena)
   152 #define IS_NMT_RECORDER(flags)      ((flags & ot_masks) == otNMTRecorder)
   153 #define NMT_CAN_TRACK(flags)        (!IS_NMT_RECORDER(flags) && !(IS_MEMORY_TYPE(flags, mtDontTrack)))
   155 typedef unsigned short MEMFLAGS;
   157 extern bool NMT_track_callsite;
   159 // debug build does not inline
   160 #if defined(_DEBUG_)
   161   #define CURRENT_PC       (NMT_track_callsite ? os::get_caller_pc(1) : 0)
   162   #define CALLER_PC        (NMT_track_callsite ? os::get_caller_pc(2) : 0)
   163   #define CALLER_CALLER_PC (NMT_track_callsite ? os::get_caller_pc(3) : 0)
   164 #else
   165   #define CURRENT_PC      (NMT_track_callsite? os::get_caller_pc(0) : 0)
   166   #define CALLER_PC       (NMT_track_callsite ? os::get_caller_pc(1) : 0)
   167   #define CALLER_CALLER_PC (NMT_track_callsite ? os::get_caller_pc(2) : 0)
   168 #endif
   172 template <MEMFLAGS F> class CHeapObj ALLOCATION_SUPER_CLASS_SPEC {
   173  public:
   174   _NOINLINE_ void* operator new(size_t size, address caller_pc = 0);
   175   _NOINLINE_ void* operator new (size_t size, const std::nothrow_t&  nothrow_constant,
   176                                address caller_pc = 0);
   178   void  operator delete(void* p);
   179 };
   181 // Base class for objects allocated on the stack only.
   182 // Calling new or delete will result in fatal error.
   184 class StackObj ALLOCATION_SUPER_CLASS_SPEC {
   185  public:
   186   void* operator new(size_t size);
   187   void  operator delete(void* p);
   188 };
   190 // Base class for objects used as value objects.
   191 // Calling new or delete will result in fatal error.
   192 //
   193 // Portability note: Certain compilers (e.g. gcc) will
   194 // always make classes bigger if it has a superclass, even
   195 // if the superclass does not have any virtual methods or
   196 // instance fields. The HotSpot implementation relies on this
   197 // not to happen. So never make a ValueObj class a direct subclass
   198 // of this object, but use the VALUE_OBJ_CLASS_SPEC class instead, e.g.,
   199 // like this:
   200 //
   201 //   class A VALUE_OBJ_CLASS_SPEC {
   202 //     ...
   203 //   }
   204 //
   205 // With gcc and possible other compilers the VALUE_OBJ_CLASS_SPEC can
   206 // be defined as a an empty string "".
   207 //
   208 class _ValueObj {
   209  public:
   210   void* operator new(size_t size);
   211   void operator delete(void* p);
   212 };
   214 // Base class for classes that constitute name spaces.
   216 class AllStatic {
   217  public:
   218   AllStatic()  { ShouldNotCallThis(); }
   219   ~AllStatic() { ShouldNotCallThis(); }
   220 };
   223 //------------------------------Chunk------------------------------------------
   224 // Linked list of raw memory chunks
   225 class Chunk: CHeapObj<mtChunk> {
   226   friend class VMStructs;
   228  protected:
   229   Chunk*       _next;     // Next Chunk in list
   230   const size_t _len;      // Size of this Chunk
   231  public:
   232   void* operator new(size_t size, size_t length);
   233   void  operator delete(void* p);
   234   Chunk(size_t length);
   236   enum {
   237     // default sizes; make them slightly smaller than 2**k to guard against
   238     // buddy-system style malloc implementations
   239 #ifdef _LP64
   240     slack      = 40,            // [RGV] Not sure if this is right, but make it
   241                                 //       a multiple of 8.
   242 #else
   243     slack      = 20,            // suspected sizeof(Chunk) + internal malloc headers
   244 #endif
   246     init_size  =  1*K  - slack, // Size of first chunk
   247     medium_size= 10*K  - slack, // Size of medium-sized chunk
   248     size       = 32*K  - slack, // Default size of an Arena chunk (following the first)
   249     non_pool_size = init_size + 32 // An initial size which is not one of above
   250   };
   252   void chop();                  // Chop this chunk
   253   void next_chop();             // Chop next chunk
   254   static size_t aligned_overhead_size(void) { return ARENA_ALIGN(sizeof(Chunk)); }
   256   size_t length() const         { return _len;  }
   257   Chunk* next() const           { return _next;  }
   258   void set_next(Chunk* n)       { _next = n;  }
   259   // Boundaries of data area (possibly unused)
   260   char* bottom() const          { return ((char*) this) + aligned_overhead_size();  }
   261   char* top()    const          { return bottom() + _len; }
   262   bool contains(char* p) const  { return bottom() <= p && p <= top(); }
   264   // Start the chunk_pool cleaner task
   265   static void start_chunk_pool_cleaner_task();
   267   static void clean_chunk_pool();
   268 };
   270 //------------------------------Arena------------------------------------------
   271 // Fast allocation of memory
   272 class Arena : public CHeapObj<mtNone|otArena> {
   273 protected:
   274   friend class ResourceMark;
   275   friend class HandleMark;
   276   friend class NoHandleMark;
   277   friend class VMStructs;
   279   Chunk *_first;                // First chunk
   280   Chunk *_chunk;                // current chunk
   281   char *_hwm, *_max;            // High water mark and max in current chunk
   282   void* grow(size_t x);         // Get a new Chunk of at least size x
   283   size_t _size_in_bytes;        // Size of arena (used for native memory tracking)
   285   NOT_PRODUCT(static julong _bytes_allocated;) // total #bytes allocated since start
   286   friend class AllocStats;
   287   debug_only(void* malloc(size_t size);)
   288   debug_only(void* internal_malloc_4(size_t x);)
   289   NOT_PRODUCT(void inc_bytes_allocated(size_t x);)
   291   void signal_out_of_memory(size_t request, const char* whence) const;
   293   void check_for_overflow(size_t request, const char* whence) const {
   294     if (UINTPTR_MAX - request < (uintptr_t)_hwm) {
   295       signal_out_of_memory(request, whence);
   296     }
   297  }
   299  public:
   300   Arena();
   301   Arena(size_t init_size);
   302   Arena(Arena *old);
   303   ~Arena();
   304   void  destruct_contents();
   305   char* hwm() const             { return _hwm; }
   307   // new operators
   308   void* operator new (size_t size);
   309   void* operator new (size_t size, const std::nothrow_t& nothrow_constant);
   311   // dynamic memory type tagging
   312   void* operator new(size_t size, MEMFLAGS flags);
   313   void* operator new(size_t size, const std::nothrow_t& nothrow_constant, MEMFLAGS flags);
   314   void  operator delete(void* p);
   316   // Fast allocate in the arena.  Common case is: pointer test + increment.
   317   void* Amalloc(size_t x) {
   318     assert(is_power_of_2(ARENA_AMALLOC_ALIGNMENT) , "should be a power of 2");
   319     x = ARENA_ALIGN(x);
   320     debug_only(if (UseMallocOnly) return malloc(x);)
   321     check_for_overflow(x, "Arena::Amalloc");
   322     NOT_PRODUCT(inc_bytes_allocated(x);)
   323     if (_hwm + x > _max) {
   324       return grow(x);
   325     } else {
   326       char *old = _hwm;
   327       _hwm += x;
   328       return old;
   329     }
   330   }
   331   // Further assume size is padded out to words
   332   void *Amalloc_4(size_t x) {
   333     assert( (x&(sizeof(char*)-1)) == 0, "misaligned size" );
   334     debug_only(if (UseMallocOnly) return malloc(x);)
   335     check_for_overflow(x, "Arena::Amalloc_4");
   336     NOT_PRODUCT(inc_bytes_allocated(x);)
   337     if (_hwm + x > _max) {
   338       return grow(x);
   339     } else {
   340       char *old = _hwm;
   341       _hwm += x;
   342       return old;
   343     }
   344   }
   346   // Allocate with 'double' alignment. It is 8 bytes on sparc.
   347   // In other cases Amalloc_D() should be the same as Amalloc_4().
   348   void* Amalloc_D(size_t x) {
   349     assert( (x&(sizeof(char*)-1)) == 0, "misaligned size" );
   350     debug_only(if (UseMallocOnly) return malloc(x);)
   351 #if defined(SPARC) && !defined(_LP64)
   352 #define DALIGN_M1 7
   353     size_t delta = (((size_t)_hwm + DALIGN_M1) & ~DALIGN_M1) - (size_t)_hwm;
   354     x += delta;
   355 #endif
   356     check_for_overflow(x, "Arena::Amalloc_D");
   357     NOT_PRODUCT(inc_bytes_allocated(x);)
   358     if (_hwm + x > _max) {
   359       return grow(x); // grow() returns a result aligned >= 8 bytes.
   360     } else {
   361       char *old = _hwm;
   362       _hwm += x;
   363 #if defined(SPARC) && !defined(_LP64)
   364       old += delta; // align to 8-bytes
   365 #endif
   366       return old;
   367     }
   368   }
   370   // Fast delete in area.  Common case is: NOP (except for storage reclaimed)
   371   void Afree(void *ptr, size_t size) {
   372 #ifdef ASSERT
   373     if (ZapResourceArea) memset(ptr, badResourceValue, size); // zap freed memory
   374     if (UseMallocOnly) return;
   375 #endif
   376     if (((char*)ptr) + size == _hwm) _hwm = (char*)ptr;
   377   }
   379   void *Arealloc( void *old_ptr, size_t old_size, size_t new_size );
   381   // Move contents of this arena into an empty arena
   382   Arena *move_contents(Arena *empty_arena);
   384   // Determine if pointer belongs to this Arena or not.
   385   bool contains( const void *ptr ) const;
   387   // Total of all chunks in use (not thread-safe)
   388   size_t used() const;
   390   // Total # of bytes used
   391   size_t size_in_bytes() const         {  return _size_in_bytes; };
   392   void set_size_in_bytes(size_t size);
   394   static void free_malloced_objects(Chunk* chunk, char* hwm, char* max, char* hwm2)  PRODUCT_RETURN;
   395   static void free_all(char** start, char** end)                                     PRODUCT_RETURN;
   397   // how many arena instances
   398   NOT_PRODUCT(static volatile jint _instance_count;)
   399 private:
   400   // Reset this Arena to empty, access will trigger grow if necessary
   401   void   reset(void) {
   402     _first = _chunk = NULL;
   403     _hwm = _max = NULL;
   404     set_size_in_bytes(0);
   405   }
   406 };
   408 // One of the following macros must be used when allocating
   409 // an array or object from an arena
   410 #define NEW_ARENA_ARRAY(arena, type, size) \
   411   (type*) (arena)->Amalloc((size) * sizeof(type))
   413 #define REALLOC_ARENA_ARRAY(arena, type, old, old_size, new_size)    \
   414   (type*) (arena)->Arealloc((char*)(old), (old_size) * sizeof(type), \
   415                             (new_size) * sizeof(type) )
   417 #define FREE_ARENA_ARRAY(arena, type, old, size) \
   418   (arena)->Afree((char*)(old), (size) * sizeof(type))
   420 #define NEW_ARENA_OBJ(arena, type) \
   421   NEW_ARENA_ARRAY(arena, type, 1)
   424 //%note allocation_1
   425 extern char* resource_allocate_bytes(size_t size);
   426 extern char* resource_allocate_bytes(Thread* thread, size_t size);
   427 extern char* resource_reallocate_bytes( char *old, size_t old_size, size_t new_size);
   428 extern void resource_free_bytes( char *old, size_t size );
   430 //----------------------------------------------------------------------
   431 // Base class for objects allocated in the resource area per default.
   432 // Optionally, objects may be allocated on the C heap with
   433 // new(ResourceObj::C_HEAP) Foo(...) or in an Arena with new (&arena)
   434 // ResourceObj's can be allocated within other objects, but don't use
   435 // new or delete (allocation_type is unknown).  If new is used to allocate,
   436 // use delete to deallocate.
   437 class ResourceObj ALLOCATION_SUPER_CLASS_SPEC {
   438  public:
   439   enum allocation_type { STACK_OR_EMBEDDED = 0, RESOURCE_AREA, C_HEAP, ARENA, allocation_mask = 0x3 };
   440   static void set_allocation_type(address res, allocation_type type) NOT_DEBUG_RETURN;
   441 #ifdef ASSERT
   442  private:
   443   // When this object is allocated on stack the new() operator is not
   444   // called but garbage on stack may look like a valid allocation_type.
   445   // Store negated 'this' pointer when new() is called to distinguish cases.
   446   // Use second array's element for verification value to distinguish garbage.
   447   uintptr_t _allocation_t[2];
   448   bool is_type_set() const;
   449  public:
   450   allocation_type get_allocation_type() const;
   451   bool allocated_on_stack()    const { return get_allocation_type() == STACK_OR_EMBEDDED; }
   452   bool allocated_on_res_area() const { return get_allocation_type() == RESOURCE_AREA; }
   453   bool allocated_on_C_heap()   const { return get_allocation_type() == C_HEAP; }
   454   bool allocated_on_arena()    const { return get_allocation_type() == ARENA; }
   455   ResourceObj(); // default construtor
   456   ResourceObj(const ResourceObj& r); // default copy construtor
   457   ResourceObj& operator=(const ResourceObj& r); // default copy assignment
   458   ~ResourceObj();
   459 #endif // ASSERT
   461  public:
   462   void* operator new(size_t size, allocation_type type, MEMFLAGS flags);
   463   void* operator new(size_t size, Arena *arena) {
   464       address res = (address)arena->Amalloc(size);
   465       DEBUG_ONLY(set_allocation_type(res, ARENA);)
   466       return res;
   467   }
   468   void* operator new(size_t size) {
   469       address res = (address)resource_allocate_bytes(size);
   470       DEBUG_ONLY(set_allocation_type(res, RESOURCE_AREA);)
   471       return res;
   472   }
   473   void  operator delete(void* p);
   474 };
   476 // One of the following macros must be used when allocating an array
   477 // or object to determine whether it should reside in the C heap on in
   478 // the resource area.
   480 #define NEW_RESOURCE_ARRAY(type, size)\
   481   (type*) resource_allocate_bytes((size) * sizeof(type))
   483 #define NEW_RESOURCE_ARRAY_IN_THREAD(thread, type, size)\
   484   (type*) resource_allocate_bytes(thread, (size) * sizeof(type))
   486 #define REALLOC_RESOURCE_ARRAY(type, old, old_size, new_size)\
   487   (type*) resource_reallocate_bytes((char*)(old), (old_size) * sizeof(type), (new_size) * sizeof(type) )
   489 #define FREE_RESOURCE_ARRAY(type, old, size)\
   490   resource_free_bytes((char*)(old), (size) * sizeof(type))
   492 #define FREE_FAST(old)\
   493     /* nop */
   495 #define NEW_RESOURCE_OBJ(type)\
   496   NEW_RESOURCE_ARRAY(type, 1)
   498 #define NEW_C_HEAP_ARRAY(type, size, memflags)\
   499   (type*) (AllocateHeap((size) * sizeof(type), memflags))
   501 #define REALLOC_C_HEAP_ARRAY(type, old, size, memflags)\
   502   (type*) (ReallocateHeap((char*)old, (size) * sizeof(type), memflags))
   504 #define FREE_C_HEAP_ARRAY(type,old,memflags) \
   505   FreeHeap((char*)(old), memflags)
   507 #define NEW_C_HEAP_OBJ(type, memflags)\
   508   NEW_C_HEAP_ARRAY(type, 1, memflags)
   511 #define NEW_C_HEAP_ARRAY2(type, size, memflags, pc)\
   512   (type*) (AllocateHeap((size) * sizeof(type), memflags, pc))
   514 #define REALLOC_C_HEAP_ARRAY2(type, old, size, memflags, pc)\
   515   (type*) (ReallocateHeap((char*)old, (size) * sizeof(type), memflags, pc))
   517 #define NEW_C_HEAP_OBJ2(type, memflags, pc)\
   518   NEW_C_HEAP_ARRAY2(type, 1, memflags, pc)
   521 extern bool warn_new_operator;
   523 // for statistics
   524 #ifndef PRODUCT
   525 class AllocStats : StackObj {
   526   julong start_mallocs, start_frees;
   527   julong start_malloc_bytes, start_mfree_bytes, start_res_bytes;
   528  public:
   529   AllocStats();
   531   julong num_mallocs();    // since creation of receiver
   532   julong alloc_bytes();
   533   julong num_frees();
   534   julong free_bytes();
   535   julong resource_bytes();
   536   void   print();
   537 };
   538 #endif
   541 //------------------------------ReallocMark---------------------------------
   542 // Code which uses REALLOC_RESOURCE_ARRAY should check an associated
   543 // ReallocMark, which is declared in the same scope as the reallocated
   544 // pointer.  Any operation that could __potentially__ cause a reallocation
   545 // should check the ReallocMark.
   546 class ReallocMark: public StackObj {
   547 protected:
   548   NOT_PRODUCT(int _nesting;)
   550 public:
   551   ReallocMark()   PRODUCT_RETURN;
   552   void check()    PRODUCT_RETURN;
   553 };
   555 #endif // SHARE_VM_MEMORY_ALLOCATION_HPP

mercurial