Home | History | Annotate | Download | only in base
      1 /*
      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  */
     16 
     17 #include "utils.h"
     18 
     19 #include <inttypes.h>
     20 #include <pthread.h>
     21 #include <sys/stat.h>
     22 #include <sys/types.h>
     23 #include <unistd.h>
     24 
     25 #include <fstream>
     26 #include <memory>
     27 
     28 #include "android-base/file.h"
     29 #include "android-base/stringprintf.h"
     30 #include "android-base/strings.h"
     31 
     32 #include "bit_utils.h"
     33 #include "os.h"
     34 
     35 #if defined(__APPLE__)
     36 #include <crt_externs.h>
     37 #include <sys/syscall.h>
     38 #include "AvailabilityMacros.h"  // For MAC_OS_X_VERSION_MAX_ALLOWED
     39 #endif
     40 
     41 #if defined(__BIONIC__)
     42 // membarrier(2) is only supported for target builds (b/111199492).
     43 #include <linux/membarrier.h>
     44 #include <sys/syscall.h>
     45 #endif
     46 
     47 #if defined(__linux__)
     48 #include <linux/unistd.h>
     49 #include <sys/syscall.h>
     50 #endif
     51 
     52 #if defined(_WIN32)
     53 #include <windows.h>
     54 // This include needs to be here due to our coding conventions.  Unfortunately
     55 // it drags in the definition of the dread ERROR macro.
     56 #ifdef ERROR
     57 #undef ERROR
     58 #endif
     59 #endif
     60 
     61 namespace art {
     62 
     63 using android::base::ReadFileToString;
     64 using android::base::StringPrintf;
     65 
     66 #if defined(__arm__)
     67 
     68 namespace {
     69 
     70 // Bitmap of caches to flush for cacheflush(2). Must be zero for ARM.
     71 static constexpr int kCacheFlushFlags = 0x0;
     72 
     73 // Number of retry attempts when flushing cache ranges.
     74 static constexpr size_t kMaxFlushAttempts = 4;
     75 
     76 int CacheFlush(uintptr_t start, uintptr_t limit) {
     77   // The signature of cacheflush(2) seems to vary by source. On ARM the system call wrapper
     78   //    (bionic/SYSCALLS.TXT) has the form: int cacheflush(long start, long end, long flags);
     79   int r = cacheflush(start, limit, kCacheFlushFlags);
     80   if (r == -1) {
     81     CHECK_NE(errno, EINVAL);
     82   }
     83   return r;
     84 }
     85 
     86 bool TouchAndFlushCacheLinesWithinPage(uintptr_t start, uintptr_t limit, size_t attempts) {
     87   CHECK_LT(start, limit);
     88   CHECK_EQ(RoundDown(start, kPageSize), RoundDown(limit - 1, kPageSize)) << "range spans pages";
     89   // Declare a volatile variable so the compiler does not elide reads from the page being touched.
     90   volatile uint8_t v = 0;
     91   for (size_t i = 0; i < attempts; ++i) {
     92     // Touch page to maximize chance page is resident.
     93     v = *reinterpret_cast<uint8_t*>(start);
     94 
     95     if (LIKELY(CacheFlush(start, limit) == 0)) {
     96       return true;
     97     }
     98   }
     99   return false;
    100 }
    101 
    102 }  // namespace
    103 
    104 bool FlushCpuCaches(void* begin, void* end) {
    105   // This method is specialized for ARM as the generic implementation below uses the
    106   // __builtin___clear_cache() intrinsic which is declared as void. On ARMv7 flushing the CPU
    107   // caches is a privileged operation. The Linux kernel allows these operations to fail when they
    108   // trigger a fault (e.g. page not resident). We use a wrapper for the ARM specific cacheflush()
    109   // system call to detect the failure and potential erroneous state of the data and instruction
    110   // caches.
    111   //
    112   // The Android bug for this is b/132205399 and there's a similar discussion on
    113   // https://reviews.llvm.org/D37788. This is primarily an issue for the dual view JIT where the
    114   // pages where code is executed are only ever RX and never RWX. When attempting to invalidate
    115   // instruction cache lines in the RX mapping after writing fresh code in the RW mapping, the
    116   // page may not be resident (due to memory pressure), and this means that a fault is raised in
    117   // the midst of a cacheflush() call and the instruction cache lines are not invalidated and so
    118   // have stale code.
    119   //
    120   // Other architectures fair better for reasons such as:
    121   //
    122   // (1) stronger coherence between the data and instruction caches.
    123   //
    124   // (2) fault handling that allows flushing/invalidation to continue after
    125   //     a missing page has been faulted in.
    126 
    127   // In the common case, this flush of the complete range succeeds.
    128   uintptr_t start = reinterpret_cast<uintptr_t>(begin);
    129   const uintptr_t limit = reinterpret_cast<uintptr_t>(end);
    130   if (LIKELY(CacheFlush(start, limit) == 0)) {
    131     return true;
    132   }
    133 
    134   // A rare failure has occurred implying that part of the range (begin, end] has been swapped
    135   // out. Retry flushing but this time grouping cache-line flushes on individual pages and
    136   // touching each page before flushing.
    137   uintptr_t next_page = RoundUp(start + 1, kPageSize);
    138   while (start < limit) {
    139     uintptr_t boundary = std::min(next_page, limit);
    140     if (!TouchAndFlushCacheLinesWithinPage(start, boundary, kMaxFlushAttempts)) {
    141       return false;
    142     }
    143     start = boundary;
    144     next_page += kPageSize;
    145   }
    146   return true;
    147 }
    148 
    149 #else
    150 
    151 bool FlushCpuCaches(void* begin, void* end) {
    152   __builtin___clear_cache(reinterpret_cast<char*>(begin), reinterpret_cast<char*>(end));
    153   return true;
    154 }
    155 
    156 #endif
    157 
    158 pid_t GetTid() {
    159 #if defined(__APPLE__)
    160   uint64_t owner;
    161   CHECK_PTHREAD_CALL(pthread_threadid_np, (nullptr, &owner), __FUNCTION__);  // Requires Mac OS 10.6
    162   return owner;
    163 #elif defined(__BIONIC__)
    164   return gettid();
    165 #elif defined(_WIN32)
    166   return static_cast<pid_t>(::GetCurrentThreadId());
    167 #else
    168   return syscall(__NR_gettid);
    169 #endif
    170 }
    171 
    172 std::string GetThreadName(pid_t tid) {
    173   std::string result;
    174 #ifdef _WIN32
    175   UNUSED(tid);
    176   result = "<unknown>";
    177 #else
    178   // TODO: make this less Linux-specific.
    179   if (ReadFileToString(StringPrintf("/proc/self/task/%d/comm", tid), &result)) {
    180     result.resize(result.size() - 1);  // Lose the trailing '\n'.
    181   } else {
    182     result = "<unknown>";
    183   }
    184 #endif
    185   return result;
    186 }
    187 
    188 std::string PrettySize(int64_t byte_count) {
    189   // The byte thresholds at which we display amounts.  A byte count is displayed
    190   // in unit U when kUnitThresholds[U] <= bytes < kUnitThresholds[U+1].
    191   static const int64_t kUnitThresholds[] = {
    192     0,       // B up to...
    193     10*KB,   // KB up to...
    194     10*MB,   // MB up to...
    195     10LL*GB  // GB from here.
    196   };
    197   static const int64_t kBytesPerUnit[] = { 1, KB, MB, GB };
    198   static const char* const kUnitStrings[] = { "B", "KB", "MB", "GB" };
    199   const char* negative_str = "";
    200   if (byte_count < 0) {
    201     negative_str = "-";
    202     byte_count = -byte_count;
    203   }
    204   int i = arraysize(kUnitThresholds);
    205   while (--i > 0) {
    206     if (byte_count >= kUnitThresholds[i]) {
    207       break;
    208     }
    209   }
    210   return StringPrintf("%s%" PRId64 "%s",
    211                       negative_str, byte_count / kBytesPerUnit[i], kUnitStrings[i]);
    212 }
    213 
    214 void Split(const std::string& s, char separator, std::vector<std::string>* result) {
    215   const char* p = s.data();
    216   const char* end = p + s.size();
    217   while (p != end) {
    218     if (*p == separator) {
    219       ++p;
    220     } else {
    221       const char* start = p;
    222       while (++p != end && *p != separator) {
    223         // Skip to the next occurrence of the separator.
    224       }
    225       result->push_back(std::string(start, p - start));
    226     }
    227   }
    228 }
    229 
    230 void SetThreadName(const char* thread_name) {
    231   int hasAt = 0;
    232   int hasDot = 0;
    233   const char* s = thread_name;
    234   while (*s) {
    235     if (*s == '.') {
    236       hasDot = 1;
    237     } else if (*s == '@') {
    238       hasAt = 1;
    239     }
    240     s++;
    241   }
    242   int len = s - thread_name;
    243   if (len < 15 || hasAt || !hasDot) {
    244     s = thread_name;
    245   } else {
    246     s = thread_name + len - 15;
    247   }
    248 #if defined(__linux__) || defined(_WIN32)
    249   // pthread_setname_np fails rather than truncating long strings.
    250   char buf[16];       // MAX_TASK_COMM_LEN=16 is hard-coded in the kernel.
    251   strncpy(buf, s, sizeof(buf)-1);
    252   buf[sizeof(buf)-1] = '\0';
    253   errno = pthread_setname_np(pthread_self(), buf);
    254   if (errno != 0) {
    255     PLOG(WARNING) << "Unable to set the name of current thread to '" << buf << "'";
    256   }
    257 #else  // __APPLE__
    258   pthread_setname_np(thread_name);
    259 #endif
    260 }
    261 
    262 void GetTaskStats(pid_t tid, char* state, int* utime, int* stime, int* task_cpu) {
    263   *utime = *stime = *task_cpu = 0;
    264 #ifdef _WIN32
    265   // TODO: implement this.
    266   UNUSED(tid);
    267   *state = 'S';
    268 #else
    269   std::string stats;
    270   // TODO: make this less Linux-specific.
    271   if (!ReadFileToString(StringPrintf("/proc/self/task/%d/stat", tid), &stats)) {
    272     return;
    273   }
    274   // Skip the command, which may contain spaces.
    275   stats = stats.substr(stats.find(')') + 2);
    276   // Extract the three fields we care about.
    277   std::vector<std::string> fields;
    278   Split(stats, ' ', &fields);
    279   *state = fields[0][0];
    280   *utime = strtoull(fields[11].c_str(), nullptr, 10);
    281   *stime = strtoull(fields[12].c_str(), nullptr, 10);
    282   *task_cpu = strtoull(fields[36].c_str(), nullptr, 10);
    283 #endif
    284 }
    285 
    286 void SleepForever() {
    287   while (true) {
    288     usleep(1000000);
    289   }
    290 }
    291 
    292 std::string GetProcessStatus(const char* key) {
    293   // Build search pattern of key and separator.
    294   std::string pattern(key);
    295   pattern.push_back(':');
    296 
    297   // Search for status lines starting with pattern.
    298   std::ifstream fs("/proc/self/status");
    299   std::string line;
    300   while (std::getline(fs, line)) {
    301     if (strncmp(pattern.c_str(), line.c_str(), pattern.size()) == 0) {
    302       // Skip whitespace in matching line (if any).
    303       size_t pos = line.find_first_not_of(" \t", pattern.size());
    304       if (UNLIKELY(pos == std::string::npos)) {
    305         break;
    306       }
    307       return std::string(line, pos);
    308     }
    309   }
    310   return "<unknown>";
    311 }
    312 
    313 }  // namespace art
    314