diff --git a/pthread_trace.cc b/pthread_trace.cc index 53b7a2d..565dabd 100644 --- a/pthread_trace.cc +++ b/pthread_trace.cc @@ -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); } } diff --git a/test/BUILD b/test/BUILD index 31d5ae3..8cf4cbe 100644 --- a/test/BUILD +++ b/test/BUILD @@ -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", ) @@ -28,6 +48,6 @@ cc_test( "//:proto", "@google_benchmark//:benchmark_main" ], - args=["--benchmark_min_time=1x"], + args = ["--benchmark_min_time=1x"], size = "small", ) diff --git a/test/benchmark.cc b/test/thread_benchmark.cc similarity index 100% rename from test/benchmark.cc rename to test/thread_benchmark.cc diff --git a/test/trace_test.sh b/test/trace_test.sh new file mode 100755 index 0000000..9a99cea --- /dev/null +++ b/test/trace_test.sh @@ -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" \ No newline at end of file