blob: 19311b3dedc09c5c61a6229a6ee50a35fba6a3f0 [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 */
Elliott Hughes11e45072011-08-16 17:40:46 -070016
Elliott Hughes42ee1422011-09-06 12:33:32 -070017#include "utils.h"
18
Nicolas Geoffray8852e532019-10-30 09:43:35 +000019#include <dirent.h>
Christopher Ferris943af7d2014-01-16 12:41:46 -080020#include <inttypes.h>
Elliott Hughes92b3b562011-09-08 16:32:26 -070021#include <pthread.h>
Brian Carlstroma9f19782011-10-13 00:14:47 -070022#include <sys/stat.h>
Elliott Hughes42ee1422011-09-06 12:33:32 -070023#include <sys/types.h>
24#include <unistd.h>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070025
Wei Li8991ad02018-09-13 16:43:39 +080026#include <fstream>
Ian Rogers700a4022014-05-19 16:49:03 -070027#include <memory>
Elliott Hughes42ee1422011-09-06 12:33:32 -070028
David Sehr013fd802018-01-11 22:55:24 -080029#include "android-base/file.h"
Andreas Gampe46ee31b2016-12-14 10:11:49 -080030#include "android-base/stringprintf.h"
Andreas Gampe9186ced2016-12-12 14:28:21 -080031#include "android-base/strings.h"
32
Orion Hodsonaeb02232019-06-25 14:18:18 +010033#include "bit_utils.h"
David Sehr1979c642018-04-26 14:41:18 -070034#include "os.h"
Elliott Hughes11e45072011-08-16 17:40:46 -070035
Elliott Hughes4ae722a2012-03-13 11:08:51 -070036#if defined(__APPLE__)
David Sehrfa442002016-08-22 18:42:08 -070037#include <crt_externs.h>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070038#include <sys/syscall.h>
39#include "AvailabilityMacros.h" // For MAC_OS_X_VERSION_MAX_ALLOWED
Elliott Hughes4ae722a2012-03-13 11:08:51 -070040#endif
41
Orion Hodsonf2331362018-07-11 15:14:10 +010042#if defined(__BIONIC__)
43// membarrier(2) is only supported for target builds (b/111199492).
44#include <linux/membarrier.h>
45#include <sys/syscall.h>
46#endif
47
Elliott Hughes058a6de2012-05-24 19:13:02 -070048#if defined(__linux__)
Elliott Hughese1aee692012-01-17 16:40:10 -080049#include <linux/unistd.h>
David Sehr10db8fe2018-07-18 11:01:20 -070050#include <sys/syscall.h>
Nicolas Geoffray8d6651d2019-07-08 10:03:16 +010051#include <sys/utsname.h>
David Sehr10db8fe2018-07-18 11:01:20 -070052#endif
53
54#if defined(_WIN32)
55#include <windows.h>
56// This include needs to be here due to our coding conventions. Unfortunately
57// it drags in the definition of the dread ERROR macro.
58#ifdef ERROR
59#undef ERROR
60#endif
Elliott Hughese1aee692012-01-17 16:40:10 -080061#endif
62
Elliott Hughes11e45072011-08-16 17:40:46 -070063namespace art {
64
David Sehr013fd802018-01-11 22:55:24 -080065using android::base::ReadFileToString;
Andreas Gampe46ee31b2016-12-14 10:11:49 -080066using android::base::StringPrintf;
67
Orion Hodsonaeb02232019-06-25 14:18:18 +010068#if defined(__arm__)
69
70namespace {
71
72// Bitmap of caches to flush for cacheflush(2). Must be zero for ARM.
73static constexpr int kCacheFlushFlags = 0x0;
74
75// Number of retry attempts when flushing cache ranges.
76static constexpr size_t kMaxFlushAttempts = 4;
77
78int CacheFlush(uintptr_t start, uintptr_t limit) {
79 // The signature of cacheflush(2) seems to vary by source. On ARM the system call wrapper
80 // (bionic/SYSCALLS.TXT) has the form: int cacheflush(long start, long end, long flags);
81 int r = cacheflush(start, limit, kCacheFlushFlags);
82 if (r == -1) {
83 CHECK_NE(errno, EINVAL);
84 }
85 return r;
86}
87
88bool TouchAndFlushCacheLinesWithinPage(uintptr_t start, uintptr_t limit, size_t attempts) {
89 CHECK_LT(start, limit);
90 CHECK_EQ(RoundDown(start, kPageSize), RoundDown(limit - 1, kPageSize)) << "range spans pages";
91 // Declare a volatile variable so the compiler does not elide reads from the page being touched.
92 volatile uint8_t v = 0;
93 for (size_t i = 0; i < attempts; ++i) {
94 // Touch page to maximize chance page is resident.
95 v = *reinterpret_cast<uint8_t*>(start);
96
97 if (LIKELY(CacheFlush(start, limit) == 0)) {
98 return true;
99 }
100 }
101 return false;
102}
103
104} // namespace
105
106bool FlushCpuCaches(void* begin, void* end) {
107 // This method is specialized for ARM as the generic implementation below uses the
108 // __builtin___clear_cache() intrinsic which is declared as void. On ARMv7 flushing the CPU
109 // caches is a privileged operation. The Linux kernel allows these operations to fail when they
110 // trigger a fault (e.g. page not resident). We use a wrapper for the ARM specific cacheflush()
111 // system call to detect the failure and potential erroneous state of the data and instruction
112 // caches.
113 //
114 // The Android bug for this is b/132205399 and there's a similar discussion on
115 // https://reviews.llvm.org/D37788. This is primarily an issue for the dual view JIT where the
116 // pages where code is executed are only ever RX and never RWX. When attempting to invalidate
117 // instruction cache lines in the RX mapping after writing fresh code in the RW mapping, the
118 // page may not be resident (due to memory pressure), and this means that a fault is raised in
119 // the midst of a cacheflush() call and the instruction cache lines are not invalidated and so
120 // have stale code.
121 //
122 // Other architectures fair better for reasons such as:
123 //
124 // (1) stronger coherence between the data and instruction caches.
125 //
126 // (2) fault handling that allows flushing/invalidation to continue after
127 // a missing page has been faulted in.
128
129 uintptr_t start = reinterpret_cast<uintptr_t>(begin);
130 const uintptr_t limit = reinterpret_cast<uintptr_t>(end);
131 if (LIKELY(CacheFlush(start, limit) == 0)) {
132 return true;
133 }
134
135 // A rare failure has occurred implying that part of the range (begin, end] has been swapped
136 // out. Retry flushing but this time grouping cache-line flushes on individual pages and
137 // touching each page before flushing.
138 uintptr_t next_page = RoundUp(start + 1, kPageSize);
139 while (start < limit) {
140 uintptr_t boundary = std::min(next_page, limit);
141 if (!TouchAndFlushCacheLinesWithinPage(start, boundary, kMaxFlushAttempts)) {
142 return false;
143 }
144 start = boundary;
145 next_page += kPageSize;
146 }
147 return true;
148}
149
150#else
151
152bool FlushCpuCaches(void* begin, void* end) {
153 __builtin___clear_cache(reinterpret_cast<char*>(begin), reinterpret_cast<char*>(end));
154 return true;
155}
156
157#endif
158
Nicolas Geoffray8d6651d2019-07-08 10:03:16 +0100159bool CacheOperationsMaySegFault() {
160#if defined(__linux__) && defined(__aarch64__)
161 // Avoid issue on older ARM64 kernels where data cache operations could be classified as writes
162 // and cause segmentation faults. This was fixed in Linux 3.11rc2:
163 //
164 // https://github.com/torvalds/linux/commit/db6f41063cbdb58b14846e600e6bc3f4e4c2e888
165 //
166 // This behaviour means we should avoid the dual view JIT on the device. This is just
167 // an issue when running tests on devices that have an old kernel.
168 static constexpr int kRequiredMajor = 3;
169 static constexpr int kRequiredMinor = 12;
170 struct utsname uts;
171 int major, minor;
172 if (uname(&uts) != 0 ||
173 strcmp(uts.sysname, "Linux") != 0 ||
174 sscanf(uts.release, "%d.%d", &major, &minor) != 2 ||
175 (major < kRequiredMajor || (major == kRequiredMajor && minor < kRequiredMinor))) {
176 return true;
177 }
178#endif
179 return false;
180}
181
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800182pid_t GetTid() {
Brian Carlstromf3a26412012-08-24 11:06:02 -0700183#if defined(__APPLE__)
184 uint64_t owner;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700185 CHECK_PTHREAD_CALL(pthread_threadid_np, (nullptr, &owner), __FUNCTION__); // Requires Mac OS 10.6
Brian Carlstromf3a26412012-08-24 11:06:02 -0700186 return owner;
Elliott Hughes323aa862014-08-20 15:00:04 -0700187#elif defined(__BIONIC__)
188 return gettid();
David Sehr10db8fe2018-07-18 11:01:20 -0700189#elif defined(_WIN32)
190 return static_cast<pid_t>(::GetCurrentThreadId());
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800191#else
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800192 return syscall(__NR_gettid);
193#endif
194}
195
Elliott Hughes289be852012-06-12 13:57:20 -0700196std::string GetThreadName(pid_t tid) {
197 std::string result;
David Sehr10db8fe2018-07-18 11:01:20 -0700198#ifdef _WIN32
199 UNUSED(tid);
200 result = "<unknown>";
201#else
David Sehr013fd802018-01-11 22:55:24 -0800202 // TODO: make this less Linux-specific.
Elliott Hughes289be852012-06-12 13:57:20 -0700203 if (ReadFileToString(StringPrintf("/proc/self/task/%d/comm", tid), &result)) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700204 result.resize(result.size() - 1); // Lose the trailing '\n'.
Elliott Hughes289be852012-06-12 13:57:20 -0700205 } else {
206 result = "<unknown>";
207 }
David Sehr10db8fe2018-07-18 11:01:20 -0700208#endif
Elliott Hughes289be852012-06-12 13:57:20 -0700209 return result;
210}
211
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800212std::string PrettySize(int64_t byte_count) {
Elliott Hughesc967f782012-04-16 10:23:15 -0700213 // The byte thresholds at which we display amounts. A byte count is displayed
214 // in unit U when kUnitThresholds[U] <= bytes < kUnitThresholds[U+1].
Ian Rogersef7d42f2014-01-06 12:55:46 -0800215 static const int64_t kUnitThresholds[] = {
David Srbeckyde6c7142019-01-09 11:27:40 +0000216 0, // B up to...
217 10*KB, // KB up to...
218 10*MB, // MB up to...
219 10LL*GB // GB from here.
Elliott Hughesc967f782012-04-16 10:23:15 -0700220 };
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800221 static const int64_t kBytesPerUnit[] = { 1, KB, MB, GB };
Elliott Hughesc967f782012-04-16 10:23:15 -0700222 static const char* const kUnitStrings[] = { "B", "KB", "MB", "GB" };
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800223 const char* negative_str = "";
224 if (byte_count < 0) {
225 negative_str = "-";
226 byte_count = -byte_count;
227 }
Elliott Hughesc967f782012-04-16 10:23:15 -0700228 int i = arraysize(kUnitThresholds);
229 while (--i > 0) {
230 if (byte_count >= kUnitThresholds[i]) {
231 break;
232 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800233 }
Brian Carlstrom474cc792014-03-07 14:18:15 -0800234 return StringPrintf("%s%" PRId64 "%s",
235 negative_str, byte_count / kBytesPerUnit[i], kUnitStrings[i]);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800236}
237
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700238void Split(const std::string& s, char separator, std::vector<std::string>* result) {
Elliott Hughes34023802011-08-30 12:06:17 -0700239 const char* p = s.data();
240 const char* end = p + s.size();
241 while (p != end) {
Elliott Hughes48436bb2012-02-07 15:23:28 -0800242 if (*p == separator) {
Elliott Hughes34023802011-08-30 12:06:17 -0700243 ++p;
244 } else {
245 const char* start = p;
Elliott Hughes48436bb2012-02-07 15:23:28 -0800246 while (++p != end && *p != separator) {
247 // Skip to the next occurrence of the separator.
Elliott Hughes34023802011-08-30 12:06:17 -0700248 }
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700249 result->push_back(std::string(start, p - start));
Elliott Hughes34023802011-08-30 12:06:17 -0700250 }
251 }
252}
253
Elliott Hughes22869a92012-03-27 14:08:24 -0700254void SetThreadName(const char* thread_name) {
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700255 bool hasAt = false;
256 bool hasDot = false;
Elliott Hughes22869a92012-03-27 14:08:24 -0700257 const char* s = thread_name;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700258 while (*s) {
259 if (*s == '.') {
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700260 hasDot = true;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700261 } else if (*s == '@') {
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700262 hasAt = true;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700263 }
264 s++;
265 }
Elliott Hughes22869a92012-03-27 14:08:24 -0700266 int len = s - thread_name;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700267 if (len < 15 || hasAt || !hasDot) {
Elliott Hughes22869a92012-03-27 14:08:24 -0700268 s = thread_name;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700269 } else {
Elliott Hughes22869a92012-03-27 14:08:24 -0700270 s = thread_name + len - 15;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700271 }
David Sehr10db8fe2018-07-18 11:01:20 -0700272#if defined(__linux__) || defined(_WIN32)
Elliott Hughes7c6a61e2012-03-12 18:01:41 -0700273 // pthread_setname_np fails rather than truncating long strings.
Elliott Hughes0a18df82015-01-09 15:16:16 -0800274 char buf[16]; // MAX_TASK_COMM_LEN=16 is hard-coded in the kernel.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700275 strncpy(buf, s, sizeof(buf)-1);
276 buf[sizeof(buf)-1] = '\0';
277 errno = pthread_setname_np(pthread_self(), buf);
278 if (errno != 0) {
279 PLOG(WARNING) << "Unable to set the name of current thread to '" << buf << "'";
280 }
Elliott Hughes0a18df82015-01-09 15:16:16 -0800281#else // __APPLE__
Elliott Hughes22869a92012-03-27 14:08:24 -0700282 pthread_setname_np(thread_name);
Elliott Hughesdcc24742011-09-07 14:02:44 -0700283#endif
284}
285
Brian Carlstrom29212012013-09-12 22:18:30 -0700286void GetTaskStats(pid_t tid, char* state, int* utime, int* stime, int* task_cpu) {
287 *utime = *stime = *task_cpu = 0;
David Sehr10db8fe2018-07-18 11:01:20 -0700288#ifdef _WIN32
289 // TODO: implement this.
290 UNUSED(tid);
291 *state = 'S';
292#else
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700293 std::string stats;
David Sehr013fd802018-01-11 22:55:24 -0800294 // TODO: make this less Linux-specific.
Elliott Hughes8a31b502012-04-30 19:36:11 -0700295 if (!ReadFileToString(StringPrintf("/proc/self/task/%d/stat", tid), &stats)) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700296 return;
297 }
298 // Skip the command, which may contain spaces.
299 stats = stats.substr(stats.find(')') + 2);
300 // Extract the three fields we care about.
301 std::vector<std::string> fields;
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700302 Split(stats, ' ', &fields);
Brian Carlstrom29212012013-09-12 22:18:30 -0700303 *state = fields[0][0];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700304 *utime = strtoull(fields[11].c_str(), nullptr, 10);
305 *stime = strtoull(fields[12].c_str(), nullptr, 10);
306 *task_cpu = strtoull(fields[36].c_str(), nullptr, 10);
David Sehr10db8fe2018-07-18 11:01:20 -0700307#endif
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700308}
309
Mathieu Chartier4d87df62016-01-07 15:14:19 -0800310void SleepForever() {
311 while (true) {
Hans Boehm65c18a22020-01-03 23:37:13 +0000312 sleep(100000000);
Mathieu Chartier4d87df62016-01-07 15:14:19 -0800313 }
314}
315
Wei Li8991ad02018-09-13 16:43:39 +0800316std::string GetProcessStatus(const char* key) {
317 // Build search pattern of key and separator.
318 std::string pattern(key);
319 pattern.push_back(':');
320
321 // Search for status lines starting with pattern.
322 std::ifstream fs("/proc/self/status");
323 std::string line;
324 while (std::getline(fs, line)) {
325 if (strncmp(pattern.c_str(), line.c_str(), pattern.size()) == 0) {
326 // Skip whitespace in matching line (if any).
327 size_t pos = line.find_first_not_of(" \t", pattern.size());
328 if (UNLIKELY(pos == std::string::npos)) {
329 break;
330 }
331 return std::string(line, pos);
332 }
333 }
334 return "<unknown>";
335}
336
Nicolas Geoffrayccb0b5f2019-08-15 18:10:50 +0100337bool IsAddressKnownBackedByFileOrShared(const void* addr) {
338 // We use the Linux pagemap interface for knowing if an address is backed
339 // by a file or is shared. See:
340 // https://www.kernel.org/doc/Documentation/vm/pagemap.txt
341 uintptr_t vmstart = reinterpret_cast<uintptr_t>(AlignDown(addr, kPageSize));
342 off_t index = (vmstart / kPageSize) * sizeof(uint64_t);
343 android::base::unique_fd pagemap(open("/proc/self/pagemap", O_RDONLY | O_CLOEXEC));
344 if (pagemap == -1) {
345 return false;
346 }
347 if (lseek(pagemap, index, SEEK_SET) != index) {
348 return false;
349 }
350 uint64_t flags;
351 if (read(pagemap, &flags, sizeof(uint64_t)) != sizeof(uint64_t)) {
352 return false;
353 }
354 // From https://www.kernel.org/doc/Documentation/vm/pagemap.txt:
355 // * Bit 61 page is file-page or shared-anon (since 3.5)
356 return (flags & (1LL << 61)) != 0;
357}
358
Nicolas Geoffray8852e532019-10-30 09:43:35 +0000359int GetTaskCount() {
360 DIR* directory = opendir("/proc/self/task");
361 if (directory == nullptr) {
362 return -1;
363 }
364
365 uint32_t count = 0;
366 struct dirent* entry = nullptr;
367 while ((entry = readdir(directory)) != nullptr) {
368 if ((strcmp(entry->d_name, ".") == 0) || (strcmp(entry->d_name, "..") == 0)) {
369 continue;
370 }
371 ++count;
372 }
373 closedir(directory);
374 return count;
375}
376
Elliott Hughes42ee1422011-09-06 12:33:32 -0700377} // namespace art