Repository navigation
Conversation
tryRunPipeline() uses cfeJobComplete() to decide whether a CFE job can be run, but cfeJobComplete() tests the newest job in cfeJobQueue_ while the job that is then run is the oldest one. A job that has everything it needs is left waiting whenever the newest job is incomplete. That is the state of the queue whenever a frame's CFE config buffer is dequeued after its image and stats buffers. The config buffer is not part of the completeness test, so it finds the newest job complete and opens a new one that holds nothing else. A job that could not start when its frame arrived, because no request was queued or the Back End was still busy, then has to wait for the next frame, which in turn waits for the one after it. The camera gains one frame of latency on every such occasion and never loses it. Each waiting job holds a CFE image buffer. Once all four are in the queue the CFE has nothing to capture into, no further buffers are dequeued, the newest job can never complete and the camera stops for good. Nothing is reported, as the dequeue watchdog of the CFE image node only runs while buffers are queued on it. Fix this by testing the job that is about to be run. cfeJobComplete() now takes the job to test: cfeBufferDequeue() passes the newest job and tryRunPipeline() the oldest. Fixes: 841ef2b ("pipeline: rpi: Add support for Raspberry Pi 5") Link: raspberrypi#369 Signed-off-by: solomai <solomaj@gmail.com>
Collaborator
|
Thank you for this fix. I will submit the change to the upstream libcamera mailing list. However, I will adjust the commit message to be less verbose. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reported in #369, which has the full analysis and a standalone reproducer.
Problem
tryRunPipeline()askscfeJobComplete()whether a CFE job can be run.cfeJobComplete()tests the newest job incfeJobQueue_(back()), but the job that is then run is the oldest one (front()). A complete job at the head of the queue is left waiting whenever the newest job is incomplete, even with the pipeline idle and a request queued.This is the normal state of the queue whenever a frame's CFE config buffer is dequeued after its image and stats buffers. The config buffer is not part of the completeness test, so it finds the newest job complete and opens a new one that holds nothing else. Until the next frame arrives
cfeJobComplete()is false, so a job can only be started in the instant between the dequeue of a frame's image/stats buffers and the dequeue of its config buffer: once per frame. If the pipeline cannot start at that instant, the job waits for the next frame, which then waits for the one after it — one frame of latency, gained for good, on every occurrence.Each waiting job holds a CFE image buffer. With the four allocated by default, the fourth occurrence leaves all of them in
cfeJobQueue_: the CFE has nothing to capture into, no buffers are dequeued any more, the newest job can never complete andtryRunPipeline()returns early from then on. Requests are never completed again and nothing is logged, because the dequeue watchdog on the CFE image node only runs while buffers are queued on it, so the frontend timeout does not fire and the camera stays in the running state.When it happens
The order in which a frame's three buffers are dequeued follows the file descriptor numbers of the CFE nodes: the buffers become ready together and
EventDispatcherPollhandles ready descriptors in ascending order. The nodes are opened as image, config, stats, so a process with consecutive descriptors dequeues the config buffer second and never sees this. If other threads open and close descriptors while the camera manager starts, the config node can end up with the highest of the three, and the config buffer is then dequeued last for the lifetime of the process; in one multi-threaded application this was the case for a given camera in 11 of 50 process starts. Such a process runs normally as long as a request is queued and the pipeline is idle whenever a frame arrives, but an application with no request queued for four consecutive frames (a gap of 100 to 133 ms at 30 fps) stops the camera for good.Fix
Test the job that is about to be run.
cfeJobComplete()now takes the job to test:cfeBufferDequeue()passes the newest job, as before, andtryRunPipeline()the oldest. A complete job at the head of the queue runs as soon as the pipeline is idle and a request is available, whatever the state of the newest job, so a backlog drains as soon as requests come back.Testing
Raspberry Pi 5, kernel 6.18.50+rpt-rpi-2712, two IMX219 modules, one viewfinder stream, the default four requests, 30 fps. The reproducer from #369 holds back all four requests as they complete and queues them again a given time later; latency is request completion time minus
SensorTimestamp.With the usual descriptor order every row is "no change", before and after.
"Before" is
mainat 670581c, the stock 0.7.2+rpt20261009-1 packages and a 0.7.2+rpt20260817 build; "after" is 670581c with this change (48 trials, no frame delivered out of order in 26,763). With a busy Back End instead (640x480 sensor mode at 200 fps, 3840x2160 output), the camera with the config node last delivered 66 frames per second before the change and 114 after, the same as with the usual order.pisp.cppis identical onmainandnext, so the change applies to both.utils/checkstyle.pyreports no issue.