From: Christopher Ferris Date: Thu, 9 Mar 2023 00:56:24 +0000 (-0800) Subject: [scudo] Add a fast get time version. X-Git-Tag: upstream/17.0.6~15320 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=32be3405f57f1e4d0ec0da943434113450583e89;p=platform%2Fupstream%2Fllvm.git [scudo] Add a fast get time version. On Android, the _COARSE version of clock_gettime is about twice as fast. Therefore, add a getMonotonicTimeFast function that is used in the releaseToOSMaybe functions. Reviewed By: Chia-hungDuan Differential Revision: https://reviews.llvm.org/D145636 --- diff --git a/compiler-rt/lib/scudo/standalone/common.h b/compiler-rt/lib/scudo/standalone/common.h index aa15e9e..e1ca264 100644 --- a/compiler-rt/lib/scudo/standalone/common.h +++ b/compiler-rt/lib/scudo/standalone/common.h @@ -147,6 +147,9 @@ const char *getEnv(const char *Name); uptr GetRSS(); u64 getMonotonicTime(); +// Gets the time faster but with less accuracy. Can call getMonotonicTime +// if no fast version is available. +u64 getMonotonicTimeFast(); u32 getThreadID(); diff --git a/compiler-rt/lib/scudo/standalone/fuchsia.cpp b/compiler-rt/lib/scudo/standalone/fuchsia.cpp index 6aae03b..ef93542 100644 --- a/compiler-rt/lib/scudo/standalone/fuchsia.cpp +++ b/compiler-rt/lib/scudo/standalone/fuchsia.cpp @@ -200,6 +200,7 @@ void HybridMutex::unlock() __TA_NO_THREAD_SAFETY_ANALYSIS { void HybridMutex::assertHeldImpl() __TA_NO_THREAD_SAFETY_ANALYSIS {} u64 getMonotonicTime() { return _zx_clock_get_monotonic(); } +u64 getMonotonicTimeFast() { return _zx_clock_get_monotonic(); } u32 getNumberOfCPUs() { return _zx_system_get_num_cpus(); } diff --git a/compiler-rt/lib/scudo/standalone/linux.cpp b/compiler-rt/lib/scudo/standalone/linux.cpp index 33757e2..e285d8a 100644 --- a/compiler-rt/lib/scudo/standalone/linux.cpp +++ b/compiler-rt/lib/scudo/standalone/linux.cpp @@ -140,6 +140,17 @@ u64 getMonotonicTime() { static_cast(TS.tv_nsec); } +u64 getMonotonicTimeFast() { +#if defined(CLOCK_MONOTONIC_COARSE) + timespec TS; + clock_gettime(CLOCK_MONOTONIC_COARSE, &TS); + return static_cast(TS.tv_sec) * (1000ULL * 1000 * 1000) + + static_cast(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 diff --git a/compiler-rt/lib/scudo/standalone/primary32.h b/compiler-rt/lib/scudo/standalone/primary32.h index 5d157f0..5d3bc65 100644 --- a/compiler-rt/lib/scudo/standalone/primary32.h +++ b/compiler-rt/lib/scudo/standalone/primary32.h @@ -73,7 +73,7 @@ public: DCHECK(isAligned(reinterpret_cast(this), alignof(ThisT))); PossibleRegions.init(); u32 Seed; - const u64 Time = getMonotonicTime(); + const u64 Time = getMonotonicTimeFast(); if (!getRandom(reinterpret_cast(&Seed), sizeof(Seed))) Seed = static_cast( Time ^ (reinterpret_cast(SizeClassInfoArray) >> 6)); @@ -758,7 +758,7 @@ private: return 0; if (Sci->ReleaseInfo.LastReleaseAtNs + static_cast(IntervalMs) * 1000000 > - getMonotonicTime()) { + getMonotonicTimeFast()) { return 0; // Memory was returned recently. } } @@ -850,7 +850,7 @@ private: Sci->ReleaseInfo.LastReleasedBytes = Recorder.getReleasedBytes(); TotalReleasedBytes += Sci->ReleaseInfo.LastReleasedBytes; } - Sci->ReleaseInfo.LastReleaseAtNs = getMonotonicTime(); + Sci->ReleaseInfo.LastReleaseAtNs = getMonotonicTimeFast(); return TotalReleasedBytes; } diff --git a/compiler-rt/lib/scudo/standalone/primary64.h b/compiler-rt/lib/scudo/standalone/primary64.h index 7868111..cbd1136 100644 --- a/compiler-rt/lib/scudo/standalone/primary64.h +++ b/compiler-rt/lib/scudo/standalone/primary64.h @@ -71,7 +71,7 @@ public: nullptr, PrimarySize, "scudo:primary_reserve", MAP_NOACCESS, &Data)); u32 Seed; - const u64 Time = getMonotonicTime(); + const u64 Time = getMonotonicTimeFast(); if (!getRandom(reinterpret_cast(&Seed), sizeof(Seed))) Seed = static_cast(Time ^ (PrimaryBase >> 12)); const uptr PageSize = getPageSizeCached(); @@ -796,7 +796,7 @@ private: return 0; if (Region->ReleaseInfo.LastReleaseAtNs + static_cast(IntervalMs) * 1000000 > - getMonotonicTime()) { + getMonotonicTimeFast()) { return 0; // Memory was returned recently. } } @@ -995,7 +995,7 @@ private: Region->ReleaseInfo.RangesReleased += Recorder.getReleasedRangesCount(); Region->ReleaseInfo.LastReleasedBytes = Recorder.getReleasedBytes(); } - Region->ReleaseInfo.LastReleaseAtNs = getMonotonicTime(); + Region->ReleaseInfo.LastReleaseAtNs = getMonotonicTimeFast(); // Merge GroupToRelease back to the Region::FreeList. Note that both // `Region->FreeList` and `GroupToRelease` are sorted. diff --git a/compiler-rt/lib/scudo/standalone/trusty.cpp b/compiler-rt/lib/scudo/standalone/trusty.cpp index 592514d..c08a4e6 100644 --- a/compiler-rt/lib/scudo/standalone/trusty.cpp +++ b/compiler-rt/lib/scudo/standalone/trusty.cpp @@ -85,6 +85,17 @@ u64 getMonotonicTime() { static_cast(TS.tv_nsec); } +u64 getMonotonicTimeFast() { +#if defined(CLOCK_MONOTONIC_COARSE) + timespec TS; + clock_gettime(CLOCK_MONOTONIC_COARSE, &TS); + return static_cast(TS.tv_sec) * (1000ULL * 1000 * 1000) + + static_cast(TS.tv_nsec); +#else + return getMonotonicTime(); +#endif +} + u32 getNumberOfCPUs() { return 0; } u32 getThreadID() { return 0; }