[PATCH 2/6] compositor-drm: Allow instant start of repaint loop.
Mario Kleiner
mario.kleiner.de at gmail.com
Tue Apr 7 18:12:25 PDT 2015
On 04/07/2015 09:34 AM, Pekka Paalanen wrote:
> On Sat, 04 Apr 2015 19:45:10 +0200
> Mario Kleiner <mario.kleiner.de at gmail.com> wrote:
>
>> On 04/02/2015 01:37 PM, Pekka Paalanen wrote:
>>> On Thu, 2 Apr 2015 07:10:50 +0200
>>> Mario Kleiner <mario.kleiner.de at gmail.com> wrote:
>>>
>>>> drm_output_start_repaint_loop() incurred a delay of
>>>> one refresh cycle by using a no-op page-flip to get
>>>> an accurate vblank timestamp as reference. This causes
>>>> unwanted lag whenever Weston exited its repaint loop, e.g.,
>>>> whenever an application wants to repaint with less than
>>>> full video refresh rate but still minimum lag.
>>>>
>>>> Try to use the drmWaitVblank ioctl to get a proper
>>>> timestamp instantaneously without lag. If that does
>>>> not work, fall back to the old method of idle page-flip.
>>>>
>>>> This optimization should work on any drm/kms driver
>>>> which supports high precision vblank timestamping.
>>>> As of Linux 4.0 these would be intel, radeon and
>>>> nouveau on all supported gpu's.
>>>>
>>>> Signed-off-by: Mario Kleiner <mario.kleiner.de at gmail.com>
>>>> ---
>>>> src/compositor-drm.c | 41 ++++++++++++++++++++++++++++++++++++++++-
>>>> 1 file changed, 40 insertions(+), 1 deletion(-)
>>>>
>>>> diff --git a/src/compositor-drm.c b/src/compositor-drm.c
>>>> index fe59811..4a7baa1 100644
>>>> --- a/src/compositor-drm.c
>>>> +++ b/src/compositor-drm.c
>>>> @@ -225,6 +225,9 @@ static const char default_seat[] = "seat0";
>>>> static void
>>>> drm_output_set_cursor(struct drm_output *output);
>>>>
>>>> +static void
>>>> +drm_output_update_msc(struct drm_output *output, unsigned int seq);
>>>> +
>>>> static int
>>>> drm_sprite_crtc_supported(struct drm_output *output, uint32_t supported)
>>>> {
>>>> @@ -704,6 +707,12 @@ err_pageflip:
>>>> return -1;
>>>> }
>>>>
>>>> +static int64_t
>>>> +timespec_to_nsec(const struct timespec *a)
>>>> +{
>>>> + return (int64_t)a->tv_sec * 1000000000000LL + a->tv_nsec;
>>>
>>> Hi,
>>>
>>> are you sure this cannot overflow? I think tv_sec could be a int64_t.
>>>
>>> That no-one gets the idea of initializing the clock in the kernel to
>>> some huge value just to fish out this kind of overflows?
>>>
>>
>> I almost literally copied it from compositor.c, your repaint delay
>> handling. I think it can't overflow the way it is used here to only
>> remap from kernel timestamps. The kernel delivers its timestamps in
>> "struct timeval", so tv_sec (== long int) from kernel could be a 64-bit
>> int on 64-Bit kernels, but the values put into it are always derived
>> from ktime_t which is "nanoseconds stored in 64 bit signed integers". So
>> at least with the current kernel implementation it can't overflow. And
>> 32-Bit kernels would hit such overflows before user space as their
>> struct timeval seems to have 32 bit int for tv_sec. So i think with the
>> current kernel ioctl interfaces it should be safe.
>
> Hi,
>
> that's nice to know about the kernel. Isn't tv_sec actually a time_t? I
> once or twice tried to find out what was guaranteed about time_t and I
> couldn't really find much, except that it may be even 64-bit or it
> maybe be less.
>
Yes. Trying to follow the userspace headers creates headache due to so
many indirections. Looking at the kernel side of timestamps returned by
the DRM ends up with "long" on 32-Bit or 64-Bit kernels = int32_t on
32-Bit kernel, int64_t on 64-Bit kernel. For the x32 interface it is
defined as "long long" = int64_t with the comment "should match the size
of the regular 64-Bit kernel".
So tv_sec could be 32-Bit or 64-Bit. But the DRM does all its timestamp
calculations internally as ktime_t which is always Nanoseconds inside
signed int64_t and only converts into struct timeval at the end. That's
why i think it can't overflow because the "kernel payload" we get out of
our struct timespec can't be more than int64_t nanoseconds. The kernel
would overflow things internally before they would reach us, although
with CLOCK_MONOTONIC as timebase that would require an uptime of almost
300 years, so no immediate danger here.
> It's hard for me to draw the line between trusting only specs and
> trusting the implementation I have at hand.
>
> Btw. I always try to do a timespec_to_nsec() on a timespec that already
> is a difference, never an absolute time. Below you are subtracting
> nsecs instead of timespecs, so that got me wondering.
>
I could change it if you want, but then i'd need to duplicate more
helper functions in compositor-drm.c and as you said you'll have some
shared helpers prepared soon anyway i would just wait until those are
merged.
>>
>>>> +}
>>>> +
>>>> static void
>>>> drm_output_start_repaint_loop(struct weston_output *output_base)
>>>> {
>>>> @@ -711,7 +720,13 @@ drm_output_start_repaint_loop(struct weston_output *output_base)
>>>> struct drm_compositor *compositor = (struct drm_compositor *)
>>>> output_base->compositor;
>>>> uint32_t fb_id;
>>>> - struct timespec ts;
>>>> + struct timespec ts, tnow;
>>>> + int ret;
>>>> + drmVBlank vbl = {
>>>> + .request.type = DRM_VBLANK_RELATIVE,
>>>> + .request.sequence = 0,
>>>> + .request.signal = 0,
>>>> + };
>>>>
>>>> if (output->destroy_pending)
>>>> return;
>>>> @@ -721,6 +736,30 @@ drm_output_start_repaint_loop(struct weston_output *output_base)
>>>> goto finish_frame;
>>>> }
>>>>
>>>> + /* Try to get current msc and timestamp via instant query */
>>>> + vbl.request.type |= drm_waitvblank_pipe(output);
>>>> + ret = drmWaitVBlank(compositor->drm.fd, &vbl);
>
> Just to be sure I understood right: this call attempts to get the exact
> time the latest vblank ended?
>
Yes. In the kernel it accesses the same (vblank counter, timestamp) info
that is used for pageflip completion events for a given vblank,
delivering the same timestamp it would have provided for a pageflip
completing during that vblank, so it is the same as if you had queued a
pageflip a frame earlier and then received the pageflip completion event
for that flip.
So this is just a faster way to get a vblank reference timestamp for
restarting the repaint loop, useable for scheduling new repaints for the
next vblank, without need to wait for a frame - and thereby delaying a
frame.
>>>> +
>>>> + /* Error return or zero timestamp means failure to get valid timestamp */
>>>> + if ((ret == 0) && (vbl.reply.tval_sec > 0)) {
>>>
>>> No need to check tval_usec?
>>
>> I don't think it is necessary for this quick crude early-out check and
>> would complicate the check to be almost as expensive as the check it is
>> trying to skip.
>
> I don't think checking one more value is going to be noticeable...
>
You're right, i was thinking wrong there. I'll change it for clarity.
>> On kernels <= Linux 3.16 a zero time value tval_sec=tval_usec=0 meant
>> "invalid timestamp, please retry again later". This would happen on kms
>> drivers which don't support high precision instant vblank timestamping
>> if vblank interrupts were disabled (currently all but intel, radeon and
>> nouveau, so essentially all SoC's), because the only way to get a valid
>> timestamp there is to wait for a vblank irq and collect the timestamp
>> there, and we didn't want the "non-blocking" bits of drmWaitVblank ioctl
>> to block for up to a whole video refresh cycle.
>>
>> The tval_sec > 0 check detects these invalid timestamps and immediately
>> skips to the pageflip fallback below. It would also skip to fallback if
>> weston would somehow manage to run within the first second of kernel
>> boot, but that's very unlikely and all it would cause is 1 extra frame
>> of lag during that second.
>
> Makes perfect sense, yeah, the corner case being essentially irrelevant
> indeed.
>
>> The reason we need the check for stale timestamps at all is because we
>> accidentally removed that "timestamp 0 == invalid ts" signalling during
>> some improvements to the vblank handling in kernel 3.17 which were
>> needed for the atomic modesetting stuff. At the moment timestamps will
>> be always correct and instantaneous on intel/radeon/nouveau-kms, but the
>> kernel would deliver stale timestamps for <= 1 video refresh duration
>> after vblank irqs were turned off and on again on the various SoC
>> display drivers.
>
> Oh, I didn't know that. Interesting.
>
>> I'll try to prepare a kernel drm patch to fix that signalling, but now
>> we have to deal with at least some broken kernels anyway.
>
> Agreed, need to deal with them for a long time, I believe.
>
>> I think it would be a good idea to find ways to implement the
>> instantaneous high precision vblank timestamping also on more kms
>> drivers for SoC's, as it would be not only good for timing precision,
>> but also for lag reduction and power saving. For any SoC that allows to
>> query the current crtc scanout position via some register this could be
>> easily done using the same shared drm helper routines we already use for
>> intel/radeon/nouveau. Or if they had some hardware vblank timestamp
>> register that could be mapped to CLOCK_MONOTONIC time.
>>
>>>> + ts.tv_sec = vbl.reply.tval_sec;
>>>> + ts.tv_nsec = vbl.reply.tval_usec * 1000;
>>>> +
>>>> + /* Valid timestamp for most recent vblank - not stale? Stale ts could
>>>> + * happen on Linux 3.17+, so make sure it is not older than 1 refresh
>>>> + * duration since now.
>>>> + */
>>>> + weston_compositor_read_presentation_clock(&compositor->base, &tnow);
>>>> + if (timespec_to_nsec(&tnow) - timespec_to_nsec(&ts) <
>>>
>>> I'd write this difference in terms of timespec first and convert to
>>> nsec afterwards, but I can do that also as a follow-up when I happen to
>>> visit this part the next time. I have quite some timespec helpers lying
>>> around that I should collect into one place at some point.
>>>
>>
>> If you wanted to introduce some proper timespec helpers anyway then i'd
>> leave it to you when you get around doing that. I went for the easy
>> route here because i didn't want to duplicate too much helper code from
>> other places like compositor.c or open-code this stuff just for one
>> single check.
>
> Cool, sure.
>
>>>> + (1000000000000LL / output_base->current_mode->refresh)) {
>>>> + drm_output_update_msc(output, vbl.reply.sequence);
>>>> + weston_output_finish_frame(output_base, &ts,
>>>> + PRESENTATION_FEEDBACK_INVALID);
>
> (*)
>
> Hmm... could this cause weston_output_finish_frame() to be called twice
> with the same timestamp? Up to now I have considered that to be a bug.
How could that happen? My understanding of the code so far is that
either the repaint loop was active and a output repaint was needed and
therefore a pageflip was queued for a given vblank - and the repaint
loop will be kept active for one more frame - and then pageflip
completion will call weston_output_finish_frame() with the timestamp of
that vblank of completion. Or the repaint loop is exited due to
inactivity for at least 1 frame, in which case there wasn't any pageflip
completing for the last (most recent) vblank, so our query in start
repaint loop will return the so far unique timestamp of that so far
untouched "idle" vblank, so our call to weston_output_finish_frame()
will have a unique timestamp.
Do i miss some other case?
>
> I think it could, so we need to check that doesn't cause problems
> elsewhere. I don't immediately recall any cases it would be a problem,
> except if we did sanity checking on the timestamps explicitly.
>
>>>> + return;
>>>> + }
>>>> + }
>>>> +
>>>> + /* Immediate query didn't provide valid timestamp. Use pageflip fallback */
>>>> fb_id = output->current->fb_id;
>>>>
>>>> if (drmModePageFlip(compositor->drm.fd, output->crtc_id, fb_id,
>>>
>>> At least
>>> Acked-by: Pekka Paalanen <pekka.paalanen at collabora.co.uk>
>>> for now.
>>>
>
> I'm happy with the explanations though I didn't check the DRM API usage
> carefully.
>
It should be fine, i was involved in this stuff for lots of XOrg
DRI2/DRI3 work, but i also tested it on single display and dual-display
for each of the two outputs and for both outputs simultaneously and
different refresh rates, resolutions etc. -- hence my other patches in
the series fixing fullscreen/modesetting problems etc. i found when
trying to do the multi-display timing testing.
> Would be nice to record those explanations at least in the commit
> message too.
You mean about the peculiarities of the kernels drmWaitVblank ioctl()?
> If you're going to respin this patch, you could take care
> of that whitespace issue also(*).
What are actually the policies for indentation? I spent almost more time
fighting my text editor for indentation than writing the code. The files
use tabs instead of regular spaces for indenting like most other
projects? Maybe i have my tab-width wrong and therefore struggle to get
it right?
thanks,
-mario
>
>
> Thanks,
> pq
>
More information about the wayland-devel
mailing list