author | stuefe |
Mon, 25 Nov 2019 16:35:14 +0100 | |
branch | stuefe-new-metaspace-branch |
changeset 59257 | 990b1fed3b47 |
parent 58227 | 0e7d9a23261e |
permissions | -rw-r--r-- |
58063
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
1 |
/* |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
2 |
* Copyright (c) 2019, SAP SE. All rights reserved. |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
3 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
4 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
5 |
* |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
6 |
* This code is free software; you can redistribute it and/or modify it |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
7 |
* under the terms of the GNU General Public License version 2 only, as |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
8 |
* published by the Free Software Foundation. |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
9 |
* |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
14 |
* accompanied this code). |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
15 |
* |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
16 |
* You should have received a copy of the GNU General Public License version |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation, |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
19 |
* |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
21 |
* or visit www.oracle.com if you need additional information or have any |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
22 |
* questions. |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
23 |
* |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
24 |
*/ |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
25 |
|
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
26 |
#ifndef SHARE_MEMORY_METASPACE_COUNTER_HPP |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
27 |
#define SHARE_MEMORY_METASPACE_COUNTER_HPP |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
28 |
|
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
29 |
#include "metaprogramming/isSigned.hpp" |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
30 |
#include "runtime/atomic.hpp" |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
31 |
#include "utilities/debug.hpp" |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
32 |
#include "utilities/globalDefinitions.hpp" |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
33 |
|
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
34 |
|
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
35 |
namespace metaspace { |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
36 |
|
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
37 |
// A very simple helper class which counts something, offers decrement/increment |
59257 | 38 |
// methods and, on debug, checks for overflow/underflow on increment/decrement. |
58063
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
39 |
// |
59257 | 40 |
// Two variants exists, a thread-safe one using atomic counting, a normal one |
41 |
// which does not. |
|
58063
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
42 |
template <class T> |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
43 |
class AbstractCounter { |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
44 |
|
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
45 |
T _c; |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
46 |
|
58227 | 47 |
// Optional name for easier reference |
48 |
const char* const _name; |
|
49 |
||
58063
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
50 |
// Only allow unsigned values for now |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
51 |
STATIC_ASSERT(IsSigned<T>::value == false); |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
52 |
|
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
53 |
public: |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
54 |
|
58227 | 55 |
AbstractCounter(const char* name) : _c(0), _name(name) {} |
56 |
AbstractCounter() : _c(0), _name("") {} |
|
58063
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
57 |
|
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
58 |
T get() const { return _c; } |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
59 |
|
58227 | 60 |
void increment() { increment_by(1); } |
61 |
void decrement() { decrement_by(1); } |
|
62 |
||
63 |
void increment_by(T v) { |
|
64 |
assert(_c + v >= _c, |
|
65 |
"%s overflow (" UINT64_FORMAT "+" UINT64_FORMAT ")", |
|
66 |
_name, (uint64_t)_c, (uint64_t)v); |
|
67 |
_c += v; |
|
68 |
} |
|
69 |
||
70 |
void decrement_by(T v) { |
|
71 |
assert(_c - v <= _c, |
|
72 |
"%s underflow (" UINT64_FORMAT "-" UINT64_FORMAT ")", |
|
73 |
_name, (uint64_t)_c, (uint64_t)v); |
|
74 |
_c -= v; |
|
75 |
} |
|
58063
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
76 |
|
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
77 |
void reset() { _c = 0; } |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
78 |
|
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
79 |
#ifdef ASSERT |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
80 |
void check(T expected) const { |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
81 |
assert(_c == expected, "Counter mismatch: %d, expected: %d.", |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
82 |
(int)_c, (int)expected); |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
83 |
} |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
84 |
#endif |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
85 |
|
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
86 |
}; |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
87 |
|
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
88 |
typedef AbstractCounter<size_t> SizeCounter; |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
89 |
typedef AbstractCounter<unsigned> IntCounter; |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
90 |
|
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
91 |
|
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
92 |
template <class T> |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
93 |
class AbstractAtomicCounter { |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
94 |
|
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
95 |
volatile T _c; |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
96 |
|
58227 | 97 |
// Optional name for easier reference |
98 |
const char* const _name; |
|
99 |
||
58063
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
100 |
// Only allow unsigned values for now |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
101 |
STATIC_ASSERT(IsSigned<T>::value == false); |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
102 |
|
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
103 |
public: |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
104 |
|
58227 | 105 |
|
106 |
AbstractAtomicCounter(const char* name) : _c(0), _name(name) {} |
|
107 |
AbstractAtomicCounter() : _c(0), _name("") {} |
|
58063
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
108 |
|
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
109 |
T get() const { return _c; } |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
110 |
|
58227 | 111 |
void increment() { |
112 |
assert(_c + 1 != 0, |
|
113 |
"%s overflow (" UINT64_FORMAT "+1)", |
|
114 |
_name, (uint64_t)_c); |
|
115 |
Atomic::inc(&_c); |
|
116 |
} |
|
117 |
||
118 |
void decrement() { |
|
119 |
assert(_c >= 1, |
|
120 |
"%s underflow (" UINT64_FORMAT "-1)", |
|
121 |
_name, (uint64_t)_c); |
|
122 |
Atomic::dec(&_c); |
|
123 |
} |
|
124 |
||
125 |
void increment_by(T v) { |
|
126 |
T v1 = _c; |
|
127 |
T v2 = Atomic::add(v, &_c); |
|
128 |
assert(v2 > v1, |
|
129 |
"%s overflow (" UINT64_FORMAT "+" UINT64_FORMAT ")", |
|
130 |
_name, (uint64_t)v1, (uint64_t)v); |
|
131 |
} |
|
132 |
||
133 |
void decrement_by(T v) { |
|
134 |
assert(_c >= v, |
|
135 |
"%s underflow (" UINT64_FORMAT "-" UINT64_FORMAT ")", |
|
136 |
_name, (uint64_t)_c, (uint64_t)v); |
|
137 |
Atomic::sub(v, &_c); |
|
138 |
} |
|
58063
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
139 |
|
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
140 |
#ifdef ASSERT |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
141 |
void check(T expected) const { |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
142 |
assert(_c == expected, "Counter mismatch: %d, expected: %d.", |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
143 |
(int)_c, (int)expected); |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
144 |
} |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
145 |
#endif |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
146 |
|
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
147 |
}; |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
148 |
|
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
149 |
typedef AbstractAtomicCounter<size_t> SizeAtomicCounter; |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
150 |
|
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
151 |
|
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
152 |
|
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
153 |
} // namespace metaspace |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
154 |
|
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
155 |
#endif // SHARE_MEMORY_METASPACE_WORDSIZECOUNTER_HPP |
bdf136b8ae0e
Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents:
diff
changeset
|
156 |