Skip to content

driver: Add guest-side clock synchronization Operating Modes - #31

Open
changyeon-jo wants to merge 1 commit into
chromeos:mainfrom
changyeon-jo:clock-sync-opmodes
Open

changyeon-jo wants to merge 1 commit into
chromeos:mainfrom
changyeon-jo:clock-sync-opmodes

Conversation

@changyeon-jo

Copy link
Copy Markdown

The Guest-side External Camera HAL queries buffer age against Guest
CLOCK_MONOTONIC, while the Host-side emulated virtio-media device
timestamp is generated in the Host's CLOCK_MONOTONIC domain. Because
the Host and Guest boot times differ (creating a monotonic offset of
~190 seconds on Cuttlefish), this clock domain mismatch causes the HAL
to reject all dequeued buffers as "too old" and drops 100% of frames,
yielding a "can't connect to camera" timeout in the camera app.

This change introduces three guest-side clock synchronization operating
modes to bridge the monotonic gap inside the guest driver:

  • Mode 0 (Passthrough): Verbatim Host-domain timestamp passthrough
    (Backward-compatible default).
  • Mode 1 (Guest Stamping): Statelessly overwrites incoming buffer
    timestamps with the Guest's live CLOCK_MONOTONIC (ktime_get_ns())
    upon dequeue. Foolproof and immune to VM suspend/resume.
  • Mode 2 (Dynamic Translation): Dynamically translates Host timestamps
    onto the Guest timeline using a min-filtered calibration baseline and
    auto-recalibration (150ms optimized threshold) on VM resume.

Both the active mode and recalibration threshold can be dynamically
toggled and inspected at runtime via writeable module parameters in sysfs:

# Toggle to Mode 1 (Guest-Local Stamping)
echo 1 > /sys/module/virtio_media/parameters/timestamp_mode

# Confirm the current mode
cat /sys/module/virtio_media/parameters/timestamp_mode

# Configure recalibration threshold (e.g. 200ms)
echo 200000000 > /sys/module/virtio_media/parameters/recalib_threshold_ns

The Guest-side External Camera HAL queries buffer age against Guest
CLOCK_MONOTONIC, while the Host-side emulated virtio-media device
timestamp is generated in the Host's CLOCK_MONOTONIC domain. Because
the Host and Guest boot times differ (creating a monotonic offset of
~190 seconds on Cuttlefish), this clock domain mismatch causes the HAL
to reject all dequeued buffers as "too old" and drops 100% of frames,
yielding a "can't connect to camera" timeout in the camera app.

This change introduces three guest-side clock synchronization
operating modes to bridge the monotonic gap inside the guest driver:

* Mode 0 (Passthrough): Verbatim Host-domain timestamp passthrough
  (Backward-compatible default).
* Mode 1 (Guest Stamping): Statelessly overwrites incoming buffer
  timestamps with the Guest's live CLOCK_MONOTONIC (ktime_get_ns())
  upon dequeue. Foolproof and immune to VM suspend/resume.
* Mode 2 (Dynamic Translation): Dynamically translates Host timestamps
  onto the Guest timeline via real-time adaptive offset tracking:
  - Startup Calibration: Uses a 10-frame sliding window to isolate the
    absolute least-latency delivery baseline on cold start.
  - Continuous Bounded Slew: Asymmetrically tracks positive clock drift
    instantly (capped at 1ms/frame) while decaying downward via a /1024
    exponential filter to gracefully ignore high-latency host spikes.
  - Step Recalibration: Immediately snaps on supra-threshold jumps
    (default 1s, tunable via sysfs) to support seamless VM Resume, and
    flushes the +1us monotonicity clamp to avoid downstream throttling.
  - Resilience Strike Latching: Automatically falls back to Mode 0 on
    sustained bi-directional wild jitter strikes to guarantee UVC safety.

Both the active mode and recalibration threshold can be dynamically
toggled and inspected at runtime via writeable module parameters in
sysfs:

    # Toggle to Mode 2 (Dynamic Offset Translation)
    echo 2 > /sys/module/virtio_media/parameters/timestamp_mode

    # Configure recalibration threshold (e.g. 1 second default)
    echo 1000000000 > \
         /sys/module/virtio_media/parameters/recalib_threshold_ns

    # Confirm the current calibrated parameters
    cat /sys/module/virtio_media/parameters/timestamp_mode
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.

2 participants