Skip to content

Commit

Permalink
Added --all option and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeShirnia committed Apr 29, 2024
1 parent b8de459 commit 70b6fad
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
27 changes: 18 additions & 9 deletions oom_investigate.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ def generator():
if not self._system_ram:
ram = self.get_ram_from_logs(line)
if ram:
self._system_ram = ram
self._system_ram = round(ram)
# Start of a new OOM incident
if self.is_oom_start(line):
line = self.strip_brackets_pid(line)
Expand Down Expand Up @@ -715,11 +715,10 @@ def print_pretty_log_info(self):


def run(system, options):
show_counter, reverse, quick = (
options.show_counter,
options.reverse,
options.quick,
)
reverse, quick = options.reverse, options.quick

# Account for --all flag
show_counter = -1 if options.show_all else options.show_counter

# Parse the log file and extract OOM incidents
analyzer = OOMAnalyzer(system)
Expand Down Expand Up @@ -791,7 +790,7 @@ def run(system, options):
elif not reverse:
last_incident = oom_instance

if index < show_counter:
if show_counter == -1 or index < show_counter:
sliced_oom_instance_numbers.append(oom_instance["incident_number"])
oom_lines.extend(analyzer.print_pretty_oom_instance(oom_instance))
# Find the largest incident
Expand Down Expand Up @@ -870,7 +869,8 @@ def run(system, options):
lines.append(system._header(" OOM Incidents"))
lines.append(system.spacer)
lines.append("")
lines.append("Displaying {} OOM incidents:".format(show_counter))
show = "all" if show_counter == -1 else show_counter
lines.append("Displaying {} OOM incidents:".format(show))
lines.append("")

# Display OOM incidents based on the show_counter and reverse (if provided)
Expand All @@ -879,7 +879,7 @@ def run(system, options):
lines.append("")

# Only display this message if there are more oom incidents than the show_counter
if total_incidents > show_counter:
if total_incidents > show_counter and show_counter != -1:
lines.append("")
lines.append(
system._warning(
Expand Down Expand Up @@ -953,6 +953,15 @@ def main():
default=5,
help="Override the default number of OOM incidents to show",
)
parser.add_option(
"-a",
"--all",
dest="show_all",
default=False,
action="store_true",
help="Show all OOM incidents found in the log file. If both -a and -s are provided, "
"-a will take precedence.",
)
parser.add_option(
"-r",
"--reverse",
Expand Down
8 changes: 8 additions & 0 deletions tests/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class TestSystem:
"dmesg": False,
"quick": False,
"version": None,
"show_all": False
}
)

Expand All @@ -40,13 +41,20 @@ def test_file_processing(self, capsys):
# Test specific functionality when '-f messages' is passed
options = self.values
options.file = "tests/assets/logs/messages"
# While we're here, we might as well test the --all functionality so we don't have to pass
# the large log file too many times
options.show_all = True

with pytest.raises(SystemExit) as ex:
self.system.log_to_use = options.file
run(self.system, options)
assert ex.code == 0

out, _ = capsys.readouterr()
assert "Using Log File: \x1b[0m\x1b[1;32mtests/assets/logs/messages" in out
assert "OOM Incidents: \x1b[0m\x1b[1;31m19\x1b[0m\n" in out
assert "OOM Incident: \x1b[0m\x1b[0;96m19\x1b[0m" in out
assert "Displaying all OOM incidents:" in out

def test_default_system_log(self):
# Test default system log file
Expand Down

0 comments on commit 70b6fad

Please sign in to comment.