blob: 0e8231a92c2ae80ed20b8dfa8110eeb9ed06bbcd [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 Shapiroa5d5cfd2011-06-21 12:46:59 -070016
David Sehrc431b9d2018-03-02 12:01:51 -080017#ifndef ART_LIBARTBASE_BASE_UTILS_H_
18#define ART_LIBARTBASE_BASE_UTILS_H_
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070019
Elliott Hughes92b3b562011-09-08 16:32:26 -070020#include <pthread.h>
Alex Light15324762015-11-19 11:03:10 -080021#include <stdlib.h>
Elliott Hughese222ee02012-12-13 14:41:43 -080022
Alex Light15324762015-11-19 11:03:10 -080023#include <random>
Elliott Hughes34023802011-08-30 12:06:17 -070024#include <string>
Elliott Hughes34023802011-08-30 12:06:17 -070025
Andreas Gampe57943812017-12-06 21:39:13 -080026#include <android-base/logging.h>
Andreas Gampef9411702018-09-06 17:16:57 -070027#include <android-base/parseint.h>
Andreas Gampe57943812017-12-06 21:39:13 -080028
David Sehr1979c642018-04-26 14:41:18 -070029#include "casts.h"
30#include "enums.h"
31#include "globals.h"
32#include "macros.h"
Calin Juravlebb0b53f2014-05-23 17:33:29 +010033
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070034namespace art {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070035
Ian Rogersef7d42f2014-01-06 12:55:46 -080036static 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 Carlstromdb4d5402011-08-09 12:18:28 -070041
Elliott Hughesc967f782012-04-16 10:23:15 -070042// Returns a human-readable size string such as "1MB".
Eric Holkf1e1dd12020-08-21 15:38:12 -070043std::string PrettySize(uint64_t size_in_bytes);
Ian Rogers3bb17a62012-01-27 23:56:44 -080044
Elliott Hughes48436bb2012-02-07 15:23:28 -080045// Splits a string using the given separator character into a vector of
Elliott Hughes34023802011-08-30 12:06:17 -070046// strings. Empty strings will be omitted.
Alex Light60117ae2021-02-08 17:46:15 -080047template<typename StrIn, typename Str>
48void Split(const StrIn& s, char separator, std::vector<Str>* out_result);
49
50template<typename Str>
51void Split(const Str& s, char separator, size_t len, Str* out_result);
52
53template<typename StrIn, typename Str, size_t kLen>
54void Split(const StrIn& s, char separator, std::array<Str, kLen>* out_result) {
55 Split<Str>(Str(s), separator, kLen, &((*out_result)[0]));
56}
Elliott Hughes48436bb2012-02-07 15:23:28 -080057
Elliott Hughes42ee1422011-09-06 12:33:32 -070058// Returns the calling thread's tid. (The C libraries don't expose this.)
Eric Holkf1e1dd12020-08-21 15:38:12 -070059uint32_t GetTid();
Elliott Hughes42ee1422011-09-06 12:33:32 -070060
Elliott Hughes289be852012-06-12 13:57:20 -070061// Returns the given thread's name.
62std::string GetThreadName(pid_t tid);
63
Elliott Hughesdcc24742011-09-07 14:02:44 -070064// Sets the name of the current thread. The name may be truncated to an
65// implementation-defined limit.
Elliott Hughes22869a92012-03-27 14:08:24 -070066void SetThreadName(const char* thread_name);
Elliott Hughesdcc24742011-09-07 14:02:44 -070067
David Sehr891a50e2017-10-27 17:01:07 -070068// Reads data from "/proc/self/task/${tid}/stat".
69void GetTaskStats(pid_t tid, char* state, int* utime, int* stime, int* task_cpu);
David Brazdil7b49e6c2016-09-01 11:06:18 +010070
Mathieu Chartierd22d5482012-11-06 17:14:12 -080071class VoidFunctor {
Mathieu Chartier357e9be2012-08-01 11:00:14 -070072 public:
Mathieu Chartierd22d5482012-11-06 17:14:12 -080073 template <typename A>
Roland Levillain4b8f1ec2015-08-26 18:34:03 +010074 inline void operator() (A a ATTRIBUTE_UNUSED) const {
Mathieu Chartierd22d5482012-11-06 17:14:12 -080075 }
76
77 template <typename A, typename B>
Roland Levillain4b8f1ec2015-08-26 18:34:03 +010078 inline void operator() (A a ATTRIBUTE_UNUSED, B b ATTRIBUTE_UNUSED) const {
Mathieu Chartierd22d5482012-11-06 17:14:12 -080079 }
80
81 template <typename A, typename B, typename C>
Roland Levillain4b8f1ec2015-08-26 18:34:03 +010082 inline void operator() (A a ATTRIBUTE_UNUSED, B b ATTRIBUTE_UNUSED, C c ATTRIBUTE_UNUSED) const {
Mathieu Chartierd22d5482012-11-06 17:14:12 -080083 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -070084};
85
Mathieu Chartier50030ef2015-05-08 14:19:26 -070086inline bool TestBitmap(size_t idx, const uint8_t* bitmap) {
87 return ((bitmap[idx / kBitsPerByte] >> (idx % kBitsPerByte)) & 0x01) != 0;
88}
89
Mathieu Chartiere401d142015-04-22 13:56:20 -070090static inline constexpr bool ValidPointerSize(size_t pointer_size) {
91 return pointer_size == 4 || pointer_size == 8;
92}
Mathieu Chartier50030ef2015-05-08 14:19:26 -070093
Nicolas Geoffray6bc43742015-10-12 18:11:10 +010094static 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 Light15324762015-11-19 11:03:10 -0800102#if defined(__BIONIC__)
103struct Arc4RandomGenerator {
Vladimir Marko4f990712021-07-14 12:45:13 +0100104 using result_type = uint32_t;
Alex Light15324762015-11-19 11:03:10 -0800105 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};
109using RNG = Arc4RandomGenerator;
110#else
111using RNG = std::random_device;
112#endif
113
114template <typename T>
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700115static T GetRandomNumber(T min, T max) {
Alex Light15324762015-11-19 11:03:10 -0800116 CHECK_LT(min, max);
117 std::uniform_int_distribution<T> dist(min, max);
118 RNG rng;
119 return dist(rng);
120}
121
Mathieu Chartier4d87df62016-01-07 15:14:19 -0800122// Sleep forever and never come back.
123NO_RETURN void SleepForever();
124
Orion Hodsonaeb02232019-06-25 14:18:18 +0100125// Flush CPU caches. Returns true on success, false if flush failed.
126WARN_UNUSED bool FlushCpuCaches(void* begin, void* end);
Orion Hodsonf2331362018-07-11 15:14:10 +0100127
Nicolas Geoffray8d6651d2019-07-08 10:03:16 +0100128// On some old kernels, a cache operation may segfault.
129WARN_UNUSED bool CacheOperationsMaySegFault();
130
Andreas Gampebda1d602016-08-29 17:43:45 -0700131template <typename T>
132constexpr 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
buzbee31afbec2017-03-14 15:30:19 -0700141// Return -1 if <, 0 if ==, 1 if >.
142template <typename T>
143inline 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.
148template <typename T>
149inline static int32_t Signum(T opnd) {
150 return (opnd < 0) ? -1 : ((opnd == 0) ? 0 : 1);
151}
152
Mathieu Chartier3425d022017-10-03 16:22:05 -0700153template <typename Func, typename... Args>
154static inline void CheckedCall(const Func& function, const char* what, Args... args) {
155 int rc = function(args...);
156 if (UNLIKELY(rc != 0)) {
Mathieu Chartier3425d022017-10-03 16:22:05 -0700157 PLOG(FATAL) << "Checked call failed for " << what;
158 }
159}
160
Wei Li8991ad02018-09-13 16:43:39 +0800161// 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.
164std::string GetProcessStatus(const char* key);
165
Nicolas Geoffrayccb0b5f2019-08-15 18:10:50 +0100166// 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.
169bool IsAddressKnownBackedByFileOrShared(const void* addr);
170
Nicolas Geoffray8852e532019-10-30 09:43:35 +0000171// Returns the number of threads running.
172int GetTaskCount();
173
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700174} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700175
David Sehrc431b9d2018-03-02 12:01:51 -0800176#endif // ART_LIBARTBASE_BASE_UTILS_H_