1 /*
   2  * Copyright (c) 2017, Google 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 #ifndef SHARE_VM_RUNTIME_HEAPMONITORING_HPP
  26 #define SHARE_VM_RUNTIME_HEAPMONITORING_HPP
  27 
  28 #include "gc/shared/referenceProcessor.hpp"
  29 #include "runtime/sharedRuntime.hpp"
  30 
  31 // Support class for sampling heap allocations across the VM.
  32 class HeapMonitoring {
  33  private:
  34   // Cheap random number generator
  35   static uint64_t _rnd;
  36   static bool _initialized;
  37   static jint _monitoring_rate;
  38 
  39   // Statics for the fast log
  40   static const int kFastlogNumBits = 10;
  41   static const int kFastlogMask = (1 << kFastlogNumBits) - 1;
  42   static double _log_table[1<<kFastlogNumBits];  // Constant
  43 
  44   static void pick_next_sample(JavaThread *t);
  45 
  46   // Returns the next prng value.
  47   // pRNG is: aX+b mod c with a = 0x5DEECE66D, b =  0xB, c = 1<<48
  48   // This is the lrand64 generator.
  49   static inline uint64_t next_random(uint64_t rnd) {
  50     const uint64_t prng_mult = 0x5DEECE66DLL;
  51     const uint64_t prng_add = 0xB;
  52     const uint64_t prng_mod_power = 48;
  53     const uint64_t prng_mod_mask =
  54         ~((~static_cast<uint64_t>(0)) << prng_mod_power);
  55     return (prng_mult * rnd + prng_add) & prng_mod_mask;
  56   }
  57 
  58   // Adapted from //util/math/fastmath.[h|cc] by Noam Shazeer
  59   // This mimics the VeryFastLog2 code in those files
  60   static inline double fast_log2(const double & d) {
  61     assert(d>0, "bad value passed to assert");
  62     uint64_t x = 0;
  63     memcpy(&x, &d, sizeof(uint64_t));
  64     const uint32_t x_high = x >> 32;
  65     const uint32_t y = x_high >> (20 - kFastlogNumBits) & kFastlogMask;
  66     const int32_t exponent = ((x_high >> 20) & 0x7FF) - 1023;
  67     return exponent + _log_table[y];
  68   }
  69 
  70  public:
  71   static void get_live_traces(jvmtiStackTraces* stack_traces);
  72   static void get_garbage_traces(jvmtiStackTraces* stack_traces);
  73   static void get_frequent_garbage_traces(jvmtiStackTraces* stack_traces);
  74   static void release_traces(jvmtiStackTraces *trace_info);
  75   static void initialize_profiling(jint monitoring_rate, jint max_storage);
  76   static bool initialized();
  77   static bool *initialized_address();
  78 
  79   // Called when o is allocated, called by interpreter and C1.
  80   static void object_alloc_unsized(oopDesc* o);
  81   static void object_alloc(oopDesc* o, intx byte_size);
  82 
  83   // Called when o is allocated from C2 directly,
  84   // we know the thread, and we have done the sampling.
  85   static void object_alloc_do_sample(Thread *t, oopDesc *o, intx size_in_bytes);
  86 
  87   // Called to clean up oops that have been saved by our sampling function,
  88   // but which no longer have other references in the heap.
  89   static void do_weak_oops(AbstractRefProcTaskExecutor *task_executor,
  90                            BoolObjectClosure* is_alive,
  91                            OopClosure *f,
  92                            VoidClosure *complete_gc);
  93   static void do_weak_oops(OopClosure* oop_closure) {
  94     do_weak_oops(NULL, NULL, oop_closure, NULL);
  95   }
  96 };
  97 
  98 #endif // SHARE_VM_RUNTIME_HEAPMONITORING_HPP