Skip to content

Commit

Permalink
Cleaning up and improving Cgroup methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkae committed Sep 20, 2023
1 parent 1f300ba commit 79ea321
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions mir-ci/mir_ci/cgroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,16 @@ def get_cgroup_dir(pid: int) -> pathlib.Path:

@staticmethod
def _get_cgroup_dir_internal(pid: int) -> pathlib.Path:
path = None
cgroup_file = f"/proc/{pid}/cgroup"

with open(cgroup_file, "r") as group_file:
lines = group_file.readlines()
for line in lines:
for line in group_file.readlines():
if not line.startswith("0::"):
continue

path = pathlib.Path(f"/sys/fs/cgroup/{line[3:]}".strip())
return pathlib.Path(f"/sys/fs/cgroup/{line[3:]}".strip())

if not path:
raise RuntimeError(f"Unable to find path for process with pid: {pid}")
return path
raise RuntimeError(f"Unable to find path for process with pid: {pid}")

def _read_file(self, file_name: str) -> Iterator[str]:
if not self.path:
Expand All @@ -58,16 +54,16 @@ def get_cpu_time_microseconds(self) -> int:
if split_line[0] == "usage_usec":
return int(split_line[1])

return 0
raise RuntimeError(f"Unable to get the cpu time for cgroup with pid: {self.pid}")

def get_current_memory(self) -> int:
for line in self._read_file("memory.current"):
return int(line)

return 0
raise RuntimeError(f"Unable to get the current memory for cgroup with pid: {self.pid}")

def get_peak_memory(self) -> int:
for line in self._read_file("memory.peak"):
return int(line)

return 0
raise RuntimeError(f"Unable to get the peak memory for cgroup with pid: {self.pid}")

0 comments on commit 79ea321

Please sign in to comment.