1 /*
   2  * Copyright (c) 2018, 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  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "gc/g1/g1RegionMarkStatsCache.inline.hpp"
  27 #include "memory/allocation.inline.hpp"
  28 
  29 G1RegionMarkStatsCache::G1RegionMarkStatsCache(G1RegionMarkStats* target, uint max_regions, uint num_cache_entries) :
  30   _num_stats(max_regions),
  31   _target(target),
  32   _num_cache_entries(num_cache_entries),
  33   _cache_hits(0),
  34   _cache_misses(0) {
  35 
  36   guarantee(is_power_of_2(num_cache_entries),
  37             "Number of cache entries must be power of two, but is %u", num_cache_entries);
  38   _cache = NEW_C_HEAP_ARRAY(G1RegionMarkStatsCacheEntry, _num_cache_entries, mtGC);
  39   for (uint i = 0; i < _num_cache_entries; i++) {
  40     _cache[i].clear();
  41   }
  42   _num_cache_entries_mask = _num_cache_entries - 1;
  43 }
  44 
  45 G1RegionMarkStatsCache::~G1RegionMarkStatsCache() {
  46   FREE_C_HEAP_ARRAY(G1RegionMarkStatsCacheEntry, _cache);
  47 }
  48 
  49 // Evict all remaining statistics, returning cache hits and misses.
  50 Pair<size_t, size_t> G1RegionMarkStatsCache::evict_all() {
  51   for (uint i = 0; i < _num_cache_entries; i++) {
  52     evict(i);
  53   }
  54   return Pair<size_t,size_t>(_cache_hits, _cache_misses);
  55 }
  56 
  57 // Reset all cache entries to their default values.
  58 void G1RegionMarkStatsCache::reset() {
  59   _cache_hits = 0;
  60   _cache_misses = 0;
  61 
  62   for (uint i = 0; i < _num_cache_entries; i++) {
  63     _cache[i].clear();
  64   }
  65 }