void Oomd::updateContext()

in src/oomd/Oomd.cpp [60:110]


void Oomd::updateContext() {
  // Update information about swapfree
  SystemContext system_ctx;
  auto swaps = Fs::readFileByLine("/proc/swaps");

  // TODO(dschatzberg): Handle error here
  if (swaps) {
    // For each swap, tally up used and total
    for (size_t i = 1; i < swaps->size(); ++i) {
      auto parts = Util::split((*swaps)[i], '\t');
      // The /proc/swaps format is pretty bad. The first field is padded by
      // spaces but the rest of the fields are padded by '\t'. Since we don't
      // really care about the first field, we'll just split by '\t'.
      OCHECK_EXCEPT(
          parts.size() == 4, std::runtime_error("/proc/swaps malformed"));
      system_ctx.swaptotal += std::stoll(parts[1]) * 1024; // Values are in KB
      system_ctx.swapused += std::stoll(parts[2]) * 1024; // Values are in KB
    }
  }

  auto swappiness = Fs::getSwappiness();
  if (swappiness) {
    system_ctx.swappiness = *swappiness;
  }

  if (auto vmstat_opt = Fs::getVmstat()) {
    system_ctx.vmstat = std::move(*vmstat_opt);

    // Factor for calculating moving average
    const static double factor60 = std::exp(-interval_.count() / 60.0);
    const static double factor300 = std::exp(-interval_.count() / 300.0);

    auto& prev_system_ctx = ctx_.getSystemContext();
    if (prev_system_ctx.vmstat.size() > 0) {
      auto swapout_bps = (system_ctx.vmstat.at("pswpout") -
                          prev_system_ctx.vmstat.at("pswpout")) *
          4096.0 / interval_.count();
      system_ctx.swapout_bps_60 = swapout_bps +
          factor60 * (prev_system_ctx.swapout_bps_60 - swapout_bps);
      system_ctx.swapout_bps_300 = swapout_bps +
          factor300 * (prev_system_ctx.swapout_bps_300 - swapout_bps);
    }
  }

  ctx_.setSystemContext(system_ctx);
  ctx_.setPrekillHooksHandler([&](const CgroupContext& cgroup_ctx) {
    return engine_->firePrekillHook(cgroup_ctx, ctx_);
  });
  ctx_.refresh();
  ctx_.bumpCurrentTick();
}