bool BaseKillPlugin::tryToKillCgroup()

in src/oomd/plugins/BaseKillPlugin.cpp [446:489]


bool BaseKillPlugin::tryToKillCgroup(
    const CgroupContext& target,
    const KillUuid& kill_uuid,
    bool dry) {
  using namespace std::chrono_literals;

  const std::string& cgroup_path = target.cgroup().absolutePath();

  int last_nr_killed = 0;
  int nr_killed = 0;
  int tries = 10;

  if (dry) {
    OLOG << "OOMD: In dry-run mode; would have tried to kill " << cgroup_path;
    return true;
  }

  OLOG << "Trying to kill " << cgroup_path;

  reportKillUuidToXattr(cgroup_path, kill_uuid);
  reportKillInitiationToXattr(cgroup_path);
  while (tries--) {
    // Descendent cgroups created during killing will be missed because
    // getAndTryToKillPids reads cgroup children from OomdContext's cache

    nr_killed += getAndTryToKillPids(target);

    if (nr_killed == last_nr_killed) {
      break;
    }

    // Give it a breather before killing again
    //
    // Don't sleep after the first round of kills b/c the majority of the
    // time the sleep isn't necessary. The system responds fast enough.
    if (last_nr_killed) {
      std::this_thread::sleep_for(1s);
    }

    last_nr_killed = nr_killed;
  }
  reportKillCompletionToXattr(cgroup_path, nr_killed);
  return nr_killed > 0;
}