utils.h revision 120f1c74a9768e958377b6c97897511b27ae58c8
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 17a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro#ifndef ART_SRC_UTILS_H_ 18a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro#define ART_SRC_UTILS_H_ 19a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro 20578bbdc684db8ed68e9fedbc678669d27fa68b6eBrian Carlstrom#include "globals.h" 21db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom#include "logging.h" 226b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom#include "primitive.h" 2311e45077acba2e757799a00b3be9d63fec36a7ccElliott Hughes#include "stringpiece.h" 24c7ac37f0b8b64cfb53d8b9cc8dddbb34be3dd5eeElliott Hughes#include "stringprintf.h" 25a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro 2692b3b5623ec8b65f3e099c076e247bb8273692f8Elliott Hughes#include <pthread.h> 2734023801bd544e613d6e85c9a5b2e743f3710e8fElliott Hughes#include <string> 2834023801bd544e613d6e85c9a5b2e743f3710e8fElliott Hughes#include <vector> 2934023801bd544e613d6e85c9a5b2e743f3710e8fElliott Hughes 306b6b5f0e67ce03f38223a525612955663bc1799bCarl Shapironamespace art { 31a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro 3254e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughesclass Class; 330571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogersclass DexFile; 34a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughesclass Field; 3566f19258f9728d4ffe026074d8fd429d639802faMathieu Chartierclass AbstractMethod; 3611e45077acba2e757799a00b3be9d63fec36a7ccElliott Hughesclass Object; 375174fe6e4e931c423e910366ff22ce0838567940Elliott Hughesclass String; 3811e45077acba2e757799a00b3be9d63fec36a7ccElliott Hughes 390325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartierenum TimeUnit { 400325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier kTimeUnitNanosecond, 410325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier kTimeUnitMicrosecond, 420325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier kTimeUnitMillisecond, 430325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier kTimeUnitSecond, 440325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier}; 450325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier 46a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapirotemplate<typename T> 47a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapirostatic inline bool IsPowerOfTwo(T x) { 48a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro return (x & (x - 1)) == 0; 49a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro} 50a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro 5106b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughestemplate<int n, typename T> 5206b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughesstatic inline bool IsAligned(T x) { 5306b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughes COMPILE_ASSERT((n & (n - 1)) == 0, n_not_power_of_two); 54a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro return (x & (n - 1)) == 0; 55a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro} 56a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro 5706b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughestemplate<int n, typename T> 5806b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughesstatic inline bool IsAligned(T* x) { 5989521898b56f2ebc3fb68acfb6bc6dde9b6f5c38Brian Carlstrom return IsAligned<n>(reinterpret_cast<const uintptr_t>(x)); 60a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro} 61a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro 6206b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughes#define CHECK_ALIGNED(value, alignment) \ 6389521898b56f2ebc3fb68acfb6bc6dde9b6f5c38Brian Carlstrom CHECK(::art::IsAligned<alignment>(value)) << reinterpret_cast<const void*>(value) 6406b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughes 6506b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughes#define DCHECK_ALIGNED(value, alignment) \ 6689521898b56f2ebc3fb68acfb6bc6dde9b6f5c38Brian Carlstrom DCHECK(::art::IsAligned<alignment>(value)) << reinterpret_cast<const void*>(value) 6706b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughes 68a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro// Check whether an N-bit two's-complement representation can hold value. 69a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapirostatic inline bool IsInt(int N, word value) { 70a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro CHECK_LT(0, N); 71a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro CHECK_LT(N, kBitsPerWord); 72a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro word limit = static_cast<word>(1) << (N - 1); 73a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro return (-limit <= value) && (value < limit); 74a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro} 75a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro 76a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapirostatic inline bool IsUint(int N, word value) { 77a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro CHECK_LT(0, N); 78a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro CHECK_LT(N, kBitsPerWord); 79a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro word limit = static_cast<word>(1) << N; 80a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro return (0 <= value) && (value < limit); 81a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro} 82a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro 83a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapirostatic inline bool IsAbsoluteUint(int N, word value) { 84a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro CHECK_LT(0, N); 85a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro CHECK_LT(N, kBitsPerWord); 86a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro if (value < 0) value = -value; 87a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro return IsUint(N, value); 88a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro} 89a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro 90b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogersstatic inline int32_t Low16Bits(int32_t value) { 91b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers return static_cast<int32_t>(value & 0xffff); 92b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers} 93b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers 94b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogersstatic inline int32_t High16Bits(int32_t value) { 95b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers return static_cast<int32_t>(value >> 16); 96b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers} 97a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro 98a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapirostatic inline int32_t Low32Bits(int64_t value) { 99a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro return static_cast<int32_t>(value); 100a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro} 101a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro 102a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapirostatic inline int32_t High32Bits(int64_t value) { 103a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro return static_cast<int32_t>(value >> 32); 104a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro} 105a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro 10661e019d291583029c01b61b93bea750f2b663c37Carl Shapirotemplate<typename T> 10761e019d291583029c01b61b93bea750f2b663c37Carl Shapirostatic inline T RoundDown(T x, int n) { 10861e019d291583029c01b61b93bea750f2b663c37Carl Shapiro CHECK(IsPowerOfTwo(n)); 10961e019d291583029c01b61b93bea750f2b663c37Carl Shapiro return (x & -n); 11061e019d291583029c01b61b93bea750f2b663c37Carl Shapiro} 11161e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 11261e019d291583029c01b61b93bea750f2b663c37Carl Shapirotemplate<typename T> 11361e019d291583029c01b61b93bea750f2b663c37Carl Shapirostatic inline T RoundUp(T x, int n) { 11461e019d291583029c01b61b93bea750f2b663c37Carl Shapiro return RoundDown(x + n - 1, n); 11561e019d291583029c01b61b93bea750f2b663c37Carl Shapiro} 11661e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 117a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro// Implementation is from "Hacker's Delight" by Henry S. Warren, Jr., 1181fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro// figure 3-3, page 48, where the function is called clp2. 1191fb8620309a4e94d11879aabc33364acfa733904Carl Shapirostatic inline uint32_t RoundUpToPowerOfTwo(uint32_t x) { 1201fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro x = x - 1; 1211fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro x = x | (x >> 1); 1221fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro x = x | (x >> 2); 1231fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro x = x | (x >> 4); 1241fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro x = x | (x >> 8); 1251fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro x = x | (x >> 16); 1261fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro return x + 1; 1271fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro} 1281fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro 1291fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro// Implementation is from "Hacker's Delight" by Henry S. Warren, Jr., 130a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro// figure 5-2, page 66, where the function is called pop. 131a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapirostatic inline int CountOneBits(uint32_t x) { 132a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro x = x - ((x >> 1) & 0x55555555); 133a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro x = (x & 0x33333333) + ((x >> 2) & 0x33333333); 134a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro x = (x + (x >> 4)) & 0x0F0F0F0F; 135a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro x = x + (x >> 8); 136a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro x = x + (x >> 16); 137a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro return static_cast<int>(x & 0x0000003F); 138a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro} 139a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro 140db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom#define CLZ(x) __builtin_clz(x) 141105afd2bd8f9f0ddfcfcb4b8db9f356ee82ae8cdElliott Hughes#define CTZ(x) __builtin_ctz(x) 142db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom 14346b92ba72247e10884714d0b683bdb5e9d9ce59dElliott Hughesstatic inline bool NeedsEscaping(uint16_t ch) { 14446b92ba72247e10884714d0b683bdb5e9d9ce59dElliott Hughes return (ch < ' ' || ch > '~'); 14546b92ba72247e10884714d0b683bdb5e9d9ce59dElliott Hughes} 14646b92ba72247e10884714d0b683bdb5e9d9ce59dElliott Hughes 147c7ac37f0b8b64cfb53d8b9cc8dddbb34be3dd5eeElliott Hughesstatic inline std::string PrintableChar(uint16_t ch) { 148c7ac37f0b8b64cfb53d8b9cc8dddbb34be3dd5eeElliott Hughes std::string result; 14946b92ba72247e10884714d0b683bdb5e9d9ce59dElliott Hughes result += '\''; 15046b92ba72247e10884714d0b683bdb5e9d9ce59dElliott Hughes if (NeedsEscaping(ch)) { 15146b92ba72247e10884714d0b683bdb5e9d9ce59dElliott Hughes StringAppendF(&result, "\\u%04x", ch); 15246b92ba72247e10884714d0b683bdb5e9d9ce59dElliott Hughes } else { 153c7ac37f0b8b64cfb53d8b9cc8dddbb34be3dd5eeElliott Hughes result += ch; 154c7ac37f0b8b64cfb53d8b9cc8dddbb34be3dd5eeElliott Hughes } 15546b92ba72247e10884714d0b683bdb5e9d9ce59dElliott Hughes result += '\''; 156c7ac37f0b8b64cfb53d8b9cc8dddbb34be3dd5eeElliott Hughes return result; 157c7ac37f0b8b64cfb53d8b9cc8dddbb34be3dd5eeElliott Hughes} 158c7ac37f0b8b64cfb53d8b9cc8dddbb34be3dd5eeElliott Hughes 15982914b6164fd0109531391975389e4f0ff6832c8Elliott Hughes// Returns an ASCII string corresponding to the given UTF-8 string. 16082914b6164fd0109531391975389e4f0ff6832c8Elliott Hughes// Java escapes are used for non-ASCII characters. 16182914b6164fd0109531391975389e4f0ff6832c8Elliott Hughesstd::string PrintableString(const std::string& utf8); 162c7ac37f0b8b64cfb53d8b9cc8dddbb34be3dd5eeElliott Hughes 163f1a5adc87760f938b01df26d906295063546b259Elliott Hughes// Tests whether 's' starts with 'prefix'. 164f1a5adc87760f938b01df26d906295063546b259Elliott Hughesbool StartsWith(const std::string& s, const char* prefix); 165f1a5adc87760f938b01df26d906295063546b259Elliott Hughes 1667a967b3d4468ab56bf1b75ebd4d7bf9e6798761bBrian Carlstrom// Tests whether 's' starts with 'suffix'. 1677a967b3d4468ab56bf1b75ebd4d7bf9e6798761bBrian Carlstrombool EndsWith(const std::string& s, const char* suffix); 1687a967b3d4468ab56bf1b75ebd4d7bf9e6798761bBrian Carlstrom 16954e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes// Used to implement PrettyClass, PrettyField, PrettyMethod, and PrettyTypeOf, 17054e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes// one of which is probably more useful to you. 171a0b8feb34a8492c6b8d430f6ca0716e7ff4f4c57Elliott Hughes// Returns a human-readable equivalent of 'descriptor'. So "I" would be "int", 172a0b8feb34a8492c6b8d430f6ca0716e7ff4f4c57Elliott Hughes// "[[I" would be "int[][]", "[Ljava/lang/String;" would be 173a0b8feb34a8492c6b8d430f6ca0716e7ff4f4c57Elliott Hughes// "java.lang.String[]", and so forth. 1745174fe6e4e931c423e910366ff22ce0838567940Elliott Hughesstd::string PrettyDescriptor(const String* descriptor); 1756c8867daab4af4667e0e816f6beafa7c5d13e043Elliott Hughesstd::string PrettyDescriptor(const std::string& descriptor); 1766b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstromstd::string PrettyDescriptor(Primitive::Type type); 17700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstd::string PrettyDescriptor(const Class* klass) 178b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 17911e45077acba2e757799a00b3be9d63fec36a7ccElliott Hughes 18054e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes// Returns a human-readable signature for 'f'. Something like "a.b.C.f" or 18154e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes// "int a.b.C.f" (depending on the value of 'with_type'). 18200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstd::string PrettyField(const Field* f, bool with_type = true) 183b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 1846f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstromstd::string PrettyField(uint32_t field_idx, const DexFile& dex_file, bool with_type = true); 185a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes 186a0b8feb34a8492c6b8d430f6ca0716e7ff4f4c57Elliott Hughes// Returns a human-readable signature for 'm'. Something like "a.b.C.m" or 187a0b8feb34a8492c6b8d430f6ca0716e7ff4f4c57Elliott Hughes// "a.b.C.m(II)V" (depending on the value of 'with_signature'). 18866f19258f9728d4ffe026074d8fd429d639802faMathieu Chartierstd::string PrettyMethod(const AbstractMethod* m, bool with_signature = true) 189b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 1900571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogersstd::string PrettyMethod(uint32_t method_idx, const DexFile& dex_file, bool with_signature = true); 191a0b8feb34a8492c6b8d430f6ca0716e7ff4f4c57Elliott Hughes 192a0b8feb34a8492c6b8d430f6ca0716e7ff4f4c57Elliott Hughes// Returns a human-readable form of the name of the *class* of the given object. 193a0b8feb34a8492c6b8d430f6ca0716e7ff4f4c57Elliott Hughes// So given an instance of java.lang.String, the output would 19411e45077acba2e757799a00b3be9d63fec36a7ccElliott Hughes// be "java.lang.String". Given an array of int, the output would be "int[]". 19511e45077acba2e757799a00b3be9d63fec36a7ccElliott Hughes// Given String.class, the output would be "java.lang.Class<java.lang.String>". 19600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstd::string PrettyTypeOf(const Object* obj) 197b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 1984c70d77decc1217c8b1ebf8126b8d9d024c75d82Mathieu Chartier 1994c70d77decc1217c8b1ebf8126b8d9d024c75d82Mathieu Chartier// Returns a human-readable form of the type at an index in the specified dex file. 2004c70d77decc1217c8b1ebf8126b8d9d024c75d82Mathieu Chartier// Example outputs: char[], java.lang.String. 20118c24b6e05e7591069f1a2ac9c614b28fc0853acMathieu Chartierstd::string PrettyType(uint32_t type_idx, const DexFile& dex_file); 20254e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes 20354e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes// Returns a human-readable form of the name of the given class. 20454e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes// Given String.class, the output would be "java.lang.Class<java.lang.String>". 20500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstd::string PrettyClass(const Class* c) 206b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 20711e45077acba2e757799a00b3be9d63fec36a7ccElliott Hughes 208d81871cbbaa34c649e488f94f61a981db33123e5Ian Rogers// Returns a human-readable form of the name of the given class with its class loader. 20900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstd::string PrettyClassAndClassLoader(const Class* c) 210b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 211d81871cbbaa34c649e488f94f61a981db33123e5Ian Rogers 212c967f78cd29b6019f7cfca40a02e9b677112da70Elliott Hughes// Returns a human-readable size string such as "1MB". 2133bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogersstd::string PrettySize(size_t size_in_bytes); 2143bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers 2153bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers// Returns a human-readable time string which prints every nanosecond while trying to limit the 2163bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers// number of trailing zeros. Prints using the largest human readable unit up to a second. 2173bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers// e.g. "1ms", "1.000000001s", "1.001us" 2183bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogersstd::string PrettyDuration(uint64_t nano_duration); 2193bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers 2200325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier// Format a nanosecond time to specified units. 2210325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartierstd::string FormatDuration(uint64_t nano_duration, TimeUnit time_unit); 2220325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier 2230325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier// Get the appropriate unit for a nanosecond duration. 2240325e6296d2370c42e7be80d846bfc7f8b28423bMathieu ChartierTimeUnit GetAppropriateTimeUnit(uint64_t nano_duration); 2250325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier 2260325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier// Get the divisor to convert from a nanoseconds to a time unit 2270325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartieruint64_t GetNsToTimeUnitDivisor(TimeUnit time_unit); 2280325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier 22979082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes// Performs JNI name mangling as described in section 11.3 "Linking Native Methods" 23079082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes// of the JNI spec. 23179082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughesstd::string MangleForJni(const std::string& s); 23279082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes 233f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom// Turn "java.lang.String" into "Ljava/lang/String;". 234f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstromstd::string DotToDescriptor(const char* class_name); 235f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom 236aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom// Turn "Ljava/lang/String;" into "java.lang.String". 237f1a5adc87760f938b01df26d906295063546b259Elliott Hughesstd::string DescriptorToDot(const char* descriptor); 238aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom 23991bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes// Turn "Ljava/lang/String;" into "java/lang/String". 24091bf6cd47174f5c17265320f7a350722720390a5Elliott Hughesstd::string DescriptorToName(const char* descriptor); 24191bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes 242906e685ce43092812bf403016057376d0657a671Elliott Hughes// Tests for whether 's' is a valid class name in the three common forms: 243906e685ce43092812bf403016057376d0657a671Elliott Hughesbool IsValidBinaryClassName(const char* s); // "java.lang.String" 244906e685ce43092812bf403016057376d0657a671Elliott Hughesbool IsValidJniClassName(const char* s); // "java/lang/String" 245906e685ce43092812bf403016057376d0657a671Elliott Hughesbool IsValidDescriptor(const char* s); // "Ljava/lang/String;" 24664bf5a33d55aa779ef452552a466943002d39e4fElliott Hughes 24710037c866b04550fc5461058c398c2e3e509381ajeffhao// Returns whether the given string is a valid field or method name, 24810037c866b04550fc5461058c398c2e3e509381ajeffhao// additionally allowing names that begin with '<' and end with '>'. 24910037c866b04550fc5461058c398c2e3e509381ajeffhaobool IsValidMemberName(const char* s); 25010037c866b04550fc5461058c398c2e3e509381ajeffhao 25179082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes// Returns the JNI native function name for the non-overloaded method 'm'. 25266f19258f9728d4ffe026074d8fd429d639802faMathieu Chartierstd::string JniShortName(const AbstractMethod* m) 253b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 25479082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes// Returns the JNI native function name for the overloaded method 'm'. 25566f19258f9728d4ffe026074d8fd429d639802faMathieu Chartierstd::string JniLongName(const AbstractMethod* m) 256b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 25779082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes 258d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughesbool ReadFileToString(const std::string& file_name, std::string* result); 259c143c55718342519db5398e41dda31422cf16c79buzbee 260e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes// Returns the current date in ISO yyyy-mm-dd hh:mm:ss format. 261e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughesstd::string GetIsoDate(); 262e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes 2630512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughes// Returns the monotonic time since some unspecified starting point in milliseconds. 2647162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughesuint64_t MilliTime(); 2657162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes 2660512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughes// Returns the monotonic time since some unspecified starting point in microseconds. 267a9ef3fd82bebc6370fc3ddbb094988feb6c83022jeffhaouint64_t MicroTime(); 268a9ef3fd82bebc6370fc3ddbb094988feb6c83022jeffhao 2690512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughes// Returns the monotonic time since some unspecified starting point in nanoseconds. 27083df2ac4a051ee10eafde7587de2faf3f0150fadElliott Hughesuint64_t NanoTime(); 27183df2ac4a051ee10eafde7587de2faf3f0150fadElliott Hughes 2720512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughes// Returns the thread-specific CPU-time clock in microseconds or -1 if unavailable. 273a9ef3fd82bebc6370fc3ddbb094988feb6c83022jeffhaouint64_t ThreadCpuMicroTime(); 274a9ef3fd82bebc6370fc3ddbb094988feb6c83022jeffhao 2750512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughes// Returns the thread-specific CPU-time clock in nanoseconds or -1 if unavailable. 2760512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesuint64_t ThreadCpuNanoTime(); 2770512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughes 278bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes// Converts the given number of nanoseconds to milliseconds. 2793bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogersstatic inline uint64_t NsToMs(uint64_t ns) { 2803bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers return ns / 1000 / 1000; 2813bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers} 2823bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers 2833bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers// Converts the given number of milliseconds to nanoseconds 2843bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogersstatic inline uint64_t MsToNs(uint64_t ns) { 2853bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers return ns * 1000 * 1000; 2863bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers} 2873bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers 28848436bbff87aae61bba20029b4382fa41ce787feElliott Hughes// Splits a string using the given separator character into a vector of 28934023801bd544e613d6e85c9a5b2e743f3710e8fElliott Hughes// strings. Empty strings will be omitted. 29048436bbff87aae61bba20029b4382fa41ce787feElliott Hughesvoid Split(const std::string& s, char separator, std::vector<std::string>& result); 29148436bbff87aae61bba20029b4382fa41ce787feElliott Hughes 29248436bbff87aae61bba20029b4382fa41ce787feElliott Hughes// Joins a vector of strings into a single string, using the given separator. 29348436bbff87aae61bba20029b4382fa41ce787feElliott Hughestemplate <typename StringT> std::string Join(std::vector<StringT>& strings, char separator); 29434023801bd544e613d6e85c9a5b2e743f3710e8fElliott Hughes 29542ee14279065352a4b9a3e8028d02c567e847d05Elliott Hughes// Returns the calling thread's tid. (The C libraries don't expose this.) 29642ee14279065352a4b9a3e8028d02c567e847d05Elliott Hughespid_t GetTid(); 29742ee14279065352a4b9a3e8028d02c567e847d05Elliott Hughes 298289be85116aaf7c48413858b5d0448868b4e61f3Elliott Hughes// Returns the given thread's name. 299289be85116aaf7c48413858b5d0448868b4e61f3Elliott Hughesstd::string GetThreadName(pid_t tid); 300289be85116aaf7c48413858b5d0448868b4e61f3Elliott Hughes 301120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers// Returns details of the given thread's stack. 302120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogersvoid GetThreadStack(pthread_t thread, void*& stack_base, size_t& stack_size); 303e188419b971936086a188843378375f5ced13724Elliott Hughes 304bfe487be25652c5456236661b9d9c3579d2296c1Elliott Hughes// Reads data from "/proc/self/task/${tid}/stat". 305ba0b9c55adce3f533845ab1b25c552589e5b4118Elliott Hughesvoid GetTaskStats(pid_t tid, char& state, int& utime, int& stime, int& task_cpu); 306bfe487be25652c5456236661b9d9c3579d2296c1Elliott Hughes 3071bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes// Returns the name of the scheduler group for the given thread the current process, or the empty string. 3081bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughesstd::string GetSchedulerGroupName(pid_t tid); 3091bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes 310dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes// Sets the name of the current thread. The name may be truncated to an 311dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes// implementation-defined limit. 31222869a9026a08b544eca4cefd67386d347e30d2cElliott Hughesvoid SetThreadName(const char* thread_name); 313dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes 31446e251bf7200cc06f5a9a82ee2030e650f5e1443Elliott Hughes// Dumps the native stack for thread 'tid' to 'os'. 31546e251bf7200cc06f5a9a82ee2030e650f5e1443Elliott Hughesvoid DumpNativeStack(std::ostream& os, pid_t tid, const char* prefix = "", bool include_count = true); 31646e251bf7200cc06f5a9a82ee2030e650f5e1443Elliott Hughes 31746e251bf7200cc06f5a9a82ee2030e650f5e1443Elliott Hughes// Dumps the kernel stack for thread 'tid' to 'os'. Note that this is only available on linux-x86. 31846e251bf7200cc06f5a9a82ee2030e650f5e1443Elliott Hughesvoid DumpKernelStack(std::ostream& os, pid_t tid, const char* prefix = "", bool include_count = true); 31946e251bf7200cc06f5a9a82ee2030e650f5e1443Elliott Hughes 320a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstrom// Find $ANDROID_ROOT, /system, or abort 321a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstromconst char* GetAndroidRoot(); 322a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstrom 323a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstrom// Find $ANDROID_DATA, /data, or abort 324a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstromconst char* GetAndroidData(); 325a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstrom 326b7bbba49d88eae58223d9878da4069bf6d7140bfBrian Carlstrom// Returns the art-cache location, or dies trying. 327795e33081c73a6fead31f45b03fdfb022ef7eab8Shih-wei Liaostd::string GetArtCacheOrDie(const char* android_data); 328a9f19787df870456f5ae654bc9967d319d1bffe0Brian Carlstrom 329262bf46ddc91e5b4fbd367127ff21a1877d939f2jeffhao// Returns the art-cache location for a DexFile or OatFile, or dies trying. 330262bf46ddc91e5b4fbd367127ff21a1877d939f2jeffhaostd::string GetArtCacheFilenameOrDie(const std::string& location); 331262bf46ddc91e5b4fbd367127ff21a1877d939f2jeffhao 3327a967b3d4468ab56bf1b75ebd4d7bf9e6798761bBrian Carlstrom// Check whether the given filename has a valid extension 333262bf46ddc91e5b4fbd367127ff21a1877d939f2jeffhaobool IsValidZipFilename(const std::string& filename); 334262bf46ddc91e5b4fbd367127ff21a1877d939f2jeffhaobool IsValidDexFilename(const std::string& filename); 3357a967b3d4468ab56bf1b75ebd4d7bf9e6798761bBrian Carlstrombool IsValidOatFilename(const std::string& filename); 336b7bbba49d88eae58223d9878da4069bf6d7140bfBrian Carlstrom 337357e9be24c17a6bc2ae9fb53f25c73503116101dMathieu Chartierclass IdentityFunctor { 338357e9be24c17a6bc2ae9fb53f25c73503116101dMathieu Chartier public: 339357e9be24c17a6bc2ae9fb53f25c73503116101dMathieu Chartier template <typename T> 340357e9be24c17a6bc2ae9fb53f25c73503116101dMathieu Chartier inline T operator () (T t) const { return t; } 341357e9be24c17a6bc2ae9fb53f25c73503116101dMathieu Chartier}; 342357e9be24c17a6bc2ae9fb53f25c73503116101dMathieu Chartier 3436b6b5f0e67ce03f38223a525612955663bc1799bCarl Shapiro} // namespace art 344a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro 345a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro#endif // ART_SRC_UTILS_H_ 346