Bug 18121 - monotonic clock checks are not fully portable
Summary: monotonic clock checks are not fully portable
Status: RESOLVED MOVED
Alias: None
Product: dbus
Classification: Unclassified
Component: core (show other bugs)
Version: 1.5
Hardware: All All
: low minor
Assignee: D-Bus Maintainers
QA Contact: D-Bus Maintainers
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-10-18 20:22 UTC by Keith Mok
Modified: 2018-10-12 21:04 UTC (History)
2 users (show)

See Also:
i915 platform:
i915 features:


Attachments
Patch for using monotonic timeout for pthread (2.66 KB, patch)
2008-10-18 20:22 UTC, Keith Mok
Details | Splinter Review
updated CLOCK_MONOTONIC patch (3.69 KB, patch)
2008-11-10 09:40 UTC, Colin Walters
Details | Splinter Review
switch to monotonic clock (4.39 KB, patch)
2009-04-28 02:47 UTC, Janne Karhunen
Details | Splinter Review
take 3 of monotonic clock patch (5.30 KB, patch)
2009-05-12 02:23 UTC, Janne Karhunen
Details | Splinter Review

Description Keith Mok 2008-10-18 20:22:59 UTC
Created attachment 19739 [details] [review]
Patch for using monotonic timeout for pthread

Pthread timeout currently based on system clock. The patch attached change pthread to use monotonic timer if available.
Comment 1 Colin Walters 2008-11-10 09:40:56 UTC
Created attachment 20192 [details] [review]
updated CLOCK_MONOTONIC patch

This patch is a rewrite of the first; we use the POSIX clock_* APIs, and both detect bits in configure and do a runtime check (exactly once, instead of every time a cond var is created).
Comment 2 Janne Karhunen 2009-04-27 22:15:02 UTC
Humm; what's the status of this patch, is it possibly going in? That being asked, _dbus_get_current_time seems affected as well, might make sense to patch that as well. Care if I rework that patch a little and propose a bit wider one?
Comment 3 Janne Karhunen 2009-04-28 02:47:59 UTC
Created attachment 25224 [details] [review]
switch to monotonic clock

Another attempt to use monotonic time source if available.
Comment 4 Janne Karhunen 2009-05-12 02:23:25 UTC
Created attachment 25787 [details] [review]
take 3 of monotonic clock patch

Quickly tested switch to monotonic timesource. Also fixes cases where transactions are incorrectly declared as 'timed out' when system clock is being set during transaction. Vital for mobile devices..
Comment 5 Colin Walters 2009-07-10 19:12:06 UTC
Hmm, no changing dbus_get_current_time globally is too big of a hammer.  In some cases it'd be OK, but at least one usage in dbus-keyring.c looks like it expects it to be wall-clock.

Other uses of dbus_get_current_time are broken though - I'm looking in particular at _dbus_connection_block_pending_call.  One thing is unclear to me - is it defined whether CLOCK_MONOTONIC increments when the OS is suspended, or not?  It seems to me one could meet the MONOTONIC guarantee by either 

1) Taking a snapshot of time at boot, and ignoring any backwards changes
2) Some division of jiffies

Reading the Linux source right now, it's looking to me like it's #1.  And we actually need something closer to 2), i.e. we don't want to time out method calls just because you suspended.

But...that could be a separate bug.  If the pthread timeouts are what are biting you we can get that in now.

Comment 6 Colin Walters 2009-07-10 19:18:00 UTC
Also, the Makefile.am changes are wrong, we should only link to -lrt if we need to, otherwise people running NetBSD 0.3 on their SPARCstation or some other random crap will probably complain.


Comment 7 Colin Walters 2009-07-10 19:18:26 UTC
Related to this bug:

http://bugzilla.gnome.org/show_bug.cgi?id=540545
Comment 8 Colin Walters 2009-07-10 19:57:48 UTC
commit ae24bb35e2ee3ecde990f55852982b573754ec43
Author: Colin Walters <walters@verbum.org>
Date:   Fri Jul 10 22:27:55 2009 -0400

    Bug 18121 - Use a monotonic clock for pthread timeouts
    
    Patch based on one from Keith Mok <ek9852@gmail.com>, some
    followup work from Janne Karhunen <Janne.Karhunen@gmail.com>.
    
    We don't want condition variable timeouts to be affected by the system clock.
    Use the POSIX CLOCK_MONOTONIC if available.

Comment 9 Thiago Macieira 2009-08-17 07:00:24 UTC
Comment on attachment 25787 [details] [review]
take 3 of monotonic clock patch

The removal of the comma in AC_CHECK_FUNC seems wrong: we're now running AC_CHECK_LIB as a result of *finding* pthread_cond_timedwait. (as opposed to not finding it)

