|
1 | 1 | import os |
2 | | - |
3 | | -from datetime import datetime |
| 2 | +import time |
4 | 3 |
|
5 | 4 | from flagscale.runner.utils import logger |
6 | 5 |
|
| 6 | +# Record the number of rows last diagnosed and analyzed by each host |
| 7 | +_diagnostic_offsets = {} |
| 8 | + |
7 | 9 | error_types = { |
8 | 10 | # Success indicators |
9 | 11 | "completed": "Completed: The task finished successfully with no errors.", |
|
23 | 25 | "error": "GeneralError: An error occurred during training.", |
24 | 26 | # Process errors |
25 | 27 | "killed": "ProcessKilled: Training process was killed.", |
| 28 | + "kill": "ProcessKilled: Process was manually killed.", |
| 29 | + "sigterm": "ProcessKilled: Process received SIGTERM signal.", |
| 30 | + "sigkill": "ProcessKilled: Process received SIGKILL signal.", |
| 31 | + "signal": "ProcessKilled: Process received termination signal.", |
| 32 | + "terminated": "ProcessKilled: Process was terminated.", |
| 33 | + "sigint": "ProcessKilled: Process received SIGINT signal (Ctrl+C).", |
26 | 34 | "segmentation fault": "SegmentationFault: Process crashed due to memory access error.", |
27 | 35 | "core dumped": "CoreDump: Process crashed and dumped core.", |
28 | 36 | # CUDA errors |
|
39 | 47 | } |
40 | 48 |
|
41 | 49 |
|
| 50 | +def find_error_lines(lines, error_key, start_line=0): |
| 51 | + """ |
| 52 | + Search for the error keyword in the log line and return a list of line numbers |
| 53 | + Args: |
| 54 | + lines (list): All lines of the log file |
| 55 | + error_key (str): errors keyword |
| 56 | + start_line (int): The line number where the search began |
| 57 | + Returns: |
| 58 | + list: A list of errors line numbers |
| 59 | + """ |
| 60 | + matches = [] |
| 61 | + for i in range(start_line, len(lines)): |
| 62 | + if error_key.lower() in lines[i].lower(): |
| 63 | + matches.append(i + 1) # Line numbers start from 1 |
| 64 | + return matches |
| 65 | + |
| 66 | + |
| 67 | +def format_line_range(line_numbers): |
| 68 | + """ |
| 69 | + Format the line number range display |
| 70 | + Args: |
| 71 | + line_numbers (list): List of line numbers |
| 72 | + Returns: |
| 73 | + str: Formatted line number range, such as "111-112" or "111" |
| 74 | + """ |
| 75 | + if not line_numbers: |
| 76 | + return "unknown" |
| 77 | + elif len(line_numbers) == 1: |
| 78 | + return str(line_numbers[0]) |
| 79 | + else: |
| 80 | + return f"{min(line_numbers)}-{max(line_numbers)}" |
| 81 | + |
| 82 | + |
42 | 83 | def generate_diagnostic_report(config, host, node_rank, log_file, return_content=False): |
43 | 84 | """ |
44 | | - Generate a diagnostic report from a log file. |
| 85 | + Generate an incremental diagnostic report from a log file. |
45 | 86 | Args: |
46 | 87 | config (DictConfig): Configuration object. |
47 | 88 | host (str): Hostname or IP. |
48 | 89 | node_rank (int): Node rank. |
49 | 90 | log_file (str): Path to the log file. |
50 | 91 | return_content (bool): If True, return report as string instead of writing to file. |
51 | 92 | Returns: |
52 | | - str: Diagnostic report content if return_content=True, else None. |
| 93 | + str: Diagnostic report content if return_content=True, else diagnostic file path. |
53 | 94 | """ |
54 | | - report_content = f"Diagnostic Report for {host} (node {node_rank})\n" |
55 | | - report_content += f"Generated at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n" |
56 | | - # report_content += f"Log file analyzed: {log_file}\n" |
57 | | - report_content += "Analysis:\n" |
| 95 | + global _diagnostic_offsets |
| 96 | + |
| 97 | + # Generate the path of the diagnostic file |
| 98 | + log_dir = os.path.dirname(log_file) |
| 99 | + diagnostic_file = os.path.join(log_dir, f"host_{node_rank}_{host}_diagnostic.txt") |
| 100 | + host_key = f"{host}_{node_rank}" |
58 | 101 |
|
59 | 102 | try: |
60 | 103 | if not os.path.exists(log_file) or os.path.getsize(log_file) == 0: |
61 | | - report_content += "- Log file is empty or does not exist, no analysis possible.\n" |
62 | | - return report_content if return_content else None |
63 | | - else: |
64 | | - with open(log_file, 'r') as f: |
65 | | - log_content = f.read() |
66 | | - if not log_content.strip(): |
67 | | - report_content += "- Log file is empty, no analysis possible.\n" |
68 | | - else: |
69 | | - matched_errors = [] |
70 | | - for key, desc in error_types.items(): |
71 | | - if key in log_content.lower(): |
72 | | - matched_errors.append(desc) |
73 | | - |
74 | | - if matched_errors: |
75 | | - for err in matched_errors: |
76 | | - report_content += f"- {err}\n" |
77 | | - else: |
78 | | - report_content += "- No errors or unknown error detected in logs.\n" |
79 | | - except Exception as e: |
80 | | - logger.error(f"Failed to read log file {log_file} for {host} (node {node_rank}): {e}") |
81 | | - report_content += f"- Error reading log file: {e}\n" |
| 104 | + logger.debug(f"Log file {log_file} is empty or does not exist") |
| 105 | + return diagnostic_file if not return_content else "" |
| 106 | + # Read all lines of the log file |
| 107 | + with open(log_file, 'r', encoding='utf-8', errors='ignore') as f: |
| 108 | + all_lines = f.readlines() |
82 | 109 |
|
83 | | - if return_content: |
84 | | - return report_content |
85 | | - else: |
86 | | - try: |
87 | | - diagnostic_file = log_file.replace("temp", "diagnostic").replace(".log", ".txt") |
| 110 | + # Get the number of rows analyzed last time |
| 111 | + last_analyzed_line = _diagnostic_offsets.get(host_key, 0) |
| 112 | + |
| 113 | + # If it is the first analysis, create the header of the diagnostic file |
| 114 | + if last_analyzed_line == 0 and not os.path.exists(diagnostic_file): |
| 115 | + os.makedirs(os.path.dirname(diagnostic_file), exist_ok=True) |
| 116 | + header_content = f"Diagnostic Report for {host} (node {node_rank})\n" |
| 117 | + header_content += ( |
| 118 | + f"Generated at {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())}\n" |
| 119 | + ) |
| 120 | + header_content += "Analysis:\n" |
| 121 | + |
| 122 | + with open(diagnostic_file, 'w', encoding='utf-8') as f: |
| 123 | + f.write(header_content) |
| 124 | + |
| 125 | + # Only analyze the newly added rows |
| 126 | + new_lines = all_lines[last_analyzed_line:] |
| 127 | + if not new_lines: |
| 128 | + logger.debug(f"No new lines to analyze in {log_file}") |
| 129 | + return diagnostic_file if not return_content else "" |
| 130 | + |
| 131 | + # Analyze the errors in the new line |
| 132 | + current_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) |
| 133 | + new_errors = [] |
| 134 | + for key, desc in error_types.items(): |
| 135 | + # Look for errors in the newly added lines |
| 136 | + error_lines = find_error_lines(all_lines, key, last_analyzed_line) |
| 137 | + if error_lines: |
| 138 | + line_range = format_line_range(error_lines) |
| 139 | + log_filename = os.path.basename(log_file) |
| 140 | + error_entry = f"[{current_time}] {desc} Check {log_filename} line:{line_range}" |
| 141 | + new_errors.append(error_entry) |
| 142 | + |
| 143 | + # Update Analysis Location |
| 144 | + _diagnostic_offsets[host_key] = len(all_lines) |
| 145 | + |
| 146 | + # If there are new errors, append them to the diagnostic file |
| 147 | + if new_errors: |
88 | 148 | os.makedirs(os.path.dirname(diagnostic_file), exist_ok=True) |
89 | | - with open(diagnostic_file, 'w') as f: |
90 | | - f.write(report_content) |
| 149 | + with open(diagnostic_file, 'a', encoding='utf-8') as f: |
| 150 | + for error in new_errors: |
| 151 | + f.write(f"{error}\n") |
| 152 | + |
91 | 153 | logger.debug( |
92 | | - f"Generated diagnostic report for {host} (node {node_rank}) at {diagnostic_file}" |
| 154 | + f"Added {len(new_errors)} new errors to diagnostic report for {host} (node {node_rank})" |
93 | 155 | ) |
| 156 | + |
| 157 | + if return_content: |
| 158 | + return '\n'.join(new_errors) if new_errors else "" |
| 159 | + else: |
94 | 160 | return diagnostic_file |
95 | | - except Exception as e: |
96 | | - logger.error(f"Failed to write diagnostic report for {host} (node {node_rank}): {e}") |
| 161 | + except Exception as e: |
| 162 | + logger.error(f"Failed to generate diagnostic report for {host} (node {node_rank}): {e}") |
| 163 | + if return_content: |
| 164 | + return f"Error analyzing log file: {e}" |
| 165 | + else: |
97 | 166 | return None |
0 commit comments