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_LIBARTBASE_BASE_UTILS_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_LIBARTBASE_BASE_UTILS_H_
19*795d594fSAndroid Build Coastguard Worker
20*795d594fSAndroid Build Coastguard Worker #include <pthread.h>
21*795d594fSAndroid Build Coastguard Worker #include <stdlib.h>
22*795d594fSAndroid Build Coastguard Worker
23*795d594fSAndroid Build Coastguard Worker #include <random>
24*795d594fSAndroid Build Coastguard Worker #include <string>
25*795d594fSAndroid Build Coastguard Worker
26*795d594fSAndroid Build Coastguard Worker #include <android-base/logging.h>
27*795d594fSAndroid Build Coastguard Worker #include <android-base/parseint.h>
28*795d594fSAndroid Build Coastguard Worker
29*795d594fSAndroid Build Coastguard Worker #include "casts.h"
30*795d594fSAndroid Build Coastguard Worker #include "globals.h"
31*795d594fSAndroid Build Coastguard Worker #include "macros.h"
32*795d594fSAndroid Build Coastguard Worker #include "pointer_size.h"
33*795d594fSAndroid Build Coastguard Worker
34*795d594fSAndroid Build Coastguard Worker #if defined(__linux__)
35*795d594fSAndroid Build Coastguard Worker #include <sys/utsname.h>
36*795d594fSAndroid Build Coastguard Worker #endif
37*795d594fSAndroid Build Coastguard Worker
38*795d594fSAndroid Build Coastguard Worker namespace art {
39*795d594fSAndroid Build Coastguard Worker
PointerToLowMemUInt32(const void * p)40*795d594fSAndroid Build Coastguard Worker static inline uint32_t PointerToLowMemUInt32(const void* p) {
41*795d594fSAndroid Build Coastguard Worker uintptr_t intp = reinterpret_cast<uintptr_t>(p);
42*795d594fSAndroid Build Coastguard Worker DCHECK_LE(intp, 0xFFFFFFFFU);
43*795d594fSAndroid Build Coastguard Worker return intp & 0xFFFFFFFFU;
44*795d594fSAndroid Build Coastguard Worker }
45*795d594fSAndroid Build Coastguard Worker
46*795d594fSAndroid Build Coastguard Worker // Returns a human-readable size string such as "1MB".
47*795d594fSAndroid Build Coastguard Worker std::string PrettySize(uint64_t size_in_bytes);
48*795d594fSAndroid Build Coastguard Worker
49*795d594fSAndroid Build Coastguard Worker // Splits a string using the given separator character into a vector of
50*795d594fSAndroid Build Coastguard Worker // strings. Empty strings will be omitted.
51*795d594fSAndroid Build Coastguard Worker template<typename StrIn, typename Str>
52*795d594fSAndroid Build Coastguard Worker void Split(const StrIn& s, char separator, std::vector<Str>* out_result);
53*795d594fSAndroid Build Coastguard Worker
54*795d594fSAndroid Build Coastguard Worker template<typename Str>
55*795d594fSAndroid Build Coastguard Worker void Split(const Str& s, char separator, size_t len, Str* out_result);
56*795d594fSAndroid Build Coastguard Worker
57*795d594fSAndroid Build Coastguard Worker template<typename StrIn, typename Str, size_t kLen>
Split(const StrIn & s,char separator,std::array<Str,kLen> * out_result)58*795d594fSAndroid Build Coastguard Worker void Split(const StrIn& s, char separator, std::array<Str, kLen>* out_result) {
59*795d594fSAndroid Build Coastguard Worker Split<Str>(Str(s), separator, kLen, &((*out_result)[0]));
60*795d594fSAndroid Build Coastguard Worker }
61*795d594fSAndroid Build Coastguard Worker
62*795d594fSAndroid Build Coastguard Worker // Returns the calling thread's tid. (The C libraries don't expose this.)
63*795d594fSAndroid Build Coastguard Worker uint32_t GetTid();
64*795d594fSAndroid Build Coastguard Worker
65*795d594fSAndroid Build Coastguard Worker // Returns the given thread's name.
66*795d594fSAndroid Build Coastguard Worker std::string GetThreadName(pid_t tid);
67*795d594fSAndroid Build Coastguard Worker
68*795d594fSAndroid Build Coastguard Worker // Sets the pthread name of the current thread. The name may be truncated to an
69*795d594fSAndroid Build Coastguard Worker // implementation-defined limit.
70*795d594fSAndroid Build Coastguard Worker void SetThreadName(const char* thread_name);
71*795d594fSAndroid Build Coastguard Worker
72*795d594fSAndroid Build Coastguard Worker // Sets the pthread name of the given thread. The name may be truncated to an
73*795d594fSAndroid Build Coastguard Worker // implementation-defined limit. Does nothing if not supported by the OS.
74*795d594fSAndroid Build Coastguard Worker void SetThreadName(pthread_t thr, const char* thread_name);
75*795d594fSAndroid Build Coastguard Worker
76*795d594fSAndroid Build Coastguard Worker // Reads data from "/proc/self/task/${tid}/stat".
77*795d594fSAndroid Build Coastguard Worker void GetTaskStats(pid_t tid, char* state, int* utime, int* stime, int* task_cpu);
78*795d594fSAndroid Build Coastguard Worker
79*795d594fSAndroid Build Coastguard Worker class VoidFunctor {
80*795d594fSAndroid Build Coastguard Worker public:
81*795d594fSAndroid Build Coastguard Worker template <typename A>
operator()82*795d594fSAndroid Build Coastguard Worker inline void operator()([[maybe_unused]] A a) const {}
83*795d594fSAndroid Build Coastguard Worker
84*795d594fSAndroid Build Coastguard Worker template <typename A, typename B>
operator()85*795d594fSAndroid Build Coastguard Worker inline void operator()([[maybe_unused]] A a, [[maybe_unused]] B b) const {}
86*795d594fSAndroid Build Coastguard Worker
87*795d594fSAndroid Build Coastguard Worker template <typename A, typename B, typename C>
operator()88*795d594fSAndroid Build Coastguard Worker inline void operator()([[maybe_unused]] A a, [[maybe_unused]] B b, [[maybe_unused]] C c) const {}
89*795d594fSAndroid Build Coastguard Worker };
90*795d594fSAndroid Build Coastguard Worker
EntryPointToCodePointer(const void * entry_point)91*795d594fSAndroid Build Coastguard Worker static inline const void* EntryPointToCodePointer(const void* entry_point) {
92*795d594fSAndroid Build Coastguard Worker uintptr_t code = reinterpret_cast<uintptr_t>(entry_point);
93*795d594fSAndroid Build Coastguard Worker // TODO: Make this Thumb2 specific. It is benign on other architectures as code is always at
94*795d594fSAndroid Build Coastguard Worker // least 2 byte aligned.
95*795d594fSAndroid Build Coastguard Worker code &= ~0x1;
96*795d594fSAndroid Build Coastguard Worker return reinterpret_cast<const void*>(code);
97*795d594fSAndroid Build Coastguard Worker }
98*795d594fSAndroid Build Coastguard Worker
99*795d594fSAndroid Build Coastguard Worker #if defined(__BIONIC__)
100*795d594fSAndroid Build Coastguard Worker struct Arc4RandomGenerator {
101*795d594fSAndroid Build Coastguard Worker using result_type = uint32_t;
minArc4RandomGenerator102*795d594fSAndroid Build Coastguard Worker static constexpr uint32_t min() { return std::numeric_limits<uint32_t>::min(); }
maxArc4RandomGenerator103*795d594fSAndroid Build Coastguard Worker static constexpr uint32_t max() { return std::numeric_limits<uint32_t>::max(); }
operatorArc4RandomGenerator104*795d594fSAndroid Build Coastguard Worker uint32_t operator() () { return arc4random(); }
105*795d594fSAndroid Build Coastguard Worker };
106*795d594fSAndroid Build Coastguard Worker using RNG = Arc4RandomGenerator;
107*795d594fSAndroid Build Coastguard Worker #else
108*795d594fSAndroid Build Coastguard Worker using RNG = std::random_device;
109*795d594fSAndroid Build Coastguard Worker #endif
110*795d594fSAndroid Build Coastguard Worker
111*795d594fSAndroid Build Coastguard Worker template <typename T>
GetRandomNumber(T min,T max)112*795d594fSAndroid Build Coastguard Worker static T GetRandomNumber(T min, T max) {
113*795d594fSAndroid Build Coastguard Worker CHECK_LT(min, max);
114*795d594fSAndroid Build Coastguard Worker std::uniform_int_distribution<T> dist(min, max);
115*795d594fSAndroid Build Coastguard Worker RNG rng;
116*795d594fSAndroid Build Coastguard Worker return dist(rng);
117*795d594fSAndroid Build Coastguard Worker }
118*795d594fSAndroid Build Coastguard Worker
119*795d594fSAndroid Build Coastguard Worker // Sleep forever and never come back.
120*795d594fSAndroid Build Coastguard Worker NO_RETURN void SleepForever();
121*795d594fSAndroid Build Coastguard Worker
122*795d594fSAndroid Build Coastguard Worker // Flush CPU caches. Returns true on success, false if flush failed.
123*795d594fSAndroid Build Coastguard Worker WARN_UNUSED bool FlushCpuCaches(void* begin, void* end);
124*795d594fSAndroid Build Coastguard Worker
125*795d594fSAndroid Build Coastguard Worker #if defined(__linux__)
126*795d594fSAndroid Build Coastguard Worker bool IsKernelVersionAtLeast(int reqd_major, int reqd_minor);
127*795d594fSAndroid Build Coastguard Worker #endif
128*795d594fSAndroid Build Coastguard Worker
129*795d594fSAndroid Build Coastguard Worker // On some old kernels, a cache operation may segfault.
130*795d594fSAndroid Build Coastguard Worker WARN_UNUSED bool CacheOperationsMaySegFault();
131*795d594fSAndroid Build Coastguard Worker
132*795d594fSAndroid Build Coastguard Worker // Is the execution environment on a virtual machine? See ART_TEST_ON_VM.
133*795d594fSAndroid Build Coastguard Worker WARN_UNUSED bool RunningOnVM();
134*795d594fSAndroid Build Coastguard Worker
135*795d594fSAndroid Build Coastguard Worker template <typename Func, typename... Args>
CheckedCall(const Func & function,const char * what,Args...args)136*795d594fSAndroid Build Coastguard Worker static inline void CheckedCall(const Func& function, const char* what, Args... args) {
137*795d594fSAndroid Build Coastguard Worker int rc = function(args...);
138*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(rc != 0)) {
139*795d594fSAndroid Build Coastguard Worker PLOG(FATAL) << "Checked call failed for " << what;
140*795d594fSAndroid Build Coastguard Worker }
141*795d594fSAndroid Build Coastguard Worker }
142*795d594fSAndroid Build Coastguard Worker
143*795d594fSAndroid Build Coastguard Worker // Forces the compiler to emit a load instruction, but discards the value.
144*795d594fSAndroid Build Coastguard Worker // Useful when dealing with memory paging.
145*795d594fSAndroid Build Coastguard Worker template <typename T>
ForceRead(const T * pointer)146*795d594fSAndroid Build Coastguard Worker inline void ForceRead(const T* pointer) {
147*795d594fSAndroid Build Coastguard Worker static_cast<void>(*const_cast<volatile T*>(pointer));
148*795d594fSAndroid Build Coastguard Worker }
149*795d594fSAndroid Build Coastguard Worker
150*795d594fSAndroid Build Coastguard Worker // Lookup value for a given key in /proc/self/status. Keys and values are separated by a ':' in
151*795d594fSAndroid Build Coastguard Worker // the status file. Returns value found on success and "<unknown>" if the key is not found or
152*795d594fSAndroid Build Coastguard Worker // there is an I/O error.
153*795d594fSAndroid Build Coastguard Worker std::string GetProcessStatus(const char* key);
154*795d594fSAndroid Build Coastguard Worker
155*795d594fSAndroid Build Coastguard Worker // Copy a prefix of /proc/tid/stat of the given length into buf. Return the number of bytes
156*795d594fSAndroid Build Coastguard Worker // actually read, 0 on error.
157*795d594fSAndroid Build Coastguard Worker size_t GetOsThreadStat(pid_t tid, char* buf, size_t len);
158*795d594fSAndroid Build Coastguard Worker
159*795d594fSAndroid Build Coastguard Worker // Return a short prefix of /proc/tid/stat as quickly and robustly as possible. Used for debugging
160*795d594fSAndroid Build Coastguard Worker // timing issues and possibly issues with /proc itself. Always atomic.
161*795d594fSAndroid Build Coastguard Worker std::string GetOsThreadStatQuick(pid_t tid);
162*795d594fSAndroid Build Coastguard Worker
163*795d594fSAndroid Build Coastguard Worker // Return a concatenation of the output of GetOsThreadStatQuick(tid) for all other tids.
164*795d594fSAndroid Build Coastguard Worker // Less robust against concurrent change, but individual stat strings should still always
165*795d594fSAndroid Build Coastguard Worker // be consistent. Called only when we are nearly certain to crash anyway.
166*795d594fSAndroid Build Coastguard Worker std::string GetOtherThreadOsStats();
167*795d594fSAndroid Build Coastguard Worker
168*795d594fSAndroid Build Coastguard Worker } // namespace art
169*795d594fSAndroid Build Coastguard Worker
170*795d594fSAndroid Build Coastguard Worker #endif // ART_LIBARTBASE_BASE_UTILS_H_
171