rss_limit_checker.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  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. #include "rss_limit_checker.h"
  9. #include "atomic_helpers.h"
  10. #include "string_utils.h"
  11. namespace scudo {
  12. void RssLimitChecker::check(u64 NextCheck) {
  13. // The interval for the checks is 250ms.
  14. static constexpr u64 CheckInterval = 250 * 1000000;
  15. // Early return in case another thread already did the calculation.
  16. if (!atomic_compare_exchange_strong(&RssNextCheckAtNS, &NextCheck,
  17. getMonotonicTime() + CheckInterval,
  18. memory_order_relaxed)) {
  19. return;
  20. }
  21. const uptr CurrentRssMb = GetRSS() >> 20;
  22. RssLimitExceeded Result = RssLimitExceeded::Neither;
  23. if (UNLIKELY(HardRssLimitMb && HardRssLimitMb < CurrentRssMb))
  24. Result = RssLimitExceeded::Hard;
  25. else if (UNLIKELY(SoftRssLimitMb && SoftRssLimitMb < CurrentRssMb))
  26. Result = RssLimitExceeded::Soft;
  27. atomic_store_relaxed(&RssLimitStatus, static_cast<u8>(Result));
  28. }
  29. } // namespace scudo