int BaseKillPlugin::getAndTryToKillPids()

in src/oomd/plugins/BaseKillPlugin.cpp [403:444]


int BaseKillPlugin::getAndTryToKillPids(const CgroupContext& target) {
  static constexpr size_t stream_size = 20;
  int nr_killed = 0;

  const auto fd = ::openat(target.fd().fd(), Fs::kProcsFile, O_RDONLY);
  if (fd == -1) {
    return 0;
  }
  auto fp = ::fdopen(fd, "r");
  if (fp == nullptr) {
    ::close(fd);
    return 0;
  }
  std::vector<int> pids;
  char* line = nullptr;
  size_t len = 0;
  ssize_t read;
  errno = 0;
  while ((read = ::getline(&line, &len, fp)) != -1) {
    OCHECK(line != nullptr);
    pids.push_back(std::stoi(line));
    if (pids.size() == stream_size) {
      nr_killed += tryToKillPids(pids);
      pids.clear();
    }
  }
  nr_killed += tryToKillPids(pids);
  ::free(line);
  ::fclose(fp);

  // target.children is cached, and may be stale
  if (const auto& children = target.children()) {
    for (const auto& child_name : *children) {
      if (auto child_ctx =
              target.oomd_ctx().addChildToCacheAndGet(target, child_name)) {
        nr_killed += getAndTryToKillPids(*child_ctx);
      }
    }
  }

  return nr_killed;
}