draws sth 2

This commit is contained in:
2026-04-23 11:29:39 +07:00
parent 5c27225465
commit a96cc69e05
3 changed files with 103 additions and 54 deletions
+66 -24
View File
@@ -1,27 +1,42 @@
import os
import re
import sys
from collections import defaultdict
# ================= BACKEND FIX (IMPORTANT FOR HYPRLAND) =================
import matplotlib
try:
# Wayland / Hyprland recommended backend
matplotlib.use("QtAgg")
except Exception:
# fallback for headless systems
matplotlib.use("Agg")
import matplotlib.pyplot as plt
# ================= INPUT =================
if len(sys.argv) < 2:
print("Usage: python analyze_log.py log.txt")
print("Usage: python analyze_log.py <logfile> [output_dir]")
sys.exit(1)
logfile = sys.argv[1]
out_dir = sys.argv[2] if len(sys.argv) >= 3 else "out"
# ================= PARSE LOG =================
# ================= PATH LOGIC =================
base_name = os.path.splitext(os.path.basename(logfile))[0]
# normalize folder structure
out_dir = os.path.normpath(out_dir)
pics_dir = os.path.join(out_dir, "pics")
tables_dir = os.path.join(out_dir, "tables")
os.makedirs(pics_dir, exist_ok=True)
os.makedirs(tables_dir, exist_ok=True)
output_png = os.path.join(pics_dir, f"{base_name}.png")
output_md = os.path.join(tables_dir, f"{base_name}.md")
# ================= PARSE =================
pattern = re.compile(r"(START|END).*TID=(\d+).*range=\[(\d+),(\d+)\].*time=([\d.]+)")
events = defaultdict(dict)
@@ -36,6 +51,7 @@ with open(logfile) as f:
key = (tid, int(l), int(r))
events[key][typ] = float(t)
# ================= BUILD ROWS =================
rows = []
@@ -61,40 +77,66 @@ if not rows:
print("No valid events found")
sys.exit(1)
# ================= OFFSET =================
t0 = rows[0]["start"]
for r in rows:
r["offset"] = r["start"] - t0
# ================= PRINT TABLE =================
print("\n## TABLE\n")
print("| TID | Range | Start | End | Duration | Offset |")
print("|-----|-------|-------|-----|----------|--------|")
for r in rows:
print(
f"| {r['tid']} | {r['range']} | "
f"{r['start']:.6f} | {r['end']:.6f} | "
f"{r['duration']:.6f} | {r['offset']:.6f} |"
)
# ================= COLOR MAP (LOGICAL) =================
unique_tids = sorted(set(r["tid"] for r in rows))
color_map = {
tid: plt.cm.tab20(i % 20) # stable, readable palette
for i, tid in enumerate(unique_tids)
}
# ================= SAVE MARKDOWN =================
with open(output_md, "w") as f:
f.write("# Execution Table\n\n")
f.write("| TID | Range | Start | End | Duration | Offset |\n")
f.write("|-----|-------|-------|-----|----------|--------|\n")
for r in rows:
f.write(
f"| {r['tid']} | {r['range']} | "
f"{r['start']:.6f} | {r['end']:.6f} | "
f"{r['duration']:.6f} | {r['offset']:.6f} |\n"
)
print(f"[OK] Table saved: {output_md}")
# ================= PLOT =================
plt.figure(figsize=(10, 6))
plt.figure(figsize=(12, 6))
for i, r in enumerate(rows):
plt.plot([r["offset"], r["offset"] + r["duration"]], [i, i], linewidth=4)
plt.plot(
[r["offset"], r["offset"] + r["duration"]],
[i, i],
color=color_map[r["tid"]],
linewidth=4,
)
# legend (TID → color)
for tid in unique_tids:
plt.plot([], [], color=color_map[tid], label=f"TID {tid}")
plt.legend(loc="upper right", fontsize=8)
plt.xlabel("Time (seconds from start)")
plt.ylabel("Tasks")
plt.title("Execution Timeline")
plt.title(f"Execution Timeline: {base_name}")
plt.grid(True)
plt.tight_layout()
# ================= SAVE (IMPORTANT FOR LAB) =================
plt.savefig("timeline.png", dpi=200)
print("\nSaved: timeline.png")
# ================= SAVE IMAGE =================
plt.savefig(output_png, dpi=200)
print(f"[OK] Plot saved: {output_png}")
# ================= SHOW (HYPRLAND WINDOW) =================
plt.show()
# ================= SHOW =================
# plt.show()