1 //===-- common.cpp ----------------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "rss_limit_checker.h" 10 #include "atomic_helpers.h" 11 #include "string_utils.h" 12 13 namespace scudo { 14 check(u64 NextCheck)15void RssLimitChecker::check(u64 NextCheck) { 16 // The interval for the checks is 250ms. 17 static constexpr u64 CheckInterval = 250 * 1000000; 18 19 // Early return in case another thread already did the calculation. 20 if (!atomic_compare_exchange_strong(&RssNextCheckAtNS, &NextCheck, 21 getMonotonicTime() + CheckInterval, 22 memory_order_relaxed)) { 23 return; 24 } 25 26 const uptr CurrentRssMb = GetRSS() >> 20; 27 28 RssLimitExceeded Result = RssLimitExceeded::Neither; 29 if (UNLIKELY(HardRssLimitMb && HardRssLimitMb < CurrentRssMb)) 30 Result = RssLimitExceeded::Hard; 31 else if (UNLIKELY(SoftRssLimitMb && SoftRssLimitMb < CurrentRssMb)) 32 Result = RssLimitExceeded::Soft; 33 34 atomic_store_relaxed(&RssLimitStatus, static_cast<u8>(Result)); 35 } 36 37 } // namespace scudo 38