summaryrefslogtreecommitdiff
path: root/libartbase/base/utils.cc
diff options
context:
space:
mode:
Diffstat (limited to 'libartbase/base/utils.cc')
-rw-r--r--libartbase/base/utils.cc85
1 files changed, 45 insertions, 40 deletions
diff --git a/libartbase/base/utils.cc b/libartbase/base/utils.cc
index 74cc5b97b2..b989d9e495 100644
--- a/libartbase/base/utils.cc
+++ b/libartbase/base/utils.cc
@@ -19,11 +19,10 @@
#include <inttypes.h>
#include <pthread.h>
#include <sys/stat.h>
-#include <sys/syscall.h>
#include <sys/types.h>
-#include <sys/wait.h>
#include <unistd.h>
+#include <fstream>
#include <memory>
#include "android-base/file.h"
@@ -46,6 +45,16 @@
#if defined(__linux__)
#include <linux/unistd.h>
+#include <sys/syscall.h>
+#endif
+
+#if defined(_WIN32)
+#include <windows.h>
+// This include needs to be here due to our coding conventions. Unfortunately
+// it drags in the definition of the dread ERROR macro.
+#ifdef ERROR
+#undef ERROR
+#endif
#endif
namespace art {
@@ -60,6 +69,8 @@ pid_t GetTid() {
return owner;
#elif defined(__BIONIC__)
return gettid();
+#elif defined(_WIN32)
+ return static_cast<pid_t>(::GetCurrentThreadId());
#else
return syscall(__NR_gettid);
#endif
@@ -67,12 +78,17 @@ pid_t GetTid() {
std::string GetThreadName(pid_t tid) {
std::string result;
+#ifdef _WIN32
+ UNUSED(tid);
+ result = "<unknown>";
+#else
// TODO: make this less Linux-specific.
if (ReadFileToString(StringPrintf("/proc/self/task/%d/comm", tid), &result)) {
result.resize(result.size() - 1); // Lose the trailing '\n'.
} else {
result = "<unknown>";
}
+#endif
return result;
}
@@ -80,10 +96,10 @@ std::string PrettySize(int64_t byte_count) {
// The byte thresholds at which we display amounts. A byte count is displayed
// in unit U when kUnitThresholds[U] <= bytes < kUnitThresholds[U+1].
static const int64_t kUnitThresholds[] = {
- 0, // B up to...
- 3*1024, // KB up to...
- 2*1024*1024, // MB up to...
- 1024*1024*1024 // GB from here.
+ 0, // B up to...
+ 10*KB, // KB up to...
+ 10*MB, // MB up to...
+ 10LL*GB // GB from here.
};
static const int64_t kBytesPerUnit[] = { 1, KB, MB, GB };
static const char* const kUnitStrings[] = { "B", "KB", "MB", "GB" };
@@ -136,7 +152,7 @@ void SetThreadName(const char* thread_name) {
} else {
s = thread_name + len - 15;
}
-#if defined(__linux__)
+#if defined(__linux__) || defined(_WIN32)
// pthread_setname_np fails rather than truncating long strings.
char buf[16]; // MAX_TASK_COMM_LEN=16 is hard-coded in the kernel.
strncpy(buf, s, sizeof(buf)-1);
@@ -152,6 +168,11 @@ void SetThreadName(const char* thread_name) {
void GetTaskStats(pid_t tid, char* state, int* utime, int* stime, int* task_cpu) {
*utime = *stime = *task_cpu = 0;
+#ifdef _WIN32
+ // TODO: implement this.
+ UNUSED(tid);
+ *state = 'S';
+#else
std::string stats;
// TODO: make this less Linux-specific.
if (!ReadFileToString(StringPrintf("/proc/self/task/%d/stat", tid), &stats)) {
@@ -166,6 +187,7 @@ void GetTaskStats(pid_t tid, char* state, int* utime, int* stime, int* task_cpu)
*utime = strtoull(fields[11].c_str(), nullptr, 10);
*stime = strtoull(fields[12].c_str(), nullptr, 10);
*task_cpu = strtoull(fields[36].c_str(), nullptr, 10);
+#endif
}
static void ParseStringAfterChar(const std::string& s,
@@ -213,42 +235,25 @@ void SleepForever() {
}
}
-bool FlushInstructionPipeline() {
- // membarrier(2) is only supported for target builds (b/111199492).
-#if defined(__BIONIC__)
- static constexpr int kSyncCoreMask =
- MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE |
- MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE;
- static bool have_probed = false;
- static bool have_sync_core = false;
-
- if (UNLIKELY(!have_probed)) {
- // Probe membarrier(2) commands supported by kernel.
- int commands = syscall(__NR_membarrier, MEMBARRIER_CMD_QUERY, 0);
- if (commands >= 0) {
- have_sync_core = (commands & kSyncCoreMask) == kSyncCoreMask;
- if (have_sync_core) {
- // Register with kernel that we'll be using the private expedited sync core command.
- CheckedCall(syscall,
- "membarrier register sync core",
- __NR_membarrier,
- MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE,
- 0);
+std::string GetProcessStatus(const char* key) {
+ // Build search pattern of key and separator.
+ std::string pattern(key);
+ pattern.push_back(':');
+
+ // Search for status lines starting with pattern.
+ std::ifstream fs("/proc/self/status");
+ std::string line;
+ while (std::getline(fs, line)) {
+ if (strncmp(pattern.c_str(), line.c_str(), pattern.size()) == 0) {
+ // Skip whitespace in matching line (if any).
+ size_t pos = line.find_first_not_of(" \t", pattern.size());
+ if (UNLIKELY(pos == std::string::npos)) {
+ break;
}
+ return std::string(line, pos);
}
- have_probed = true;
- }
-
- if (have_sync_core) {
- CheckedCall(syscall,
- "membarrier sync core",
- __NR_membarrier,
- MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE,
- 0);
- return true;
}
-#endif // defined(__BIONIC__)
- return false;
+ return "<unknown>";
}
} // namespace art