src/base/util/memory_pressure/system_memory_pressure_evaluator_chromeos.cc
// CalculateReservedFreeKB() calculates the reserved free memory in KiB from
// /proc/zoneinfo.  Reserved pages are free pages reserved for emergent kernel
// allocation and are not available to the user space.  It's the sum of high
// watermarks and max protection pages of memory zones.  It implements the same
// reserved pages calculation in linux kernel calculate_totalreserve_pages().
//
// /proc/zoneinfo example:
// ...
// Node 0, zone    DMA32
//   pages free     422432
//         min      16270
//         low      20337
//         high     24404
//         ...
//         protection: (0, 0, 1953, 1953)
//
// The high field is the high watermark for this zone.  The protection field is
// the protected pages for lower zones.  See the lowmem_reserve_ratio section in
// https://www.kernel.org/doc/Documentation/sysctl/vm.txt.
CalculateReservedFreeKB() calculates the reserved free memory in KiB from
proc/zoneinfo.  Reserved pages are free pages reserved for emergent kernel
allocation and are not available to the user space.  It's the sum of high
watermarks and max protection pages of memory zones.  It implements the same
reserved pages calculation in linux kernel calculate_totalreserve_pages().



The high field is the high watermark for this zone.  The protection field is
the protected pages for lower zones.
CalculateReservedFreeKB () calculates the reserved free memory in KiB from proc / zoneinfo . Reserved pages are free pages reserved for emergent kernel allocation and are not available to the user space . It ' s the sum of high watermarks and max protection pages of memory zones . It implements the same reserved pages calculation in linux kernel calculate_totalreserve_pages () . The high field is the high watermark for this zone . The protection field is the protected pages for lower zones .
uint64_t SystemMemoryPressureEvaluator::CalculateReservedFreeKB(
    const std::string& zoneinfo) {
  constexpr uint64_t kPageSizeKB = 4;
  uint64_t num_reserved_pages = 0;
  for (const base::StringPiece& line : base::SplitStringPiece(
           zoneinfo, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY)) {
    std::vector<base::StringPiece> tokens = base::SplitStringPiece(
        line, base::kWhitespaceASCII, base::TRIM_WHITESPACE,
        base::SPLIT_WANT_NONEMPTY);
    if (tokens.size() < 2) {
      continue;
    }
    if (tokens[0] == "high") {
      uint64_t high = 0;
      if (base::StringToUint64(tokens[1], &high)) {
        num_reserved_pages += high;
      } else {
        LOG(ERROR) << "Couldn't parse the high field in /proc/zoneinfo: "
                   << tokens[1];
      }
    } else if (tokens[0] == "protection:") {
      uint64_t max = 0;
      for (size_t i = 1; i < tokens.size(); ++i) {
        uint64_t num = 0;
        base::StringPiece entry;
        if (i == 1) {
          entry = tokens[i].substr(1, tokens[i].size() - 2);
        } else {
          entry = tokens[i].substr(0, tokens[i].size() - 1);
        }
        if (base::StringToUint64(entry, &num)) {
          max = std::max(max, num);
        } else {
          LOG(ERROR)
              << "Couldn't parse the protection field in /proc/zoneinfo: "
              << entry;
        }
      }
      num_reserved_pages += max;
    }
  }
  return num_reserved_pages * kPageSizeKB;
}
uint64_t SystemMemoryPressureEvaluator :: CalculateReservedFreeKB ( const std :: string & zoneinfo ) { constexpr uint64_t kPageSizeKB = 4 ; uint64_t num_reserved_pages = 0 ; for ( const base :: StringPiece & line : base :: SplitStringPiece ( zoneinfo , " \n " , base :: KEEP_WHITESPACE , base :: SPLIT_WANT_NONEMPTY ) ) { std :: vector < base :: StringPiece > tokens = base :: SplitStringPiece ( line , base :: kWhitespaceASCII , base :: TRIM_WHITESPACE , base :: SPLIT_WANT_NONEMPTY ) ; if ( tokens . size ( ) < 2 ) { continue ; } if ( tokens [ 0 ] == " " ) { uint64_t high = 0 ; if ( base :: StringToUint64 ( tokens [ 1 ] , & high ) ) { num_reserved_pages += high ; } else { LOG ( ERROR ) << " " << tokens [ 1 ] ; } } else if ( tokens [ 0 ] == " " ) { uint64_t max = 0 ; for ( size_t i = 1 ; i < tokens . size ( ) ; ++ i ) { uint64_t num = 0 ; base :: StringPiece entry ; if ( i == 1 ) { entry = tokens [ i ] . substr ( 1 , tokens [ i ] . size ( ) - 2 ) ; } else { entry = tokens [ i ] . substr ( 0 , tokens [ i ] . size ( ) - 1 ) ; } if ( base :: StringToUint64 ( entry , & num ) ) { max = std :: max ( max , num ) ; } else { LOG ( ERROR ) << " " << entry ; } } num_reserved_pages += max ; } } return num_reserved_pages * kPageSizeKB ; }
CalculateReservedFreeKB() calculates the reserved free memory in KiB from
proc/zoneinfo.
CalculateReservedFreeKB () calculates the reserved free memory in KiB from proc / zoneinfo .
// Skip the line if there are not enough tokens. // Parse the high watermark. // Parse the protection pages. // Exclude the leading '(' and the trailing ','. // Exclude the trailing ',' or ')'.