Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,19 @@ jobs:
with:
python-version: "3.12"
- uses: pre-commit/action@v3.0.1

coverage:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- name: Install GCC 14
run: |
sudo apt-get update
sudo apt-get install -y --no-install-recommends g++-14
- name: Configure, build, and test with MC/DC coverage
env:
CXX: g++-14
run: |
cmake --preset coverage
cmake --build --preset coverage
ctest --preset coverage
37 changes: 37 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,43 @@ if(PROJECT_IS_TOP_LEVEL AND BUILD_TESTING)
mpmcqueue_executable(MPMCQueueExample src/MPMCQueueExample.cpp)
mpmcqueue_executable(MPMCQueueTest src/MPMCQueueTest.cpp)
add_test(NAME MPMCQueueTest COMMAND MPMCQueueTest)

option(MPMCQueue_COVERAGE "Check MC/DC coverage with gcov" OFF)
if(MPMCQueue_COVERAGE)
if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_VERSION VERSION_LESS 14)
message(FATAL_ERROR "MPMCQueue_COVERAGE requires GCC 14 or newer")
endif()
if(CMAKE_CONFIGURATION_TYPES)
message(FATAL_ERROR "MPMCQueue_COVERAGE requires a single-config generator")
endif()

target_compile_options(MPMCQueueTest PRIVATE
-O0 --coverage -fcondition-coverage -fprofile-update=atomic
)
target_link_options(MPMCQueueTest PRIVATE --coverage)

find_package(Python3 COMPONENTS Interpreter REQUIRED)
get_filename_component(compiler_dir "${CMAKE_CXX_COMPILER}" DIRECTORY)
if(CMAKE_CXX_COMPILER MATCHES "g[+][+](-[0-9]+)$")
set(gcov_name "gcov${CMAKE_MATCH_1}")
else()
set(gcov_name gcov)
endif()
find_program(GCOV_EXECUTABLE NAMES "${gcov_name}" HINTS "${compiler_dir}" REQUIRED)
set(profile "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/MPMCQueueTest.dir/src/MPMCQueueTest.cpp")
add_test(NAME MPMCQueueCoverageReset COMMAND "${CMAKE_COMMAND}" -E rm -f "${profile}.gcda")
add_test(NAME MPMCQueueMCDC
COMMAND ${Python3_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/src/check_mcdc.py"
--gcno "${profile}.gcno"
--gcov "${GCOV_EXECUTABLE}"
)
set_tests_properties(MPMCQueueCoverageReset PROPERTIES FIXTURES_SETUP queue_coverage_reset)
set_tests_properties(MPMCQueueTest PROPERTIES
FIXTURES_REQUIRED queue_coverage_reset
FIXTURES_SETUP queue_coverage
)
set_tests_properties(MPMCQueueMCDC PROPERTIES FIXTURES_REQUIRED queue_coverage DEPENDS MPMCQueueTest)
endif()
endif()

# -----------------------------------------------------------------------------
Expand Down
19 changes: 19 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@
"cacheVariables": {
"CMAKE_CXX_FLAGS": "-fsanitize=thread -fno-omit-frame-pointer"
}
},
{
"name": "coverage",
"displayName": "Debug (GCC MC/DC)",
"inherits": "debug",
"cacheVariables": {
"CMAKE_CXX_STANDARD": "20",
"MPMCQueue_COVERAGE": "ON"
}
}
],
"buildPresets": [
Expand All @@ -72,6 +81,11 @@
"name": "tsan",
"inherits": "base",
"configurePreset": "tsan"
},
{
"name": "coverage",
"inherits": "base",
"configurePreset": "coverage"
}
],
"testPresets": [
Expand Down Expand Up @@ -101,6 +115,11 @@
"name": "tsan",
"inherits": "base",
"configurePreset": "tsan"
},
{
"name": "coverage",
"inherits": "base",
"configurePreset": "coverage"
}
]
}
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,16 @@ implementation:
- A multithreaded fuzz test that all elements are enqueued and
dequeued correctly under heavy contention.

To check MC/DC coverage of the queue header, use GCC 14 or newer with its
matching gcov and a single-config CMake generator:

CXX=g++-14 cmake --preset coverage
cmake --build --preset coverage
ctest --preset coverage

CTest clears the previous profile before each run. The check combines condition
outcomes across template instantiations and fails on uncovered code or outcomes.

## TODO

- [X] Add allocator supports so that the queue could be used with huge pages and
Expand Down
30 changes: 26 additions & 4 deletions src/MPMCQueueTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <cstddef>
#include <cstdint>
#include <exception>
#include <limits>
#include <memory>
#include <new>
#include <rigtorp/MPMCQueue.h>
Expand Down Expand Up @@ -271,9 +272,20 @@ int main(int argc, char *argv[]) {
assert(throws && state.count == 0);
}

// Fuzz test
{
const std::size_t numOps = 1000;
bool throws = false;
try {
MPMCQueue<int> q(std::numeric_limits<std::size_t>::max());
} catch (const std::bad_array_new_length &) {
throws = true;
}
assert(throws);
}

// Exercise both ticket paths under contention, including try-operation
// retries when another producer or consumer claims a ticket first.
for (bool tryOperations : {false, true}) {
const std::size_t numOps = 10000;
const std::size_t numThreads = 10;
MPMCQueue<std::uint64_t> q(numThreads);
std::atomic<bool> flag(false);
Expand All @@ -284,7 +296,12 @@ int main(int argc, char *argv[]) {
while (!flag)
;
for (auto j = i; j < numOps; j += numThreads) {
q.push(static_cast<std::uint64_t>(j));
if (tryOperations) {
while (!q.try_push(static_cast<std::uint64_t>(j)))
;
} else {
q.push(static_cast<std::uint64_t>(j));
}
}
}));
}
Expand All @@ -295,7 +312,12 @@ int main(int argc, char *argv[]) {
std::uint64_t threadSum = 0;
for (auto j = i; j < numOps; j += numThreads) {
std::uint64_t v;
q.pop(v);
if (tryOperations) {
while (!q.try_pop(v))
;
} else {
q.pop(v);
}
threadSum += v;
}
sum += threadSum;
Expand Down
93 changes: 93 additions & 0 deletions src/check_mcdc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/usr/bin/env python3
"""Verify MC/DC coverage of include/rigtorp/MPMCQueue.h using gcov -g."""

import argparse
import collections
import re
import subprocess
import sys
import tempfile
from pathlib import Path


def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--gcno", type=Path, required=True, help="Path to MPMCQueueTest.cpp.gcno")
parser.add_argument("--gcov", default="gcov", help="gcov executable")
args = parser.parse_args()

gcno = args.gcno.resolve()
if not gcno.with_suffix(".gcda").is_file():
parser.error(f"missing profile data for {gcno}; run MPMCQueueTest first")
with tempfile.TemporaryDirectory(prefix="mpmc-mcdc-") as tmpdir:
subprocess.run(
[args.gcov, "-g", str(gcno)],
cwd=tmpdir,
stdout=subprocess.DEVNULL,
check=True,
)
gcov_file = Path(tmpdir) / "MPMCQueue.h.gcov"
if not gcov_file.exists():
raise SystemExit(f"Missing {gcov_file}")
lines = gcov_file.read_text().splitlines()

src = {}
exec_counts = collections.defaultdict(int)
inst_conds = collections.defaultdict(list)
cur_ln = None
cur_inst = None

for line in lines:
match = re.match(r"^\s*([^:]+):\s*(\d+):(.*)$", line)
if match and not line.startswith("condition"):
cnt, ln, code = match.group(1).strip(), int(match.group(2)), match.group(3)
cur_ln = ln
src.setdefault(ln, code)
if cnt not in ("-", "#####", "====="):
exec_counts[ln] += int(cnt.rstrip("*"))
cur_inst = None
elif line.startswith("condition outcomes covered"):
match2 = re.match(r"condition outcomes covered (\d+)/(\d+)", line)
if match2 and cur_ln is not None:
cur_inst = [int(match2.group(2)), set()]
inst_conds[cur_ln].append(cur_inst)
elif line.startswith("condition ") and cur_inst is not None:
missing = re.fullmatch(r"condition\s+(\d+) not covered \((true|false|true false)\)", line)
if not missing:
raise SystemExit(f"Unexpected gcov output: {line}")
for outcome in missing[2].split():
cur_inst[1].add((int(missing[1]), outcome))

never_exec = {
int(m.group(1))
for line in lines
for m in [re.match(r"^\s*(?:#####|=====):\s*(\d+):", line)]
if m and exec_counts[int(m.group(1))] == 0
}

gaps = {}
total_outcomes = 0
covered_outcomes = 0
for ln, insts in sorted(inst_conds.items()):
tot = max(t for t, _ in insts)
common_uncov = set.intersection(*(u for _, u in insts))
cov = tot - len(common_uncov)
total_outcomes += tot
covered_outcomes += cov
if common_uncov:
gaps[ln] = (cov, tot, src[ln].strip(), sorted(common_uncov))

print(f"MPMCQueue.h MC/DC condition outcomes: {covered_outcomes}/{total_outcomes}")
for ln in sorted(never_exec):
print(f"UNEXECUTED L{ln}: {src[ln]}")
for ln, (cov, tot, code, uncov) in gaps.items():
print(f"CONDITION GAP L{ln} ({cov}/{tot}): {code} -> {uncov}")

if not total_outcomes:
print("No instrumented conditions found", file=sys.stderr)
if not total_outcomes or never_exec or gaps:
sys.exit(1)


if __name__ == "__main__":
main()
Loading