Skip to content

Commit de18d27

Browse files
committed
fstests_watchdog: optimize SSH usage to avoid unnecessary calls
Only use SSH to get boot time when we actually need to convert kernel timestamps (lines with [timestamp] format). This avoids unnecessary SSH calls when: 1. Linux version is found and we're processing from that point 2. The log lines don't have kernel timestamps that need conversion This significantly improves performance by avoiding SSH roundtrips when they're not needed, while still maintaining the functionality for timestamp conversion when required. The SSH fallback for kernel version detection (in fstests_watchdog.py) remains unchanged and only triggers when the journal doesn't have the Linux version line. Generated-by: Claude AI Signed-off-by: Luis Chamberlain <[email protected]>
1 parent 1c73c3a commit de18d27

File tree

1 file changed

+55
-39
lines changed

1 file changed

+55
-39
lines changed

scripts/workflows/lib/crash.py

Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -553,61 +553,77 @@ def sort_key(path):
553553
else:
554554
start_index = last_linux_version_line
555555

556-
# Try to get boot time from the target host if reachable
557-
boot_time = None
558-
try:
559-
# First try to get boot time from target host via SSH
560-
result = subprocess.run(
561-
[
562-
"ssh",
563-
"-o",
564-
"ConnectTimeout=5",
565-
"-o",
566-
"StrictHostKeyChecking=no",
567-
self.host_name,
568-
"awk '/^btime/ {print $2}' /proc/stat",
569-
],
570-
capture_output=True,
571-
text=True,
572-
timeout=10,
573-
)
574-
if result.returncode == 0 and result.stdout.strip():
575-
btime = int(result.stdout.strip())
576-
boot_time = datetime.fromtimestamp(btime)
577-
logger.debug(
578-
f"Got boot time from target host {self.host_name}: {boot_time}"
579-
)
580-
except (subprocess.TimeoutExpired, subprocess.SubprocessError, ValueError) as e:
581-
logger.debug(f"Failed to get boot time from target host: {e}")
556+
# Check if we need to convert timestamps (lines with [timestamp] format)
557+
needs_timestamp_conversion = False
558+
for line in decoded_lines[
559+
start_index : start_index + 10
560+
]: # Check first 10 lines
561+
if re.match(r"\[\s*\d+\.\d+\]", line):
562+
needs_timestamp_conversion = True
563+
break
582564

583-
# Fallback to localhost boot time (not ideal but better than nothing)
584-
if boot_time is None:
565+
boot_time = None
566+
if needs_timestamp_conversion:
567+
# Only try to get boot time if we need to convert timestamps
585568
try:
586-
btime_output = subprocess.run(
587-
["awk", "/^btime/ {print $2}", "/proc/stat"],
569+
# First try to get boot time from target host via SSH
570+
result = subprocess.run(
571+
[
572+
"ssh",
573+
"-o",
574+
"ConnectTimeout=5",
575+
"-o",
576+
"StrictHostKeyChecking=no",
577+
self.host_name,
578+
"awk '/^btime/ {print $2}' /proc/stat",
579+
],
588580
capture_output=True,
589581
text=True,
590-
check=True,
582+
timeout=10,
591583
)
592-
btime = int(btime_output.stdout.strip())
593-
boot_time = datetime.fromtimestamp(btime)
594-
logger.debug(f"Using localhost boot time as fallback: {boot_time}")
595-
except Exception as e:
596-
logger.warning(f"Failed to get boot time: {e}")
597-
# Just return the lines without timestamp conversion
598-
return "\n".join(decoded_lines[start_index:])
584+
if result.returncode == 0 and result.stdout.strip():
585+
btime = int(result.stdout.strip())
586+
boot_time = datetime.fromtimestamp(btime)
587+
logger.debug(
588+
f"Got boot time from target host {self.host_name}: {boot_time}"
589+
)
590+
except (
591+
subprocess.TimeoutExpired,
592+
subprocess.SubprocessError,
593+
ValueError,
594+
) as e:
595+
logger.debug(f"Failed to get boot time from target host: {e}")
596+
597+
# Fallback to localhost boot time (not ideal but better than nothing)
598+
if boot_time is None:
599+
try:
600+
btime_output = subprocess.run(
601+
["awk", "/^btime/ {print $2}", "/proc/stat"],
602+
capture_output=True,
603+
text=True,
604+
check=True,
605+
)
606+
btime = int(btime_output.stdout.strip())
607+
boot_time = datetime.fromtimestamp(btime)
608+
logger.debug(f"Using localhost boot time as fallback: {boot_time}")
609+
except Exception as e:
610+
logger.warning(f"Failed to get boot time: {e}")
611+
# Just return the lines without timestamp conversion
612+
return "\n".join(decoded_lines[start_index:])
599613

600614
# Convert logs from last boot only
601615
converted_lines = []
602616
for line in decoded_lines[start_index:]:
603617
match = re.match(r"\[\s*(\d+\.\d+)\] (.*)", line)
604-
if match:
618+
if match and boot_time:
619+
# Only convert timestamp if we have a boot_time
605620
seconds = float(match.group(1))
606621
wall_time = boot_time + timedelta(seconds=seconds)
607622
timestamp = wall_time.strftime("%b %d %H:%M:%S")
608623
converted_lines.append(f"{timestamp} {self.host_name} {match.group(2)}")
609624
else:
610625
# Keep lines that don't match the kernel timestamp format as-is
626+
# or if we don't have boot_time for conversion
611627
# This helps preserve any Linux version lines that might be there
612628
converted_lines.append(line)
613629

0 commit comments

Comments
 (0)