1 /*
2 * Copyright (c) 2013, 2016, Oracle and/or its affiliates. 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 *
47 _card_counts.initialize(card_counts_storage);
48 }
49 }
50
51 G1HotCardCache::~G1HotCardCache() {
52 if (default_use_cache()) {
53 assert(_hot_cache != NULL, "Logic");
54 ArrayAllocator<jbyte*>::free(_hot_cache, _hot_cache_size);
55 _hot_cache = NULL;
56 }
57 }
58
59 jbyte* G1HotCardCache::insert(jbyte* card_ptr) {
60 uint count = _card_counts.add_card_count(card_ptr);
61 if (!_card_counts.is_hot(count)) {
62 // The card is not hot so do not store it in the cache;
63 // return it for immediate refining.
64 return card_ptr;
65 }
66 // Otherwise, the card is hot.
67 size_t index = Atomic::add(1, &_hot_cache_idx) - 1;
68 size_t masked_index = index & (_hot_cache_size - 1);
69 jbyte* current_ptr = _hot_cache[masked_index];
70
71 // Try to store the new card pointer into the cache. Compare-and-swap to guard
72 // against the unlikely event of a race resulting in another card pointer to
73 // have already been written to the cache. In this case we will return
74 // card_ptr in favor of the other option, which would be starting over. This
75 // should be OK since card_ptr will likely be the older card already when/if
76 // this ever happens.
77 jbyte* previous_ptr = (jbyte*)Atomic::cmpxchg_ptr(card_ptr,
78 &_hot_cache[masked_index],
79 current_ptr);
80 return (previous_ptr == current_ptr) ? previous_ptr : card_ptr;
81 }
82
83 void G1HotCardCache::drain(CardTableEntryClosure* cl, uint worker_i) {
84 assert(default_use_cache(), "Drain only necessary if we use the hot card cache.");
85
86 assert(_hot_cache != NULL, "Logic");
87 assert(!use_cache(), "cache should be disabled");
|
1 /*
2 * Copyright (c) 2013, 2017, Oracle and/or its affiliates. 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 *
47 _card_counts.initialize(card_counts_storage);
48 }
49 }
50
51 G1HotCardCache::~G1HotCardCache() {
52 if (default_use_cache()) {
53 assert(_hot_cache != NULL, "Logic");
54 ArrayAllocator<jbyte*>::free(_hot_cache, _hot_cache_size);
55 _hot_cache = NULL;
56 }
57 }
58
59 jbyte* G1HotCardCache::insert(jbyte* card_ptr) {
60 uint count = _card_counts.add_card_count(card_ptr);
61 if (!_card_counts.is_hot(count)) {
62 // The card is not hot so do not store it in the cache;
63 // return it for immediate refining.
64 return card_ptr;
65 }
66 // Otherwise, the card is hot.
67 size_t index = Atomic::add(1u, &_hot_cache_idx) - 1;
68 size_t masked_index = index & (_hot_cache_size - 1);
69 jbyte* current_ptr = _hot_cache[masked_index];
70
71 // Try to store the new card pointer into the cache. Compare-and-swap to guard
72 // against the unlikely event of a race resulting in another card pointer to
73 // have already been written to the cache. In this case we will return
74 // card_ptr in favor of the other option, which would be starting over. This
75 // should be OK since card_ptr will likely be the older card already when/if
76 // this ever happens.
77 jbyte* previous_ptr = (jbyte*)Atomic::cmpxchg_ptr(card_ptr,
78 &_hot_cache[masked_index],
79 current_ptr);
80 return (previous_ptr == current_ptr) ? previous_ptr : card_ptr;
81 }
82
83 void G1HotCardCache::drain(CardTableEntryClosure* cl, uint worker_i) {
84 assert(default_use_cache(), "Drain only necessary if we use the hot card cache.");
85
86 assert(_hot_cache != NULL, "Logic");
87 assert(!use_cache(), "cache should be disabled");
|