Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Re)add etime and cmd into process stats #175

Merged
merged 5 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/con_duct/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import logging
import math
import os
import re
import shutil
import socket
import subprocess
Expand Down Expand Up @@ -126,14 +127,29 @@
rss: int # Memory Resident Set Size in Bytes
vsz: int # Virtual Memory size in Bytes
timestamp: str
etime: str
cmd: str

def max(self, other: ProcessStats) -> ProcessStats:
cmd = self.cmd
if self.cmd != other.cmd:
lgr.debug(
f"cmd has changed. Previous measurement was {self.cmd}, now {other.cmd}."
)
# Brackets indicate that the kernel has substituted an abbreviation.
surrounded_by_brackets = r"\[.*?\]"
if re.search(surrounded_by_brackets, self.cmd):
lgr.debug(f"using {other.cmd}.")
cmd = other.cmd

Check warning on line 143 in src/con_duct/__main__.py

View check run for this annotation

Codecov / codecov/patch

src/con_duct/__main__.py#L142-L143

Added lines #L142 - L143 were not covered by tests
lgr.debug(f"using {self.cmd}.")
return ProcessStats(
pcpu=max(self.pcpu, other.pcpu),
pmem=max(self.pmem, other.pmem),
rss=max(self.rss, other.rss),
vsz=max(self.vsz, other.vsz),
timestamp=max(self.timestamp, other.timestamp),
etime=other.etime, # For the aggregate always take the latest
cmd=cmd,
)

def __post_init__(self) -> None:
Expand Down Expand Up @@ -403,14 +419,14 @@
"-s",
str(self.session_id),
"-o",
"pid,pcpu,pmem,rss,vsz,etime,cmd",
"pid,pcpu,pmem,rss,vsz,etime,stat,cmd",
],
text=True,
)
for line in output.splitlines()[1:]:
if line:
pid, pcpu, pmem, rss_kib, vsz_kib, etime, cmd = line.split(
maxsplit=6
maxsplit=6,
)
sample.add_pid(
int(pid),
Expand All @@ -420,6 +436,8 @@
rss=int(rss_kib) * 1024,
vsz=int(vsz_kib) * 1024,
timestamp=datetime.now().astimezone().isoformat(),
etime=etime,
cmd=cmd,
),
)
except subprocess.CalledProcessError as exc: # when session_id has no processes
Expand Down
54 changes: 37 additions & 17 deletions test/test_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,23 @@
)

stat0 = ProcessStats(
pcpu=0.0, pmem=0, rss=0, vsz=0, timestamp="2024-06-11T10:09:37-04:00"
pcpu=0.0,
pmem=0,
rss=0,
vsz=0,
timestamp="2024-06-11T10:09:37-04:00",
etime="00:00",
cmd="cmd 1",
)

stat1 = ProcessStats(
pcpu=1.0, pmem=0, rss=0, vsz=0, timestamp="2024-06-11T10:13:23-04:00"
pcpu=1.0,
pmem=0,
rss=0,
vsz=0,
timestamp="2024-06-11T10:13:23-04:00",
etime="00:02",
cmd="cmd 1",
)


Expand Down Expand Up @@ -102,45 +114,53 @@ def test_averages_three_samples() -> None:


@pytest.mark.parametrize(
"pcpu, pmem, rss, vsz",
"pcpu, pmem, rss, vsz, etime, cmd",
[
(1.0, 1.1, 1024, 1025),
(0.5, 0.7, 20.48, 40.96),
(1, 2, 3, 4),
(0, 0.0, 0, 0.0),
(2.5, 3.5, 8192, 16384),
(100.0, 99.9, 65536, 131072),
(1.0, 1.1, 1024, 1025, "00:00", "cmd"),
(0.5, 0.7, 20.48, 40.96, "00:01", "any"),
(1, 2, 3, 4, "100:1000", "string"),
(0, 0.0, 0, 0.0, "999:999:999", "can have spaces"),
(2.5, 3.5, 8192, 16384, "any", "for --this --kind of thing"),
(100.0, 99.9, 65536, 131072, "string", "cmd"),
],
)
def test_process_stats_green(pcpu: float, pmem: float, rss: int, vsz: int) -> None:
def test_process_stats_green(
pcpu: float, pmem: float, rss: int, vsz: int, etime: str, cmd: str
) -> None:
# Assert does not raise
ProcessStats(
pcpu=pcpu,
pmem=pmem,
rss=rss,
vsz=vsz,
timestamp=datetime.now().astimezone().isoformat(),
etime=etime,
cmd=cmd,
)


@pytest.mark.parametrize(
"pcpu, pmem, rss, vsz",
"pcpu, pmem, rss, vsz, etime, cmd",
[
("only", 1.1, 1024, 1025),
(0.5, "takes", 20.48, 40.96),
(1, 2, "one", 4),
(1, 2, 3, "value"),
("2", "fail", "or", "more"),
("only", 1.1, 1024, 1025, "etime", "cmd"),
(0.5, "takes", 20.48, 40.96, "some", "str"),
(1, 2, "one", 4, "anything", "accepted"),
(1, 2, 3, "value", "etime", "cmd"),
("2", "fail", "or", "more", "etime", "cmd"),
],
)
def test_process_stats_red(pcpu: float, pmem: float, rss: int, vsz: int) -> None:
def test_process_stats_red(
pcpu: float, pmem: float, rss: int, vsz: int, etime: str, cmd: str
) -> None:
with pytest.raises(AssertionError):
ProcessStats(
pcpu=pcpu,
pmem=pmem,
rss=rss,
vsz=vsz,
timestamp=datetime.now().astimezone().isoformat(),
etime=etime,
cmd=cmd,
)


Expand Down
Loading