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..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) { @@ -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); 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.