-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
86 lines (73 loc) · 2.24 KB
/
script.py
File metadata and controls
86 lines (73 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# import os
# import matplotlib.pyplot as plt
# import numpy as np
#
# os.system("gcc -O0 tlb-size.c")
# page_size = 10
# for i in range(1000):
# page_size+=100
# # print(f"curr page size: {page_size}")
# os.system(f"taskset -c 0 ./a.out {page_size} >> output.txt")
#
# import os
import re
import subprocess
import matplotlib
matplotlib.use("TkAgg") # you're on i3wm
import matplotlib.pyplot as plt
C_FILE = "tlb-size.c"
OUTPUT = "output.txt"
# 1) Compile C
subprocess.run(["gcc", "-O0", C_FILE], check=True)
# 2) Run experiments (clear previous output first)
open(OUTPUT, "w").close()
page_size = 1
for i in range(200):
page_size += 1
# Run pinned to CPU 0, capture stdout, append to file
res = subprocess.run(
["taskset", "-c", "0", "./a.out", str(page_size)],
check=True,
capture_output=True,
text=True,
)
with open(OUTPUT, "a") as f:
f.write(res.stdout)
if not res.stdout.endswith("\n"):
f.write("\n") # ensure newline between runs
# 3) Parse output.txt
iters, pages, times = [], [], []
pat = re.compile(
r"iter:\s*(\d+)\s+pages:\s*(\d+)\s+total_avg_access_time_for_10_runs:\s*([0-9]*\.?[0-9]+)"
)
with open(OUTPUT, "r") as f:
for line in f:
m = pat.search(line)
if m:
iters.append(int(m.group(1)))
pages.append(int(m.group(2)))
times.append(float(m.group(3)))
print(f"Parsed {len(iters)} lines from {OUTPUT}.")
if not iters:
raise RuntimeError(
"No data parsed. Check that your C program prints lines like:\n"
"iter: <n> pages: <n> total_avg_access_time_for_10_runs: <time>"
)
# 4) Plot: Iteration vs Time
plt.figure(figsize=(10, 6))
plt.plot(iters, times, marker="o", linestyle="-")
plt.xlabel("Iteration")
plt.ylabel("Average Access Time (ns)")
plt.title("TLB Experiment: Iteration vs Access Time")
plt.grid(True)
plt.tight_layout()
plt.show()
# Optional: if you also want Page Size vs Time, uncomment below:
# plt.figure(figsize=(10, 6))
# plt.plot(pages, times, marker="o", linestyle="-")
# plt.xlabel("Page Size (whatever unit your a.out expects)")
# plt.ylabel("Average Access Time (ns)")
# plt.title("TLB Experiment: Page Size vs Access Time")
# plt.grid(True)
# plt.tight_layout()
# plt.show()