xref: /aosp_15_r20/art/libartbase/base/globals.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
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_LIBARTBASE_BASE_GLOBALS_H_
18 #define ART_LIBARTBASE_BASE_GLOBALS_H_
19 
20 #include <stddef.h>
21 #include <stdint.h>
22 
23 #include "base/macros.h"
24 
25 namespace art {
26 
27 static constexpr size_t KB = 1024;
28 static constexpr size_t MB = KB * KB;
29 static constexpr size_t GB = KB * KB * KB;
30 
31 // Runtime sizes.
32 static constexpr size_t kBitsPerByte = 8;
33 static constexpr size_t kBitsPerByteLog2 = 3;
34 static constexpr int kBitsPerIntPtrT = sizeof(intptr_t) * kBitsPerByte;
35 
36 // Required stack alignment
37 static constexpr size_t kStackAlignment = 16;
38 
39 // Minimum supported page size.
40 static constexpr size_t kMinPageSize = 4096;
41 
42 #if defined(ART_PAGE_SIZE_AGNOSTIC)
43 static constexpr bool kPageSizeAgnostic = true;
44 // Maximum supported page size.
45 static constexpr size_t kMaxPageSize = 16384;
46 #else
47 static constexpr bool kPageSizeAgnostic = false;
48 // Maximum supported page size.
49 static constexpr size_t kMaxPageSize = kMinPageSize;
50 #endif
51 
52 // Targets can have different page size (eg. 4kB or 16kB). Because Art can crosscompile, it needs
53 // to be able to generate OAT (ELF) and other image files with alignment other than the host page
54 // size. kElfSegmentAlignment needs to be equal to the largest page size supported. Effectively,
55 // this is the value to be used in images files for aligning contents to page size.
56 // However, it's temporarily set to 4096 now, to prevent dex2oat from creating sparse files.
57 // TODO(b/378794327): Fix this.
58 static constexpr size_t kElfSegmentAlignment = kMinPageSize;
59 
60 // Clion, clang analyzer, etc can falsely believe that "if (kIsDebugBuild)" always
61 // returns the same value. By wrapping into a call to another constexpr function, we force it
62 // to realize that is not actually always evaluating to the same value.
GlobalsReturnSelf(bool self)63 static constexpr bool GlobalsReturnSelf(bool self) { return self; }
64 
65 // Whether or not this is a debug build. Useful in conditionals where NDEBUG isn't.
66 // TODO: Use only __clang_analyzer__ here. b/64455231
67 #if defined(NDEBUG) && !defined(__CLION_IDE__)
68 static constexpr bool kIsDebugBuild = GlobalsReturnSelf(false);
69 #else
70 static constexpr bool kIsDebugBuild = GlobalsReturnSelf(true);
71 #endif
72 
73 #if defined(ART_PGO_INSTRUMENTATION)
74 static constexpr bool kIsPGOInstrumentation = true;
75 #else
76 static constexpr bool kIsPGOInstrumentation = false;
77 #endif
78 
79 // ART_TARGET - Defined for target builds of ART.
80 // ART_TARGET_LINUX - Defined for target Linux builds of ART.
81 // ART_TARGET_ANDROID - Defined for target Android builds of ART.
82 // ART_TARGET_FUCHSIA - Defined for Fuchsia builds of ART.
83 // Note: Either ART_TARGET_LINUX, ART_TARGET_ANDROID or ART_TARGET_FUCHSIA
84 //       need to be set when ART_TARGET is set.
85 // Note: When ART_TARGET_LINUX is defined mem_map.h will not be using Ashmem for memory mappings
86 // (usually only available on Android kernels).
87 #if defined(ART_TARGET)
88 // Useful in conditionals where ART_TARGET isn't.
89 static constexpr bool kIsTargetBuild = true;
90 # if defined(ART_TARGET_LINUX)
91 static constexpr bool kIsTargetLinux = true;
92 static constexpr bool kIsTargetFuchsia = false;
93 static constexpr bool kIsTargetAndroid = false;
94 # elif defined(ART_TARGET_ANDROID)
95 static constexpr bool kIsTargetLinux = false;
96 static constexpr bool kIsTargetFuchsia = false;
97 static constexpr bool kIsTargetAndroid = true;
98 # elif defined(ART_TARGET_FUCHSIA)
99 static constexpr bool kIsTargetLinux = false;
100 static constexpr bool kIsTargetFuchsia = true;
101 static constexpr bool kIsTargetAndroid = false;
102 # else
103 # error "Either ART_TARGET_LINUX, ART_TARGET_ANDROID or ART_TARGET_FUCHSIA " \
104         "needs to be defined for target builds."
105 # endif
106 #else
107 static constexpr bool kIsTargetBuild = false;
108 # if defined(ART_TARGET_LINUX)
109 # error "ART_TARGET_LINUX defined for host build."
110 # elif defined(ART_TARGET_ANDROID)
111 # error "ART_TARGET_ANDROID defined for host build."
112 # elif defined(ART_TARGET_FUCHSIA)
113 # error "ART_TARGET_FUCHSIA defined for host build."
114 # else
115 static constexpr bool kIsTargetLinux = false;
116 static constexpr bool kIsTargetFuchsia = false;
117 static constexpr bool kIsTargetAndroid = false;
118 # endif
119 #endif
120 
121 // Additional statically-linked ART binaries (dex2oats, oatdumps, etc.) are
122 // always available on the host
123 #if !defined(ART_TARGET)
124 static constexpr bool kHostStaticBuildEnabled = true;
125 #else
126 static constexpr bool kHostStaticBuildEnabled = false;
127 #endif
128 
129 // Within libart, gPageSize should be used to get the page size value once Runtime is initialized.
130 // For most other cases MemMap::GetPageSize() should be used instead. However, where MemMap is
131 // unavailable e.g. during static initialization or another stage when MemMap isn't yet initialized,
132 // or in a component which might operate without MemMap being initialized, the GetPageSizeSlow()
133 // would be generally suitable. For performance-sensitive code, GetPageSizeSlow() shouldn't be used
134 // without caching the value to remove repeated calls of the function.
135 #ifdef ART_PAGE_SIZE_AGNOSTIC
GetPageSizeSlow()136 inline ALWAYS_INLINE size_t GetPageSizeSlow() {
137   static_assert(kPageSizeAgnostic, "The dynamic version is only for page size agnostic build");
138 #ifdef __linux__
139   static const size_t page_size = sysconf(_SC_PAGE_SIZE);
140 #else
141   static const size_t page_size = 4096;
142 #endif
143   return page_size;
144 }
145 #else
GetPageSizeSlow()146 constexpr size_t GetPageSizeSlow() {
147   static_assert(!kPageSizeAgnostic, "The constexpr version is only for page size agnostic build");
148   return kMinPageSize;
149 }
150 #endif
151 
152 }  // namespace art
153 
154 #endif  // ART_LIBARTBASE_BASE_GLOBALS_H_
155