diff options
Diffstat (limited to 'standalone/linux.cpp')
-rw-r--r-- | standalone/linux.cpp | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/standalone/linux.cpp b/standalone/linux.cpp index c77c1bb600d..e285d8a3d2d 100644 --- a/standalone/linux.cpp +++ b/standalone/linux.cpp @@ -11,6 +11,7 @@ #if SCUDO_LINUX #include "common.h" +#include "internal_defs.h" #include "linux.h" #include "mutex.h" #include "string_utils.h" @@ -19,6 +20,7 @@ #include <fcntl.h> #include <linux/futex.h> #include <sched.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/mman.h> @@ -127,6 +129,10 @@ void HybridMutex::unlock() { } } +void HybridMutex::assertHeldImpl() { + CHECK(atomic_load(&M, memory_order_acquire) != Unlocked); +} + u64 getMonotonicTime() { timespec TS; clock_gettime(CLOCK_MONOTONIC, &TS); @@ -134,6 +140,17 @@ u64 getMonotonicTime() { static_cast<u64>(TS.tv_nsec); } +u64 getMonotonicTimeFast() { +#if defined(CLOCK_MONOTONIC_COARSE) + timespec TS; + clock_gettime(CLOCK_MONOTONIC_COARSE, &TS); + return static_cast<u64>(TS.tv_sec) * (1000ULL * 1000 * 1000) + + static_cast<u64>(TS.tv_nsec); +#else + return getMonotonicTime(); +#endif +} + u32 getNumberOfCPUs() { cpu_set_t CPUs; // sched_getaffinity can fail for a variety of legitimate reasons (lack of @@ -180,6 +197,39 @@ bool getRandom(void *Buffer, uptr Length, UNUSED bool Blocking) { extern "C" WEAK int async_safe_write_log(int pri, const char *tag, const char *msg); +static uptr GetRSSFromBuffer(const char *Buf) { + // The format of the file is: + // 1084 89 69 11 0 79 0 + // We need the second number which is RSS in pages. + const char *Pos = Buf; + // Skip the first number. + while (*Pos >= '0' && *Pos <= '9') + Pos++; + // Skip whitespaces. + while (!(*Pos >= '0' && *Pos <= '9') && *Pos != 0) + Pos++; + // Read the number. + u64 Rss = 0; + for (; *Pos >= '0' && *Pos <= '9'; Pos++) + Rss = Rss * 10 + static_cast<u64>(*Pos) - '0'; + return static_cast<uptr>(Rss * getPageSizeCached()); +} + +uptr GetRSS() { + // TODO: We currently use sanitizer_common's GetRSS which reads the + // RSS from /proc/self/statm by default. We might want to + // call getrusage directly, even if it's less accurate. + auto Fd = open("/proc/self/statm", O_RDONLY); + char Buf[64]; + s64 Len = read(Fd, Buf, sizeof(Buf) - 1); + close(Fd); + if (Len <= 0) + return 0; + Buf[Len] = 0; + + return GetRSSFromBuffer(Buf); +} + void outputRaw(const char *Buffer) { if (&async_safe_write_log) { constexpr s32 AndroidLogInfo = 4; |