Skip to content

Commit 7efb833

Browse files
committed
Issue #22287: On UNIX, _PyTime_gettimeofday() now uses
clock_gettime(CLOCK_REALTIME) if available. As a side effect, Python now depends on the librt library on Solaris and on Linux (only with glibc older than 2.17).
1 parent 0dc10e0 commit 7efb833

File tree

5 files changed

+48
-35
lines changed

5 files changed

+48
-35
lines changed

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ Core and Builtins
124124
Library
125125
-------
126126

127+
- Issue #22287: On UNIX, _PyTime_gettimeofday() now uses
128+
clock_gettime(CLOCK_REALTIME) if available. As a side effect, Python now
129+
depends on the librt library on Solaris and on Linux (only with glibc older
130+
than 2.17).
131+
127132
- Issue #22182: Use e.args to unpack exceptions correctly in
128133
distutils.file_util.move_file. Patch by Claudiu Popa.
129134

Modules/timemodule.c

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,28 +1535,6 @@ static PyObject*
15351535
floattime(_Py_clock_info_t *info)
15361536
{
15371537
_PyTime_timeval t;
1538-
#ifdef HAVE_CLOCK_GETTIME
1539-
struct timespec tp;
1540-
int ret;
1541-
1542-
/* _PyTime_gettimeofday() does not use clock_gettime()
1543-
because it would require to link Python to the rt (real-time)
1544-
library, at least on Linux */
1545-
ret = clock_gettime(CLOCK_REALTIME, &tp);
1546-
if (ret == 0) {
1547-
if (info) {
1548-
struct timespec res;
1549-
info->implementation = "clock_gettime(CLOCK_REALTIME)";
1550-
info->monotonic = 0;
1551-
info->adjustable = 1;
1552-
if (clock_getres(CLOCK_REALTIME, &res) == 0)
1553-
info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
1554-
else
1555-
info->resolution = 1e-9;
1556-
}
1557-
return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9);
1558-
}
1559-
#endif
15601538
_PyTime_gettimeofday_info(&t, info);
15611539
return PyFloat_FromDouble((double)t.tv_sec + t.tv_usec * 1e-6);
15621540
}

Python/pytime.c

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,39 @@ pygettimeofday(_PyTime_timeval *tp, _Py_clock_info_t *info)
5656
fail, so we fall back on ftime() or time().
5757
Note: clock resolution does not imply clock accuracy! */
5858

59-
#ifdef HAVE_GETTIMEOFDAY
59+
#if (defined(HAVE_CLOCK_GETTIME) || defined(HAVE_GETTIMEOFDAY) \
60+
|| defined(HAVE_FTIME))
6061
int err;
62+
#endif
63+
#ifdef HAVE_CLOCK_GETTIME
64+
struct timespec ts;
65+
#endif
66+
#ifdef HAVE_FTIME
67+
struct timeb t;
68+
#endif
69+
70+
/* test clock_gettime(CLOCK_REALTIME) */
71+
#ifdef HAVE_CLOCK_GETTIME
72+
err = clock_gettime(CLOCK_REALTIME, &ts);
73+
if (err == 0) {
74+
if (info) {
75+
struct timespec res;
76+
info->implementation = "clock_gettime(CLOCK_REALTIME)";
77+
info->monotonic = 0;
78+
info->adjustable = 1;
79+
if (clock_getres(CLOCK_REALTIME, &res) == 0)
80+
info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
81+
else
82+
info->resolution = 1e-9;
83+
}
84+
tp->tv_sec = ts.tv_sec;
85+
tp->tv_usec = ts.tv_nsec / 1000;
86+
return;
87+
}
88+
#endif
89+
90+
/* test gettimeofday() */
91+
#ifdef HAVE_GETTIMEOFDAY
6192
#ifdef GETTIMEOFDAY_NO_TZ
6293
err = gettimeofday(tp);
6394
#else
@@ -74,18 +105,15 @@ pygettimeofday(_PyTime_timeval *tp, _Py_clock_info_t *info)
74105
}
75106
#endif /* HAVE_GETTIMEOFDAY */
76107

77-
#if defined(HAVE_FTIME)
78-
{
79-
struct timeb t;
80-
ftime(&t);
81-
tp->tv_sec = t.time;
82-
tp->tv_usec = t.millitm * 1000;
83-
if (info) {
84-
info->implementation = "ftime()";
85-
info->resolution = 1e-3;
86-
info->monotonic = 0;
87-
info->adjustable = 1;
88-
}
108+
#ifdef HAVE_FTIME
109+
ftime(&t);
110+
tp->tv_sec = t.time;
111+
tp->tv_usec = t.millitm * 1000;
112+
if (info) {
113+
info->implementation = "ftime()";
114+
info->resolution = 1e-3;
115+
info->monotonic = 0;
116+
info->adjustable = 1;
89117
}
90118
#else /* !HAVE_FTIME */
91119
tp->tv_sec = time(NULL);

configure

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11904,6 +11904,7 @@ fi
1190411904
$as_echo "$ac_cv_lib_rt_clock_gettime" >&6; }
1190511905
if test "x$ac_cv_lib_rt_clock_gettime" = xyes; then :
1190611906

11907+
LIBS="$LIBS -lrt"
1190711908
$as_echo "#define HAVE_CLOCK_GETTIME 1" >>confdefs.h
1190811909

1190911910

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3319,6 +3319,7 @@ AC_CHECK_FUNCS(gettimeofday,
33193319

33203320
AC_CHECK_FUNCS(clock_gettime, [], [
33213321
AC_CHECK_LIB(rt, clock_gettime, [
3322+
LIBS="$LIBS -lrt"
33223323
AC_DEFINE(HAVE_CLOCK_GETTIME, 1)
33233324
AC_DEFINE(TIMEMODULE_LIB, [rt],
33243325
[Library needed by timemodule.c: librt may be needed for clock_gettime()])

0 commit comments

Comments
 (0)