nicer graph in 1

This commit is contained in:
2026-04-25 06:59:06 +07:00
parent dfe87e9b31
commit df31bae969
8 changed files with 113 additions and 78 deletions
+37 -11
View File
@@ -1,17 +1,22 @@
import os
import re
import sys
import matplotlib.pyplot as plt
OUT_DIR = "out"
os.makedirs(OUT_DIR, exist_ok=True)
LOG_RE = re.compile(
r"(PROC_START|PROC_END)\s+pid=(\d+)\s+ppid=(\d+)\s+depth=(\d+)\s+l=(\d+)\s+r=(\d+)\s+ts=([\d.]+)"
)
class Proc:
def __init__(self, pid, ppid):
def __init__(self, pid, ppid, depth):
self.pid = pid
self.ppid = ppid
self.depth = depth
self.start = None
self.end = None
self.children = []
@@ -23,18 +28,23 @@ class Proc:
def parse_log(path):
procs = {}
for line in open(path):
m = LOG_RE.search(line)
if not m:
continue
typ, pid, ppid, depth, l, r, ts = m.groups()
pid, ppid, ts = int(pid), int(ppid), float(ts)
pid, ppid, depth, ts = int(pid), int(ppid), int(depth), float(ts)
if pid not in procs:
procs[pid] = Proc(pid, ppid)
procs[pid] = Proc(pid, ppid, depth)
if typ == "PROC_START":
procs[pid].start = ts
else:
procs[pid].end = ts
return procs
@@ -61,37 +71,53 @@ def dfs_order(root):
def write_md(procs, ordered):
out_path = os.path.join(OUT_DIR, "processes.md")
first = min(p.start for p in procs.values())
with open("processes.md", "w") as f:
f.write("| PID | PPID | Start | End | Offset | Duration |\n")
f.write("|---|---|---|---|---|---|\n")
with open(out_path, "w") as f:
f.write("| PID | PPID | Depth | Start | End | Offset | Duration |\n")
f.write("|---|---|---|---|---|---|---|\n")
for p in ordered:
f.write(
f"| {p.pid} | {p.ppid} | {p.start:.6f} | {p.end:.6f} | {p.start - first:.6f} | {p.duration:.6f} |\n"
f"| {p.pid} | {p.ppid} | {p.depth} | "
f"{p.start:.6f} | {p.end:.6f} | "
f"{p.start - first:.6f} | {p.duration:.6f} |\n"
)
def draw_gantt(ordered):
out_path = os.path.join(OUT_DIR, "gantt.png")
first = min(p.start for p in ordered)
fig, ax = plt.subplots(figsize=(12, 6))
for i, p in enumerate(ordered):
ax.barh(i, p.duration, left=p.start - first, height=0.6)
ax.text(p.start - first, i, f" PID {p.pid}", va="center")
ax.text(p.start - first, i, f"PID {p.pid} ({p.depth})", va="center")
ax.set_yticks(range(len(ordered)))
ax.set_yticklabels([p.pid for p in ordered])
ax.set_yticklabels([f"{p.pid} ({p.depth})" for p in ordered])
ax.set_xlabel("Seconds from first process start")
ax.set_title("Process Gantt")
ax.set_title("Process Gantt (Tree + Depth)")
ax.invert_yaxis()
plt.tight_layout()
plt.savefig("gantt.png")
plt.savefig(out_path)
def main():
procs = parse_log(sys.argv[1])
root = build_tree(procs)
ordered = dfs_order(root)
write_md(procs, ordered)
draw_gantt(ordered)
print(f"Outputs saved to: {OUT_DIR}/")
if __name__ == "__main__":
main()
BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 43 KiB

After

Width:  |  Height:  |  Size: 52 KiB

