1/*
2 * Copyright (c) 2018, 2019, 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#ifndef SHARE_JFR_RECORDER_STORAGE_JFRMEMORYSPACERETRIEVAL_HPP
26#define SHARE_JFR_RECORDER_STORAGE_JFRMEMORYSPACERETRIEVAL_HPP
27
28#include "memory/allocation.hpp"
29#include "jfr/recorder/repository/jfrChunkWriter.hpp"
30#include "jfr/recorder/storage/jfrBuffer.hpp"
31#include "jfr/utilities/jfrAllocation.hpp"
32#include "jfr/utilities/jfrTypes.hpp"
33
34/*
35* Some policy classes for getting mspace memory
36*/
37
38template <typename Mspace>
39class JfrMspaceRetrieval : AllStatic {
40 public:
41 typedef typename Mspace::Type Type;
42 static Type* get(size_t size, Mspace* mspace, typename Mspace::Iterator& iterator, Thread* thread) {
43 while (iterator.has_next()) {
44 Type* const t = iterator.next();
45 if (t->retired()) continue;
46 if (t->try_acquire(thread)) {
47 assert(!t->retired(), "invariant");
48 if (t->free_size() >= size) {
49 return t;
50 }
51 t->set_retired();
52 mspace->register_full(t, thread);
53 }
54 }
55 return NULL;
56 }
57};
58
59template <typename Mspace>
60class JfrMspaceAlternatingRetrieval {
61 private:
62 // provides stochastic distribution over "deque" endpoints; racy is ok here
63 static bool _last_access;
64 public:
65 typedef typename Mspace::Type Type;
66 static Type* get(size_t size, Mspace* mspace, Thread* thread) {
67 typename Mspace::Iterator iterator(mspace->free(), (_last_access = !_last_access) ? forward : backward);
68 return JfrMspaceRetrieval<Mspace>::get(size, mspace, iterator, thread);
69 }
70};
71
72template <typename Mspace>
73bool JfrMspaceAlternatingRetrieval<Mspace>::_last_access = false;
74
75template <typename Mspace>
76class JfrMspaceSequentialRetrieval {
77 public:
78 typedef typename Mspace::Type Type;
79 static Type* get(size_t size, Mspace* mspace, Thread* thread) {
80 typename Mspace::Iterator iterator(mspace->free());
81 return JfrMspaceRetrieval<Mspace>::get(size, mspace, iterator, thread);
82 }
83};
84
85template <typename Mspace>
86class JfrExclusiveRetrieval : AllStatic {
87public:
88 typedef typename Mspace::Type Type;
89 static Type* get(size_t size, Mspace* mspace, typename Mspace::Iterator& iterator, Thread* thread) {
90 assert(mspace->is_locked(), "invariant");
91 if (iterator.has_next()) {
92 Type* const t = iterator.next();
93 assert(!t->retired(), "invariant");
94 assert(t->identity() == NULL, "invariant");
95 assert(t->free_size() >= size, "invariant");
96 t->acquire(thread);
97 return t;
98 }
99 return NULL;
100 }
101};
102
103template <typename Mspace>
104class JfrThreadLocalRetrieval {
105public:
106 typedef typename Mspace::Type Type;
107 static Type* get(size_t size, Mspace* mspace, Thread* thread) {
108 typename Mspace::Iterator iterator(mspace->free(), forward);
109 return JfrExclusiveRetrieval<Mspace>::get(size, mspace, iterator, thread);
110 }
111};
112
113#endif // SHARE_JFR_RECORDER_STORAGE_JFRMEMORYSPACERETRIEVAL_HPP
114