Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 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 | */ |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 16 | |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 17 | #ifndef ART_LIBARTBASE_BASE_UTILS_H_ |
| 18 | #define ART_LIBARTBASE_BASE_UTILS_H_ |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 19 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 20 | #include <pthread.h> |
Alex Light | 1532476 | 2015-11-19 11:03:10 -0800 | [diff] [blame] | 21 | #include <stdlib.h> |
Elliott Hughes | e222ee0 | 2012-12-13 14:41:43 -0800 | [diff] [blame] | 22 | |
Alex Light | 1532476 | 2015-11-19 11:03:10 -0800 | [diff] [blame] | 23 | #include <random> |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 24 | #include <string> |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 25 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 26 | #include <android-base/logging.h> |
Andreas Gampe | f941170 | 2018-09-06 17:16:57 -0700 | [diff] [blame] | 27 | #include <android-base/parseint.h> |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 28 | |
David Sehr | 1979c64 | 2018-04-26 14:41:18 -0700 | [diff] [blame] | 29 | #include "casts.h" |
| 30 | #include "enums.h" |
| 31 | #include "globals.h" |
| 32 | #include "macros.h" |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 33 | |
Lokesh Gidra | ca5ed9f | 2022-04-20 01:39:28 +0000 | [diff] [blame] | 34 | #if defined(__linux__) |
| 35 | #include <sys/utsname.h> |
| 36 | #endif |
| 37 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 38 | namespace art { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 39 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 40 | static inline uint32_t PointerToLowMemUInt32(const void* p) { |
| 41 | uintptr_t intp = reinterpret_cast<uintptr_t>(p); |
| 42 | DCHECK_LE(intp, 0xFFFFFFFFU); |
| 43 | return intp & 0xFFFFFFFFU; |
| 44 | } |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 45 | |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 46 | // Returns a human-readable size string such as "1MB". |
Eric Holk | f1e1dd1 | 2020-08-21 15:38:12 -0700 | [diff] [blame] | 47 | std::string PrettySize(uint64_t size_in_bytes); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 48 | |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 49 | // Splits a string using the given separator character into a vector of |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 50 | // strings. Empty strings will be omitted. |
Alex Light | 60117ae | 2021-02-08 17:46:15 -0800 | [diff] [blame] | 51 | template<typename StrIn, typename Str> |
| 52 | void Split(const StrIn& s, char separator, std::vector<Str>* out_result); |
| 53 | |
| 54 | template<typename Str> |
| 55 | void Split(const Str& s, char separator, size_t len, Str* out_result); |
| 56 | |
| 57 | template<typename StrIn, typename Str, size_t kLen> |
| 58 | void Split(const StrIn& s, char separator, std::array<Str, kLen>* out_result) { |
| 59 | Split<Str>(Str(s), separator, kLen, &((*out_result)[0])); |
| 60 | } |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 61 | |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 62 | // Returns the calling thread's tid. (The C libraries don't expose this.) |
Eric Holk | f1e1dd1 | 2020-08-21 15:38:12 -0700 | [diff] [blame] | 63 | uint32_t GetTid(); |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 64 | |
Elliott Hughes | 289be85 | 2012-06-12 13:57:20 -0700 | [diff] [blame] | 65 | // Returns the given thread's name. |
| 66 | std::string GetThreadName(pid_t tid); |
| 67 | |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 68 | // Sets the name of the current thread. The name may be truncated to an |
| 69 | // implementation-defined limit. |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 70 | void SetThreadName(const char* thread_name); |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 71 | |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 72 | // Reads data from "/proc/self/task/${tid}/stat". |
| 73 | void GetTaskStats(pid_t tid, char* state, int* utime, int* stime, int* task_cpu); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 74 | |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 75 | class VoidFunctor { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 76 | public: |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 77 | template <typename A> |
Stefano Cianciulli | 78f3c72 | 2023-05-16 10:32:54 +0000 | [diff] [blame] | 78 | inline void operator()([[maybe_unused]] A a) const {} |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 79 | |
| 80 | template <typename A, typename B> |
Stefano Cianciulli | 78f3c72 | 2023-05-16 10:32:54 +0000 | [diff] [blame] | 81 | inline void operator()([[maybe_unused]] A a, [[maybe_unused]] B b) const {} |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 82 | |
| 83 | template <typename A, typename B, typename C> |
Stefano Cianciulli | 78f3c72 | 2023-05-16 10:32:54 +0000 | [diff] [blame] | 84 | inline void operator()([[maybe_unused]] A a, [[maybe_unused]] B b, [[maybe_unused]] C c) const {} |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
Mathieu Chartier | 50030ef | 2015-05-08 14:19:26 -0700 | [diff] [blame] | 87 | inline bool TestBitmap(size_t idx, const uint8_t* bitmap) { |
| 88 | return ((bitmap[idx / kBitsPerByte] >> (idx % kBitsPerByte)) & 0x01) != 0; |
| 89 | } |
| 90 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 91 | static inline constexpr bool ValidPointerSize(size_t pointer_size) { |
| 92 | return pointer_size == 4 || pointer_size == 8; |
| 93 | } |
Mathieu Chartier | 50030ef | 2015-05-08 14:19:26 -0700 | [diff] [blame] | 94 | |
Nicolas Geoffray | 6bc4374 | 2015-10-12 18:11:10 +0100 | [diff] [blame] | 95 | static inline const void* EntryPointToCodePointer(const void* entry_point) { |
| 96 | uintptr_t code = reinterpret_cast<uintptr_t>(entry_point); |
| 97 | // TODO: Make this Thumb2 specific. It is benign on other architectures as code is always at |
| 98 | // least 2 byte aligned. |
| 99 | code &= ~0x1; |
| 100 | return reinterpret_cast<const void*>(code); |
| 101 | } |
| 102 | |
Alex Light | 1532476 | 2015-11-19 11:03:10 -0800 | [diff] [blame] | 103 | #if defined(__BIONIC__) |
| 104 | struct Arc4RandomGenerator { |
Vladimir Marko | 4f99071 | 2021-07-14 12:45:13 +0100 | [diff] [blame] | 105 | using result_type = uint32_t; |
Alex Light | 1532476 | 2015-11-19 11:03:10 -0800 | [diff] [blame] | 106 | static constexpr uint32_t min() { return std::numeric_limits<uint32_t>::min(); } |
| 107 | static constexpr uint32_t max() { return std::numeric_limits<uint32_t>::max(); } |
| 108 | uint32_t operator() () { return arc4random(); } |
| 109 | }; |
| 110 | using RNG = Arc4RandomGenerator; |
| 111 | #else |
| 112 | using RNG = std::random_device; |
| 113 | #endif |
| 114 | |
| 115 | template <typename T> |
Mathieu Chartier | dc00f18 | 2016-07-14 10:10:44 -0700 | [diff] [blame] | 116 | static T GetRandomNumber(T min, T max) { |
Alex Light | 1532476 | 2015-11-19 11:03:10 -0800 | [diff] [blame] | 117 | CHECK_LT(min, max); |
| 118 | std::uniform_int_distribution<T> dist(min, max); |
| 119 | RNG rng; |
| 120 | return dist(rng); |
| 121 | } |
| 122 | |
Mathieu Chartier | 4d87df6 | 2016-01-07 15:14:19 -0800 | [diff] [blame] | 123 | // Sleep forever and never come back. |
| 124 | NO_RETURN void SleepForever(); |
| 125 | |
Orion Hodson | aeb0223 | 2019-06-25 14:18:18 +0100 | [diff] [blame] | 126 | // Flush CPU caches. Returns true on success, false if flush failed. |
| 127 | WARN_UNUSED bool FlushCpuCaches(void* begin, void* end); |
Orion Hodson | f233136 | 2018-07-11 15:14:10 +0100 | [diff] [blame] | 128 | |
Lokesh Gidra | ca5ed9f | 2022-04-20 01:39:28 +0000 | [diff] [blame] | 129 | #if defined(__linux__) |
| 130 | bool IsKernelVersionAtLeast(int reqd_major, int reqd_minor); |
| 131 | #endif |
| 132 | |
Nicolas Geoffray | 8d6651d | 2019-07-08 10:03:16 +0100 | [diff] [blame] | 133 | // On some old kernels, a cache operation may segfault. |
| 134 | WARN_UNUSED bool CacheOperationsMaySegFault(); |
| 135 | |
Andreas Gampe | bda1d60 | 2016-08-29 17:43:45 -0700 | [diff] [blame] | 136 | template <typename T> |
| 137 | constexpr PointerSize ConvertToPointerSize(T any) { |
| 138 | if (any == 4 || any == 8) { |
| 139 | return static_cast<PointerSize>(any); |
| 140 | } else { |
| 141 | LOG(FATAL); |
| 142 | UNREACHABLE(); |
| 143 | } |
| 144 | } |
| 145 | |
buzbee | 31afbec | 2017-03-14 15:30:19 -0700 | [diff] [blame] | 146 | // Return -1 if <, 0 if ==, 1 if >. |
| 147 | template <typename T> |
| 148 | inline static int32_t Compare(T lhs, T rhs) { |
| 149 | return (lhs < rhs) ? -1 : ((lhs == rhs) ? 0 : 1); |
| 150 | } |
| 151 | |
| 152 | // Return -1 if < 0, 0 if == 0, 1 if > 0. |
| 153 | template <typename T> |
| 154 | inline static int32_t Signum(T opnd) { |
| 155 | return (opnd < 0) ? -1 : ((opnd == 0) ? 0 : 1); |
| 156 | } |
| 157 | |
Mathieu Chartier | 3425d02 | 2017-10-03 16:22:05 -0700 | [diff] [blame] | 158 | template <typename Func, typename... Args> |
| 159 | static inline void CheckedCall(const Func& function, const char* what, Args... args) { |
| 160 | int rc = function(args...); |
| 161 | if (UNLIKELY(rc != 0)) { |
Mathieu Chartier | 3425d02 | 2017-10-03 16:22:05 -0700 | [diff] [blame] | 162 | PLOG(FATAL) << "Checked call failed for " << what; |
| 163 | } |
| 164 | } |
| 165 | |
Krzysztof KosiĆski | 58b1b82 | 2022-09-21 01:52:56 +0000 | [diff] [blame] | 166 | // Forces the compiler to emit a load instruction, but discards the value. |
| 167 | // Useful when dealing with memory paging. |
| 168 | template <typename T> |
| 169 | inline void ForceRead(const T* pointer) { |
| 170 | static_cast<void>(*const_cast<volatile T*>(pointer)); |
| 171 | } |
| 172 | |
Wei Li | 8991ad0 | 2018-09-13 16:43:39 +0800 | [diff] [blame] | 173 | // Lookup value for a given key in /proc/self/status. Keys and values are separated by a ':' in |
| 174 | // the status file. Returns value found on success and "<unknown>" if the key is not found or |
| 175 | // there is an I/O error. |
| 176 | std::string GetProcessStatus(const char* key); |
| 177 | |
Nicolas Geoffray | ccb0b5f | 2019-08-15 18:10:50 +0100 | [diff] [blame] | 178 | // Return whether the address is guaranteed to be backed by a file or is shared. |
| 179 | // This information can be used to know whether MADV_DONTNEED will make |
| 180 | // following accesses repopulate the memory or return zero. |
| 181 | bool IsAddressKnownBackedByFileOrShared(const void* addr); |
| 182 | |
Nicolas Geoffray | 8852e53 | 2019-10-30 09:43:35 +0000 | [diff] [blame] | 183 | // Returns the number of threads running. |
| 184 | int GetTaskCount(); |
| 185 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 186 | } // namespace art |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 187 | |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 188 | #endif // ART_LIBARTBASE_BASE_UTILS_H_ |