1 /*
   2  * Copyright (c) 2014, 2018, 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  */
  24 
  25 #include "precompiled.hpp"
  26 #include "jfr/recorder/jfrRecorder.hpp"
  27 #include "jfr/utilities/jfrAllocation.hpp"
  28 #include "jfr/utilities/jfrTypes.hpp"
  29 #include "memory/allocation.inline.hpp"
  30 #include "runtime/atomic.inline.hpp"
  31 #include "runtime/orderAccess.inline.hpp"
  32 #include "runtime/vm_version.hpp"
  33 #include "runtime/mutexLocker.hpp"
  34 #include "utilities/debug.hpp"
  35 #include "utilities/macros.hpp"
  36 #include "utilities/nativeCallStack.hpp"
  37 
  38 jlong atomic_add_jlong(jlong value, jlong volatile* const dest) {
  39   jlong compare_value;
  40   jlong exchange_value;
  41 #ifndef SUPPORTS_NATIVE_CX8
  42   if (!VM_Version::supports_cx8()) {
  43     MutexLockerEx mu(JfrCounters_lock, Mutex::_no_safepoint_check_flag);
  44     return *dest += value;
  45   }
  46 #endif
  47   do {
  48     compare_value = OrderAccess::load_acquire(dest);
  49     exchange_value = compare_value + value;
  50   } while (Atomic::cmpxchg(exchange_value, dest, compare_value) != compare_value);
  51   return exchange_value;
  52 }
  53 
  54 #ifdef ASSERT
  55 
  56 // debug statistics
  57 static volatile jlong _allocated_bytes = 0;
  58 static volatile jlong _deallocated_bytes = 0;
  59 static volatile jlong _live_set_bytes = 0;
  60 
  61 static void add(size_t alloc_size) {
  62   if (!JfrRecorder::is_created()) {
  63     const jlong total_allocated = atomic_add_jlong((jlong)alloc_size, &_allocated_bytes);
  64     const jlong current_live_set = atomic_add_jlong((jlong)alloc_size, &_live_set_bytes);
  65     if (LogJFR && Verbose) tty->print_cr("Allocation: [" SIZE_FORMAT "] bytes", alloc_size);
  66     if (LogJFR && Verbose) tty->print_cr("Total alloc [" JLONG_FORMAT "] bytes", total_allocated);
  67     if (LogJFR && Verbose) tty->print_cr("Liveset:    [" JLONG_FORMAT "] bytes", current_live_set);
  68   }
  69 }
  70 
  71 static void subtract(size_t dealloc_size) {
  72   if (!JfrRecorder::is_created()) {
  73     const jlong total_deallocated = atomic_add_jlong((jlong)dealloc_size, &_deallocated_bytes);
  74     const jlong current_live_set = atomic_add_jlong(((jlong)dealloc_size * -1), &_live_set_bytes);
  75     if (LogJFR && Verbose) tty->print_cr("Deallocation: [" SIZE_FORMAT "] bytes", dealloc_size);
  76     if (LogJFR && Verbose) tty->print_cr("Total dealloc [" JLONG_FORMAT "] bytes", total_deallocated);
  77     if (LogJFR && Verbose) tty->print_cr("Liveset:      [" JLONG_FORMAT "] bytes", current_live_set);
  78   }
  79 }
  80 
  81 static void hook_memory_deallocation(size_t dealloc_size) {
  82   subtract(dealloc_size);
  83 }
  84 #endif // ASSERT
  85 
  86 static void hook_memory_allocation(const char* allocation, size_t alloc_size) {
  87   if (NULL == allocation) {
  88     if (!JfrRecorder::is_created()) {
  89       if (LogJFR) tty->print_cr("Memory allocation failed for size [" SIZE_FORMAT "] bytes", alloc_size);
  90       return;
  91     } else {
  92       // after critical startup, fail as by default
  93       vm_exit_out_of_memory(alloc_size, OOM_MALLOC_ERROR, "AllocateHeap");
  94     }
  95   }
  96   debug_only(add(alloc_size));
  97 }
  98 
  99 void JfrCHeapObj::on_memory_allocation(const void* allocation, size_t size) {
 100   hook_memory_allocation((const char*)allocation, size);
 101 }
 102 
 103 void* JfrCHeapObj::operator new(size_t size) throw() {
 104   return operator new(size, std::nothrow);
 105 }
 106 
 107 void* JfrCHeapObj::operator new (size_t size, const std::nothrow_t&  nothrow_constant) throw() {
 108   void* const memory = CHeapObj<mtTracing>::operator new(size, nothrow_constant, CALLER_PC);
 109   hook_memory_allocation((const char*)memory, size);
 110   return memory;
 111 }
 112 
 113 void* JfrCHeapObj::operator new [](size_t size) throw() {
 114   return operator new[](size, std::nothrow);
 115 }
 116 
 117 void* JfrCHeapObj::operator new [](size_t size, const std::nothrow_t&  nothrow_constant) throw() {
 118   void* const memory = CHeapObj<mtTracing>::operator new[](size, nothrow_constant, CALLER_PC);
 119   hook_memory_allocation((const char*)memory, size);
 120   return memory;
 121 }
 122 
 123 void JfrCHeapObj::operator delete(void* p, size_t size) {
 124   debug_only(hook_memory_deallocation(size);)
 125   CHeapObj<mtTracing>::operator delete(p);
 126 }
 127 
 128 void JfrCHeapObj::operator delete[](void* p, size_t size) {
 129   debug_only(hook_memory_deallocation(size);)
 130   CHeapObj<mtTracing>::operator delete[](p);
 131 }
 132 
 133 char* JfrCHeapObj::realloc_array(char* old, size_t size) {
 134   char* const memory = ReallocateHeap(old, size, mtTracing, AllocFailStrategy::RETURN_NULL);
 135   hook_memory_allocation(memory, size);
 136   return memory;
 137 }
 138 
 139 void JfrCHeapObj::free(void* p, size_t size) {
 140   debug_only(hook_memory_deallocation(size);)
 141   FreeHeap(p);
 142 }
 143 
 144 char* JfrCHeapObj::allocate_array_noinline(size_t elements, size_t element_size) {
 145   return AllocateHeap(elements * element_size, mtTracing, CALLER_PC, AllocFailStrategy::RETURN_NULL);
 146 }