SystemMaybe Senpai::getReclaimableBytes()

in src/oomd/plugins/Senpai.cpp [350:384]


SystemMaybe<int64_t> Senpai::getReclaimableBytes(
    const CgroupContext& cgroup_ctx) {
  const auto& stat_opt = cgroup_ctx.memory_stat();
  if (!stat_opt) {
    return SYSTEM_ERROR(ENOENT);
  }

  auto active_file_pos = stat_opt->find("active_file");
  auto inactive_file_pos = stat_opt->find("inactive_file");
  if (active_file_pos == stat_opt->end() ||
      inactive_file_pos == stat_opt->end()) {
    throw std::runtime_error("Invalid memory.stat cgroup file");
  }
  auto file_cache = active_file_pos->second + inactive_file_pos->second;

  int64_t swappable = 0;
  const auto& system_ctx = cgroup_ctx.oomd_ctx().getSystemContext();
  if (system_ctx.swaptotal > 0 && system_ctx.swappiness > 0) {
    auto effective_swap_free_opt = cgroup_ctx.effective_swap_free();
    if (!effective_swap_free_opt) {
      return SYSTEM_ERROR(ENOENT);
    } else if (*effective_swap_free_opt > 0) {
      auto active_anon_pos = stat_opt->find("active_anon");
      auto inactive_anon_pos = stat_opt->find("inactive_anon");
      if (active_anon_pos == stat_opt->end() ||
          inactive_anon_pos == stat_opt->end()) {
        return SYSTEM_ERROR(EINVAL);
      }
      auto anon_size = active_anon_pos->second + inactive_anon_pos->second;
      swappable = std::min(*effective_swap_free_opt, anon_size);
    }
  }

  return file_cache + swappable;
}