1 /*
  2  * Copyright (c) 2016, 2020, Red Hat, Inc. 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 "gc/shenandoah/shenandoahHeap.inline.hpp"
 27 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
 28 #include "gc/shenandoah/shenandoahHeapRegionSet.hpp"
 29 #include "gc/shenandoah/shenandoahHeapRegionCounters.hpp"
 30 #include "memory/resourceArea.hpp"
 31 #include "runtime/atomic.hpp"
 32 #include "runtime/perfData.inline.hpp"
 33 
 34 ShenandoahHeapRegionCounters::ShenandoahHeapRegionCounters() :
 35   _last_sample_millis(0)
 36 {
 37   if (UsePerfData && ShenandoahRegionSampling) {
 38     EXCEPTION_MARK;
 39     ResourceMark rm;
 40     ShenandoahHeap* heap = ShenandoahHeap::heap();
 41     size_t num_regions = heap->num_regions();
 42     const char* cns = PerfDataManager::name_space("shenandoah", "regions");
 43     _name_space = NEW_C_HEAP_ARRAY(char, strlen(cns)+1, mtGC);
 44     strcpy(_name_space, cns);
 45 
 46     const char* cname = PerfDataManager::counter_name(_name_space, "timestamp");
 47     _timestamp = PerfDataManager::create_long_variable(SUN_GC, cname, PerfData::U_None, CHECK);
 48 
 49     cname = PerfDataManager::counter_name(_name_space, "max_regions");
 50     PerfDataManager::create_constant(SUN_GC, cname, PerfData::U_None, num_regions, CHECK);
 51 
 52     cname = PerfDataManager::counter_name(_name_space, "region_size");
 53     PerfDataManager::create_constant(SUN_GC, cname, PerfData::U_None, ShenandoahHeapRegion::region_size_bytes() >> 10, CHECK);
 54 
 55     cname = PerfDataManager::counter_name(_name_space, "status");
 56     _status = PerfDataManager::create_long_variable(SUN_GC, cname,
 57                                                     PerfData::U_None, CHECK);
 58 
 59     _regions_data = NEW_C_HEAP_ARRAY(PerfVariable*, num_regions, mtGC);
 60     for (uint i = 0; i < num_regions; i++) {
 61       const char* reg_name = PerfDataManager::name_space(_name_space, "region", i);
 62       const char* data_name = PerfDataManager::counter_name(reg_name, "data");
 63       const char* ns = PerfDataManager::ns_to_string(SUN_GC);
 64       const char* fullname = PerfDataManager::counter_name(ns, data_name);
 65       assert(!PerfDataManager::exists(fullname), "must not exist");
 66       _regions_data[i] = PerfDataManager::create_long_variable(SUN_GC, data_name,
 67                                                                PerfData::U_None, CHECK);
 68     }
 69   }
 70 }
 71 
 72 ShenandoahHeapRegionCounters::~ShenandoahHeapRegionCounters() {
 73   if (_name_space != nullptr) FREE_C_HEAP_ARRAY(char, _name_space);
 74 }
 75 
 76 void ShenandoahHeapRegionCounters::update() {
 77   if (ShenandoahRegionSampling) {
 78     jlong current = nanos_to_millis(os::javaTimeNanos());
 79     jlong last = _last_sample_millis;
 80     if (current - last > ShenandoahRegionSamplingRate &&
 81             Atomic::cmpxchg(&_last_sample_millis, last, current) == last) {
 82 
 83       ShenandoahHeap* heap = ShenandoahHeap::heap();
 84       jlong status = 0;
 85       if (heap->is_concurrent_mark_in_progress())      status |= 1 << 0;
 86       if (heap->is_evacuation_in_progress())           status |= 1 << 1;
 87       if (heap->is_update_refs_in_progress())          status |= 1 << 2;
 88       _status->set_value(status);
 89 
 90       _timestamp->set_value(os::elapsed_counter());
 91 
 92       size_t num_regions = heap->num_regions();
 93 
 94       {
 95         ShenandoahHeapLocker locker(heap->lock());
 96         size_t rs = ShenandoahHeapRegion::region_size_bytes();
 97         for (uint i = 0; i < num_regions; i++) {
 98           ShenandoahHeapRegion* r = heap->get_region(i);
 99           jlong data = 0;
100           data |= ((100 * r->used() / rs)                & PERCENT_MASK) << USED_SHIFT;
101           data |= ((100 * r->get_live_data_bytes() / rs) & PERCENT_MASK) << LIVE_SHIFT;
102           data |= ((100 * r->get_tlab_allocs() / rs)     & PERCENT_MASK) << TLAB_SHIFT;
103           data |= ((100 * r->get_gclab_allocs() / rs)    & PERCENT_MASK) << GCLAB_SHIFT;
104           data |= ((100 * r->get_shared_allocs() / rs)   & PERCENT_MASK) << SHARED_SHIFT;
105           data |= (r->state_ordinal() & STATUS_MASK) << STATUS_SHIFT;
106           _regions_data[i]->set_value(data);
107         }
108       }
109 
110     }
111   }
112 }