BIN
View File
Binary file not shown.
+30 -30
View File
@@ -1,30 +1,30 @@
PROC_START pid=41115 ppid=41113 depth=0 l=0 r=19999 ts=1777074730.331155
PROC_START pid=41116 ppid=41115 depth=1 l=0 r=9999 ts=1777074730.332433
PROC_START pid=41117 ppid=41115 depth=1 l=10000 r=19999 ts=1777074730.332633
PROC_START pid=41118 ppid=41116 depth=2 l=0 r=4999 ts=1777074730.333202
PROC_START pid=41119 ppid=41117 depth=2 l=10000 r=14999 ts=1777074730.333253
PROC_START pid=41120 ppid=41116 depth=2 l=5000 r=9999 ts=1777074730.333499
PROC_START pid=41121 ppid=41117 depth=2 l=15000 r=19999 ts=1777074730.333522
PROC_START pid=41122 ppid=41118 depth=3 l=0 r=2499 ts=1777074730.333905
PROC_START pid=41123 ppid=41119 depth=3 l=10000 r=12499 ts=1777074730.334001
PROC_START pid=41124 ppid=41121 depth=3 l=15000 r=17499 ts=1777074730.334220
PROC_START pid=41125 ppid=41120 depth=3 l=5000 r=7499 ts=1777074730.334244
PROC_START pid=41127 ppid=41119 depth=3 l=12500 r=14999 ts=1777074730.334636
PROC_START pid=41126 ppid=41118 depth=3 l=2500 r=4999 ts=1777074730.334760
PROC_START pid=41128 ppid=41121 depth=3 l=17500 r=19999 ts=1777074730.334799
PROC_START pid=41129 ppid=41120 depth=3 l=7500 r=9999 ts=1777074730.334870
PROC_END pid=41123 ppid=41119 depth=3 l=10000 r=12499 ts=1777074730.336351
PROC_END pid=41127 ppid=41119 depth=3 l=12500 r=14999 ts=1777074730.336705
PROC_END pid=41124 ppid=41121 depth=3 l=15000 r=17499 ts=1777074730.336857
PROC_END pid=41129 ppid=41120 depth=3 l=7500 r=9999 ts=1777074730.336864
PROC_END pid=41125 ppid=41120 depth=3 l=5000 r=7499 ts=1777074730.337630
PROC_END pid=41119 ppid=41117 depth=2 l=10000 r=14999 ts=1777074730.337559
PROC_END pid=41128 ppid=41121 depth=3 l=17500 r=19999 ts=1777074730.337974
PROC_END pid=41121 ppid=41117 depth=2 l=15000 r=19999 ts=1777074730.338710
PROC_END pid=41120 ppid=41116 depth=2 l=5000 r=9999 ts=1777074730.338724
PROC_END pid=41117 ppid=41115 depth=1 l=10000 r=19999 ts=1777074730.339777
PROC_END pid=41122 ppid=41118 depth=3 l=0 r=2499 ts=1777074730.340717
PROC_END pid=41126 ppid=41118 depth=3 l=2500 r=4999 ts=1777074730.340948
PROC_END pid=41118 ppid=41116 depth=2 l=0 r=4999 ts=1777074730.341717
PROC_END pid=41116 ppid=41115 depth=1 l=0 r=9999 ts=1777074730.342666
PROC_END pid=41115 ppid=41113 depth=0 l=0 r=19999 ts=1777074730.343971
PROC_START pid=44626 ppid=44625 depth=0 l=0 r=19999 ts=1777075065.157101
PROC_START pid=44627 ppid=44626 depth=1 l=0 r=9999 ts=1777075065.157322
PROC_START pid=44628 ppid=44626 depth=1 l=10000 r=19999 ts=1777075065.157416
PROC_START pid=44629 ppid=44627 depth=2 l=0 r=4999 ts=1777075065.157527
PROC_START pid=44630 ppid=44627 depth=2 l=5000 r=9999 ts=1777075065.157613
PROC_START pid=44631 ppid=44628 depth=2 l=10000 r=14999 ts=1777075065.157635
PROC_START pid=44633 ppid=44629 depth=3 l=0 r=2499 ts=1777075065.157754
PROC_START pid=44632 ppid=44628 depth=2 l=15000 r=19999 ts=1777075065.157782
PROC_START pid=44634 ppid=44630 depth=3 l=5000 r=7499 ts=1777075065.157849
PROC_START pid=44636 ppid=44629 depth=3 l=2500 r=4999 ts=1777075065.157935
PROC_START pid=44637 ppid=44630 depth=3 l=7500 r=9999 ts=1777075065.158011
PROC_START pid=44635 ppid=44631 depth=3 l=10000 r=12499 ts=1777075065.158041
PROC_START pid=44640 ppid=44632 depth=3 l=17500 r=19999 ts=1777075065.158172
PROC_START pid=44639 ppid=44632 depth=3 l=15000 r=17499 ts=1777075065.158210
PROC_END pid=44633 ppid=44629 depth=3 l=0 r=2499 ts=1777075065.158251
PROC_END pid=44634 ppid=44630 depth=3 l=5000 r=7499 ts=1777075065.158348
PROC_END pid=44636 ppid=44629 depth=3 l=2500 r=4999 ts=1777075065.158461
PROC_END pid=44635 ppid=44631 depth=3 l=10000 r=12499 ts=1777075065.158538
PROC_END pid=44640 ppid=44632 depth=3 l=17500 r=19999 ts=1777075065.158648
PROC_END pid=44639 ppid=44632 depth=3 l=15000 r=17499 ts=1777075065.158726
PROC_END pid=44629 ppid=44627 depth=2 l=0 r=4999 ts=1777075065.158737
PROC_START pid=44638 ppid=44631 depth=3 l=12500 r=14999 ts=1777075065.158738
PROC_END pid=44637 ppid=44630 depth=3 l=7500 r=9999 ts=1777075065.158767
PROC_END pid=44632 ppid=44628 depth=2 l=15000 r=19999 ts=1777075065.158967
PROC_END pid=44638 ppid=44631 depth=3 l=12500 r=14999 ts=1777075065.159187
PROC_END pid=44631 ppid=44628 depth=2 l=10000 r=14999 ts=1777075065.159407
PROC_END pid=44628 ppid=44626 depth=1 l=10000 r=19999 ts=1777075065.159698
PROC_END pid=44630 ppid=44627 depth=2 l=5000 r=9999 ts=1777075065.159747
PROC_END pid=44627 ppid=44626 depth=1 l=0 r=9999 ts=1777075065.160043
PROC_END pid=44626 ppid=44625 depth=0 l=0 r=19999 ts=1777075065.160444
+4 -12
View File
@@ -28,33 +28,25 @@ std::string now() {
return oss.str();
}
/* ========= ONLY MODIFIED PART ========= */
void log_start(int l, int r, int depth) {
std::cout
<< "PROC_START"
<< " pid=" << getpid()
std::cout << "PROC_START pid=" << getpid()
<< " ppid=" << getppid()
<< " depth=" << depth
<< " l=" << l
<< " r=" << r
<< " ts=" << now()
<< '\n'
<< std::flush;
<< '\n' << std::flush;
}
void log_end(int l, int r, int depth) {
std::cout
<< "PROC_END"
<< " pid=" << getpid()
std::cout << "PROC_END pid=" << getpid()
<< " ppid=" << getppid()
<< " depth=" << depth
<< " l=" << l
<< " r=" << r
<< " ts=" << now()
<< '\n'
<< std::flush;
<< '\n' << std::flush;
}
/* ====================================== */
void merge_range(int* arr, int l, int m, int r) {
std::vector<int> temp;
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

+17
View File
@@ -0,0 +1,17 @@
| PID | PPID | Depth | Start | End | Offset | Duration |
|---|---|---|---|---|---|---|
| 44626 | 44625 | 0 | 1777075065.157101 | 1777075065.160444 | 0.000000 | 0.003343 |
| 44627 | 44626 | 1 | 1777075065.157322 | 1777075065.160043 | 0.000221 | 0.002721 |
| 44629 | 44627 | 2 | 1777075065.157527 | 1777075065.158737 | 0.000426 | 0.001210 |
| 44633 | 44629 | 3 | 1777075065.157754 | 1777075065.158251 | 0.000653 | 0.000497 |
| 44636 | 44629 | 3 | 1777075065.157935 | 1777075065.158461 | 0.000834 | 0.000526 |
| 44630 | 44627 | 2 | 1777075065.157613 | 1777075065.159747 | 0.000512 | 0.002134 |
| 44634 | 44630 | 3 | 1777075065.157849 | 1777075065.158348 | 0.000748 | 0.000499 |
| 44637 | 44630 | 3 | 1777075065.158011 | 1777075065.158767 | 0.000910 | 0.000756 |
| 44628 | 44626 | 1 | 1777075065.157416 | 1777075065.159698 | 0.000315 | 0.002282 |
| 44631 | 44628 | 2 | 1777075065.157635 | 1777075065.159407 | 0.000534 | 0.001772 |
| 44635 | 44631 | 3 | 1777075065.158041 | 1777075065.158538 | 0.000940 | 0.000497 |
| 44638 | 44631 | 3 | 1777075065.158738 | 1777075065.159187 | 0.001637 | 0.000449 |
| 44632 | 44628 | 2 | 1777075065.157782 | 1777075065.158967 | 0.000681 | 0.001185 |
| 44640 | 44632 | 3 | 1777075065.158172 | 1777075065.158648 | 0.001071 | 0.000476 |
| 44639 | 44632 | 3 | 1777075065.158210 | 1777075065.158726 | 0.001109 | 0.000516 |
+15 -15
View File
@@ -1,17 +1,17 @@
| PID | PPID | Start | End | Offset | Duration |
|---|---|---|---|---|---|
| 41115 | 41113 | 1777074730.331155 | 1777074730.343971 | 0.000000 | 0.012816 |
| 41116 | 41115 | 1777074730.332433 | 1777074730.342666 | 0.001278 | 0.010233 |
| 41118 | 41116 | 1777074730.333202 | 1777074730.341717 | 0.002047 | 0.008515 |
| 41122 | 41118 | 1777074730.333905 | 1777074730.340717 | 0.002750 | 0.006812 |
| 41126 | 41118 | 1777074730.334760 | 1777074730.340948 | 0.003605 | 0.006188 |
| 41120 | 41116 | 1777074730.333499 | 1777074730.338724 | 0.002344 | 0.005225 |
| 41125 | 41120 | 1777074730.334244 | 1777074730.337630 | 0.003089 | 0.003386 |
| 41129 | 41120 | 1777074730.334870 | 1777074730.336864 | 0.003715 | 0.001994 |
| 41117 | 41115 | 1777074730.332633 | 1777074730.339777 | 0.001478 | 0.007144 |
| 41119 | 41117 | 1777074730.333253 | 1777074730.337559 | 0.002098 | 0.004306 |
| 41123 | 41119 | 1777074730.334001 | 1777074730.336351 | 0.002846 | 0.002350 |
| 41127 | 41119 | 1777074730.334636 | 1777074730.336705 | 0.003481 | 0.002069 |
| 41121 | 41117 | 1777074730.333522 | 1777074730.338710 | 0.002367 | 0.005188 |
| 41124 | 41121 | 1777074730.334220 | 1777074730.336857 | 0.003065 | 0.002637 |
| 41128 | 41121 | 1777074730.334799 | 1777074730.337974 | 0.003644 | 0.003175 |
| 43025 | 43024 | 1777074933.591973 | 1777074933.597747 | 0.000000 | 0.005774 |
| 43026 | 43025 | 1777074933.592392 | 1777074933.595743 | 0.000419 | 0.003351 |
| 43028 | 43026 | 1777074933.592776 | 1777074933.594957 | 0.000803 | 0.002181 |
| 43032 | 43028 | 1777074933.593184 | 1777074933.594108 | 0.001211 | 0.000924 |
| 43035 | 43028 | 1777074933.593475 | 1777074933.594547 | 0.001502 | 0.001072 |
| 43030 | 43026 | 1777074933.592938 | 1777074933.595233 | 0.000965 | 0.002295 |
| 43034 | 43030 | 1777074933.593322 | 1777074933.594447 | 0.001349 | 0.001125 |
| 43037 | 43030 | 1777074933.593732 | 1777074933.594871 | 0.001759 | 0.001139 |
| 43027 | 43025 | 1777074933.592520 | 1777074933.597099 | 0.000547 | 0.004579 |
| 43029 | 43027 | 1777074933.592913 | 1777074933.595208 | 0.000940 | 0.002295 |
| 43033 | 43029 | 1777074933.593304 | 1777074933.594236 | 0.001331 | 0.000932 |
| 43036 | 43029 | 1777074933.593614 | 1777074933.594681 | 0.001641 | 0.001067 |
| 43031 | 43027 | 1777074933.593108 | 1777074933.596602 | 0.001135 | 0.003494 |
| 43038 | 43031 | 1777074933.593782 | 1777074933.594834 | 0.001809 | 0.001052 |
| 43039 | 43031 | 1777074933.595271 | 1777074933.596147 | 0.003298 | 0.000876 |