annotate src/share/vm/utilities/bitMap.hpp @ 2533:842b840e67db

7046558: G1: concurrent marking optimizations Summary: Some optimizations to improve the concurrent marking phase: specialize the main oop closure, make sure a few methods in the fast path are properly inlined, a few more bits and pieces, and some cosmetic fixes. Reviewed-by: stefank, johnc
author tonyp
date Tue, 14 Jun 2011 10:33:43 -0400
parents f95d63e2154a
children 2a0172480595
rev   line source
duke@0 1 /*
tonyp@2533 2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
trims@1472 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1472 20 * or visit www.oracle.com if you need additional information or have any
trims@1472 21 * questions.
duke@0 22 *
duke@0 23 */
duke@0 24
stefank@1879 25 #ifndef SHARE_VM_UTILITIES_BITMAP_HPP
stefank@1879 26 #define SHARE_VM_UTILITIES_BITMAP_HPP
stefank@1879 27
stefank@1879 28 #include "memory/allocation.hpp"
stefank@1879 29 #include "utilities/top.hpp"
stefank@1879 30
ysr@342 31 // Forward decl;
ysr@342 32 class BitMapClosure;
duke@0 33
ysr@342 34 // Operations for bitmaps represented as arrays of unsigned integers.
ysr@342 35 // Bit offsets are numbered from 0 to size-1.
duke@0 36
duke@0 37 class BitMap VALUE_OBJ_CLASS_SPEC {
duke@0 38 friend class BitMap2D;
duke@0 39
duke@0 40 public:
duke@0 41 typedef size_t idx_t; // Type used for bit and word indices.
ysr@342 42 typedef uintptr_t bm_word_t; // Element type of array that represents
ysr@342 43 // the bitmap.
duke@0 44
duke@0 45 // Hints for range sizes.
duke@0 46 typedef enum {
duke@0 47 unknown_range, small_range, large_range
duke@0 48 } RangeSizeHint;
duke@0 49
duke@0 50 private:
ysr@342 51 bm_word_t* _map; // First word in bitmap
ysr@342 52 idx_t _size; // Size of bitmap (in bits)
duke@0 53
duke@0 54 // Puts the given value at the given offset, using resize() to size
duke@0 55 // the bitmap appropriately if needed using factor-of-two expansion.
duke@0 56 void at_put_grow(idx_t index, bool value);
duke@0 57
duke@0 58 protected:
duke@0 59 // Return the position of bit within the word that contains it (e.g., if
duke@0 60 // bitmap words are 32 bits, return a number 0 <= n <= 31).
duke@0 61 static idx_t bit_in_word(idx_t bit) { return bit & (BitsPerWord - 1); }
duke@0 62
duke@0 63 // Return a mask that will select the specified bit, when applied to the word
duke@0 64 // containing the bit.
ysr@342 65 static bm_word_t bit_mask(idx_t bit) { return (bm_word_t)1 << bit_in_word(bit); }
duke@0 66
duke@0 67 // Return the index of the word containing the specified bit.
duke@0 68 static idx_t word_index(idx_t bit) { return bit >> LogBitsPerWord; }
duke@0 69
duke@0 70 // Return the bit number of the first bit in the specified word.
duke@0 71 static idx_t bit_index(idx_t word) { return word << LogBitsPerWord; }
duke@0 72
duke@0 73 // Return the array of bitmap words, or a specific word from it.
ysr@342 74 bm_word_t* map() const { return _map; }
ysr@342 75 bm_word_t map(idx_t word) const { return _map[word]; }
duke@0 76
duke@0 77 // Return a pointer to the word containing the specified bit.
ysr@342 78 bm_word_t* word_addr(idx_t bit) const { return map() + word_index(bit); }
duke@0 79
duke@0 80 // Set a word to a specified value or to all ones; clear a word.
ysr@342 81 void set_word (idx_t word, bm_word_t val) { _map[word] = val; }
duke@0 82 void set_word (idx_t word) { set_word(word, ~(uintptr_t)0); }
duke@0 83 void clear_word(idx_t word) { _map[word] = 0; }
duke@0 84
duke@0 85 // Utilities for ranges of bits. Ranges are half-open [beg, end).
duke@0 86
duke@0 87 // Ranges within a single word.
ysr@342 88 bm_word_t inverted_bit_mask_for_range(idx_t beg, idx_t end) const;
ysr@342 89 void set_range_within_word (idx_t beg, idx_t end);
ysr@342 90 void clear_range_within_word (idx_t beg, idx_t end);
ysr@342 91 void par_put_range_within_word (idx_t beg, idx_t end, bool value);
duke@0 92
duke@0 93 // Ranges spanning entire words.
ysr@342 94 void set_range_of_words (idx_t beg, idx_t end);
ysr@342 95 void clear_range_of_words (idx_t beg, idx_t end);
ysr@342 96 void set_large_range_of_words (idx_t beg, idx_t end);
ysr@342 97 void clear_large_range_of_words (idx_t beg, idx_t end);
duke@0 98
duke@0 99 // The index of the first full word in a range.
ysr@342 100 idx_t word_index_round_up(idx_t bit) const;
duke@0 101
jcoomes@809 102 // Verification.
jcoomes@809 103 inline void verify_index(idx_t index) const NOT_DEBUG_RETURN;
jcoomes@809 104 inline void verify_range(idx_t beg_index, idx_t end_index) const
jcoomes@809 105 NOT_DEBUG_RETURN;
duke@0 106
jcoomes@809 107 // Statistics.
ysr@342 108 static idx_t* _pop_count_table;
ysr@342 109 static void init_pop_count_table();
ysr@342 110 static idx_t num_set_bits(bm_word_t w);
ysr@342 111 static idx_t num_set_bits_from_table(unsigned char c);
duke@0 112
duke@0 113 public:
duke@0 114
duke@0 115 // Constructs a bitmap with no map, and size 0.
duke@0 116 BitMap() : _map(NULL), _size(0) {}
duke@0 117
ysr@342 118 // Constructs a bitmap with the given map and size.
ysr@342 119 BitMap(bm_word_t* map, idx_t size_in_bits);
duke@0 120
ysr@342 121 // Constructs an empty bitmap of the given size (that is, this clears the
ysr@342 122 // new bitmap). Allocates the map array in resource area if
ysr@342 123 // "in_resource_area" is true, else in the C heap.
ysr@342 124 BitMap(idx_t size_in_bits, bool in_resource_area = true);
duke@0 125
ysr@342 126 // Set the map and size.
ysr@342 127 void set_map(bm_word_t* map) { _map = map; }
duke@0 128 void set_size(idx_t size_in_bits) { _size = size_in_bits; }
duke@0 129
ysr@342 130 // Allocates necessary data structure, either in the resource area
ysr@342 131 // or in the C heap, as indicated by "in_resource_area."
duke@0 132 // Preserves state currently in bit map by copying data.
duke@0 133 // Zeros any newly-addressable bits.
ysr@342 134 // If "in_resource_area" is false, frees the current map.
ysr@342 135 // (Note that this assumes that all calls to "resize" on the same BitMap
ysr@342 136 // use the same value for "in_resource_area".)
ysr@342 137 void resize(idx_t size_in_bits, bool in_resource_area = true);
duke@0 138
duke@0 139 // Accessing
duke@0 140 idx_t size() const { return _size; }
duke@0 141 idx_t size_in_words() const {
duke@0 142 return word_index(size() + BitsPerWord - 1);
duke@0 143 }
duke@0 144
duke@0 145 bool at(idx_t index) const {
duke@0 146 verify_index(index);
duke@0 147 return (*word_addr(index) & bit_mask(index)) != 0;
duke@0 148 }
duke@0 149
duke@0 150 // Align bit index up or down to the next bitmap word boundary, or check
duke@0 151 // alignment.
duke@0 152 static idx_t word_align_up(idx_t bit) {
duke@0 153 return align_size_up(bit, BitsPerWord);
duke@0 154 }
duke@0 155 static idx_t word_align_down(idx_t bit) {
duke@0 156 return align_size_down(bit, BitsPerWord);
duke@0 157 }
duke@0 158 static bool is_word_aligned(idx_t bit) {
duke@0 159 return word_align_up(bit) == bit;
duke@0 160 }
duke@0 161
duke@0 162 // Set or clear the specified bit.
duke@0 163 inline void set_bit(idx_t bit);
tonyp@2533 164 inline void clear_bit(idx_t bit);
duke@0 165
duke@0 166 // Atomically set or clear the specified bit.
tonyp@2533 167 inline bool par_set_bit(idx_t bit);
tonyp@2533 168 inline bool par_clear_bit(idx_t bit);
duke@0 169
duke@0 170 // Put the given value at the given offset. The parallel version
duke@0 171 // will CAS the value into the bitmap and is quite a bit slower.
duke@0 172 // The parallel version also returns a value indicating if the
duke@0 173 // calling thread was the one that changed the value of the bit.
duke@0 174 void at_put(idx_t index, bool value);
duke@0 175 bool par_at_put(idx_t index, bool value);
duke@0 176
duke@0 177 // Update a range of bits. Ranges are half-open [beg, end).
duke@0 178 void set_range (idx_t beg, idx_t end);
duke@0 179 void clear_range (idx_t beg, idx_t end);
duke@0 180 void set_large_range (idx_t beg, idx_t end);
duke@0 181 void clear_large_range (idx_t beg, idx_t end);
duke@0 182 void at_put_range(idx_t beg, idx_t end, bool value);
duke@0 183 void par_at_put_range(idx_t beg, idx_t end, bool value);
duke@0 184 void at_put_large_range(idx_t beg, idx_t end, bool value);
duke@0 185 void par_at_put_large_range(idx_t beg, idx_t end, bool value);
duke@0 186
duke@0 187 // Update a range of bits, using a hint about the size. Currently only
duke@0 188 // inlines the predominant case of a 1-bit range. Works best when hint is a
duke@0 189 // compile-time constant.
ysr@342 190 void set_range(idx_t beg, idx_t end, RangeSizeHint hint);
ysr@342 191 void clear_range(idx_t beg, idx_t end, RangeSizeHint hint);
ysr@342 192 void par_set_range(idx_t beg, idx_t end, RangeSizeHint hint);
ysr@342 193 void par_clear_range (idx_t beg, idx_t end, RangeSizeHint hint);
ysr@342 194
ysr@342 195 // It performs the union operation between subsets of equal length
ysr@342 196 // of two bitmaps (the target bitmap of the method and the
ysr@342 197 // from_bitmap) and stores the result to the target bitmap. The
ysr@342 198 // from_start_index represents the first bit index of the subrange
ysr@342 199 // of the from_bitmap. The to_start_index is the equivalent of the
ysr@342 200 // target bitmap. Both indexes should be word-aligned, i.e. they
ysr@342 201 // should correspond to the first bit on a bitmap word (it's up to
ysr@342 202 // the caller to ensure this; the method does check it). The length
ysr@342 203 // of the subset is specified with word_num and it is in number of
ysr@342 204 // bitmap words. The caller should ensure that this is at least 2
ysr@342 205 // (smaller ranges are not support to save extra checks). Again,
ysr@342 206 // this is checked in the method.
ysr@342 207 //
ysr@342 208 // Atomicity concerns: it is assumed that any contention on the
ysr@342 209 // target bitmap with other threads will happen on the first and
ysr@342 210 // last words; the ones in between will be "owned" exclusively by
ysr@342 211 // the calling thread and, in fact, they will already be 0. So, the
ysr@342 212 // method performs a CAS on the first word, copies the next
ysr@342 213 // word_num-2 words, and finally performs a CAS on the last word.
ysr@342 214 void mostly_disjoint_range_union(BitMap* from_bitmap,
ysr@342 215 idx_t from_start_index,
ysr@342 216 idx_t to_start_index,
ysr@342 217 size_t word_num);
ysr@342 218
duke@0 219
duke@0 220 // Clearing
duke@0 221 void clear_large();
ysr@342 222 inline void clear();
duke@0 223
ysr@342 224 // Iteration support. Returns "true" if the iteration completed, false
ysr@342 225 // if the iteration terminated early (because the closure "blk" returned
ysr@342 226 // false).
ysr@342 227 bool iterate(BitMapClosure* blk, idx_t leftIndex, idx_t rightIndex);
ysr@342 228 bool iterate(BitMapClosure* blk) {
duke@0 229 // call the version that takes an interval
ysr@342 230 return iterate(blk, 0, size());
duke@0 231 }
duke@0 232
ysr@342 233 // Looking for 1's and 0's at indices equal to or greater than "l_index",
ysr@342 234 // stopping if none has been found before "r_index", and returning
ysr@342 235 // "r_index" (which must be at most "size") in that case.
ysr@342 236 idx_t get_next_one_offset_inline (idx_t l_index, idx_t r_index) const;
ysr@342 237 idx_t get_next_zero_offset_inline(idx_t l_index, idx_t r_index) const;
ysr@342 238
ysr@342 239 // Like "get_next_one_offset_inline", except requires that "r_index" is
ysr@342 240 // aligned to bitsizeof(bm_word_t).
ysr@342 241 idx_t get_next_one_offset_inline_aligned_right(idx_t l_index,
ysr@342 242 idx_t r_index) const;
ysr@342 243
ysr@342 244 // Non-inline versionsof the above.
duke@0 245 idx_t get_next_one_offset (idx_t l_index, idx_t r_index) const;
duke@0 246 idx_t get_next_zero_offset(idx_t l_index, idx_t r_index) const;
duke@0 247
duke@0 248 idx_t get_next_one_offset(idx_t offset) const {
duke@0 249 return get_next_one_offset(offset, size());
duke@0 250 }
duke@0 251 idx_t get_next_zero_offset(idx_t offset) const {
duke@0 252 return get_next_zero_offset(offset, size());
duke@0 253 }
duke@0 254
ysr@342 255 // Returns the number of bits set in the bitmap.
ysr@342 256 idx_t count_one_bits() const;
duke@0 257
duke@0 258 // Set operations.
duke@0 259 void set_union(BitMap bits);
duke@0 260 void set_difference(BitMap bits);
duke@0 261 void set_intersection(BitMap bits);
duke@0 262 // Returns true iff "this" is a superset of "bits".
duke@0 263 bool contains(const BitMap bits) const;
duke@0 264 // Returns true iff "this and "bits" have a non-empty intersection.
duke@0 265 bool intersects(const BitMap bits) const;
duke@0 266
duke@0 267 // Returns result of whether this map changed
duke@0 268 // during the operation
duke@0 269 bool set_union_with_result(BitMap bits);
duke@0 270 bool set_difference_with_result(BitMap bits);
duke@0 271 bool set_intersection_with_result(BitMap bits);
duke@0 272
ysr@342 273 // Requires the submap of "bits" starting at offset to be at least as
ysr@342 274 // large as "this". Modifies "this" to be the intersection of its
ysr@342 275 // current contents and the submap of "bits" starting at "offset" of the
ysr@342 276 // same length as "this."
ysr@342 277 // (For expedience, currently requires the offset to be aligned to the
ysr@342 278 // bitsize of a uintptr_t. This should go away in the future though it
ysr@342 279 // will probably remain a good case to optimize.)
ysr@342 280 void set_intersection_at_offset(BitMap bits, idx_t offset);
ysr@342 281
duke@0 282 void set_from(BitMap bits);
duke@0 283
duke@0 284 bool is_same(BitMap bits);
duke@0 285
duke@0 286 // Test if all bits are set or cleared
duke@0 287 bool is_full() const;
duke@0 288 bool is_empty() const;
duke@0 289
duke@0 290
duke@0 291 #ifndef PRODUCT
duke@0 292 public:
duke@0 293 // Printing
duke@0 294 void print_on(outputStream* st) const;
duke@0 295 #endif
duke@0 296 };
duke@0 297
duke@0 298 // Convenience class wrapping BitMap which provides multiple bits per slot.
duke@0 299 class BitMap2D VALUE_OBJ_CLASS_SPEC {
duke@0 300 public:
ysr@342 301 typedef BitMap::idx_t idx_t; // Type used for bit and word indices.
ysr@342 302 typedef BitMap::bm_word_t bm_word_t; // Element type of array that
ysr@342 303 // represents the bitmap.
duke@0 304 private:
duke@0 305 BitMap _map;
duke@0 306 idx_t _bits_per_slot;
duke@0 307
duke@0 308 idx_t bit_index(idx_t slot_index, idx_t bit_within_slot_index) const {
duke@0 309 return slot_index * _bits_per_slot + bit_within_slot_index;
duke@0 310 }
duke@0 311
duke@0 312 void verify_bit_within_slot_index(idx_t index) const {
duke@0 313 assert(index < _bits_per_slot, "bit_within_slot index out of bounds");
duke@0 314 }
duke@0 315
duke@0 316 public:
duke@0 317 // Construction. bits_per_slot must be greater than 0.
ysr@342 318 BitMap2D(bm_word_t* map, idx_t size_in_slots, idx_t bits_per_slot);
duke@0 319
duke@0 320 // Allocates necessary data structure in resource area. bits_per_slot must be greater than 0.
duke@0 321 BitMap2D(idx_t size_in_slots, idx_t bits_per_slot);
duke@0 322
duke@0 323 idx_t size_in_bits() {
duke@0 324 return _map.size();
duke@0 325 }
duke@0 326
duke@0 327 // Returns number of full slots that have been allocated
duke@0 328 idx_t size_in_slots() {
duke@0 329 // Round down
duke@0 330 return _map.size() / _bits_per_slot;
duke@0 331 }
duke@0 332
duke@0 333 bool is_valid_index(idx_t slot_index, idx_t bit_within_slot_index) {
duke@0 334 verify_bit_within_slot_index(bit_within_slot_index);
duke@0 335 return (bit_index(slot_index, bit_within_slot_index) < size_in_bits());
duke@0 336 }
duke@0 337
duke@0 338 bool at(idx_t slot_index, idx_t bit_within_slot_index) const {
duke@0 339 verify_bit_within_slot_index(bit_within_slot_index);
duke@0 340 return _map.at(bit_index(slot_index, bit_within_slot_index));
duke@0 341 }
duke@0 342
duke@0 343 void set_bit(idx_t slot_index, idx_t bit_within_slot_index) {
duke@0 344 verify_bit_within_slot_index(bit_within_slot_index);
duke@0 345 _map.set_bit(bit_index(slot_index, bit_within_slot_index));
duke@0 346 }
duke@0 347
duke@0 348 void clear_bit(idx_t slot_index, idx_t bit_within_slot_index) {
duke@0 349 verify_bit_within_slot_index(bit_within_slot_index);
duke@0 350 _map.clear_bit(bit_index(slot_index, bit_within_slot_index));
duke@0 351 }
duke@0 352
duke@0 353 void at_put(idx_t slot_index, idx_t bit_within_slot_index, bool value) {
duke@0 354 verify_bit_within_slot_index(bit_within_slot_index);
duke@0 355 _map.at_put(bit_index(slot_index, bit_within_slot_index), value);
duke@0 356 }
duke@0 357
duke@0 358 void at_put_grow(idx_t slot_index, idx_t bit_within_slot_index, bool value) {
duke@0 359 verify_bit_within_slot_index(bit_within_slot_index);
duke@0 360 _map.at_put_grow(bit_index(slot_index, bit_within_slot_index), value);
duke@0 361 }
duke@0 362
ysr@342 363 void clear();
duke@0 364 };
duke@0 365
ysr@342 366 // Closure for iterating over BitMaps
duke@0 367
ysr@342 368 class BitMapClosure VALUE_OBJ_CLASS_SPEC {
ysr@342 369 public:
ysr@342 370 // Callback when bit in map is set. Should normally return "true";
ysr@342 371 // return of false indicates that the bitmap iteration should terminate.
ysr@342 372 virtual bool do_bit(BitMap::idx_t offset) = 0;
ysr@342 373 };
stefank@1879 374
stefank@1879 375 #endif // SHARE_VM_UTILITIES_BITMAP_HPP