int KillSwapUsage::init()

in src/oomd/plugins/KillSwapUsage-inl.h [33:71]


int KillSwapUsage<Base>::init(
    const Engine::PluginArgs& args,
    const PluginConstructionContext& context) {
  auto meminfo = args.find("meminfo_location") != args.end()
      ? Fs::getMeminfo(args.at("meminfo_location"))
      : Fs::getMeminfo();

  // TODO(dschatzberg): Report Error
  auto swapTotal = 0;
  if (meminfo && meminfo->count("SwapTotal")) {
    swapTotal = (*meminfo)["SwapTotal"];
  }

  auto memTotal = 0;
  if (meminfo && meminfo->count("MemTotal")) {
    memTotal = (*meminfo)["MemTotal"];
  }

  if (meminfo && meminfo->count("MemTotal") && memTotal > 0) {
    swapRatio_ = float(swapTotal) / memTotal;
  }

  // erase meminfo_location since we already loaded it
  auto argsCopy = args;
  argsCopy.erase("meminfo_location");

  this->argParser_.addArgumentCustom(
      "threshold", threshold_, [swapTotal](const std::string& str) {
        int64_t res = 0;
        if (Util::parseSizeOrPercent(str, &res, swapTotal) != 0) {
          throw std::invalid_argument("Failed to parse threshold: " + str);
        }
        return res;
      });

  this->argParser_.addArgument("biased_swap_kill", biasedSwapKill_);

  return Base::init(argsCopy, context);
}