1 /*
   2  * Copyright (c) 2016, 2018, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #include "precompiled.hpp"
  25 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  26 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
  27 #include "gc/shenandoah/shenandoahHeapRegionSet.hpp"
  28 #include "gc/shenandoah/shenandoahHeapRegionCounters.hpp"
  29 #include "memory/resourceArea.hpp"
  30 #include "runtime/perfData.inline.hpp"
  31 
  32 ShenandoahHeapRegionCounters::ShenandoahHeapRegionCounters() :
  33   _last_sample_millis(0)
  34 {
  35   if (UsePerfData && ShenandoahRegionSampling) {
  36     EXCEPTION_MARK;
  37     ResourceMark rm;
  38     ShenandoahHeap* heap = ShenandoahHeap::heap();
  39     size_t num_regions = heap->num_regions();
  40     const char* cns = PerfDataManager::name_space("shenandoah", "regions");
  41     _name_space = NEW_C_HEAP_ARRAY(char, strlen(cns)+1, mtGC);
  42     strcpy(_name_space, cns);
  43 
  44     const char* cname = PerfDataManager::counter_name(_name_space, "timestamp");
  45     _timestamp = PerfDataManager::create_long_variable(SUN_GC, cname, PerfData::U_None, CHECK);
  46 
  47     cname = PerfDataManager::counter_name(_name_space, "max_regions");
  48     PerfDataManager::create_constant(SUN_GC, cname, PerfData::U_None, num_regions, CHECK);
  49 
  50     cname = PerfDataManager::counter_name(_name_space, "region_size");
  51     PerfDataManager::create_constant(SUN_GC, cname, PerfData::U_None, ShenandoahHeapRegion::region_size_bytes() >> 10, CHECK);
  52 
  53     cname = PerfDataManager::counter_name(_name_space, "status");
  54     _status = PerfDataManager::create_long_variable(SUN_GC, cname,
  55                                                     PerfData::U_None, CHECK);
  56 
  57     _regions_data = NEW_C_HEAP_ARRAY(PerfVariable*, num_regions, mtGC);
  58     for (uint i = 0; i < num_regions; i++) {
  59       const char* reg_name = PerfDataManager::name_space(_name_space, "region", i);
  60       const char* data_name = PerfDataManager::counter_name(reg_name, "data");
  61       const char* ns = PerfDataManager::ns_to_string(SUN_GC);
  62       const char* fullname = PerfDataManager::counter_name(ns, data_name);
  63       assert(!PerfDataManager::exists(fullname), "must not exist");
  64       _regions_data[i] = PerfDataManager::create_long_variable(SUN_GC, data_name,
  65                                                                PerfData::U_None, CHECK);
  66     }
  67   }
  68 }
  69 
  70 ShenandoahHeapRegionCounters::~ShenandoahHeapRegionCounters() {
  71   if (_name_space != NULL) FREE_C_HEAP_ARRAY(char, _name_space);
  72 }
  73 
  74 void ShenandoahHeapRegionCounters::update() {
  75   if (ShenandoahRegionSampling) {
  76     jlong current = os::javaTimeMillis();
  77     jlong last = _last_sample_millis;
  78     if (current - last > ShenandoahRegionSamplingRate &&
  79             Atomic::cmpxchg(current, &_last_sample_millis, last) == last) {
  80 
  81       ShenandoahHeap* heap = ShenandoahHeap::heap();
  82       jlong status = 0;
  83       if (heap->is_concurrent_mark_in_progress())      status |= 1 << 0;
  84       if (heap->is_evacuation_in_progress())           status |= 1 << 1;
  85       if (heap->is_update_refs_in_progress())          status |= 1 << 2;
  86       if (heap->is_concurrent_traversal_in_progress()) status |= 1 << 3;
  87       _status->set_value(status);
  88 
  89       _timestamp->set_value(os::elapsed_counter());
  90 
  91       size_t num_regions = heap->num_regions();
  92 
  93       {
  94         ShenandoahHeapLocker locker(heap->lock());
  95         size_t rs = ShenandoahHeapRegion::region_size_bytes();
  96         for (uint i = 0; i < num_regions; i++) {
  97           ShenandoahHeapRegion* r = heap->get_region(i);
  98           jlong data = 0;
  99           data |= ((100 * r->used() / rs)                & PERCENT_MASK) << USED_SHIFT;
 100           data |= ((100 * r->get_live_data_bytes() / rs) & PERCENT_MASK) << LIVE_SHIFT;
 101           data |= ((100 * r->get_tlab_allocs() / rs)     & PERCENT_MASK) << TLAB_SHIFT;
 102           data |= ((100 * r->get_gclab_allocs() / rs)    & PERCENT_MASK) << GCLAB_SHIFT;
 103           data |= ((100 * r->get_shared_allocs() / rs)   & PERCENT_MASK) << SHARED_SHIFT;
 104           data |= (r->state_ordinal() & STATUS_MASK) << STATUS_SHIFT;
 105           _regions_data[i]->set_value(data);
 106         }
 107       }
 108 
 109     }
 110   }
 111 }