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 26 #include "gc_implementation/shenandoah/shenandoahHeap.inline.hpp" 27 #include "gc_implementation/shenandoah/shenandoahHeapRegion.inline.hpp" 28 #include "gc_implementation/shenandoah/shenandoahHeapRegionSet.hpp" 29 #include "gc_implementation/shenandoah/shenandoahHeapRegionCounters.hpp" 30 #include "memory/resourceArea.hpp" 31 #include "runtime/perfData.hpp" 32 33 ShenandoahHeapRegionCounters::ShenandoahHeapRegionCounters() : 34 _last_sample_millis(0) 35 { 36 if (UsePerfData && ShenandoahRegionSampling) { 37 EXCEPTION_MARK; 38 ResourceMark rm; 39 ShenandoahHeap* heap = ShenandoahHeap::heap(); 40 size_t num_regions = heap->num_regions(); 41 const char* cns = PerfDataManager::name_space("shenandoah", "regions"); 42 _name_space = NEW_C_HEAP_ARRAY(char, strlen(cns)+1, mtGC); 43 strcpy(_name_space, cns); 44 45 const char* cname = PerfDataManager::counter_name(_name_space, "timestamp"); 46 _timestamp = PerfDataManager::create_long_variable(SUN_GC, cname, PerfData::U_None, CHECK); 47 48 cname = PerfDataManager::counter_name(_name_space, "max_regions"); 49 PerfDataManager::create_constant(SUN_GC, cname, PerfData::U_None, num_regions, CHECK); 50 51 cname = PerfDataManager::counter_name(_name_space, "region_size"); 52 PerfDataManager::create_constant(SUN_GC, cname, PerfData::U_None, ShenandoahHeapRegion::region_size_bytes() >> 10, CHECK); 53 54 cname = PerfDataManager::counter_name(_name_space, "status"); 55 _status = PerfDataManager::create_long_variable(SUN_GC, cname, 56 PerfData::U_None, CHECK); 57 58 _regions_data = NEW_C_HEAP_ARRAY(PerfVariable*, num_regions, mtGC); 59 for (uint i = 0; i < num_regions; i++) { 60 const char* reg_name = PerfDataManager::name_space(_name_space, "region", i); 61 const char* data_name = PerfDataManager::counter_name(reg_name, "data"); 62 const char* ns = PerfDataManager::ns_to_string(SUN_GC); 63 const char* fullname = PerfDataManager::counter_name(ns, data_name); 64 assert(!PerfDataManager::exists(fullname), "must not exist"); 65 _regions_data[i] = PerfDataManager::create_long_variable(SUN_GC, data_name, 66 PerfData::U_None, CHECK); 67 } 68 } 69 } 70 71 ShenandoahHeapRegionCounters::~ShenandoahHeapRegionCounters() { 72 if (_name_space != NULL) FREE_C_HEAP_ARRAY(char, _name_space, mtGC); 73 } 74 75 void ShenandoahHeapRegionCounters::update() { 76 if (ShenandoahRegionSampling) { 77 jlong current = os::javaTimeMillis(); 78 jlong last = _last_sample_millis; 79 if (current - last > ShenandoahRegionSamplingRate && 80 Atomic::cmpxchg(current, &_last_sample_millis, last) == last) { 81 82 ShenandoahHeap* heap = ShenandoahHeap::heap(); 83 jlong status = 0; 84 if (heap->is_concurrent_mark_in_progress()) status |= 1 << 0; 85 if (heap->is_evacuation_in_progress()) status |= 1 << 1; 86 if (heap->is_update_refs_in_progress()) status |= 1 << 2; 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 }