diff --git a/README.md b/README.md index 33755a5..dac999b 100644 --- a/README.md +++ b/README.md @@ -43,9 +43,25 @@ may yield partial results. ``` This mode scans the full log and prints each topic instance's sample count and the dropout totals. -It keeps one sample counter per observed subscription and two counters for dropouts. Memory use -also includes message formats, topic definitions, header metadata, parameter defaults, and -parsing diagnostics. +It keeps one sample counter per observed subscription and tracks dropout counts and durations. +Memory use also includes message formats, topic definitions, header metadata, parameter +defaults, and parsing diagnostics. + +## Check a recording in CI + +```sh +./install/bin/flight-log-inspector check tests/log_files/sample_log_small.ulg \ + --max-dropout-ms 30 --min-samples sensor_combined 1298 +``` + +Set at least one limit. The dropout limit is inclusive and applies to the longest individual +dropout. A recording with zero dropouts has a maximum of 0 ms. The sample minimum is also +inclusive and requires the named topic instance to be present. Use `--instance N` with +`--min-samples` to select an instance from 0 through 255. Instance 0 is the default. + +The command reports the observed value and configured threshold for each check. Exit status +0 means all checks passed, 3 means a policy failed, 2 means invalid arguments, and 1 means the +recording could not be checked due to a file or parsing error or warning. ## C++ library diff --git a/src/main.cpp b/src/main.cpp index a2962cd..092f7bd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,13 +3,17 @@ * SPDX-License-Identifier: BSD-3-Clause ****************************************************************************/ +#include #include +#include #include #include #include #include +#include #include #include +#include #include #include #include @@ -35,6 +39,7 @@ class SummaryData : public flight_log::DataContainer { { ++_dropout_count; _total_dropout_ms += item.durationMs(); + _max_dropout_ms = std::max(_max_dropout_ms, uint64_t{item.durationMs()}); } uint64_t sampleCount(uint16_t message_id) const @@ -45,13 +50,60 @@ class SummaryData : public flight_log::DataContainer { uint64_t dropoutCount() const { return _dropout_count; } uint64_t totalDropoutMs() const { return _total_dropout_ms; } + uint64_t maxDropoutMs() const { return _max_dropout_ms; } private: std::unordered_map _sample_counts; uint64_t _dropout_count{0}; uint64_t _total_dropout_ms{0}; + uint64_t _max_dropout_ms{0}; }; +struct QualityPolicy { + std::optional max_dropout_ms; + std::optional min_samples; + std::string topic; + std::optional instance; +}; + +std::optional parseQualityPolicy(int argc, char** argv) +{ + QualityPolicy policy; + for (int i = 3; i < argc;) { + const std::string_view option = argv[i++]; + if (option == "--min-samples") { + if (i == argc || argv[i][0] == '\0') { + return std::nullopt; + } + policy.topic = argv[i++]; + } + if (i == argc) { + return std::nullopt; + } + const std::string_view text = argv[i++]; + const auto end = text.data() + text.size(); + uint64_t number = 0; + const auto parsed = std::from_chars(text.data(), end, number); + if (parsed.ec != std::errc{} || parsed.ptr != end) { + return std::nullopt; + } + if (option == "--max-dropout-ms" && !policy.max_dropout_ms) { + policy.max_dropout_ms = number; + } else if (option == "--min-samples" && !policy.min_samples) { + policy.min_samples = number; + } else if (option == "--instance" && !policy.instance && + number <= std::numeric_limits::max()) { + policy.instance = static_cast(number); + } else { + return std::nullopt; + } + } + if ((!policy.max_dropout_ms && !policy.min_samples) || (policy.instance && !policy.min_samples)) { + return std::nullopt; + } + return policy; +} + void printDropouts(uint64_t count, uint64_t duration_ms) { printf("Dropouts: %" PRIu64 ", total duration: %" PRIu64 " ms\n", count, duration_ms); @@ -68,24 +120,20 @@ void printTopicCounts(flight_log::DataContainer& data, const SummaryData* summar } } -int inspectLog(const char* path, bool summary_only) +bool readLog(const char* path, const std::shared_ptr& data_container) { std::ifstream file(path, std::ios::binary); if (!file) { std::fprintf(stderr, "Cannot open log '%s'\n", path); - return 1; + return false; } std::array buffer{}; - const auto summary = summary_only ? std::make_shared() : nullptr; - const auto storage = flight_log::DataContainer::StorageConfig::FullLog; - const std::shared_ptr data_container = - summary ? summary : std::make_shared(storage); flight_log::Reader reader{data_container}; do { file.read(reinterpret_cast(buffer.data()), buffer.size()); if (file.bad() || (file.fail() && !file.eof())) { std::fprintf(stderr, "Cannot read log '%s'\n", path); - return 1; + return false; } reader.readChunk(buffer.data(), static_cast(file.gcount())); } while (file && !data_container->hadFatalError()); @@ -93,7 +141,55 @@ int inspectLog(const char* path, bool summary_only) for (const auto& parsing_error : data_container->parsingErrors()) { std::fprintf(stderr, "%s: %s\n", path, parsing_error.c_str()); } - if (data_container->hadFatalError()) { + return !data_container->hadFatalError(); +} + +int checkQuality(const char* path, const QualityPolicy& policy) +{ + const auto summary = std::make_shared(); + if (!readLog(path, summary)) { + return 1; + } + if (!summary->parsingErrors().empty()) { + std::fprintf(stderr, "%s: Cannot check recording quality after a parsing warning\n", path); + return 1; + } + + bool passed = true; + if (policy.max_dropout_ms) { + const auto observed = summary->maxDropoutMs(); + const bool within_limit = observed <= *policy.max_dropout_ms; + printf("%s maximum dropout %" PRIu64 " ms (limit %" PRIu64 " ms)\n", + within_limit ? "PASS" : "FAIL", observed, *policy.max_dropout_ms); + passed = within_limit; + } + if (policy.min_samples) { + const int instance = policy.instance.value_or(0); + const auto& subscriptions = summary->subscriptionsByNameAndMultiId(); + const auto topic = subscriptions.find({policy.topic, instance}); + if (topic == subscriptions.end()) { + printf("FAIL topic %s (%d) missing (0 samples, minimum %" PRIu64 ")\n", + policy.topic.c_str(), instance, *policy.min_samples); + passed = false; + } else { + const auto observed = summary->sampleCount(topic->second->getAddLoggedMessage().msgId()); + const bool meets_minimum = observed >= *policy.min_samples; + printf("%s topic %s (%d) samples %" PRIu64 " (minimum %" PRIu64 ")\n", + meets_minimum ? "PASS" : "FAIL", policy.topic.c_str(), instance, observed, + *policy.min_samples); + passed = passed && meets_minimum; + } + } + return passed ? 0 : 3; +} + +int inspectLog(const char* path, bool summary_only) +{ + const auto summary = summary_only ? std::make_shared() : nullptr; + const auto storage = flight_log::DataContainer::StorageConfig::FullLog; + const std::shared_ptr data_container = + summary ? summary : std::make_shared(storage); + if (!readLog(path, data_container)) { return 1; } @@ -180,18 +276,30 @@ int inspectLog(const char* path, bool summary_only) int main(int argc, char** argv) { - constexpr auto usage = "Usage: flight-log-inspector [--summary-only] \n"; - if (argc == 2 && std::string_view(argv[1]) == "--help") { + constexpr auto usage = "Usage: flight-log-inspector [--summary-only] \n" + " flight-log-inspector check [--max-dropout-ms N]\n" + " [--min-samples TOPIC N [--instance N]]\n"; + const std::string_view option = argc > 1 ? argv[1] : ""; + if (argc == 2 && option == "--help") { std::fputs(usage, stdout); return 0; } - const bool summary_only = argc > 1 && std::string_view(argv[1]) == "--summary-only"; - if (argc != (summary_only ? 3 : 2)) { + const bool summary_only = option == "--summary-only"; + const bool check_quality = argc > 2 && option == "check"; + if (!check_quality && argc != (summary_only ? 3 : 2)) { std::fputs(usage, stderr); return 2; } - const auto path = argv[summary_only ? 2 : 1]; + const auto path = argv[summary_only || check_quality ? 2 : 1]; try { + if (check_quality) { + const auto policy = parseQualityPolicy(argc, argv); + if (!policy) { + std::fputs(usage, stderr); + return 2; + } + return checkQuality(path, *policy); + } return inspectLog(path, summary_only); } catch (const std::exception& error) { std::fprintf(stderr, "%s: %s\n", path, error.what()); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 156fb14..1225a28 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -18,6 +18,11 @@ add_test(NAME cli-summary-only COMMAND ${CMAKE_COMMAND} -DLOG_DIR=${CMAKE_CURRENT_SOURCE_DIR}/log_files -P ${CMAKE_CURRENT_SOURCE_DIR}/check_summary_only.cmake ) +add_test(NAME cli-quality-check COMMAND ${CMAKE_COMMAND} + -DCLI=$ + -DLOG_DIR=${CMAKE_CURRENT_SOURCE_DIR}/log_files + -P ${CMAKE_CURRENT_SOURCE_DIR}/check_quality.cmake +) add_test(NAME cli-empty-input COMMAND ${CMAKE_COMMAND} -DCLI=$ -DARGUMENT=${empty_log} -DEXPECTED_STATUS=1 "-DEXPECTED_ERROR=Not enough data to read file magic" diff --git a/tests/check_quality.cmake b/tests/check_quality.cmake new file mode 100644 index 0000000..1b2fbc7 --- /dev/null +++ b/tests/check_quality.cmake @@ -0,0 +1,50 @@ +function(check_quality expected_status log) + execute_process(COMMAND "${CLI}" check "${LOG_DIR}/${log}" ${ARGN} + RESULT_VARIABLE status OUTPUT_VARIABLE output ERROR_VARIABLE errors + ) + if(NOT status STREQUAL expected_status) + message(FATAL_ERROR "Expected check exit ${expected_status}, got ${status}\n${output}${errors}") + endif() + set(report "${output}${errors}" PARENT_SCOPE) +endfunction() + +set(small sample_log_small.ulg) +check_quality(3 ${small} --max-dropout-ms 29 --min-samples sensor_combined 1299) +if(NOT report MATCHES "FAIL maximum dropout 30 ms [(]limit 29 ms[)]" OR + NOT report MATCHES "FAIL topic sensor_combined [(]0[)] samples 1298 [(]minimum 1299[)]") + message(FATAL_ERROR "Expected both policy failures\n${report}") +endif() + +check_quality(0 ${small} --max-dropout-ms 30 --min-samples sensor_combined 1298) +if(NOT report MATCHES "PASS maximum dropout 30 ms [(]limit 30 ms[)]" OR + NOT report MATCHES "PASS topic sensor_combined [(]0[)] samples 1298 [(]minimum 1298[)]") + message(FATAL_ERROR "Expected both inclusive thresholds to pass\n${report}") +endif() + +check_quality(0 sample.ulg --max-dropout-ms 62) +if(NOT report MATCHES "PASS maximum dropout 62 ms [(]limit 62 ms[)]") + message(FATAL_ERROR "Expected to check the largest of four dropouts\n${report}") +endif() + +check_quality(0 sample_logging_tagged_and_default_params.ulg --max-dropout-ms 0) +if(NOT report MATCHES "PASS maximum dropout 0 ms [(]limit 0 ms[)]") + message(FATAL_ERROR "Expected a zero maximum for a recording with no dropouts\n${report}") +endif() + +check_quality(0 ${small} --min-samples telemetry_status 8 --instance 1) +if(NOT report MATCHES "PASS topic telemetry_status [(]1[)] samples 8 [(]minimum 8[)]") + message(FATAL_ERROR "Expected to check the selected topic instance\n${report}") +endif() + +check_quality(3 ${small} --min-samples telemetry_status 0 --instance 2) +if(NOT report MATCHES "FAIL topic telemetry_status [(]2[)] missing [(]0 samples, minimum 0[)]") + message(FATAL_ERROR "Expected a missing topic instance to fail\n${report}") +endif() + +check_quality(2 ${small} --max-dropout-ms -1) +check_quality(2 ${small} --instance 1 --max-dropout-ms 30) +check_quality(2 ${small}) +check_quality(1 missing.ulg --max-dropout-ms 30) +if(NOT report MATCHES "Cannot open log") + message(FATAL_ERROR "Expected a file error for a missing recording\n${report}") +endif()