Also, the code should check for _POSIX_MONOTONIC_CLOCK set to 0: if that's the case, we need to call sysconf(_SC_MONOTONIC_CLOCK) to find out if the system does support monotonic clocks. If that macro is set to 0, it means the system interfaces are present, but the system may not support monotonic clocks at runtime.

The code in dbus-sysdeps-pthread.c seems to have some kind of protection, but dbus-sysdeps-unix.c is unprotected.

I also agree that clock_gettime() should be searched in libc first, before librt.
Comment 10 Simon McVittie 2012-01-23 06:00:53 UTC
(In reply to comment #9)
> The removal of the comma in AC_CHECK_FUNC seems wrong: we're now running
> AC_CHECK_LIB as a result of *finding* pthread_cond_timedwait. (as opposed to
> not finding it)
> 
> Also, the code should check for _POSIX_MONOTONIC_CLOCK set to 0: if that's the
> case, we need to call sysconf(_SC_MONOTONIC_CLOCK) to find out if the system
> does support monotonic clocks. If that macro is set to 0, it means the system
> interfaces are present, but the system may not support monotonic clocks at
> runtime.
> 
> The code in dbus-sysdeps-pthread.c seems to have some kind of protection, but
> dbus-sysdeps-unix.c is unprotected.

Any chance you could put together a patch for this, if it affects systems you're interested in?
Comment 11 Simon McVittie 2012-03-12 09:28:22 UTC
(In reply to comment #10)
> (In reply to comment #9)
> > The removal of the comma in AC_CHECK_FUNC seems wrong: we're now running
> > AC_CHECK_LIB as a result of *finding* pthread_cond_timedwait. (as opposed to
> > not finding it)

Fixed over on Bug #47239, FWIW.

> > Also, the code should check for _POSIX_MONOTONIC_CLOCK set to 0: if that's the
> > case, we need to call sysconf(_SC_MONOTONIC_CLOCK) to find out if the system
> > does support monotonic clocks.

Not done, patches welcome.
Comment 12 Thiago Macieira 2012-03-12 09:48:35 UTC
Comment on attachment 20192 [details] [review]
updated CLOCK_MONOTONIC patch

Review of attachment 20192 [details] [review]:
-----------------------------------------------------------------

::: configure.in
@@ +893,2 @@
>                                                      [THREAD_LIBS="-lpthread"])])
> +save_libs="$LIBS"

This check is unnecessary. There's a POSIX macro for this already.

::: dbus/dbus-sysdeps-pthread.c
@@ +200,5 @@
>    if (pcond == NULL)
>      return NULL;
>  
> +  pthread_condattr_init (&attr);
> +#ifdef HAVE_CLOCK_MONOTONIC

#if _POSIX_MONOTONIC_CLOCK >= 0

@@ +265,5 @@
>    
>    _dbus_assert (pmutex->count > 0);
>    _dbus_assert (pthread_equal (pmutex->holder, pthread_self ()));  
> +
> +#ifdef HAVE_CLOCK_MONOTONIC

Idem: #if _POSIX_MONOTONIC_CLOCK >= 0

@@ +348,5 @@
>  
> +static void
> +check_monotonic_clock (void)
> +{
> +#ifdef HAVE_CLOCK_MONOTONIC

Replace this function body with:

#if _POSIX_MONOTONIC_CLOCK == 0
  if (sysconf(_SC_MONOTONIC_CLOCK)) <= 0)
    return;
#endif
#if _POSIX_MONOTONIC_CLOCK >= 0
  have_monotonic_clock = TRUE;
#endif
Comment 13 Simon McVittie 2012-03-12 11:17:09 UTC
I believe the patches here have already partially been applied, or something pretty similar has been applied, or something; see Comment #8. So reviewing them isn't much use - if there are flaws in what's currently in libdbus, please fix that instead and attach a patch, and I'll be happy to review it.

To avoid conflicts in configure.ac, please consider reviewing Bug #47239 first.

> #if _POSIX_MONOTONIC_CLOCK >= 0

That does sound like a better way to do the check.
Comment 14 Simon McVittie 2012-04-12 01:07:40 UTC
(In reply to comment #5)
> Hmm, no changing dbus_get_current_time globally is too big of a hammer.  In
> some cases it'd be OK, but at least one usage in dbus-keyring.c looks like it
> expects it to be wall-clock.

This has in fact been broken since the patches from this bug were applied. Bug #48580.
Comment 15 Simon McVittie 2012-06-05 03:40:56 UTC
Retitling to describe what's left, and removing the patch tag (patches fixing the original bug have already been applied).
Comment 16 GitLab Migration User 2018-10-12 21:04:54 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to freedesktop.org's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.freedesktop.org/dbus/dbus/issues/9.


Use of freedesktop.org services, including Bugzilla, is subject to our Code of Conduct. How we collect and use information is described in our Privacy Policy.