src/share/vm/services/g1MemoryPool.hpp

Fri, 14 Jan 2011 13:47:53 -0500

author
coleenp
date
Fri, 14 Jan 2011 13:47:53 -0500
changeset 2463
17c778814856
parent 2314
f95d63e2154a
child 2821
b52782ae3880
permissions
-rw-r--r--

6811367: Fix code in HeapDumper::dump_heap() to avoid buffer overrun
Summary: Check buffer size before using and use dynamic buffer sizes for subsequent calls.
Reviewed-by: kamg, dholmes

tonyp@1524 1 /*
stefank@2314 2 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
tonyp@1524 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
tonyp@1524 4 *
tonyp@1524 5 * This code is free software; you can redistribute it and/or modify it
tonyp@1524 6 * under the terms of the GNU General Public License version 2 only, as
tonyp@1524 7 * published by the Free Software Foundation.
tonyp@1524 8 *
tonyp@1524 9 * This code is distributed in the hope that it will be useful, but WITHOUT
tonyp@1524 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
tonyp@1524 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
tonyp@1524 12 * version 2 for more details (a copy is included in the LICENSE file that
tonyp@1524 13 * accompanied this code).
tonyp@1524 14 *
tonyp@1524 15 * You should have received a copy of the GNU General Public License version
tonyp@1524 16 * 2 along with this work; if not, write to the Free Software Foundation,
tonyp@1524 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
tonyp@1524 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
tonyp@1524 22 *
tonyp@1524 23 */
tonyp@1524 24
stefank@2314 25 #ifndef SHARE_VM_SERVICES_G1MEMORYPOOL_HPP
stefank@2314 26 #define SHARE_VM_SERVICES_G1MEMORYPOOL_HPP
stefank@2314 27
stefank@2314 28 #ifndef SERIALGC
stefank@2314 29 #include "services/memoryPool.hpp"
stefank@2314 30 #include "services/memoryUsage.hpp"
stefank@2314 31 #endif
stefank@2314 32
tonyp@1524 33 class G1CollectedHeap;
tonyp@1524 34
tonyp@1524 35 // This file contains the three classes that represent the memory
tonyp@1524 36 // pools of the G1 spaces: G1EdenPool, G1SurvivorPool, and
tonyp@1524 37 // G1OldGenPool. In G1, unlike our other GCs, we do not have a
tonyp@1524 38 // physical space for each of those spaces. Instead, we allocate
tonyp@1524 39 // regions for all three spaces out of a single pool of regions (that
tonyp@1524 40 // pool basically covers the entire heap). As a result, the eden,
tonyp@1524 41 // survivor, and old gen are considered logical spaces in G1, as each
tonyp@1524 42 // is a set of non-contiguous regions. This is also reflected in the
tonyp@1524 43 // way we map them to memory pools here. The easiest way to have done
tonyp@1524 44 // this would have been to map the entire G1 heap to a single memory
tonyp@1524 45 // pool. However, it's helpful to show how large the eden and survivor
tonyp@1524 46 // get, as this does affect the performance and behavior of G1. Which
tonyp@1524 47 // is why we introduce the three memory pools implemented here.
tonyp@1524 48 //
tonyp@1524 49 // The above approach inroduces a couple of challenging issues in the
tonyp@1524 50 // implementation of the three memory pools:
tonyp@1524 51 //
tonyp@1524 52 // 1) The used space calculation for a pool is not necessarily
tonyp@1524 53 // independent of the others. We can easily get from G1 the overall
tonyp@1524 54 // used space in the entire heap, the number of regions in the young
tonyp@1524 55 // generation (includes both eden and survivors), and the number of
tonyp@1524 56 // survivor regions. So, from that we calculate:
tonyp@1524 57 //
tonyp@1524 58 // survivor_used = survivor_num * region_size
tonyp@1524 59 // eden_used = young_region_num * region_size - survivor_used
tonyp@1524 60 // old_gen_used = overall_used - eden_used - survivor_used
tonyp@1524 61 //
tonyp@1524 62 // Note that survivor_used and eden_used are upper bounds. To get the
tonyp@1524 63 // actual value we would have to iterate over the regions and add up
tonyp@1524 64 // ->used(). But that'd be expensive. So, we'll accept some lack of
tonyp@1524 65 // accuracy for those two. But, we have to be careful when calculating
tonyp@1524 66 // old_gen_used, in case we subtract from overall_used more then the
tonyp@1524 67 // actual number and our result goes negative.
tonyp@1524 68 //
tonyp@1524 69 // 2) Calculating the used space is straightforward, as described
tonyp@1524 70 // above. However, how do we calculate the committed space, given that
tonyp@1524 71 // we allocate space for the eden, survivor, and old gen out of the
tonyp@1524 72 // same pool of regions? One way to do this is to use the used value
tonyp@1524 73 // as also the committed value for the eden and survivor spaces and
tonyp@1524 74 // then calculate the old gen committed space as follows:
tonyp@1524 75 //
tonyp@1524 76 // old_gen_committed = overall_committed - eden_committed - survivor_committed
tonyp@1524 77 //
tonyp@1524 78 // Maybe a better way to do that would be to calculate used for eden
tonyp@1524 79 // and survivor as a sum of ->used() over their regions and then
tonyp@1524 80 // calculate committed as region_num * region_size (i.e., what we use
tonyp@1524 81 // to calculate the used space now). This is something to consider
tonyp@1524 82 // in the future.
tonyp@1524 83 //
tonyp@1524 84 // 3) Another decision that is again not straightforward is what is
tonyp@2109 85 // the max size that each memory pool can grow to. One way to do this
tonyp@2109 86 // would be to use the committed size for the max for the eden and
tonyp@2109 87 // survivors and calculate the old gen max as follows (basically, it's
tonyp@2109 88 // a similar pattern to what we use for the committed space, as
tonyp@2109 89 // described above):
tonyp@1524 90 //
tonyp@1524 91 // old_gen_max = overall_max - eden_max - survivor_max
tonyp@1524 92 //
tonyp@2109 93 // Unfortunately, the above makes the max of each pool fluctuate over
tonyp@2109 94 // time and, even though this is allowed according to the spec, it
tonyp@2109 95 // broke several assumptions in the M&M framework (there were cases
tonyp@2109 96 // where used would reach a value greater than max). So, for max we
tonyp@2109 97 // use -1, which means "undefined" according to the spec.
tonyp@2109 98 //
tonyp@1524 99 // 4) Now, there is a very subtle issue with all the above. The
tonyp@1524 100 // framework will call get_memory_usage() on the three pools
tonyp@1524 101 // asynchronously. As a result, each call might get a different value
tonyp@1524 102 // for, say, survivor_num which will yield inconsistent values for
tonyp@1524 103 // eden_used, survivor_used, and old_gen_used (as survivor_num is used
tonyp@1524 104 // in the calculation of all three). This would normally be
tonyp@1524 105 // ok. However, it's possible that this might cause the sum of
tonyp@1524 106 // eden_used, survivor_used, and old_gen_used to go over the max heap
tonyp@1524 107 // size and this seems to sometimes cause JConsole (and maybe other
tonyp@1524 108 // clients) to get confused. There's not a really an easy / clean
tonyp@1524 109 // solution to this problem, due to the asynchrounous nature of the
tonyp@1524 110 // framework.
tonyp@1524 111
tonyp@1524 112
tonyp@1524 113 // This class is shared by the three G1 memory pool classes
tonyp@1524 114 // (G1EdenPool, G1SurvivorPool, G1OldGenPool). Given that the way we
tonyp@1524 115 // calculate used / committed bytes for these three pools is related
tonyp@1524 116 // (see comment above), we put the calculations in this class so that
tonyp@1524 117 // we can easily share them among the subclasses.
tonyp@1524 118 class G1MemoryPoolSuper : public CollectedMemoryPool {
tonyp@1524 119 private:
tonyp@1524 120 // It returns x - y if x > y, 0 otherwise.
tonyp@1524 121 // As described in the comment above, some of the inputs to the
tonyp@1524 122 // calculations we have to do are obtained concurrently and hence
tonyp@1524 123 // may be inconsistent with each other. So, this provides a
tonyp@1524 124 // defensive way of performing the subtraction and avoids the value
tonyp@1524 125 // going negative (which would mean a very large result, given that
tonyp@1524 126 // the parameter are size_t).
tonyp@1524 127 static size_t subtract_up_to_zero(size_t x, size_t y) {
tonyp@1524 128 if (x > y) {
tonyp@1524 129 return x - y;
tonyp@1524 130 } else {
tonyp@1524 131 return 0;
tonyp@1524 132 }
tonyp@1524 133 }
tonyp@1524 134
tonyp@1524 135 protected:
tonyp@1528 136 G1CollectedHeap* _g1h;
tonyp@1528 137
tonyp@1524 138 // Would only be called from subclasses.
tonyp@1524 139 G1MemoryPoolSuper(G1CollectedHeap* g1h,
tonyp@1524 140 const char* name,
tonyp@1524 141 size_t init_size,
tonyp@1524 142 bool support_usage_threshold);
tonyp@1524 143
tonyp@1524 144 // The reason why all the code is in static methods is so that it
tonyp@1524 145 // can be safely called from the constructors of the subclasses.
tonyp@1524 146
tonyp@2109 147 static size_t undefined_max() {
tonyp@2109 148 return (size_t) -1;
tonyp@2109 149 }
tonyp@2109 150
tonyp@1524 151 static size_t overall_committed(G1CollectedHeap* g1h) {
tonyp@1524 152 return g1h->capacity();
tonyp@1524 153 }
tonyp@1524 154 static size_t overall_used(G1CollectedHeap* g1h) {
tonyp@1524 155 return g1h->used_unlocked();
tonyp@1524 156 }
tonyp@1524 157
tonyp@1524 158 static size_t eden_space_committed(G1CollectedHeap* g1h);
tonyp@1524 159 static size_t eden_space_used(G1CollectedHeap* g1h);
tonyp@1524 160
tonyp@1524 161 static size_t survivor_space_committed(G1CollectedHeap* g1h);
tonyp@1524 162 static size_t survivor_space_used(G1CollectedHeap* g1h);
tonyp@1524 163
tonyp@1524 164 static size_t old_space_committed(G1CollectedHeap* g1h);
tonyp@1524 165 static size_t old_space_used(G1CollectedHeap* g1h);
tonyp@1524 166 };
tonyp@1524 167
tonyp@1524 168 // Memory pool that represents the G1 eden.
tonyp@1524 169 class G1EdenPool : public G1MemoryPoolSuper {
tonyp@1524 170 public:
tonyp@1524 171 G1EdenPool(G1CollectedHeap* g1h);
tonyp@1524 172
tonyp@1524 173 size_t used_in_bytes() {
tonyp@1528 174 return eden_space_used(_g1h);
tonyp@1524 175 }
tonyp@1528 176 size_t max_size() const {
tonyp@2109 177 return undefined_max();
tonyp@1524 178 }
tonyp@1524 179 MemoryUsage get_memory_usage();
tonyp@1524 180 };
tonyp@1524 181
tonyp@1524 182 // Memory pool that represents the G1 survivor.
tonyp@1524 183 class G1SurvivorPool : public G1MemoryPoolSuper {
tonyp@1524 184 public:
tonyp@1524 185 G1SurvivorPool(G1CollectedHeap* g1h);
tonyp@1524 186
tonyp@1524 187 size_t used_in_bytes() {
tonyp@1528 188 return survivor_space_used(_g1h);
tonyp@1524 189 }
tonyp@1528 190 size_t max_size() const {
tonyp@2109 191 return undefined_max();
tonyp@1524 192 }
tonyp@1524 193 MemoryUsage get_memory_usage();
tonyp@1524 194 };
tonyp@1524 195
tonyp@1524 196 // Memory pool that represents the G1 old gen.
tonyp@1524 197 class G1OldGenPool : public G1MemoryPoolSuper {
tonyp@1524 198 public:
tonyp@1524 199 G1OldGenPool(G1CollectedHeap* g1h);
tonyp@1524 200
tonyp@1524 201 size_t used_in_bytes() {
tonyp@1528 202 return old_space_used(_g1h);
tonyp@1524 203 }
tonyp@1528 204 size_t max_size() const {
tonyp@2109 205 return undefined_max();
tonyp@1524 206 }
tonyp@1524 207 MemoryUsage get_memory_usage();
tonyp@1524 208 };
stefank@2314 209
stefank@2314 210 #endif // SHARE_VM_SERVICES_G1MEMORYPOOL_HPP

mercurial