Repository navigation
Expand file tree
/
Copy pathcompile-times.sh
More file actions
executable file
·275 lines (239 loc) · 8.52 KB
/
Copy pathcompile-times.sh
File metadata and controls
executable file
·275 lines (239 loc) · 8.52 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/env bash
set -euo pipefail
REPO_URL="https://github.com/ggml-org/llama.cpp"
BUILD_DIR="build"
README_FILE="README-compile-times.md"
DATA_FILE="compile-times.csv"
PLOT_FILE="compile-times.png"
# Limit per directory for faster iteration (set high to measure all)
LIMIT_PER_DIR=${LIMIT_PER_DIR:-99999}
# Files to skip (basename match)
IGNORE_FILES=(
"build-info.cpp"
"ui.cpp"
"get-model.cpp"
)
# --- Clone repo ---
if [ ! -d "llama.cpp" ]; then
echo "Cloning llama.cpp ..."
git clone --filter=tree:0 --depth=1 "$REPO_URL"
else
echo "llama.cpp directory already exists. Skipping clone."
fi
# --- Configure ---
echo "Configuring cmake ..."
rm -rf "$BUILD_DIR"
cmake -B "$BUILD_DIR" -S llama.cpp \
-DGGML_CCACHE=OFF \
-DGGML_METAL=OFF \
-DLLAMA_BUILD_TESTS=ON \
-DLLAMA_BUILD_EXAMPLES=OFF \
-DLLAMA_BUILD_UI=OFF \
-DCMAKE_DISABLE_PRECOMPILE_HEADERS=ON \
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON
# --- Measure per-file compile times ---
echo ""
echo "=== Measuring compile times ==="
COMPILE_DB="$BUILD_DIR/compile_commands.json"
if [ ! -f "$COMPILE_DB" ]; then
echo "ERROR: compile_commands.json not found."
exit 1
fi
# Collect results in a temp file: "time_ms|directory|filename"
RESULTS_FILE=$(mktemp)
trap "rm -f $RESULTS_FILE" EXIT
# Cumulative times per directory (in ms)
ggml_time=0
src_time=0
common_time=0
tools_time=0
tests_time=0
other_time=0
# Counters per directory
ggml_count=0
src_count=0
common_count=0
tools_count=0
tests_count=0
total_measured=0
count=$(jq length "$COMPILE_DB")
echo "Found $count compile commands (limit: $LIMIT_PER_DIR per directory)."
echo ""
for (( i=0; i<count; i++ )); do
command=$(jq -r ".[$i].command" "$COMPILE_DB")
file=$(jq -r ".[$i].file" "$COMPILE_DB")
fname=$(basename "$file")
# Check if file should be ignored
skip=false
for ignored in "${IGNORE_FILES[@]}"; do
if [ "$fname" = "$ignored" ]; then
skip=true
break
fi
done
[ "$skip" = true ] && continue
# Relative path from llama.cpp/ for GitHub links (use ## for longest match)
# Generated unity-build files live under $BUILD_DIR (outside llama.cpp/),
# so derive a path relative to the build directory for them.
relpath=${file##*llama.cpp/}
if [[ "$relpath" == "$file" ]]; then
relpath="build/${file##*$BUILD_DIR/}"
fi
# Determine directory category (match first component of relpath)
# Generated unity-build files live under build/ but belong to the same
# directory as their target (e.g. build/src/CMakeFiles/llama.dir/Unity/...).
case "$relpath" in
ggml/*) dir="ggml" ;;
src/*) dir="src" ;;
common/*) dir="common" ;;
tools/*) dir="tools" ;;
tests/*) dir="tests" ;;
build/src/CMakeFiles/*/Unity/*) dir="src" ;;
*) continue ;;
esac
# Check per-directory limit
count_var="${dir}_count"
[ "${!count_var}" -ge "$LIMIT_PER_DIR" ] && continue
# Extract output file path (-o <path>) and remove it to force real compilation
output=$(echo "$command" | sed -n 's/.*-o \([^ ]*\).*/\1/p')
(cd "$BUILD_DIR" && mkdir -p "$(dirname "$output")" && rm -f "$output")
# Time the compilation in milliseconds
start=$(date +%s%N)
(cd "$BUILD_DIR" && eval "$command") >/dev/null 2>&1 || true
end=$(date +%s%N)
ms=$(( (end - start) / 1000000 ))
# Convert to seconds with one decimal
sec_whole=$(( ms / 1000 ))
sec_frac=$(( (ms % 1000) / 100 ))
echo "$ms|$dir|$fname|$relpath" >> "$RESULTS_FILE"
total_measured=$(( total_measured + 1 ))
# Accumulate directory time and increment counter
time_var="${dir}_time"
count_var="${dir}_count"
eval "$time_var=\$(( $time_var + ms ))"
eval "$count_var=\$(( $count_var + 1 ))"
printf "[%d] %-50s %d.%ds\n" "$total_measured" "$relpath" "$sec_whole" "$sec_frac"
done
# Helper: convert ms to "X.Ys" format
ms_to_sec() {
local ms=$1
local whole=$(( ms / 1000 ))
local frac=$(( (ms % 1000) / 100 ))
printf "%d.%ds" "$whole" "$frac"
}
# Print per-file results sorted by time (descending)
echo ""
echo "=== Compile times (sorted) ==="
printf "%-10s %s\n" "TIME" "FILE"
printf "%-10s %s\n" "----" "----"
sort -t'|' -k1 -rn "$RESULTS_FILE" | while IFS='|' read -r ms dir fname relpath; do
printf "%-10s %s\n" "$(ms_to_sec $ms)" "$relpath"
done
# Print cumulative times per directory
echo ""
echo "=== Cumulative times by directory ==="
printf "%-10s %s\n" "TIME" "DIRECTORY"
printf "%-10s %s\n" "----" "-----------"
printf "%-10s %s/\n" "$(ms_to_sec $ggml_time)" "ggml"
printf "%-10s %s/\n" "$(ms_to_sec $src_time)" "src"
printf "%-10s %s/\n" "$(ms_to_sec $common_time)" "common"
printf "%-10s %s/\n" "$(ms_to_sec $tools_time)" "tools"
printf "%-10s %s/\n" "$(ms_to_sec $tests_time)" "tests"
# Generate README
echo ""
echo "Generating $README_FILE ..."
COMMIT=$(cd llama.cpp && git log -1 --format='%h')
REPO="https://github.com/ggml-org/llama.cpp"
{
echo "# llama.cpp Compile Times"
echo ""
echo "Auto-generated on $(date -u '+%Y-%m-%d %H:%M:%S UTC')"
echo ""
echo "## Configuration"
echo ""
echo "- **Commit:** [$COMMIT]($REPO/commit/$COMMIT) ($(cd llama.cpp && git log -1 --format='%s'))"
echo "- **CMake flags:** \`-DGGML_CCACHE=OFF -DGGML_METAL=OFF -DLLAMA_BUILD_TESTS=ON -DLLAMA_BUILD_EXAMPLES=OFF -DLLAMA_BUILD_UI=OFF -DCMAKE_DISABLE_PRECOMPILE_HEADERS=ON\`"
echo "- **Files measured:** $total_measured"
if [ "$LIMIT_PER_DIR" -lt "$count" ]; then
echo "- **Note:** Limited to $LIMIT_PER_DIR files per directory."
fi
echo ""
echo "## Compile Times Over Commits"
echo ""
if [ -f "$PLOT_FILE" ]; then
echo ""
else
echo "Plot not available (gnuplot not installed)."
fi
echo ""
echo "## Cumulative Times by Directory"
echo ""
echo "| Directory | Time |"
echo "|-----------|------|"
echo "| [ggml/]($REPO/tree/$COMMIT/ggml) | $(ms_to_sec $ggml_time) |"
echo "| [src/]($REPO/tree/$COMMIT/src) | $(ms_to_sec $src_time) |"
echo "| [common/]($REPO/tree/$COMMIT/common) | $(ms_to_sec $common_time) |"
echo "| [tools/]($REPO/tree/$COMMIT/tools) | $(ms_to_sec $tools_time) |"
echo "| [tests/]($REPO/tree/$COMMIT/tests) | $(ms_to_sec $tests_time) |"
echo ""
echo "## Compile Times by Directory"
echo ""
for dir in ggml src common tools tests; do
echo "### $dir/"
echo ""
echo "| Time | File |"
echo "|------|------|"
grep "|$dir|" "$RESULTS_FILE" | sort -t'|' -k1 -rn | while IFS='|' read -r ms d fname relpath; do
if [[ "$relpath" == build/* ]]; then
# Generated unity files are not part of the source tree, so they
# cannot be linked to a blob on GitHub.
echo "| $(ms_to_sec $ms) | $relpath |"
else
echo "| $(ms_to_sec $ms) | [$relpath]($REPO/blob/$COMMIT/$relpath) |"
fi
done
echo ""
done
} > "$README_FILE"
# --- Record data for historical tracking ---
echo ""
echo "Recording data to $DATA_FILE ..."
# Create data file with header if it doesn't exist
if [ ! -f "$DATA_FILE" ]; then
echo "commit,timestamp,ggml_ms,src_ms,common_ms,tools_ms,tests_ms" > "$DATA_FILE"
fi
# Append new data
TIMESTAMP=$(date +%s)
echo "$COMMIT,$TIMESTAMP,$ggml_time,$src_time,$common_time,$tools_time,$tests_time" >> "$DATA_FILE"
# --- Generate plot ---
echo "Generating $PLOT_FILE ..."
if command -v gnuplot >/dev/null 2>&1; then
# Create gnuplot script
GNUPLOT_SCRIPT=$(mktemp)
trap "rm -f $RESULTS_FILE $GNUPLOT_SCRIPT" EXIT
cat > "$GNUPLOT_SCRIPT" << GNUEOF
set terminal png size 800,400 enhanced
set output "$PLOT_FILE"
set title "Compile Times by Directory"
set ylabel "Time (s)"
set xlabel "Date"
set grid
set key outside right
set xdata time
set timefmt "%s"
set format x "%m-%d"
set xtics rotate by -45
set style data linespoints
set datafile separator ","
plot \\
'$DATA_FILE' skip 1 using 2:(\$3/1000.0) title 'ggml' with linespoints, \\
'$DATA_FILE' skip 1 using 2:(\$4/1000.0) title 'src' with linespoints, \\
'$DATA_FILE' skip 1 using 2:(\$5/1000.0) title 'common' with linespoints, \\
'$DATA_FILE' skip 1 using 2:(\$6/1000.0) title 'tools' with linespoints, \
'$DATA_FILE' skip 1 using 2:(\$7/1000.0) title 'tests' with linespoints
GNUEOF
gnuplot "$GNUPLOT_SCRIPT" 2>/dev/null && echo "Plot generated: $PLOT_FILE"
else
echo "Warning: gnuplot not found. Skipping plot generation."
fi
echo "Done. Output written to $README_FILE"