-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_trace.sh
More file actions
executable file
·199 lines (170 loc) · 5.6 KB
/
run_trace.sh
File metadata and controls
executable file
·199 lines (170 loc) · 5.6 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DMD_BIN="dmd"
BENCHMARK_FILE="$SCRIPT_DIR/benchmark.d"
OUT_DIR="$SCRIPT_DIR/artifacts"
TRACE_NAME="trace.json"
GRANULARITY=1
GRANULARITY_SWEEP=""
SWEEP_CSV=""
PYTHON_BIN="python3"
usage() {
cat <<USAGE
Usage: $(basename "$0") [options]
Options:
--dmd-bin <path> DMD executable (default: dmd from PATH)
--benchmark <path> Benchmark source file (default: benchmark.d)
--out-dir <path> Output directory (default: artifacts)
--trace-name <name> Trace file name (default: trace.json)
--granularity <n> ftime-trace granularity for main trace (default: 1)
--granularity-sweep <list> Comma-separated sweep values (e.g. 1,10,50,100)
--sweep-csv <path> Sweep CSV output path (default: <out-dir>/trace_granularity_sweep.csv)
--python-bin <path> Python executable (default: python3)
--help Show this message
USAGE
}
run_trace_compile() {
local granularity="$1"
local trace_path="$2"
local out_obj="$3"
set +e
"$DMD_BIN" "$BENCHMARK_FILE" "-of=$out_obj" "-c" "-ftime-trace" "-ftime-trace-file=$trace_path" "-ftime-trace-granularity=$granularity" >/dev/null 2>&1
local status=$?
set -e
if [[ $status -ne 0 ]]; then
"$DMD_BIN" "$BENCHMARK_FILE" "-of=$out_obj" "-c" "-ftime-trace=$trace_path" "-ftime-trace-granularity=$granularity" >/dev/null
fi
}
run_python() {
MPLCONFIGDIR="$OUT_DIR/.cache/matplotlib" XDG_CACHE_HOME="$OUT_DIR/.cache" "$PYTHON_BIN" "$@"
}
while [[ $# -gt 0 ]]; do
case "$1" in
--dmd-bin)
DMD_BIN="$2"
shift 2
;;
--benchmark)
BENCHMARK_FILE="$2"
shift 2
;;
--out-dir)
OUT_DIR="$2"
shift 2
;;
--trace-name)
TRACE_NAME="$2"
shift 2
;;
--granularity)
GRANULARITY="$2"
shift 2
;;
--granularity-sweep)
GRANULARITY_SWEEP="$2"
shift 2
;;
--sweep-csv)
SWEEP_CSV="$2"
shift 2
;;
--python-bin)
PYTHON_BIN="$2"
shift 2
;;
--help)
usage
exit 0
;;
*)
echo "Unknown argument: $1" >&2
usage >&2
exit 1
;;
esac
done
mkdir -p "$OUT_DIR"
TRACE_PATH="$OUT_DIR/$TRACE_NAME"
OUT_OBJ="$OUT_DIR/.trace_build.o"
mkdir -p "$OUT_DIR/.cache/matplotlib" "$OUT_DIR/.cache/fontconfig"
if [[ -z "$SWEEP_CSV" ]]; then
SWEEP_CSV="$OUT_DIR/trace_granularity_sweep.csv"
fi
echo "Generating trace with: $DMD_BIN"
run_trace_compile "$GRANULARITY" "$TRACE_PATH" "$OUT_OBJ"
rm -f "$OUT_OBJ"
if [[ ! -s "$TRACE_PATH" ]]; then
echo "Trace file was not created: $TRACE_PATH" >&2
exit 1
fi
run_python "$SCRIPT_DIR/trace_phase.py" \
--input "$TRACE_PATH" \
--out-csv "$OUT_DIR/trace_phase_summary.csv" \
--events-csv "$OUT_DIR/trace_event_summary.csv" \
--plot "$OUT_DIR/trace_phase_bar.png"
if [[ -n "$GRANULARITY_SWEEP" ]]; then
echo "granularity,trace_size_bytes,timed_events,dominant_phase,dominant_phase_pct" > "$SWEEP_CSV"
IFS=',' read -r -a SWEEP_VALUES <<< "$GRANULARITY_SWEEP"
for raw in "${SWEEP_VALUES[@]}"; do
g="$(echo "$raw" | tr -d '[:space:]')"
if [[ -z "$g" ]]; then
continue
fi
if ! [[ "$g" =~ ^[0-9]+$ ]]; then
echo "Skipping invalid granularity value: $raw"
continue
fi
SWEEP_TRACE="$OUT_DIR/.trace_g${g}.json"
SWEEP_OBJ="$OUT_DIR/.trace_g${g}.o"
SWEEP_PHASE="$OUT_DIR/.trace_g${g}_phase.csv"
SWEEP_EVENTS="$OUT_DIR/.trace_g${g}_events.csv"
run_trace_compile "$g" "$SWEEP_TRACE" "$SWEEP_OBJ"
rm -f "$SWEEP_OBJ"
if [[ ! -s "$SWEEP_TRACE" ]]; then
echo "$g,0,0,missing,0" >> "$SWEEP_CSV"
continue
fi
run_python "$SCRIPT_DIR/trace_phase.py" \
--input "$SWEEP_TRACE" \
--out-csv "$SWEEP_PHASE" \
--events-csv "$SWEEP_EVENTS" \
--plot "$OUT_DIR/.trace_g${g}.png" \
--no-plot >/dev/null
run_python - "$g" "$SWEEP_TRACE" "$SWEEP_PHASE" "$SWEEP_CSV" <<'PY'
import csv
import json
import os
import sys
from pathlib import Path
granularity = sys.argv[1]
trace_path = Path(sys.argv[2])
phase_csv = Path(sys.argv[3])
sweep_csv = Path(sys.argv[4])
trace_payload = json.loads(trace_path.read_text(encoding="utf-8", errors="ignore"))
if isinstance(trace_payload, dict):
events = trace_payload.get("traceEvents", [])
elif isinstance(trace_payload, list):
events = trace_payload
else:
events = []
timed_events = sum(1 for ev in events if isinstance(ev, dict) and ev.get("ph") in {"X", ""} and float(ev.get("dur", 0) or 0) > 0)
trace_size = os.path.getsize(trace_path)
phase = ""
pct = ""
if phase_csv.exists():
with phase_csv.open("r", newline="", encoding="utf-8") as handle:
reader = csv.DictReader(handle)
first = next(reader, None)
if first:
phase = first.get("phase", "")
pct = first.get("percent", "")
with sweep_csv.open("a", encoding="utf-8") as handle:
handle.write(f"{granularity},{trace_size},{timed_events},{phase},{pct}\n")
PY
rm -f "$SWEEP_TRACE" "$SWEEP_PHASE" "$SWEEP_EVENTS" "$OUT_DIR/.trace_g${g}.png"
done
echo "Granularity sweep CSV: $SWEEP_CSV"
fi
echo "Trace JSON: $TRACE_PATH"
echo "Phase summary: $OUT_DIR/trace_phase_summary.csv"