Skip to content

ustreamer: reduce camera memory by ~1.6 MiB PSS at 864x480/8 FPS - #255

Open
pdscomp wants to merge 2 commits into
mainfrom
doom/ustreamer
Open

ustreamer: reduce camera memory by ~1.6 MiB PSS at 864x480/8 FPS#255
pdscomp wants to merge 2 commits into
mainfrom
doom/ustreamer

Conversation

@pdscomp

@pdscomp pdscomp commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

ustreamer: reduce camera memory by ~1.6 MiB PSS at 864x480

Split from #252.

Why this PR

uStreamer is always on. On a 128 MiB machine, cutting its resident set by a third frees real headroom for calibration and printing without losing the snapshot/live-view workflow.

Justification / measurements (isolated, live CC1, active 864x480 client)

Metric Before After Delta
PSS 4.67 MiB 3.08 MiB −1.59 MiB (34%)
RSS 5.76 MiB 4.14 MiB −1.62 MiB (28%)
  • Verified on flashed CC1: snapshots and live video functional.
  • Production command unchanged in shape: 864x480, camera MJPEG, 1 worker/buffer.

Commit

  • 1d791e3 ustreamer: reduce camera streamer memory use

Risk / rollback

Low. Config revert restores previous command line; USERPTR attempt safely falls back to MMAP on cameras that reject it (validated on the CC1 UVC camera).

@pdscomp
pdscomp temporarily deployed to approval-given July 19, 2026 14:35 — with GitHub Actions Inactive
@pdscomp

pdscomp commented Jul 19, 2026

Copy link
Copy Markdown
Contributor Author

Feedback from @suchmememanyskill

  • 0002-reduce-memory-usage.patch: very large patch, will take significant time to understand.
  • ustreamer-init-d: disagrees with 10→8 FPS drop; in his testing earlier FPS didn't see a difference.
  • Patch looks untargeted; goal per-patch unclear.

Add the CC1-targeted uStreamer memory reduction patch to the Yocto recipe. It bounds frame/thread allocations, retains only the newest JPEG, avoids the permanent blank RGB canvas, and falls back from USERPTR to MMAP when the UVC camera rejects USERPTR.

Validated on the CC1 over a four-day burn-in with two clients connected and no crashes.
@pdscomp
pdscomp temporarily deployed to approval-given July 19, 2026 16:36 — with GitHub Actions Inactive
@github-actions

Copy link
Copy Markdown

✅ Build Artifacts

Branch: doom/ustreamer
Build: 1d791e3 (merge into main)

Artifact Size
CC1 Firmware 91.31 MB

View workflow run

@pdscomp

pdscomp commented Jul 19, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I agree this patch is broad and exploratory in places, so I want to clarify the goal and the risk profile.

uStreamer is a non-essential printer component, and rollback is trivial: the service can be disabled or the recipe patch can be reverted without affecting printing, Klipper, or Moonraker. That makes it a reasonable place to try several narrowly targeted memory reductions while we have a reproducible CC1 workload.

The common goal for every change here is reducing uStreamer’s PSS and RSS. During the investigation, the individual changes generally moved memory usage by roughly 100 KiB or more, and the combined result was:

  • PSS: 4.67 MiB → 3.08 MiB
  • RSS: 5.76 MiB → 4.14 MiB

The main finding was that uStreamer’s capture/processing pipeline retained multiple copies of frame buffers for throughput. The camera is already configured for low resolution and roughly 10 FPS, so that optimization is a poor trade on a 128 MiB appliance. The kernel/V4L2 capture path also reserves approximately 1.8 MiB per buffer even though the camera’s compressed MJPEG frames are usually only 20–30 KiB. Retaining several of those buffers therefore costs more than 3 MiB of scarce RAM for very little practical benefit. Keeping only the newest frame and allowing HTTP delivery to borrow it safely is where much of the improvement comes from.

The patch also keeps the camera-specific fallback behavior: the CC1 UVC device rejects USERPTR queueing, so the implementation retries with MMAP and remains functional. Snapshots and live video were verified on the target.

Based on this feedback, I reverted the 10→8 FPS change. The memory reduction patch can therefore be evaluated independently while retaining the existing 10 FPS deployment setting.

If useful, I can further split the C patch into smaller commits, but the changes are all aimed at the same measurable objective: reducing uStreamer’s resident memory footprint on the CC1.

@pdscomp
pdscomp marked this pull request as ready for review July 20, 2026 02:00
@pdscomp

pdscomp commented Jul 20, 2026

Copy link
Copy Markdown
Contributor Author

Flashed and tested PR-255-1d7 on carbon2u (CC1).

ustreamer is running and serving snapshots + live stream:

/usr/bin/ustreamer -r 864x480 -c HW -m MJPEG -b 1 -w 1 -f 10 -l --host=0.0.0.0 --allow-origin=*

Measured from /proc/[pid]/smaps_rollup:

state PSS RSS
idle 2.88 MiB 4.02 MiB
after /snapshot 3.13 MiB 4.31 MiB
after 2s /stream 2.62 MiB 3.80 MiB
  • /snapshot returns HTTP 200 with ~51 kB JPEG.
  • /stream delivers MJPEG successfully (pulled ~900 kB in 2s).
  • No crashes or elevated memory after the stream.

These are absolute numbers from the patched build PR-255-1d7, not a before/after baseline. They line up with the PR body's claim of ~3.08 MiB PSS / 4.14 MiB RSS at 8 FPS; observed at 10 FPS is slightly lower.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant