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