From 4cac3a721e4540ec615de82867d0e0d62b9507c2 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 16 Jan 2026 16:47:58 +0000 Subject: [PATCH] Add branch coverage build for simulation Adds build_coverage.sh script that: - Builds the simulation with GCC branch coverage instrumentation - Supports --report flag to run simulation and generate lcov HTML report - Uses 60 second timeout with SIGINT for graceful shutdown Also adds SIGINT handler to simulation to exit cleanly and flush coverage data. --- .gitignore | 8 +++++++- build_coverage.sh | 40 ++++++++++++++++++++++++++++++++++++++++ src/main.c | 14 +++++++++++++- 3 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 build_coverage.sh diff --git a/.gitignore b/.gitignore index a8e4cbb..a526b72 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,9 @@ toasty_metadata_server toasty_chunk_server -toasty_simulation \ No newline at end of file +toasty_simulation +toasty_simulation_coverage +*.gcno +*.gcda +*.gcov +*.info +coverage_html/ \ No newline at end of file diff --git a/build_coverage.sh b/build_coverage.sh new file mode 100644 index 0000000..bdbdbfa --- /dev/null +++ b/build_coverage.sh @@ -0,0 +1,40 @@ +#!/bin/bash +set -e + +# Build simulation with branch coverage instrumentation +gcc src/sha256.c src/basic.c src/file_system.c src/byte_queue.c src/file_tree.c src/message.c src/tcp.c src/wal.c src/hash_set.c src/client.c src/metadata_server.c src/chunk_server.c src/random_client.c src/main.c quakey/src/lfs.c quakey/src/lfs_util.c quakey/src/quakey.c -o toasty_simulation_coverage -Iquakey/include -Iinclude -Wall -Wextra -ggdb -O0 -DMAIN_SIMULATION --coverage -fprofile-arcs -ftest-coverage + +echo "Coverage build complete: toasty_simulation_coverage" +echo "" +echo "To generate HTML coverage report:" +echo " 1. Run: ./toasty_simulation_coverage" +echo " 2. Run: ./build_coverage.sh --report" + +if [ "$1" = "--report" ]; then + # Reset previous coverage data + lcov --zerocounters --directory . + + # Capture baseline coverage (before running) + lcov --capture --initial --directory . --output-file coverage_base.info --rc lcov_branch_coverage=1 + + # Run the simulation with a 60 second time limit + # Use SIGINT for graceful shutdown (allows coverage data to be flushed) + # Falls back to SIGKILL after 5 seconds if process doesn't exit + echo "Running simulation (60 second limit)..." + timeout --signal=INT --kill-after=5 60 ./toasty_simulation_coverage || true + + # Capture coverage after running + lcov --capture --directory . --output-file coverage_test.info --rc lcov_branch_coverage=1 + + # Combine baseline and test coverage + lcov --add-tracefile coverage_base.info --add-tracefile coverage_test.info --output-file coverage.info --rc lcov_branch_coverage=1 + + # Remove external/system headers from report + lcov --remove coverage.info '/usr/*' --output-file coverage.info --rc lcov_branch_coverage=1 + + # Generate HTML report + genhtml coverage.info --output-directory coverage_html --branch-coverage + + echo "" + echo "HTML coverage report generated in: coverage_html/index.html" +fi diff --git a/src/main.c b/src/main.c index 3172127..9a21a8a 100644 --- a/src/main.c +++ b/src/main.c @@ -109,8 +109,20 @@ int main(int argc, char **argv) #endif #ifdef MAIN_SIMULATION +#include + +static volatile int simulation_running = 1; + +static void sigint_handler(int sig) +{ + (void)sig; + simulation_running = 0; +} + int main(void) { + signal(SIGINT, sigint_handler); + Quakey *quakey; int ret = quakey_init(&quakey, 1); if (ret < 0) @@ -161,7 +173,7 @@ int main(void) quakey_spawn(quakey, config, "cs --addr 127.0.0.4"); } - for (;;) + while (simulation_running) quakey_schedule_one(quakey); quakey_free(quakey);