1/*
2 * Copyright (c) 2012, 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 *
23 */
24
25#include "precompiled.hpp"
26#include "gc/cms/adaptiveFreeList.hpp"
27#include "gc/cms/freeChunk.hpp"
28#include "gc/shared/collectedHeap.hpp"
29#include "runtime/globals.hpp"
30#include "runtime/mutex.hpp"
31#include "runtime/orderAccess.inline.hpp"
32#include "runtime/vmThread.hpp"
33
34template <>
35void AdaptiveFreeList<FreeChunk>::print_on(outputStream* st, const char* c) const {
36  if (c != NULL) {
37    st->print("%16s", c);
38  } else {
39    st->print(SIZE_FORMAT_W(16), size());
40  }
41  st->print("\t"
42           SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\t"
43           SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\n",
44           bfr_surp(),             surplus(),             desired(),             prev_sweep(),           before_sweep(),
45           count(),               coal_births(),          coal_deaths(),          split_births(),         split_deaths());
46}
47
48template <class Chunk>
49AdaptiveFreeList<Chunk>::AdaptiveFreeList() : FreeList<Chunk>(), _hint(0) {
50  init_statistics();
51}
52
53template <class Chunk>
54void AdaptiveFreeList<Chunk>::initialize() {
55  FreeList<Chunk>::initialize();
56  set_hint(0);
57  init_statistics(true /* split_birth */);
58}
59
60template <class Chunk>
61void AdaptiveFreeList<Chunk>::reset(size_t hint) {
62  FreeList<Chunk>::reset();
63  set_hint(hint);
64}
65
66template <class Chunk>
67void AdaptiveFreeList<Chunk>::init_statistics(bool split_birth) {
68  _allocation_stats.initialize(split_birth);
69}
70
71template <class Chunk>
72size_t AdaptiveFreeList<Chunk>::get_better_size() {
73
74  // A candidate chunk has been found.  If it is already under
75  // populated and there is a hinT, REturn the hint().  Else
76  // return the size of this chunk.
77  if (surplus() <= 0) {
78    if (hint() != 0) {
79      return hint();
80    } else {
81      return size();
82    }
83  } else {
84    // This list has a surplus so use it.
85    return size();
86  }
87}
88
89
90template <class Chunk>
91void AdaptiveFreeList<Chunk>::return_chunk_at_head(Chunk* chunk) {
92  assert_proper_lock_protection();
93  return_chunk_at_head(chunk, true);
94}
95
96template <class Chunk>
97void AdaptiveFreeList<Chunk>::return_chunk_at_head(Chunk* chunk, bool record_return) {
98  FreeList<Chunk>::return_chunk_at_head(chunk, record_return);
99#ifdef ASSERT
100  if (record_return) {
101    increment_returned_bytes_by(size()*HeapWordSize);
102  }
103#endif
104}
105
106template <class Chunk>
107void AdaptiveFreeList<Chunk>::return_chunk_at_tail(Chunk* chunk) {
108  AdaptiveFreeList<Chunk>::return_chunk_at_tail(chunk, true);
109}
110
111template <class Chunk>
112void AdaptiveFreeList<Chunk>::return_chunk_at_tail(Chunk* chunk, bool record_return) {
113  FreeList<Chunk>::return_chunk_at_tail(chunk, record_return);
114#ifdef ASSERT
115  if (record_return) {
116    increment_returned_bytes_by(size()*HeapWordSize);
117  }
118#endif
119}
120
121#ifndef PRODUCT
122template <class Chunk>
123void AdaptiveFreeList<Chunk>::verify_stats() const {
124  // The +1 of the LH comparand is to allow some "looseness" in
125  // checking: we usually call this interface when adding a block
126  // and we'll subsequently update the stats; we cannot update the
127  // stats beforehand because in the case of the large-block BT
128  // dictionary for example, this might be the first block and
129  // in that case there would be no place that we could record
130  // the stats (which are kept in the block itself).
131  assert((_allocation_stats.prev_sweep() + _allocation_stats.split_births()
132          + _allocation_stats.coal_births() + 1)   // Total Production Stock + 1
133         >= (_allocation_stats.split_deaths() + _allocation_stats.coal_deaths()
134             + (ssize_t)count()),                // Total Current Stock + depletion
135         "FreeList " PTR_FORMAT " of size " SIZE_FORMAT
136         " violates Conservation Principle: "
137         "prev_sweep(" SIZE_FORMAT ")"
138         " + split_births(" SIZE_FORMAT ")"
139         " + coal_births(" SIZE_FORMAT ") + 1 >= "
140         " split_deaths(" SIZE_FORMAT ")"
141         " coal_deaths(" SIZE_FORMAT ")"
142         " + count(" SSIZE_FORMAT ")",
143         p2i(this), size(), _allocation_stats.prev_sweep(), _allocation_stats.split_births(),
144         _allocation_stats.coal_births(), _allocation_stats.split_deaths(),
145         _allocation_stats.coal_deaths(), count());
146}
147#endif
148
149// Needs to be after the definitions have been seen.
150template class AdaptiveFreeList<FreeChunk>;
151