utils.h revision b76cac637691c29daa9c44e493b5bc26346ed116
12faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes/* 22faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Copyright (C) 2011 The Android Open Source Project 32faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * 42faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 52faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * you may not use this file except in compliance with the License. 62faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * You may obtain a copy of the License at 72faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * 82faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 92faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * 102faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Unless required by applicable law or agreed to in writing, software 112faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 122faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 132faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * See the License for the specific language governing permissions and 142faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * limitations under the License. 152faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes */ 16a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro 17fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#ifndef ART_RUNTIME_UTILS_H_ 18fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#define ART_RUNTIME_UTILS_H_ 19a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro 2092b3b5623ec8b65f3e099c076e247bb8273692f8Elliott Hughes#include <pthread.h> 21e222ee0b794f941af4fb1b32fb8224e32942ea7bElliott Hughes 2253cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light#include <limits> 2334023801bd544e613d6e85c9a5b2e743f3710e8fElliott Hughes#include <string> 2434023801bd544e613d6e85c9a5b2e743f3710e8fElliott Hughes#include <vector> 2534023801bd544e613d6e85c9a5b2e743f3710e8fElliott Hughes 26e222ee0b794f941af4fb1b32fb8224e32942ea7bElliott Hughes#include "base/logging.h" 27e222ee0b794f941af4fb1b32fb8224e32942ea7bElliott Hughes#include "globals.h" 280e12bdc49744eb6d5c29b9611a8dbe10bac4cd53Brian Carlstrom#include "instruction_set.h" 29e222ee0b794f941af4fb1b32fb8224e32942ea7bElliott Hughes#include "primitive.h" 30e222ee0b794f941af4fb1b32fb8224e32942ea7bElliott Hughes 31bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle#ifdef HAVE_ANDROID_OS 32bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle#include "cutils/properties.h" 33bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle#endif 34bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle 356b6b5f0e67ce03f38223a525612955663bc1799bCarl Shapironamespace art { 36a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro 370571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogersclass DexFile; 382dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers 392dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersnamespace mirror { 40ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromclass ArtField; 41ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromclass ArtMethod; 422dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersclass Class; 4311e45077acba2e757799a00b3be9d63fec36a7ccElliott Hughesclass Object; 445174fe6e4e931c423e910366ff22ce0838567940Elliott Hughesclass String; 452dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers} // namespace mirror 4611e45077acba2e757799a00b3be9d63fec36a7ccElliott Hughes 470325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartierenum TimeUnit { 480325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier kTimeUnitNanosecond, 490325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier kTimeUnitMicrosecond, 500325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier kTimeUnitMillisecond, 510325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier kTimeUnitSecond, 520325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier}; 530325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier 5453cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Lighttemplate <typename T> 5553cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Lightbool ParseUint(const char *in, T* out) { 5653cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light char* end; 5753cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light unsigned long long int result = strtoull(in, &end, 0); // NOLINT(runtime/int) 5853cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light if (in == end || *end != '\0') { 5953cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light return false; 6053cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light } 6153cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light if (std::numeric_limits<T>::max() < result) { 6253cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light return false; 6353cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light } 6453cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light *out = static_cast<T>(result); 6553cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light return true; 6653cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light} 6753cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light 6853cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Lighttemplate <typename T> 6953cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Lightbool ParseInt(const char* in, T* out) { 7053cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light char* end; 7153cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light long long int result = strtoll(in, &end, 0); // NOLINT(runtime/int) 7253cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light if (in == end || *end != '\0') { 7353cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light return false; 7453cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light } 7553cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light if (result < std::numeric_limits<T>::min() || std::numeric_limits<T>::max() < result) { 7653cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light return false; 7753cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light } 7853cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light *out = static_cast<T>(result); 7953cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light return true; 8053cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light} 8153cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light 82a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapirotemplate<typename T> 838194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markostatic constexpr bool IsPowerOfTwo(T x) { 84a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro return (x & (x - 1)) == 0; 85a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro} 86a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro 8706b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughestemplate<int n, typename T> 8806b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughesstatic inline bool IsAligned(T x) { 8906b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughes COMPILE_ASSERT((n & (n - 1)) == 0, n_not_power_of_two); 90a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro return (x & (n - 1)) == 0; 91a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro} 92a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro 9306b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughestemplate<int n, typename T> 9406b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughesstatic inline bool IsAligned(T* x) { 9589521898b56f2ebc3fb68acfb6bc6dde9b6f5c38Brian Carlstrom return IsAligned<n>(reinterpret_cast<const uintptr_t>(x)); 96a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro} 97a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro 98af13ad9fd18b6f75fe82e7995224c55654594f93Andreas Gampetemplate<typename T> 99af13ad9fd18b6f75fe82e7995224c55654594f93Andreas Gampestatic inline bool IsAlignedParam(T x, int n) { 100af13ad9fd18b6f75fe82e7995224c55654594f93Andreas Gampe return (x & (n - 1)) == 0; 101af13ad9fd18b6f75fe82e7995224c55654594f93Andreas Gampe} 102af13ad9fd18b6f75fe82e7995224c55654594f93Andreas Gampe 10306b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughes#define CHECK_ALIGNED(value, alignment) \ 10489521898b56f2ebc3fb68acfb6bc6dde9b6f5c38Brian Carlstrom CHECK(::art::IsAligned<alignment>(value)) << reinterpret_cast<const void*>(value) 10506b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughes 10606b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughes#define DCHECK_ALIGNED(value, alignment) \ 10789521898b56f2ebc3fb68acfb6bc6dde9b6f5c38Brian Carlstrom DCHECK(::art::IsAligned<alignment>(value)) << reinterpret_cast<const void*>(value) 108af13ad9fd18b6f75fe82e7995224c55654594f93Andreas Gampe 109af13ad9fd18b6f75fe82e7995224c55654594f93Andreas Gampe#define DCHECK_ALIGNED_PARAM(value, alignment) \ 110af13ad9fd18b6f75fe82e7995224c55654594f93Andreas Gampe DCHECK(::art::IsAlignedParam(value, alignment)) << reinterpret_cast<const void*>(value) 11106b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughes 112a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro// Check whether an N-bit two's-complement representation can hold value. 113a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapirostatic inline bool IsInt(int N, word value) { 114a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro CHECK_LT(0, N); 115a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro CHECK_LT(N, kBitsPerWord); 116a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro word limit = static_cast<word>(1) << (N - 1); 117a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro return (-limit <= value) && (value < limit); 118a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro} 119a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro 120a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapirostatic inline bool IsUint(int N, word value) { 121a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro CHECK_LT(0, N); 122a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro CHECK_LT(N, kBitsPerWord); 123a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro word limit = static_cast<word>(1) << N; 124a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro return (0 <= value) && (value < limit); 125a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro} 126a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro 127a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapirostatic inline bool IsAbsoluteUint(int N, word value) { 128a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro CHECK_LT(0, N); 129a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro CHECK_LT(N, kBitsPerWord); 130a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro if (value < 0) value = -value; 131a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro return IsUint(N, value); 132a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro} 133a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro 1344ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbeestatic inline uint16_t Low16Bits(uint32_t value) { 1354ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee return static_cast<uint16_t>(value); 136b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers} 137b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers 1384ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbeestatic inline uint16_t High16Bits(uint32_t value) { 1394ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee return static_cast<uint16_t>(value >> 16); 140b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers} 141a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro 1424ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbeestatic inline uint32_t Low32Bits(uint64_t value) { 1434ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee return static_cast<uint32_t>(value); 144a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro} 145a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro 1464ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbeestatic inline uint32_t High32Bits(uint64_t value) { 1474ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee return static_cast<uint32_t>(value >> 32); 148a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro} 149a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro 1500a9dc05e704bfd033bac2aa38a4fc6f6b8e6cf93Mathieu Chartier// A static if which determines whether to return type A or B based on the condition boolean. 1518194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markotemplate <bool condition, typename A, typename B> 1520a9dc05e704bfd033bac2aa38a4fc6f6b8e6cf93Mathieu Chartierstruct TypeStaticIf { 1538194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko typedef A type; 1540a9dc05e704bfd033bac2aa38a4fc6f6b8e6cf93Mathieu Chartier}; 1550a9dc05e704bfd033bac2aa38a4fc6f6b8e6cf93Mathieu Chartier 1560a9dc05e704bfd033bac2aa38a4fc6f6b8e6cf93Mathieu Chartier// Specialization to handle the false case. 1570a9dc05e704bfd033bac2aa38a4fc6f6b8e6cf93Mathieu Chartiertemplate <typename A, typename B> 1580a9dc05e704bfd033bac2aa38a4fc6f6b8e6cf93Mathieu Chartierstruct TypeStaticIf<false, A, B> { 1598194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko typedef B type; 1608194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko}; 1618194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko 1628194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko// Type identity. 1638194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markotemplate <typename T> 1648194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markostruct TypeIdentity { 1658194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko typedef T type; 1660a9dc05e704bfd033bac2aa38a4fc6f6b8e6cf93Mathieu Chartier}; 1670a9dc05e704bfd033bac2aa38a4fc6f6b8e6cf93Mathieu Chartier 1680941b0423537a6a5d7c1df6dd23e9864ea8f319cHiroshi Yamauchi// For rounding integers. 16961e019d291583029c01b61b93bea750f2b663c37Carl Shapirotemplate<typename T> 170b76cac637691c29daa9c44e493b5bc26346ed116Mathieu Chartierstatic constexpr T RoundDown(T x, typename TypeIdentity<T>::type n) WARN_UNUSED; 17198d1cc8033251c93786e2fa8c59a2e555a9493beMingyao Yang 17298d1cc8033251c93786e2fa8c59a2e555a9493beMingyao Yangtemplate<typename T> 1738194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markostatic constexpr T RoundDown(T x, typename TypeIdentity<T>::type n) { 1748194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko return 175836424805dd48af67088d30992a3f2e6841ca047Vladimir Marko DCHECK_CONSTEXPR(IsPowerOfTwo(n), , T(0)) 176836424805dd48af67088d30992a3f2e6841ca047Vladimir Marko (x & -n); 17761e019d291583029c01b61b93bea750f2b663c37Carl Shapiro} 17861e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 17961e019d291583029c01b61b93bea750f2b663c37Carl Shapirotemplate<typename T> 180b76cac637691c29daa9c44e493b5bc26346ed116Mathieu Chartierstatic constexpr T RoundUp(T x, typename TypeIdentity<T>::type n) WARN_UNUSED; 18198d1cc8033251c93786e2fa8c59a2e555a9493beMingyao Yang 18298d1cc8033251c93786e2fa8c59a2e555a9493beMingyao Yangtemplate<typename T> 1838194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markostatic constexpr T RoundUp(T x, typename TypeIdentity<T>::type n) { 18461e019d291583029c01b61b93bea750f2b663c37Carl Shapiro return RoundDown(x + n - 1, n); 18561e019d291583029c01b61b93bea750f2b663c37Carl Shapiro} 18661e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 1870941b0423537a6a5d7c1df6dd23e9864ea8f319cHiroshi Yamauchi// For aligning pointers. 1880941b0423537a6a5d7c1df6dd23e9864ea8f319cHiroshi Yamauchitemplate<typename T> 189b76cac637691c29daa9c44e493b5bc26346ed116Mathieu Chartierstatic inline T* AlignDown(T* x, uintptr_t n) WARN_UNUSED; 19098d1cc8033251c93786e2fa8c59a2e555a9493beMingyao Yang 19198d1cc8033251c93786e2fa8c59a2e555a9493beMingyao Yangtemplate<typename T> 1928194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markostatic inline T* AlignDown(T* x, uintptr_t n) { 1938194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko return reinterpret_cast<T*>(RoundDown(reinterpret_cast<uintptr_t>(x), n)); 1940941b0423537a6a5d7c1df6dd23e9864ea8f319cHiroshi Yamauchi} 1950941b0423537a6a5d7c1df6dd23e9864ea8f319cHiroshi Yamauchi 1960941b0423537a6a5d7c1df6dd23e9864ea8f319cHiroshi Yamauchitemplate<typename T> 197b76cac637691c29daa9c44e493b5bc26346ed116Mathieu Chartierstatic inline T* AlignUp(T* x, uintptr_t n) WARN_UNUSED; 19898d1cc8033251c93786e2fa8c59a2e555a9493beMingyao Yang 19998d1cc8033251c93786e2fa8c59a2e555a9493beMingyao Yangtemplate<typename T> 2008194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markostatic inline T* AlignUp(T* x, uintptr_t n) { 2018194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko return reinterpret_cast<T*>(RoundUp(reinterpret_cast<uintptr_t>(x), n)); 2020941b0423537a6a5d7c1df6dd23e9864ea8f319cHiroshi Yamauchi} 2030941b0423537a6a5d7c1df6dd23e9864ea8f319cHiroshi Yamauchi 204a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro// Implementation is from "Hacker's Delight" by Henry S. Warren, Jr., 2051fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro// figure 3-3, page 48, where the function is called clp2. 2061fb8620309a4e94d11879aabc33364acfa733904Carl Shapirostatic inline uint32_t RoundUpToPowerOfTwo(uint32_t x) { 2071fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro x = x - 1; 2081fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro x = x | (x >> 1); 2091fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro x = x | (x >> 2); 2101fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro x = x | (x >> 4); 2111fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro x = x | (x >> 8); 2121fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro x = x | (x >> 16); 2131fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro return x + 1; 2141fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro} 2151fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro 2168194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markotemplate<typename T> 2178194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markostatic constexpr int CLZ(T x) { 2188194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko return (sizeof(T) == sizeof(uint32_t)) 2198194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko ? __builtin_clz(x) 2208194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko : __builtin_clzll(x); 221a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro} 222a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro 223ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogerstemplate<typename T> 2248194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markostatic constexpr int CTZ(T x) { 2258194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko return (sizeof(T) == sizeof(uint32_t)) 2268194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko ? __builtin_ctz(x) 2278194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko : __builtin_ctzll(x); 228ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers} 229ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers 230ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogerstemplate<typename T> 2318194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markostatic constexpr int POPCOUNT(T x) { 2328194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko return (sizeof(T) == sizeof(uint32_t)) 2338194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko ? __builtin_popcount(x) 2348194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko : __builtin_popcountll(x); 235ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers} 236ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers 237ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstatic inline uint32_t PointerToLowMemUInt32(const void* p) { 238ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers uintptr_t intp = reinterpret_cast<uintptr_t>(p); 239ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers DCHECK_LE(intp, 0xFFFFFFFFU); 240ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers return intp & 0xFFFFFFFFU; 241ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers} 242db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom 24346b92ba72247e10884714d0b683bdb5e9d9ce59dElliott Hughesstatic inline bool NeedsEscaping(uint16_t ch) { 24446b92ba72247e10884714d0b683bdb5e9d9ce59dElliott Hughes return (ch < ' ' || ch > '~'); 24546b92ba72247e10884714d0b683bdb5e9d9ce59dElliott Hughes} 24646b92ba72247e10884714d0b683bdb5e9d9ce59dElliott Hughes 247c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampe// Interpret the bit pattern of input (type U) as type V. Requires the size 248c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampe// of V >= size of U (compile-time checked). 249c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampetemplate<typename U, typename V> 250c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampestatic inline V bit_cast(U in) { 251c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampe COMPILE_ASSERT(sizeof(U) <= sizeof(V), size_of_u_not_le_size_of_v); 252c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampe union { 253c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampe U u; 254c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampe V v; 255c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampe } tmp; 256c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampe tmp.u = in; 257c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampe return tmp.v; 258c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampe} 259c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampe 260576ca0cd692c0b6ae70e776de91015b8ff000a08Ian Rogersstd::string PrintableChar(uint16_t ch); 261c7ac37f0b8b64cfb53d8b9cc8dddbb34be3dd5eeElliott Hughes 26282914b6164fd0109531391975389e4f0ff6832c8Elliott Hughes// Returns an ASCII string corresponding to the given UTF-8 string. 26382914b6164fd0109531391975389e4f0ff6832c8Elliott Hughes// Java escapes are used for non-ASCII characters. 26482914b6164fd0109531391975389e4f0ff6832c8Elliott Hughesstd::string PrintableString(const std::string& utf8); 265c7ac37f0b8b64cfb53d8b9cc8dddbb34be3dd5eeElliott Hughes 266f1a5adc87760f938b01df26d906295063546b259Elliott Hughes// Tests whether 's' starts with 'prefix'. 267f1a5adc87760f938b01df26d906295063546b259Elliott Hughesbool StartsWith(const std::string& s, const char* prefix); 268f1a5adc87760f938b01df26d906295063546b259Elliott Hughes 2697a967b3d4468ab56bf1b75ebd4d7bf9e6798761bBrian Carlstrom// Tests whether 's' starts with 'suffix'. 2707a967b3d4468ab56bf1b75ebd4d7bf9e6798761bBrian Carlstrombool EndsWith(const std::string& s, const char* suffix); 2717a967b3d4468ab56bf1b75ebd4d7bf9e6798761bBrian Carlstrom 27254e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes// Used to implement PrettyClass, PrettyField, PrettyMethod, and PrettyTypeOf, 27354e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes// one of which is probably more useful to you. 274a0b8feb34a8492c6b8d430f6ca0716e7ff4f4c57Elliott Hughes// Returns a human-readable equivalent of 'descriptor'. So "I" would be "int", 275a0b8feb34a8492c6b8d430f6ca0716e7ff4f4c57Elliott Hughes// "[[I" would be "int[][]", "[Ljava/lang/String;" would be 276a0b8feb34a8492c6b8d430f6ca0716e7ff4f4c57Elliott Hughes// "java.lang.String[]", and so forth. 277ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstd::string PrettyDescriptor(mirror::String* descriptor) 278ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 2796c8867daab4af4667e0e816f6beafa7c5d13e043Elliott Hughesstd::string PrettyDescriptor(const std::string& descriptor); 2806b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstromstd::string PrettyDescriptor(Primitive::Type type); 281ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstd::string PrettyDescriptor(mirror::Class* klass) 282b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 28311e45077acba2e757799a00b3be9d63fec36a7ccElliott Hughes 28454e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes// Returns a human-readable signature for 'f'. Something like "a.b.C.f" or 28554e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes// "int a.b.C.f" (depending on the value of 'with_type'). 286ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstd::string PrettyField(mirror::ArtField* f, bool with_type = true) 287b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 2886f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstromstd::string PrettyField(uint32_t field_idx, const DexFile& dex_file, bool with_type = true); 289a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes 290a0b8feb34a8492c6b8d430f6ca0716e7ff4f4c57Elliott Hughes// Returns a human-readable signature for 'm'. Something like "a.b.C.m" or 291a0b8feb34a8492c6b8d430f6ca0716e7ff4f4c57Elliott Hughes// "a.b.C.m(II)V" (depending on the value of 'with_signature'). 292ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstd::string PrettyMethod(mirror::ArtMethod* m, bool with_signature = true) 293b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 2940571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogersstd::string PrettyMethod(uint32_t method_idx, const DexFile& dex_file, bool with_signature = true); 295a0b8feb34a8492c6b8d430f6ca0716e7ff4f4c57Elliott Hughes 296a0b8feb34a8492c6b8d430f6ca0716e7ff4f4c57Elliott Hughes// Returns a human-readable form of the name of the *class* of the given object. 297a0b8feb34a8492c6b8d430f6ca0716e7ff4f4c57Elliott Hughes// So given an instance of java.lang.String, the output would 29811e45077acba2e757799a00b3be9d63fec36a7ccElliott Hughes// be "java.lang.String". Given an array of int, the output would be "int[]". 29911e45077acba2e757799a00b3be9d63fec36a7ccElliott Hughes// Given String.class, the output would be "java.lang.Class<java.lang.String>". 300ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstd::string PrettyTypeOf(mirror::Object* obj) 301b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 3024c70d77decc1217c8b1ebf8126b8d9d024c75d82Mathieu Chartier 3034c70d77decc1217c8b1ebf8126b8d9d024c75d82Mathieu Chartier// Returns a human-readable form of the type at an index in the specified dex file. 3044c70d77decc1217c8b1ebf8126b8d9d024c75d82Mathieu Chartier// Example outputs: char[], java.lang.String. 30518c24b6e05e7591069f1a2ac9c614b28fc0853acMathieu Chartierstd::string PrettyType(uint32_t type_idx, const DexFile& dex_file); 30654e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes 30754e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes// Returns a human-readable form of the name of the given class. 30854e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes// Given String.class, the output would be "java.lang.Class<java.lang.String>". 309ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstd::string PrettyClass(mirror::Class* c) 310b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 31111e45077acba2e757799a00b3be9d63fec36a7ccElliott Hughes 312d81871cbbaa34c649e488f94f61a981db33123e5Ian Rogers// Returns a human-readable form of the name of the given class with its class loader. 313ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstd::string PrettyClassAndClassLoader(mirror::Class* c) 314b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 315d81871cbbaa34c649e488f94f61a981db33123e5Ian Rogers 316c967f78cd29b6019f7cfca40a02e9b677112da70Elliott Hughes// Returns a human-readable size string such as "1MB". 317e6da9af8dfe0a3e3fbc2be700554f6478380e7b9Mathieu Chartierstd::string PrettySize(int64_t size_in_bytes); 3183bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers 3193bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers// Returns a human-readable time string which prints every nanosecond while trying to limit the 3203bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers// number of trailing zeros. Prints using the largest human readable unit up to a second. 3213bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers// e.g. "1ms", "1.000000001s", "1.001us" 322f5997b4d3f889569d5a2b724d83d764bfbb8d106Mathieu Chartierstd::string PrettyDuration(uint64_t nano_duration, size_t max_fraction_digits = 3); 3233bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers 3240325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier// Format a nanosecond time to specified units. 325f5997b4d3f889569d5a2b724d83d764bfbb8d106Mathieu Chartierstd::string FormatDuration(uint64_t nano_duration, TimeUnit time_unit, 326f5997b4d3f889569d5a2b724d83d764bfbb8d106Mathieu Chartier size_t max_fraction_digits); 3270325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier 3280325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier// Get the appropriate unit for a nanosecond duration. 3290325e6296d2370c42e7be80d846bfc7f8b28423bMathieu ChartierTimeUnit GetAppropriateTimeUnit(uint64_t nano_duration); 3300325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier 3317020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison// Get the divisor to convert from a nanoseconds to a time unit. 3320325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartieruint64_t GetNsToTimeUnitDivisor(TimeUnit time_unit); 3330325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier 33479082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes// Performs JNI name mangling as described in section 11.3 "Linking Native Methods" 33579082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes// of the JNI spec. 33679082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughesstd::string MangleForJni(const std::string& s); 33779082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes 338f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom// Turn "java.lang.String" into "Ljava/lang/String;". 339f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstromstd::string DotToDescriptor(const char* class_name); 340f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom 341aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom// Turn "Ljava/lang/String;" into "java.lang.String". 342f1a5adc87760f938b01df26d906295063546b259Elliott Hughesstd::string DescriptorToDot(const char* descriptor); 343aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom 34491bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes// Turn "Ljava/lang/String;" into "java/lang/String". 34591bf6cd47174f5c17265320f7a350722720390a5Elliott Hughesstd::string DescriptorToName(const char* descriptor); 34691bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes 347906e685ce43092812bf403016057376d0657a671Elliott Hughes// Tests for whether 's' is a valid class name in the three common forms: 348906e685ce43092812bf403016057376d0657a671Elliott Hughesbool IsValidBinaryClassName(const char* s); // "java.lang.String" 349906e685ce43092812bf403016057376d0657a671Elliott Hughesbool IsValidJniClassName(const char* s); // "java/lang/String" 350906e685ce43092812bf403016057376d0657a671Elliott Hughesbool IsValidDescriptor(const char* s); // "Ljava/lang/String;" 35164bf5a33d55aa779ef452552a466943002d39e4fElliott Hughes 35210037c866b04550fc5461058c398c2e3e509381ajeffhao// Returns whether the given string is a valid field or method name, 35310037c866b04550fc5461058c398c2e3e509381ajeffhao// additionally allowing names that begin with '<' and end with '>'. 35410037c866b04550fc5461058c398c2e3e509381ajeffhaobool IsValidMemberName(const char* s); 35510037c866b04550fc5461058c398c2e3e509381ajeffhao 35679082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes// Returns the JNI native function name for the non-overloaded method 'm'. 357ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstd::string JniShortName(mirror::ArtMethod* m) 358b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 35979082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes// Returns the JNI native function name for the overloaded method 'm'. 360ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstd::string JniLongName(mirror::ArtMethod* m) 361b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 36279082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes 363d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughesbool ReadFileToString(const std::string& file_name, std::string* result); 364c143c55718342519db5398e41dda31422cf16c79buzbee 365e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes// Returns the current date in ISO yyyy-mm-dd hh:mm:ss format. 366e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughesstd::string GetIsoDate(); 367e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes 3680512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughes// Returns the monotonic time since some unspecified starting point in milliseconds. 3697162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughesuint64_t MilliTime(); 3707162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes 3710512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughes// Returns the monotonic time since some unspecified starting point in microseconds. 372a9ef3fd82bebc6370fc3ddbb094988feb6c83022jeffhaouint64_t MicroTime(); 373a9ef3fd82bebc6370fc3ddbb094988feb6c83022jeffhao 3740512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughes// Returns the monotonic time since some unspecified starting point in nanoseconds. 37583df2ac4a051ee10eafde7587de2faf3f0150fadElliott Hughesuint64_t NanoTime(); 37683df2ac4a051ee10eafde7587de2faf3f0150fadElliott Hughes 3770512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughes// Returns the thread-specific CPU-time clock in nanoseconds or -1 if unavailable. 3780512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesuint64_t ThreadCpuNanoTime(); 3790512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughes 380bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes// Converts the given number of nanoseconds to milliseconds. 381720ef7680573c1afd12f99f02eee3045daee5168Mathieu Chartierstatic constexpr inline uint64_t NsToMs(uint64_t ns) { 3823bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers return ns / 1000 / 1000; 3833bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers} 3843bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers 3853bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers// Converts the given number of milliseconds to nanoseconds 386720ef7680573c1afd12f99f02eee3045daee5168Mathieu Chartierstatic constexpr inline uint64_t MsToNs(uint64_t ns) { 3873bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers return ns * 1000 * 1000; 3883bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers} 3893bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers 390bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom#if defined(__APPLE__) 391bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom// No clocks to specify on OS/X, fake value to pass to routines that require a clock. 392bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom#define CLOCK_REALTIME 0xebadf00d 393bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom#endif 394bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom 39556edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers// Sleep for the given number of nanoseconds, a bad way to handle contention. 39656edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogersvoid NanoSleep(uint64_t ns); 39756edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers 398bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom// Initialize a timespec to either an absolute or relative time. 399bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstromvoid InitTimeSpec(bool absolute, int clock, int64_t ms, int32_t ns, timespec* ts); 400bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom 40148436bbff87aae61bba20029b4382fa41ce787feElliott Hughes// Splits a string using the given separator character into a vector of 40234023801bd544e613d6e85c9a5b2e743f3710e8fElliott Hughes// strings. Empty strings will be omitted. 40348436bbff87aae61bba20029b4382fa41ce787feElliott Hughesvoid Split(const std::string& s, char separator, std::vector<std::string>& result); 40448436bbff87aae61bba20029b4382fa41ce787feElliott Hughes 4057020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison// Trims whitespace off both ends of the given string. 4067020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allisonstd::string Trim(std::string s); 4077020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison 40848436bbff87aae61bba20029b4382fa41ce787feElliott Hughes// Joins a vector of strings into a single string, using the given separator. 40948436bbff87aae61bba20029b4382fa41ce787feElliott Hughestemplate <typename StringT> std::string Join(std::vector<StringT>& strings, char separator); 41034023801bd544e613d6e85c9a5b2e743f3710e8fElliott Hughes 41142ee14279065352a4b9a3e8028d02c567e847d05Elliott Hughes// Returns the calling thread's tid. (The C libraries don't expose this.) 41242ee14279065352a4b9a3e8028d02c567e847d05Elliott Hughespid_t GetTid(); 41342ee14279065352a4b9a3e8028d02c567e847d05Elliott Hughes 414289be85116aaf7c48413858b5d0448868b4e61f3Elliott Hughes// Returns the given thread's name. 415289be85116aaf7c48413858b5d0448868b4e61f3Elliott Hughesstd::string GetThreadName(pid_t tid); 416289be85116aaf7c48413858b5d0448868b4e61f3Elliott Hughes 417120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers// Returns details of the given thread's stack. 4182921201dce37ba40c55b89c0deca3c34bf64168eBrian Carlstromvoid GetThreadStack(pthread_t thread, void** stack_base, size_t* stack_size); 419e188419b971936086a188843378375f5ced13724Elliott Hughes 420bfe487be25652c5456236661b9d9c3579d2296c1Elliott Hughes// Reads data from "/proc/self/task/${tid}/stat". 4212921201dce37ba40c55b89c0deca3c34bf64168eBrian Carlstromvoid GetTaskStats(pid_t tid, char* state, int* utime, int* stime, int* task_cpu); 422bfe487be25652c5456236661b9d9c3579d2296c1Elliott Hughes 4231bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes// Returns the name of the scheduler group for the given thread the current process, or the empty string. 4241bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughesstd::string GetSchedulerGroupName(pid_t tid); 4251bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes 426dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes// Sets the name of the current thread. The name may be truncated to an 427dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes// implementation-defined limit. 42822869a9026a08b544eca4cefd67386d347e30d2cElliott Hughesvoid SetThreadName(const char* thread_name); 429dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes 43046e251bf7200cc06f5a9a82ee2030e650f5e1443Elliott Hughes// Dumps the native stack for thread 'tid' to 'os'. 431067d20fd2fddd947ce3a014d023e9de249082ebeKenny Rootvoid DumpNativeStack(std::ostream& os, pid_t tid, const char* prefix = "", 432a2cee18799d7e61adb037a4219adb23fef7d022bChristopher Ferris mirror::ArtMethod* current_method = nullptr) 433067d20fd2fddd947ce3a014d023e9de249082ebeKenny Root NO_THREAD_SAFETY_ANALYSIS; 43446e251bf7200cc06f5a9a82ee2030e650f5e1443Elliott Hughes 43546e251bf7200cc06f5a9a82ee2030e650f5e1443Elliott Hughes// Dumps the kernel stack for thread 'tid' to 'os'. Note that this is only available on linux-x86. 43646e251bf7200cc06f5a9a82ee2030e650f5e1443Elliott Hughesvoid DumpKernelStack(std::ostream& os, pid_t tid, const char* prefix = "", bool include_count = true); 43746e251bf7200cc06f5a9a82ee2030e650f5e1443Elliott Hughes 4387020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison// Find $ANDROID_ROOT, /system, or abort. 439a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstromconst char* GetAndroidRoot(); 440a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstrom 4417020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison// Find $ANDROID_DATA, /data, or abort. 442a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstromconst char* GetAndroidData(); 443a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstrom 44411d9f06a96a6909905c248ed684366190140095cNarayan Kamath// Returns the dalvik-cache location, or dies trying. subdir will be 44511d9f06a96a6909905c248ed684366190140095cNarayan Kamath// appended to the cache location. 44611d9f06a96a6909905c248ed684366190140095cNarayan Kamathstd::string GetDalvikCacheOrDie(const char* subdir, bool create_if_absent = true); 44711d9f06a96a6909905c248ed684366190140095cNarayan Kamath 44811d9f06a96a6909905c248ed684366190140095cNarayan Kamath// Returns the absolute dalvik-cache path for a DexFile or OatFile, or 44911d9f06a96a6909905c248ed684366190140095cNarayan Kamath// dies trying. The path returned will be rooted at cache_location. 45011d9f06a96a6909905c248ed684366190140095cNarayan Kamathstd::string GetDalvikCacheFilenameOrDie(const char* file_location, 45111d9f06a96a6909905c248ed684366190140095cNarayan Kamath const char* cache_location); 452262bf46ddc91e5b4fbd367127ff21a1877d939f2jeffhao 4530e12bdc49744eb6d5c29b9611a8dbe10bac4cd53Brian Carlstrom// Returns the system location for an image 4542afe49450f2e018f18b5de45428b9174bfd6f196Brian Carlstromstd::string GetSystemImageFilename(const char* location, InstructionSet isa); 4550e12bdc49744eb6d5c29b9611a8dbe10bac4cd53Brian Carlstrom 4560e12bdc49744eb6d5c29b9611a8dbe10bac4cd53Brian Carlstrom// Returns an .odex file name next adjacent to the dex location. 4570e12bdc49744eb6d5c29b9611a8dbe10bac4cd53Brian Carlstrom// For example, for "/foo/bar/baz.jar", return "/foo/bar/<isa>/baz.odex". 458833a48501d560c9fa7fc78ef619888138c2d374fAndreas Gampe// Note: does not support multidex location strings. 4592afe49450f2e018f18b5de45428b9174bfd6f196Brian Carlstromstd::string DexFilenameToOdexFilename(const std::string& location, InstructionSet isa); 4600e12bdc49744eb6d5c29b9611a8dbe10bac4cd53Brian Carlstrom 4617c3d13aebdd8611cae58a1048bffb13cbdc465cbBrian Carlstrom// Check whether the given magic matches a known file type. 4627c3d13aebdd8611cae58a1048bffb13cbdc465cbBrian Carlstrombool IsZipMagic(uint32_t magic); 4637c3d13aebdd8611cae58a1048bffb13cbdc465cbBrian Carlstrombool IsDexMagic(uint32_t magic); 4647c3d13aebdd8611cae58a1048bffb13cbdc465cbBrian Carlstrombool IsOatMagic(uint32_t magic); 465b7bbba49d88eae58223d9878da4069bf6d7140bfBrian Carlstrom 4666449c62e40ef3a9bb75f664f922555affb532ee4Brian Carlstrom// Wrapper on fork/execv to run a command in a subprocess. 4676449c62e40ef3a9bb75f664f922555affb532ee4Brian Carlstrombool Exec(std::vector<std::string>& arg_vector, std::string* error_msg); 4686449c62e40ef3a9bb75f664f922555affb532ee4Brian Carlstrom 469d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartierclass VoidFunctor { 470357e9be24c17a6bc2ae9fb53f25c73503116101dMathieu Chartier public: 471d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier template <typename A> 472df62950e7a32031b82360c407d46a37b94188fbbBrian Carlstrom inline void operator() (A a) const { 473d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier UNUSED(a); 474d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier } 475d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier 476d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier template <typename A, typename B> 477df62950e7a32031b82360c407d46a37b94188fbbBrian Carlstrom inline void operator() (A a, B b) const { 478d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier UNUSED(a); 479d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier UNUSED(b); 480d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier } 481d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier 482d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier template <typename A, typename B, typename C> 483df62950e7a32031b82360c407d46a37b94188fbbBrian Carlstrom inline void operator() (A a, B b, C c) const { 484d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier UNUSED(a); 485d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier UNUSED(b); 486d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier UNUSED(c); 487d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier } 488357e9be24c17a6bc2ae9fb53f25c73503116101dMathieu Chartier}; 489357e9be24c17a6bc2ae9fb53f25c73503116101dMathieu Chartier 4906b6b5f0e67ce03f38223a525612955663bc1799bCarl Shapiro} // namespace art 491a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro 492fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#endif // ART_RUNTIME_UTILS_H_ 493