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