forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathc++expr
More file actions
executable file
·380 lines (350 loc) · 10.5 KB
/
Copy pathc++expr
File metadata and controls
executable file
·380 lines (350 loc) · 10.5 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/usr/bin/env bash
set -e
usage() {
cat <<EOF >&2
USAGE: c++expr [-c CXX | -C | -I] [-i INCLUDE] [-l LIB] [-b STEPS] [-t TESTS] [-o FILE] [-O CXX_OPTS...] [-g 'GLOBAL CODE'] 'MAIN CODE'
OPTIONS:
-c CXX use specified c++ compiler
-C use cmake
-k keep generated worktree
-I integrate into ClickHouse build tree in current directory
-i INC add #include <INC>
-l LIB link against LIB (only for -I or -C)
-b STEPS_NUM make program to benchmark specified code snippet and run tests with STEPS_NUM each
-b perf-top run infinite benchmark and show perf top
-j THREADS Only make sense with -b, run specified number of std::thread in parallel
-B build-dir build directory for -I (default: "build")
-t TESTS_NUM make program to benchmark specified code snippet and run TESTS_NUM tests
-o FILE do not run, just save binary executable file
-O CXX_OPTS forward option compiler (e.g. -O "-O3 -std=c++20")
EXAMPLES:
$ c++expr -g 'int fib(int n) { return n < 2 ? n : fib(n-2) + fib(n-1); }' 'OUT(fib(10)) OUT(fib(20)) OUT(fib(30))'
fib(10) -> 55
fib(20) -> 6765
fib(30) -> 832040
$ c++expr -I -i Interpreters/Context.h 'OUT(sizeof(DB::Context))'
sizeof(DB::Context) -> 7776
$ c++expr -I -i Common/Stopwatch.h -b 10000 'Stopwatch sw;'
Steps per test: 10000
Test #0: 0.025 us 39.526 Mps
...
Test #4: 0.026 us 38.314 Mps
MainAvg: 0.026 us 38.373 Mps
EOF
exit 1
}
SOURCE_FILE=main.cpp
GLOBAL=
OUTPUT_EXECUTABLE=
INCS="vector iostream iomanip typeinfo cstdlib cmath sys/time.h"
LIBS=""
BUILD_DIR=build
BENCHMARK_STEPS=0
THREADS=1
RUN_PERFTOP=
BENCHMARK_TESTS=5
USE_CMAKE=
USE_CLICKHOUSE=
CXX=g++
CXX_OPTS=
CMD_PARAMS=
KEEP_WORKTREE=0
#
# Parse command line
#
if [ "$1" == "--help" ] || [ -z "$1" ]; then usage; fi
while getopts "vc:CIi:l:b:j:kB:t:o:O:g:" OPT; do
case "$OPT" in
v) set -x; ;;
c) CXX="$OPTARG"; ;;
C) USE_CMAKE=y; ;;
I) USE_CLICKHOUSE=y; LIBS="$LIBS clickhouse_common_io"; ;;
i) INCS="$INCS $OPTARG"; ;;
l) LIBS="$LIBS $OPTARG"; ;;
b) if [ "$OPTARG" = perf-top ]; then BENCHMARK_STEPS=-1; RUN_PERFTOP=y; else BENCHMARK_STEPS="$OPTARG"; fi; ;;
j) THREADS="$OPTARG"; INCS="$INCS mutex thread barrier";;
B) BUILD_DIR="$OPTARG"; ;;
k) KEEP_WORKTREE=1; ;;
t) BENCHMARK_TESTS="$OPTARG"; ;;
o) OUTPUT_EXECUTABLE="$OPTARG"; ;;
O) CXX_OPTS="$CXX_OPTS $OPTARG"; ;;
g) GLOBAL="$OPTARG"; ;;
esac
done
shift $(( $OPTIND - 1 ))
#
# Positional arguments
#
EXPR=$1
shift
if [ -z "$EXPR" ]; then usage; fi
#
# Arguments forwarded to program should go after main code and before --
#
while [ -n "$1" ] && [ "$1" != "--" ]; do
CMD_PARAMS="$CMD_PARAMS $1"
shift
done
if [ "$1" == "--" ]; then shift; fi
#
# Setup workdir
#
find_clickhouse_root () {
local DIR="`pwd`"
while [ $DIR != "/" ]; do
if [ ! -e "$DIR/CMakeLists.txt" ]; then
echo "error: $DIR has no CMakeLists.txt"
return 1
fi
if grep "project(ClickHouse" "$DIR/CMakeLists.txt" >/dev/null 2>&1; then
echo $DIR
return 0
fi
DIR="`dirname $DIR`"
done
echo "error: unable to find ClickHouse root folder"
return 1
}
find_clickhouse_build () {
local CLICKHOUSE_ROOT="`find_clickhouse_root`"
if [ -e "$CLICKHOUSE_ROOT/$BUILD_DIR/CMakeCache.txt" ]; then
echo "$CLICKHOUSE_ROOT/$BUILD_DIR"
return 0
fi
echo "error: $CLICKHOUSE_ROOT/$BUILD_DIR/CMakeCache.txt doesn't exist"
return 1
}
CALL_DIR=`pwd`
EXECUTABLE=cppexpr_$$
EXECUTABLE_DIR=.
if [ -n "$USE_CLICKHOUSE" ]; then
SUBDIR=cppexpr_$$
WORKDIR=$CALL_DIR/$SUBDIR
if [ ! -e $CALL_DIR/CMakeLists.txt ]; then
echo "error: $CALL_DIR/CMakeLists.txt is required for integration" >&2
exit 1
fi
CLICKHOUSE_ROOT="`find_clickhouse_root`"
BUILD_ROOT="`find_clickhouse_build`"
CLICKHOUSE_PATH="${WORKDIR/$CLICKHOUSE_ROOT}"
EXECUTABLE_DIR="${BUILD_ROOT}${CLICKHOUSE_PATH}"
if [ -z "$CLICKHOUSE_ROOT" ] || [ -z "$BUILD_ROOT" ] || [ -z "$CLICKHOUSE_PATH" ]; then
echo "error: unable to locate ClickHouse" >&2
exit 1
fi
cp $CALL_DIR/CMakeLists.txt $CALL_DIR/CMakeLists.txt.backup.$$
echo "add_subdirectory ($SUBDIR)" >>$CALL_DIR/CMakeLists.txt
cleanup() {
mv $CALL_DIR/CMakeLists.txt.backup.$$ $CALL_DIR/CMakeLists.txt
if [ $KEEP_WORKTREE -eq 0 ]; then
rm -rf $WORKDIR
rm -rf ${BUILD_ROOT}${CLICKHOUSE_PATH}
fi
}
else
WORKDIR=/var/tmp/cppexpr_$$
cleanup() {
if [ $KEEP_WORKTREE -eq 0 ]; then
rm -rf $WORKDIR
fi
}
fi
trap cleanup EXIT
mkdir -p $WORKDIR
cd $WORKDIR
#
# Generate CMakeLists.txt
#
if [ -n "$USE_CMAKE" ]; then
cat <<EOF >>CMakeLists.txt
project(CppExpr)
SET(PROJECT_NAME CppExpr)
SET(CMAKE_INCLUDE_CURRENT_DIR TRUE)
cmake_minimum_required(VERSION 2.8)
set(CMAKE_CXX_FLAGS -fPIC)
set(CMAKE_C_FLAGS -fPIC)
set(CMAKE_BUILD_TYPE Release)
set(SOURCES $SOURCE_FILE)
add_executable($EXECUTABLE \${SOURCES})
EOF
fi
#
# Generate CMakeLists.txt for integration
#
if [ -n "$USE_CLICKHOUSE" ]; then
cat <<EOF >>CMakeLists.txt
clickhouse_add_executable($EXECUTABLE $SOURCE_FILE)
EOF
fi
#
# Add libraries to CMakeLists.txt
#
if [ -n "$LIBS" ]; then
cat <<EOF >>CMakeLists.txt
target_link_libraries($EXECUTABLE PRIVATE $LIBS)
EOF
fi
#
# Generate source code
#
>$SOURCE_FILE
for INC in $INCS; do
echo "#include <$INC>" >> $SOURCE_FILE
done
cat <<EOF >>$SOURCE_FILE
#define RESULT(label, us, mps) label << ": " << std::fixed << std::setw(7) << std::setfill(' ') << std::setprecision(3) << (us) << " us\t" << std::setw(7) << std::setfill(' ') << std::setprecision(3) << (mps) << " Mps"
#define OUT(expr) std::cout << #expr << " -> " << (expr) << std::endl;
#if $THREADS == 1
#define SYNC()
#define LOG(msg) std::cout << " " << msg << std::endl
#define LOG_MAIN(msg) LOG(msg)
#define AVERAGE "MainAvg"
#else
std::barrier barrier($THREADS + 1);
std::mutex log_mutex;
#define SYNC() barrier.arrive_and_wait()
#define LOG(msg) do { std::scoped_lock lock{log_mutex}; std::cout << "\033[01;3" << (1 + thread_id % 8) << "m[" << std::setw(2) << std::setfill('0') << thread_id << "] " << msg << "\033[00m" << std::endl; } while (false)
#define LOG_MAIN(msg) do { std::scoped_lock lock{log_mutex}; std::cout << " " << msg << std::endl; } while (false)
#define AVERAGE "Average"
#endif
size_t max_tests = $BENCHMARK_TESTS;
size_t max_steps = $BENCHMARK_STEPS;
$GLOBAL
int work(int thread_id = 0) {
(void)thread_id;
try {
EOF
if [ $BENCHMARK_STEPS -eq 0 ]; then
cat <<EOF >>$SOURCE_FILE
$EXPR
EOF
else
cat <<EOF >>$SOURCE_FILE
double total = 0.0;
for (size_t test = 0; test < max_tests; test++) {
SYNC(); // sync with all threads before test
timeval beg, end;
gettimeofday(&beg, nullptr);
for (size_t step = 0; step < max_steps; step++) {
asm volatile("" ::: "memory");
$EXPR
}
gettimeofday(&end, nullptr);
SYNC(); // sync with all threads after test
double interval = static_cast<double>(end.tv_sec - beg.tv_sec)*1e6 + static_cast<double>(end.tv_usec - beg.tv_usec);
LOG(RESULT("Test #" << test, interval / static_cast<double>(max_steps), static_cast<double>(max_steps) / interval));
total += interval;
}
SYNC(); // sync for avg printing
LOG(RESULT(AVERAGE, total / static_cast<double>(max_tests) / static_cast<double>(max_steps), static_cast<double>(max_steps) / (total / static_cast<double>(max_tests))));
SYNC(); // sync for avg printing
EOF
fi
cat <<EOF >>$SOURCE_FILE
return 0;
} catch (std::exception& e) {
std::cerr << "unhandled exception (" << typeid(e).name() << "):" << e.what() << std::endl;
} catch (...) {
std::cerr << "unknown unhandled exception\n";
}
return 1;
}
int main(int argc, char** argv) {
(void)argc; (void)argv;
if (max_steps == 0)
max_steps = 1;
else
LOG_MAIN("Steps per test: " << max_steps);
EOF
if [ $THREADS -gt 1 ] && [ $BENCHMARK_STEPS -ne 0 ]; then
cat <<EOF >>$SOURCE_FILE
LOG_MAIN("Threads: " << $THREADS);
std::vector<std::thread> threads;
for (size_t i = 0; i < $THREADS; ++i)
threads.emplace_back(work, i);
timeval main_beg, main_end;
double main_total = 0.0;
for (size_t test = 0; test < max_tests; test++) {
gettimeofday(&main_beg, nullptr);
SYNC(); // sync with all threads before test
SYNC(); // sync with all threads after test
gettimeofday(&main_end, nullptr);
double interval = static_cast<double>(main_end.tv_sec - main_beg.tv_sec)*1e6 + static_cast<double>(main_end.tv_usec - main_beg.tv_usec);
double per_step = interval / (static_cast<double>(max_steps) * $THREADS);
double mps = static_cast<double>(max_steps) * $THREADS / interval;
LOG_MAIN(RESULT("Test #" << test, per_step, mps));
main_total += interval;
}
double avg = main_total / (static_cast<double>(max_tests) * static_cast<double>(max_steps) * $THREADS);
double mps = static_cast<double>(max_tests) * static_cast<double>(max_steps) * $THREADS / main_total;
SYNC(); // sync for avg printing
SYNC(); // sync for avg printing
LOG_MAIN(RESULT("MainAvg", avg, mps));
for (auto& thread : threads)
thread.join();
return 0;
EOF
else
cat <<EOF >>$SOURCE_FILE
return work();
EOF
fi
cat <<EOF >>$SOURCE_FILE
}
#ifdef OUT // just to avoid unused macro warning
#undef OUT
#endif
#undef LOG
#undef LOG_MAIN
#ifdef SYNC
#undef SYNC
#endif
#ifdef AVERAGE
#undef AVERAGE
#endif
#ifdef RESULT
#undef RESULT
#endif
EOF
#
# Compile
#
if [ -n "$USE_CMAKE" ]; then
if ! (cmake . && make); then
cat -n $SOURCE_FILE
exit 1
fi
elif [ -n "$USE_CLICKHOUSE" ]; then
if ! (cd $BUILD_ROOT && ninja -j $(nproc) $EXECUTABLE) >stdout.log 2>stderr.log; then
grep -hE 'error:|warning:|FAILED:' stdout.log stderr.log >&2
cat -n $SOURCE_FILE
exit 1
fi
else
RET=0
$CXX $CXX_OPTS -I$CALL_DIR -o $EXECUTABLE $SOURCE_FILE || RET=$?
if [ $RET -ne 0 ]; then
cat -n $SOURCE_FILE
exit $RET
fi
fi
#
# Execute
#
RET=0
if [ -z "$OUTPUT_EXECUTABLE" ]; then
if [ -z "$RUN_PERFTOP" ]; then
"$@" $EXECUTABLE_DIR/$EXECUTABLE $CMD_PARAMS || RET=$?
else
"$@" $EXECUTABLE_DIR/$EXECUTABLE $CMD_PARAMS &
PID=$!
perf top -p $PID
kill $PID
fi
else
cp $EXECUTABLE_DIR/$EXECUTABLE $CALL_DIR/$OUTPUT_EXECUTABLE
fi
#
# Cleanup is handled by trap EXIT
#
exit $RET