diff --git a/compiler-rt/lib/scudo/standalone/common.h b/compiler-rt/lib/scudo/standalone/common.h --- a/compiler-rt/lib/scudo/standalone/common.h +++ b/compiler-rt/lib/scudo/standalone/common.h @@ -132,6 +132,8 @@ const char *getEnv(const char *Name); +u64 GetRSS(); + u64 getMonotonicTime(); u32 getThreadID(); diff --git a/compiler-rt/lib/scudo/standalone/common.cpp b/compiler-rt/lib/scudo/standalone/common.cpp --- a/compiler-rt/lib/scudo/standalone/common.cpp +++ b/compiler-rt/lib/scudo/standalone/common.cpp @@ -35,4 +35,8 @@ die(); } +#if !SCUDO_LINUX +u64 GetRSS() { return 0; } +#endif + } // namespace scudo diff --git a/compiler-rt/lib/scudo/standalone/internal_defs.h b/compiler-rt/lib/scudo/standalone/internal_defs.h --- a/compiler-rt/lib/scudo/standalone/internal_defs.h +++ b/compiler-rt/lib/scudo/standalone/internal_defs.h @@ -88,6 +88,9 @@ typedef int16_t s16; typedef int32_t s32; typedef int64_t s64; +typedef int fd_t; + +#define kInvalidFd ((fd_t)-1) // The following two functions have platform specific implementations. void outputRaw(const char *Buffer); diff --git a/compiler-rt/lib/scudo/standalone/linux.cpp b/compiler-rt/lib/scudo/standalone/linux.cpp --- a/compiler-rt/lib/scudo/standalone/linux.cpp +++ b/compiler-rt/lib/scudo/standalone/linux.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -180,6 +181,36 @@ extern "C" WEAK int async_safe_write_log(int pri, const char *tag, const char *msg); +static u64 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(*pos) - '0'; + return rss * getPageSizeCached(); +} + +u64 GetRSS() { + fd_t 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; diff --git a/compiler-rt/lib/scudo/standalone/tests/common_test.cpp b/compiler-rt/lib/scudo/standalone/tests/common_test.cpp --- a/compiler-rt/lib/scudo/standalone/tests/common_test.cpp +++ b/compiler-rt/lib/scudo/standalone/tests/common_test.cpp @@ -11,7 +11,9 @@ #include "common.h" #include +#include #include +#include namespace scudo { @@ -69,4 +71,21 @@ unmap(P, Size, 0, &Data); } +#if SCUDO_LINUX +TEST(ScudoCommonTest, GetRssFromBuffer) { + constexpr size_t kAllocSize = 10000000; + constexpr u64 kError = 3000000; + + u64 Initial = scudo::GetRSS(); + EXPECT_GT(Initial, 0); + + int *Alloc = new int[kAllocSize](); + + u64 After = scudo::GetRSS(); + EXPECT_GT(After, Initial); + EXPECT_GT(After - Initial - kError, kAllocSize); + EXPECT_EQ(Alloc[0], 0); +} +#endif // SCUDO_LINUX + } // namespace scudo