Skip to content

DPL: make the stats command queue safe for concurrent updates and flushes - #16248

Merged
ktf merged 1 commit into
AliceO2Group:devfrom
ktf:pr16248
Dec 1, 2026
Merged

ktf merged 1 commit into
AliceO2Group:devfrom
ktf:pr16248

Conversation

@ktf

@ktf ktf commented Dec 1, 2026

Copy link
Copy Markdown
Member

updateStats appends to a fixed queue of commands and the one which finds it
full flushes it. With a thread pool the loop and the pool threads update the
same stats, and that went wrong several ways, all upstream:

  • A thread finding the queue full while another flushed waited on
    while (cmds.size()), which is the size of a std::array and never false, and
    it still counted as pending, which is what the flush waits for. Both waited
    for ever: SlowConsumer hung with the loop asleep inside updateStats, reached
    from DataRelayer::relay, and its producer blocked in Send.
  • processCommandQueue reopened the queue by storing nextCmd = 0 before
    insertedCmds = 0, both relaxed, so a command taken from the new queue could
    have its count wiped and never be processed. The flushing thread then reset
    both counters a second time after processCommandQueue had reopened the
    queue, losing whatever other threads had added in between.
  • processCommandQueue is also called directly, by the metrics flush on the
    loop, while pool threads may be adding commands.

So processCommandQueue now takes a mutex, closes the queue before looking at it
(whoever takes a command from then on finds it full and waits on that mutex),
waits for those already writing, and reopens it in an order which cannot lose
a count, also when a command throws. Taking a command and closing the queue are
sequentially consistent with the pending count, so that either the flush sees
a writer pending or the writer sees the queue closed. A thread finding the
queue full stops counting as pending, waits for the mutex and flushes the queue
only if nobody has reopened it meanwhile, so that threads which found it full
together do not flush it one after the other, then goes round again. The fast
path stays lock-free, and single-threaded behaviour, nextCmd back to 0 after a
flush included, is unchanged.

The new DataProcessingStatsConcurrentUpdates test has four threads add 1 twenty
thousand times each while a fifth flushes the queue directly. Before, 5 runs
out of 5 gave up after 10 s with threads still stuck; after, 200 runs out of
200 pass with the exact total.

…shes

updateStats appends to a fixed queue of commands and the one which finds it
full flushes it. With a thread pool the loop and the pool threads update the
same stats, and that went wrong several ways, all upstream:

- A thread finding the queue full while another flushed waited on
  `while (cmds.size())`, which is the size of a std::array and never false, and
  it still counted as pending, which is what the flush waits for. Both waited
  for ever: SlowConsumer hung with the loop asleep inside updateStats, reached
  from DataRelayer::relay, and its producer blocked in Send.
- processCommandQueue reopened the queue by storing nextCmd = 0 before
  insertedCmds = 0, both relaxed, so a command taken from the new queue could
  have its count wiped and never be processed. The flushing thread then reset
  both counters a second time after processCommandQueue had reopened the
  queue, losing whatever other threads had added in between.
- processCommandQueue is also called directly, by the metrics flush on the
  loop, while pool threads may be adding commands.

So processCommandQueue now takes a mutex, closes the queue before looking at it
(whoever takes a command from then on finds it full and waits on that mutex),
waits for those already writing, and reopens it in an order which cannot lose
a count, also when a command throws. Taking a command and closing the queue are
sequentially consistent with the pending count, so that either the flush sees
a writer pending or the writer sees the queue closed. A thread finding the
queue full stops counting as pending, waits for the mutex and flushes the queue
only if nobody has reopened it meanwhile, so that threads which found it full
together do not flush it one after the other, then goes round again. The fast
path stays lock-free, and single-threaded behaviour, nextCmd back to 0 after a
flush included, is unchanged.

The new DataProcessingStatsConcurrentUpdates test has four threads add 1 twenty
thousand times each while a fifth flushes the queue directly. Before, 5 runs
out of 5 gave up after 10 s with threads still stuck; after, 200 runs out of
200 pass with the exact total.
@ktf
ktf requested a review from a team as a code owner December 1, 2026 11:47
@ktf
ktf merged commit b820884 into AliceO2Group:dev Dec 1, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant