Skip to contentSkip to conversation

RFC: Change OML_sync_control UST to CLOCK_MONOTONIC

DISCUSSION Joakim Plate 5 participants 10 messages Jun 10, 2012 – Jun 14, 2012

lore
Share & tools
lore
#0 lore raw

Hi,

I'm currently trying to make use of OML_sync_control extension to schedule presentation of video frames in xbmc.

I've run into somewhat of a snag. It seem the spec doesn't specify what time the UST clock really is, nor can i find any mention of it elsewhere in docs.

Code wise it seem to be using do_gettimeofday(), which seems like a rather poor choice given that it can jump forward and back in time due to settimeofday calls.

We normally make use of clock_gettime(CLOCK_MONOTONIC) to timestamp display of video frames, so to avoid major changes I'd need a way to convert to gettimeofday (seem same as CLOCK_REALTIME).

Currently i'm trying:
struct timespec mon, rel;
clock_gettime(CLOCK_MONOTONIC, &mon);
clock_gettime(CLOCK_REALTIME , &rel);

  ticks += (rel.tv_sec  - mon.tv_sec)  * 1000000000;
  ticks += (rel.tv_nsec - mon.tv_nsec);

To convert between the two, but that is quite a hack both in the possibility of clock changes and scheduling errors.

Is there a better way, or perhaps the DRI code should use CLOCK_MONOTONIC in the first place?

Regards
/Joakim

#1 lore raw
Quoted 22 lines · Joakim Plate

Hi,

I'm currently trying to make use of OML_sync_control extension to schedule presentation of video frames in xbmc.

I've run into somewhat of a snag. It seem the spec doesn't specify what time the UST clock really is, nor can i find any mention of it elsewhere in docs.

Code wise it seem to be using do_gettimeofday(), which seems like a rather poor choice given that it can jump forward and back in time due to settimeofday calls.

We normally make use of clock_gettime(CLOCK_MONOTONIC) to timestamp display of video frames, so to avoid major changes I'd need a way to convert to gettimeofday (seem same as CLOCK_REALTIME).

Currently i'm trying:
struct timespec mon, rel;
clock_gettime(CLOCK_MONOTONIC, &mon);
clock_gettime(CLOCK_REALTIME , &rel);

Quoted 8 lines
  ticks += (rel.tv_sec  - mon.tv_sec)  * 1000000000;
  ticks += (rel.tv_nsec - mon.tv_nsec);

To convert between the two, but that is quite a hack both in the possibility of clock changes and scheduling errors.

Is there a better way, or perhaps the DRI code should use CLOCK_MONOTONIC in the first place?

From the GLX_OML_sync_control spec:

The Unadjusted System Time (or UST) is a 64-bit monotonically increasing counter [...]

So, without having thought a lot about potential ramifications, I'm inclined to say that CLOCK_MONOTONIC should indeed be used.

Signature · 6 lines
Earthling Michel Dänzer           |                   http://www.amd.com
Libre software enthusiast         |          Debian, X and DRI developer
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
#2 lore raw
Quoted 32 lines · Michel Dänzer
Quoted 25 lines · Joakim Plate

Hi,

I'm currently trying to make use of OML_sync_control extension to schedule presentation of video frames in xbmc.

I've run into somewhat of a snag. It seem the spec doesn't specify what time the UST clock really is, nor can i find any mention of it elsewhere in docs.

Code wise it seem to be using do_gettimeofday(), which seems like a rather poor choice given that it can jump forward and back in time due to settimeofday calls.

We normally make use of clock_gettime(CLOCK_MONOTONIC) to timestamp display of video frames, so to avoid major changes I'd need a way to convert to gettimeofday (seem same as CLOCK_REALTIME).

Currently i'm trying:

   struct timespec mon, rel;
   clock_gettime(CLOCK_MONOTONIC,&mon);
   clock_gettime(CLOCK_REALTIME ,&rel);

   ticks += (rel.tv_sec  - mon.tv_sec)  * 1000000000;
   ticks += (rel.tv_nsec - mon.tv_nsec);
Quoted 5 lines

To convert between the two, but that is quite a hack both in the possibility of clock changes and scheduling errors.

Is there a better way, or perhaps the DRI code should use CLOCK_MONOTONIC in the first place?

Quoted 7 lines

From the GLX_OML_sync_control spec:

The Unadjusted System Time (or UST) is a 64-bit monotonically increasing counter [...]

So, without having thought a lot about potential ramifications, I'm inclined to say that CLOCK_MONOTONIC should indeed be used.

Maybe. Isn't CLOCK_MONOTONIC per-process? UST is supposed to be relatively consistent across the system. Some relevant was elided above:

"The Unadjusted System Time (or UST) is a 64-bit monotonically increasing counter that is available throughout the system. UST is not a resource that is controlled by OpenGL, so it is not defined further as part of this extension."

do_gettimeofday is definitely the wrong choice, but it was the best thing that I knew to use back in 2002(ish) when I wrote that code. I'm just not sure CLOCK_MONOTONIC is quite right either.

What's behind door #3? :)
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

#3 lore raw
Quoted 35 lines · Ian Romanick
Quoted 34 lines · Michel Dänzer
Quoted 25 lines · Joakim Plate

Hi,

I'm currently trying to make use of OML_sync_control extension to schedule presentation of video frames in xbmc.

I've run into somewhat of a snag. It seem the spec doesn't specify what time the UST clock really is, nor can i find any mention of it elsewhere in docs.

Code wise it seem to be using do_gettimeofday(), which seems like a rather poor choice given that it can jump forward and back in time due to settimeofday calls.

We normally make use of clock_gettime(CLOCK_MONOTONIC) to timestamp display of video frames, so to avoid major changes I'd need a way to convert to gettimeofday (seem same as CLOCK_REALTIME).

Currently i'm trying:

   struct timespec mon, rel;
   clock_gettime(CLOCK_MONOTONIC,&mon);
   clock_gettime(CLOCK_REALTIME ,&rel);

   ticks += (rel.tv_sec  - mon.tv_sec)  * 1000000000;
   ticks += (rel.tv_nsec - mon.tv_nsec);
Quoted 5 lines

To convert between the two, but that is quite a hack both in the possibility of clock changes and scheduling errors.

Is there a better way, or perhaps the DRI code should use CLOCK_MONOTONIC in the first place?

From the GLX_OML_sync_control spec:

Quoted 7 lines
Quoted 5 lines

The Unadjusted System Time (or UST) is a 64-bit monotonically increasing counter [...]

So, without having thought a lot about potential ramifications, I'm inclined to say that CLOCK_MONOTONIC should indeed be used.

Maybe. Isn't CLOCK_MONOTONIC per-process?

According to the test code below, it's not here. While the clock_gettime manpage doesn't explicitly state that it's system-wide, it does mention it being affect by adjtime and NTP, which kind of implies it is?

#include <stdio.h>
#include <string.h>
#include <time.h>

int main(void)
{
struct timespec tp;
int ret;

  memset(&tp, 0, sizeof(tp));
  ret = clock_gettime(CLOCK_MONOTONIC, &tp);

printf("clock_gettime(CLOCK_MONOTONIC) returned %d, timestamp %lu.%09lu\n",

	 ret, tp.tv_sec, tp.tv_nsec);

return ret;
}

Signature · 6 lines
Earthling Michel Dänzer           |                   http://www.amd.com
Libre software enthusiast         |          Debian, X and DRI developer
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
#4 lore raw
In reply to Michel Dänzer
Quoted 6 lines · Michel Dänzer <michel <at> daenzer.net> writes:
Quoted 6 lines
Quoted 6 lines

From the GLX_OML_sync_control spec:

The Unadjusted System Time (or UST) is a 64-bit monotonically increasing counter [...]

From what I can tell, it should be using: ktime_to_ns(ktime_get()) / 1000. Only issue is that changing it will break any app relying on it being REALTIME clock.

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

#5 lore raw
Quoted 12 lines · Joakim Plate
Quoted 6 lines · Michel Dänzer <michel <at> daenzer.net> writes:
Quoted 6 lines
Quoted 6 lines

 From the GLX_OML_sync_control spec:

         The Unadjusted System Time (or UST) is a 64-bit monotonically increasing counter [...]

From what I can tell, it should be using: ktime_to_ns(ktime_get()) / 1000. Only issue is that changing it will break any app relying on it being REALTIME clock.

App that rely on it being anything special are badly broken and i don't think there is any such app. The specification strongly stress that app should make no assumption about it.

Cheers,
Jerome

#6 lore raw
Quoted 3 lines
Quoted 3 lines

