From 77ef1cf7fef5dc0af49c3d2225530a4b7d2ac057 Mon Sep 17 00:00:00 2001 From: Giulio Eulisse <10544+ktf@users.noreply.github.com> Date: Wed, 2 Dec 2026 10:23:25 +0100 Subject: [PATCH 1/2] DPL: flush changed metrics under the stats flush mutex flushChangedMetrics runs on the loop. It reads metrics and reads and resets updated and updateInfos, which a pool thread flushing the command queue writes at the same time under flushMutex. Take the same mutex. Nothing it calls goes back to updateStats or processCommandQueue: the callback only formats the metric and hands it to Monitoring, whose DPL backend queues it for the driver. --- Framework/Core/include/Framework/DataProcessingStats.h | 3 ++- Framework/Core/src/DataProcessingStats.cxx | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Framework/Core/include/Framework/DataProcessingStats.h b/Framework/Core/include/Framework/DataProcessingStats.h index 0d2a796c477d7..56e98be5ad434 100644 --- a/Framework/Core/include/Framework/DataProcessingStats.h +++ b/Framework/Core/include/Framework/DataProcessingStats.h @@ -196,7 +196,8 @@ struct DataProcessingStats { std::atomic statesSize = 0; std::array cmds = {}; - /// Held by processCommandQueue, so that the queue is flushed by one thread at a time. + /// Held by processCommandQueue, so that the queue is flushed by one thread at a time, + /// and by flushChangedMetrics, which reads and resets what the flush updates. std::mutex flushMutex; std::array metrics = {}; std::array updated = {}; diff --git a/Framework/Core/src/DataProcessingStats.cxx b/Framework/Core/src/DataProcessingStats.cxx index 849e75b3a9cb3..f923cbb32af44 100644 --- a/Framework/Core/src/DataProcessingStats.cxx +++ b/Framework/Core/src/DataProcessingStats.cxx @@ -214,6 +214,8 @@ void DataProcessingStats::processCommandQueue(bool onlyIfFull) void DataProcessingStats::flushChangedMetrics(std::function const& callback) { + // Pool threads flushing the command queue write the same metrics. + std::scoped_lock lock(flushMutex); publishingInvokedTotal++; bool publish = false; auto currentTimestamp = getTimestamp(realTimeBase, initialTimeOffset); From 6cd75217bcc82aa1e4235c78e0d4edc039bb97d0 Mon Sep 17 00:00:00 2001 From: Giulio Eulisse <10544+ktf@users.noreply.github.com> Date: Wed, 2 Dec 2026 10:23:25 +0100 Subject: [PATCH 2/2] DPL: process the stats commands in the order of their timestamps processCommandQueue sorts the indices of the queued commands by timestamp, as the comment says, so that a command is applied after those which happened before it, but then walks the queue in insertion order, ignoring the sort. Within one thread the two orders are the same, since the timestamps come from the loop's clock. Across threads they are not: a command can take its slot before another and read the clock after it. A Set then came after an Add which happened later, saw the Add's timestamp as newer and was dropped. Walk the sorted order. DataProcessingStatsOutOfOrder now checks that case. --- Framework/Core/src/DataProcessingStats.cxx | 2 +- Framework/Core/test/test_DataProcessingStats.cxx | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Framework/Core/src/DataProcessingStats.cxx b/Framework/Core/src/DataProcessingStats.cxx index f923cbb32af44..f329689a6a59d 100644 --- a/Framework/Core/src/DataProcessingStats.cxx +++ b/Framework/Core/src/DataProcessingStats.cxx @@ -124,7 +124,7 @@ void DataProcessingStats::processCommandQueue(bool onlyIfFull) // Process the commands in the order we have just computed. for (int i = 0; i < range; ++i) { - auto& cmd = cmds[i]; + auto& cmd = cmds[order[i]]; assert(cmd.id < updateInfos.size()); auto& update = updateInfos[cmd.id]; switch (cmd.op) { diff --git a/Framework/Core/test/test_DataProcessingStats.cxx b/Framework/Core/test/test_DataProcessingStats.cxx index d07343edede07..e36ff9360d904 100644 --- a/Framework/Core/test/test_DataProcessingStats.cxx +++ b/Framework/Core/test/test_DataProcessingStats.cxx @@ -192,7 +192,7 @@ TEST_CASE("DataProcessingStatsOutOfOrder") }; auto cpuTime = [](int64_t base, int64_t offset) -> int64_t { static int count = 0; - int64_t value[] = {0, 1000, 999, 998}; + int64_t value[] = {0, 1000, 999, 998, 1100, 1050}; return base + value[count++] - offset; }; DataProcessingStats stats(realtimeTime, cpuTime, {}); @@ -210,6 +210,13 @@ TEST_CASE("DataProcessingStatsOutOfOrder") stats.updateStats({DummyMetric, DataProcessingStats::Op::Set, 3}); stats.processCommandQueue(); REQUIRE(stats.metrics[DummyMetric] == 2); + + // The Add comes first in the queue, but the Set happened before it, so the + // Set must not be dropped as older than the Add. + stats.updateStats({DummyMetric, DataProcessingStats::Op::Add, 1}); + stats.updateStats({DummyMetric, DataProcessingStats::Op::Set, 7}); + stats.processCommandQueue(); + REQUIRE(stats.metrics[DummyMetric] == 8); } /// We verify that the running average is correctly computed.