1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef ART_RUNTIME_RUNTIME_GLOBALS_H_
18 #define ART_RUNTIME_RUNTIME_GLOBALS_H_
19
20 #include <android-base/logging.h>
21
22 #include "base/bit_utils.h"
23 #include "base/globals.h"
24 #include "base/macros.h"
25
26 namespace art HIDDEN {
27
28 // Size of Dex virtual registers.
29 static constexpr size_t kVRegSize = 4;
30
31 #ifdef ART_PAGE_SIZE_AGNOSTIC
32 // Accessor for the page size constant local to the libart.
33 //
34 // The value is only available after the Runtime initialization started - to ensure there is no
35 // static initialization order issues where initialization of other values is dependent on the page
36 // size. In those cases, GetPageSizeSlow() should be used.
37 struct PageSize {
PageSizePageSize38 PageSize()
39 : is_initialized_(true), is_access_allowed_(false) {}
40
size_tPageSize41 constexpr ALWAYS_INLINE operator size_t() const {
42 DCHECK(is_initialized_ && is_access_allowed_);
43 return value_;
44 }
45
46 private:
47 friend class Runtime;
48
AllowAccessPageSize49 void AllowAccess() {
50 SetAccessAllowed(true);
51 }
52
DisallowAccessPageSize53 void DisallowAccess() {
54 SetAccessAllowed(false);
55 }
56
SetAccessAllowedPageSize57 void SetAccessAllowed(bool is_allowed) {
58 // is_initialized_ is set to true when the page size value is initialized during the static
59 // initialization. This CHECK is added as an auxiliary way to help catching incorrect use of
60 // the method.
61 CHECK(is_initialized_);
62 is_access_allowed_ = is_allowed;
63 }
64
65 // The page size value.
66 //
67 // It is declared as a static constant value to ensure compiler recognizes that it doesn't change
68 // once it is initialized.
69 //
70 // It is declared as "hidden" i.e. local to the libart, to ensure:
71 // - no other library can access it, so no static initialization dependency from other libraries
72 // is possible;
73 // - the variable can be addressed via offset from the program counter, instead of the global
74 // offset table which would've added another level of indirection.
75 static const size_t value_ ALWAYS_HIDDEN;
76
77 // There are two flags in the accessor which help to ensure the value is accessed only after the
78 // static initialization is complete.
79 //
80 // is_initialized_ is used to assert the page size value is indeed initialized when the value
81 // access is allowed and when it is accessed.
82 //
83 // is_access_allowed_ is used to ensure the value is only accessed after Runtime initialization
84 // started.
85 const bool is_initialized_;
86 bool is_access_allowed_;
87 };
88
89 // gPageSize should only be used within libart. For most of the other cases MemMap::GetPageSize()
90 // or GetPageSizeSlow() should be used. See also the comment for GetPageSizeSlow().
91 extern PageSize gPageSize ALWAYS_HIDDEN;
92 #else
93 static constexpr size_t gPageSize = kMinPageSize;
94 #endif
95
96 // In the page-size-agnostic configuration the compiler may not recognise gPageSize as a
97 // power-of-two value, and may therefore miss opportunities to optimize: divisions via a
98 // right-shift, modulo via a bitwise-AND.
99 // Here, define two functions which use the optimized implementations explicitly, which should be
100 // used when dividing by or applying modulo of the page size. For simplificty, the same functions
101 // are used under both configurations, as they optimize the page-size-agnostic configuration while
102 // only replicating what the compiler already does on the non-page-size-agnostic configuration.
DivideByPageSize(size_t num)103 static constexpr ALWAYS_INLINE size_t DivideByPageSize(size_t num) {
104 return (num >> WhichPowerOf2(static_cast<size_t>(gPageSize)));
105 }
ModuloPageSize(size_t num)106 static constexpr ALWAYS_INLINE size_t ModuloPageSize(size_t num) {
107 return (num & (gPageSize-1));
108 }
109
110 // Returns whether the given memory offset can be used for generating
111 // an implicit null check.
CanDoImplicitNullCheckOn(uintptr_t offset)112 static inline bool CanDoImplicitNullCheckOn(uintptr_t offset) {
113 return offset < gPageSize;
114 }
115
116 // Required object alignment
117 static constexpr size_t kObjectAlignmentShift = 3;
118 static constexpr size_t kObjectAlignment = 1u << kObjectAlignmentShift;
119
120 // Garbage collector constants.
121 static constexpr bool kMovingCollector = true;
122 static constexpr bool kMarkCompactSupport = false && kMovingCollector;
123 // True if we allow moving classes.
124 static constexpr bool kMovingClasses = !kMarkCompactSupport;
125 // When using the Concurrent Copying (CC) collector, if
126 // `ART_USE_GENERATIONAL_CC` is true, enable generational collection by default,
127 // i.e. use sticky-bit CC for minor collections and (full) CC for major
128 // collections.
129 // This default value can be overridden with the runtime option
130 // `-Xgc:[no]generational_cc`.
131 //
132 // TODO(b/67628039): Consider either:
133 // - renaming this to a better descriptive name (e.g.
134 // `ART_USE_GENERATIONAL_CC_BY_DEFAULT`); or
135 // - removing `ART_USE_GENERATIONAL_CC` and having a fixed default value.
136 // Any of these changes will require adjusting users of this preprocessor
137 // directive and the corresponding build system environment variable (e.g. in
138 // ART's continuous testing).
139 #ifdef ART_USE_GENERATIONAL_CC
140 static constexpr bool kEnableGenerationalCCByDefault = true;
141 #else
142 static constexpr bool kEnableGenerationalCCByDefault = false;
143 #endif
144
145 // If true, enable the tlab allocator by default.
146 #ifdef ART_USE_TLAB
147 static constexpr bool kUseTlab = true;
148 #else
149 static constexpr bool kUseTlab = false;
150 #endif
151
152 // Kinds of tracing clocks.
153 enum class TraceClockSource {
154 kThreadCpu,
155 kWall,
156 kDual, // Both wall and thread CPU clocks.
157 };
158
159 #if defined(__linux__)
160 static constexpr TraceClockSource kDefaultTraceClockSource = TraceClockSource::kDual;
161 #else
162 static constexpr TraceClockSource kDefaultTraceClockSource = TraceClockSource::kWall;
163 #endif
164
165 static constexpr bool kDefaultMustRelocate = true;
166
167 // Size of a heap reference.
168 static constexpr size_t kHeapReferenceSize = sizeof(uint32_t);
169
170 } // namespace art
171
172 #endif // ART_RUNTIME_RUNTIME_GLOBALS_H_
173