/* * Copyright (c) 2017, Google and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ #ifndef SHARE_VM_RUNTIME_HEAPMONITORING_HPP #define SHARE_VM_RUNTIME_HEAPMONITORING_HPP #include "gc/shared/referenceProcessor.hpp" #include "runtime/sharedRuntime.hpp" // Support class for sampling heap allocations across the VM. class HeapMonitoring : AllStatic { private: // Cheap random number generator static uint64_t _rnd; static bool _initialized; static jint _monitoring_rate; static bool _enabled; // Statics for the fast log static const int _fast_log_num_bits = 10; static const int _fast_log_mask = (1 << _fast_log_num_bits) - 1; static double _log_table[1<<_fast_log_num_bits]; // Constant static void pick_next_sample(JavaThread *t); // Returns the next prng value. // pRNG is: aX+b mod c with a = 0x5DEECE66D, b = 0xB, c = 1<<48 // This is the lrand64 generator. static inline uint64_t next_random(uint64_t rnd) { const uint64_t prng_mult = 0x5DEECE66DLL; const uint64_t prng_add = 0xB; const uint64_t prng_mod_power = 48; const uint64_t prng_mod_mask = ~((~static_cast(0)) << prng_mod_power); return (prng_mult * rnd + prng_add) & prng_mod_mask; } static inline double fast_log2(const double & d) { assert(d>0, "bad value passed to assert"); uint64_t x = 0; memcpy(&x, &d, sizeof(uint64_t)); const uint32_t x_high = x >> 32; const uint32_t y = x_high >> (20 - _fast_log_num_bits) & _fast_log_mask; const int32_t exponent = ((x_high >> 20) & 0x7FF) - 1023; return exponent + _log_table[y]; } public: static void pick_next_sample(size_t *ptr); static void get_live_traces(jvmtiStackTraces* stack_traces); static void get_garbage_traces(jvmtiStackTraces* stack_traces); static void get_frequent_garbage_traces(jvmtiStackTraces* stack_traces); static void release_traces(jvmtiStackTraces *trace_info); static void initialize_profiling(jint monitoring_rate, jint max_storage); static void stop_profiling(); static bool initialized(); static bool *initialized_address(); // Called when o is allocated, called by interpreter and C1. static void object_alloc_unsized(oopDesc* o); static void object_alloc(oopDesc* o, intx byte_size); // Called when o is allocated from C2 directly, // we know the thread, and we have done the sampling. static void object_alloc_do_sample(Thread *t, oopDesc *o, intx size_in_bytes); // Called to clean up oops that have been saved by our sampling function, // but which no longer have other references in the heap. static void weak_oops_do(AbstractRefProcTaskExecutor *task_executor, BoolObjectClosure* is_alive, OopClosure *f, VoidClosure *complete_gc); static void weak_oops_do(OopClosure* oop_closure) { weak_oops_do(NULL, NULL, oop_closure, NULL); } static bool enabled() { return _enabled; } }; #endif // SHARE_VM_RUNTIME_HEAPMONITORING_HPP