Skip to content

Commit

Permalink
Add tracing test
Browse files Browse the repository at this point in the history
  • Loading branch information
dsharlet committed Nov 19, 2024
1 parent afd32e9 commit 737afd1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
7 changes: 5 additions & 2 deletions pthread_trace.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,16 @@ class circular_file {
buffer_ = nullptr;
}
if (fd >= 0) {
if (next_.load() < size_) {
size_t size = next_.load();
if (size < size_) {
// Remove blocks we didn't write anything to.
int result = ftruncate(fd, next_.load());
int result = ftruncate(fd, size);
(void)result;
}
::close(fd);
fd = -1;

fprintf(stderr, "pthread_trace: Recorded %zu KB trace\n", size / 1024);
}
}

Expand Down
28 changes: 24 additions & 4 deletions test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,31 @@ cc_test(
size = "small",
)

# We don't have good tests of tracing itself. It's hard to test:
# - Traces are nondeterministic (they contain pointers and timestamps)
# - I haven't figured out how to use it without LD_PRELOAD
# The best we have for now is just running one of the benchmarks that use pthreads with tracing,
# and verifying it doesn't crash and we see expected pthread_trace output.
sh_test(
name = "trace_thread_benchmark",
srcs = ["trace_test.sh"],
data = [
":thread_benchmark",
"//:pthread_trace.so",
],
args = [
"$(location //:pthread_trace.so)",
"$(location :thread_benchmark)",
"--benchmark_min_time=1x",
],
size = "small",
)

cc_test(
name = "benchmark",
srcs = ["benchmark.cc"],
name = "thread_benchmark",
srcs = ["thread_benchmark.cc"],
deps = ["@google_benchmark//:benchmark_main"],
args=["--benchmark_min_time=1x"],
args = ["--benchmark_min_time=1x"],
size = "small",
)

Expand All @@ -28,6 +48,6 @@ cc_test(
"//:proto",
"@google_benchmark//:benchmark_main"
],
args=["--benchmark_min_time=1x"],
args = ["--benchmark_min_time=1x"],
size = "small",
)
File renamed without changes.
16 changes: 16 additions & 0 deletions test/trace_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

pthread_trace="$1"
target="$2"

shift
shift

fail() { rc=$?; (( $# )) && printf '%s\n' "$*" >&2; exit $(( rc == 0 ? 1 : rc )); }

trace=$(mktemp)

exec 5>&1
output=$(PTHREAD_TRACE_BUFFER_SIZE_KB=1 PTHREAD_TRACE_PATH=$trace LD_PRELOAD="$pthread_trace" "$target" "$@" 2>&1 | tee >(cat - >&5))

echo "$output" | grep "pthread_trace: Recorded.*KB trace" > /dev/null || fail "Did not find pthread_trace report in output"

0 comments on commit 737afd1

Please sign in to comment.