From what I can tell, it should be using: ktime_to_ns(ktime_get()) / 1000.

Only

Quoted 1 line
Quoted 1 line

issue is that changing it will break any app relying on it being REALTIME

clock.

Quoted 6 lines
Quoted 1 line

App that rely on it being anything special are badly broken and i don't think there is any such app. The specification strongly stress that app should make no assumption about it.

While that may be true... Since there is no other API for getting this UST clock, it's somewhat limited in use. Even if i know vsync happened at time X, if don't know what time it is "now" how can i make use of it?

Spec says: "The Unadjusted System Time (or UST)
is a 64-bit monotonically increasing counter that is available throughout the system."

If across the system, the only API to get to this value is through GLX api, it's rather hard to make use of.

For example syncing audio to vsync. One need to sync audio output written to audio renderer now, with this clock.

Also regarding relying on current behavior... Even if this change is made now, there will be a lot of system with the old behaviour. So knowning if the change has been made in a system is crucial for supporting both / not enabling when feature is unreliable.

/Joakim

#7 lore raw
Quoted 27 lines · Joakim Plate
Quoted 3 lines
Quoted 3 lines

From what I can tell, it should be using: ktime_to_ns(ktime_get()) / 1000.

Only

Quoted 1 line
Quoted 1 line

issue is that changing it will break any app relying on it being REALTIME

clock.

Quoted 6 lines
Quoted 1 line

App that rely on it being anything special are badly broken and i don't think there is any such app. The specification strongly stress that app should make no assumption about it.

While that may be true... Since there is no other API for getting this UST clock, it's somewhat limited in use. Even if i know vsync happened at time X, if don't know what time it is "now" how can i make use of it?

Spec says: "The Unadjusted System Time (or UST)
   is a 64-bit monotonically increasing counter that is available throughout the system."

If across the system, the only API to get to this value is through GLX api, it's rather hard to make use of.

For example syncing audio to vsync. One need to sync audio output written to audio renderer now, with this clock.

Quoted 7 lines

Also regarding relying on current behavior... Even if this change is made now, there will be a lot of system with the old behaviour. So knowning if the change has been made in a system is crucial for supporting both / not enabling when feature is unreliable.

/Joakim

This extension is not for predicting when is the next vblank and it's wrong to try to use it for that. My understanding is that you should use this extension for instance to show N video frame per second, based on the number of frame you play per second you can synchronize sounds output.

Cheers,
Jerome

#8 lore raw
Quoted 36 lines · Jerome Glisse
Quoted 27 lines · Joakim Plate
Quoted 3 lines
Quoted 3 lines

From what I can tell, it should be using: ktime_to_ns(ktime_get()) / 1000.

Only

Quoted 1 line
Quoted 1 line

issue is that changing it will break any app relying on it being REALTIME

clock.

Quoted 6 lines
Quoted 1 line

App that rely on it being anything special are badly broken and i don't think there is any such app. The specification strongly stress that app should make no assumption about it.

While that may be true... Since there is no other API for getting this UST clock, it's somewhat limited in use. Even if i know vsync happened at time X, if don't know what time it is "now" how can i make use of it?

Spec says: "The Unadjusted System Time (or UST)
   is a 64-bit monotonically increasing counter that is available throughout the system."

If across the system, the only API to get to this value is through GLX api, it's rather hard to make use of.

For example syncing audio to vsync. One need to sync audio output written to audio renderer now, with this clock.

Quoted 7 lines

Also regarding relying on current behavior... Even if this change is made now, there will be a lot of system with the old behaviour. So knowning if the change has been made in a system is crucial for supporting both / not enabling when feature is unreliable.

/Joakim

Quoted 8 lines

This extension is not for predicting when is the next vblank and it's wrong to try to use it for that. My understanding is that you should use this extension for instance to show N video frame per second, based on the number of frame you play per second you can synchronize sounds output.

Cheers,
Jerome

Note that if you really think that you need something like GLX_OML_sync_control but with UST being some sensible value from the system that you can query by other mean we could easily add a new mesa extension GLX_MESA_sync_control that properly defines ust and keep everything else the same. Then we can fix the kernel and expose this extension only on fixed kernel. Of course this would probably only be available on open source driver but with a bit of lobying nvidia & amd might pick the extension in their closed source driver.

Cheers,
Jerome