1 /*
  2  * Copyright (c) 2013, 2019, 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/shenandoahMemoryPool.hpp"
 27 #include "gc/shenandoah/shenandoahYoungGeneration.hpp"
 28 #include "gc/shenandoah/shenandoahOldGeneration.hpp"
 29 
 30 ShenandoahMemoryPool::ShenandoahMemoryPool(ShenandoahHeap* heap,
 31                                            const char* name) :
 32         CollectedMemoryPool(name,
 33                             heap->initial_capacity(),
 34                             heap->max_capacity(),
 35                             true /* support_usage_threshold */),
 36                             _heap(heap) {}
 37 
 38 ShenandoahMemoryPool::ShenandoahMemoryPool(ShenandoahHeap* heap,
 39                                            const char* name,
 40                                            size_t initial_capacity,
 41                                            size_t max_capacity) :
 42         CollectedMemoryPool(name,
 43                             initial_capacity,
 44                             max_capacity,
 45                             true /* support_usage_threshold */),
 46                             _heap(heap) {}
 47 
 48 
 49 MemoryUsage ShenandoahMemoryPool::get_memory_usage() {
 50   size_t initial   = initial_size();
 51   size_t max       = max_size();
 52   size_t used      = used_in_bytes();
 53   size_t committed = _heap->committed();
 54 
 55   // These asserts can never fail: max is stable, and all updates to other values never overflow max.
 56   assert(initial <= max,    "initial: "   SIZE_FORMAT ", max: "       SIZE_FORMAT, initial,   max);
 57   assert(used <= max,       "used: "      SIZE_FORMAT ", max: "       SIZE_FORMAT, used,      max);
 58   assert(committed <= max,  "committed: " SIZE_FORMAT ", max: "       SIZE_FORMAT, committed, max);
 59 
 60   // Committed and used are updated concurrently and independently. They can momentarily break
 61   // the assert below, which would also fail in downstream code. To avoid that, adjust values
 62   // to make sense under the race. See JDK-8207200.
 63   committed = MAX2(used, committed);
 64   assert(used <= committed, "used: "      SIZE_FORMAT ", committed: " SIZE_FORMAT, used,      committed);
 65 
 66   return MemoryUsage(initial, used, committed, max);
 67 }
 68 
 69 size_t ShenandoahMemoryPool::used_in_bytes() {
 70   return _heap->used();
 71 }
 72 
 73 size_t ShenandoahMemoryPool::max_size() const {
 74   return _heap->max_capacity();
 75 }
 76 
 77 ShenandoahYoungGenMemoryPool::ShenandoahYoungGenMemoryPool(ShenandoahHeap* heap) :
 78         ShenandoahMemoryPool(heap,
 79                              "Shenandoah Young Gen",
 80                              0,
 81                              heap->max_capacity()) { }
 82 
 83 MemoryUsage ShenandoahYoungGenMemoryPool::get_memory_usage() {
 84   size_t initial   = initial_size();
 85   size_t max       = max_size();
 86   size_t used      = used_in_bytes();
 87   size_t committed = _heap->young_generation()->used_regions_size();
 88 
 89   return MemoryUsage(initial, used, committed, max);
 90 }
 91 
 92 size_t ShenandoahYoungGenMemoryPool::used_in_bytes() {
 93   return _heap->young_generation()->used();
 94 }
 95 
 96 size_t ShenandoahYoungGenMemoryPool::max_size() const {
 97   return _heap->young_generation()->max_capacity();
 98 }
 99 
100 ShenandoahOldGenMemoryPool::ShenandoahOldGenMemoryPool(ShenandoahHeap* heap) :
101         ShenandoahMemoryPool(heap,
102                              "Shenandoah Old Gen",
103                              0,
104                              heap->max_capacity()) { }
105 
106 MemoryUsage ShenandoahOldGenMemoryPool::get_memory_usage() {
107   size_t initial   = initial_size();
108   size_t max       = max_size();
109   size_t used      = used_in_bytes();
110   size_t committed = _heap->old_generation()->used_regions_size();
111 
112   return MemoryUsage(initial, used, committed, max);
113 }
114 
115 size_t ShenandoahOldGenMemoryPool::used_in_bytes() {
116   return _heap->old_generation()->used();
117 }
118 
119 size_t ShenandoahOldGenMemoryPool::max_size() const {
120   return _heap->old_generation()->max_capacity();
121 }