1 /*
   2  * Copyright (c) 2018, 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 #include "runtime/sharedRuntime.hpp"
  26 #include "runtime/threadHeapSampler.hpp"
  27 #include "runtime/heapMonitoring.hpp"
  28 
  29 void ThreadHeapSampler::pick_next_sample(size_t overflowed_bytes) {
  30   HeapMonitoring::pick_next_sample(&_bytes_until_sample);
  31 
  32   // Try to correct sample size by removing extra space from last allocation.
  33   if (overflowed_bytes > 0 && _bytes_until_sample > overflowed_bytes) {
  34     _bytes_until_sample -= overflowed_bytes;
  35   }
  36 }
  37 
  38 void ThreadHeapSampler::check_for_sampling(HeapWord* ptr, size_t allocation_size, size_t bytes_since_allocation) {
  39   oopDesc* oop = reinterpret_cast<oopDesc*>(ptr);
  40   size_t total_allocated_bytes = bytes_since_allocation + allocation_size;
  41 
  42   // If not yet time for a sample, skip it.
  43   if (total_allocated_bytes < _bytes_until_sample) {
  44     _bytes_until_sample -= total_allocated_bytes;
  45     return;
  46   }
  47 
  48   HeapMonitoring::object_alloc_do_sample(_thread, oop, allocation_size);
  49   JvmtiExport::sampled_object_alloc_event_collector(oop);
  50 
  51   size_t overflow_bytes = total_allocated_bytes - _bytes_until_sample;
  52   pick_next_sample(overflow_bytes);
  53 }