diff -r 9db3bedcd86c Doc/library/time.rst --- a/Doc/library/time.rst Fri Apr 13 20:51:20 2012 +0100 +++ b/Doc/library/time.rst Fri Apr 13 23:00:11 2012 +0200 @@ -220,6 +220,33 @@ The module defines the following functio Nonzero if a DST timezone is defined. +.. function:: get_clock_info(name) + + Get information of the specified clock. Supported clocks: + + * ``'clock'``: :func:`time.clock` + * ``'monotonic'``: :func:`time.monotonic` + * ``'perf_counter'``: :func:`time.perf_counter` + * ``'process_time'``: :func:`time.process_time` + * ``'time'``: :func:`time.time` + + Return a dictionary with the following keys: + + * Mandatory keys + + * ``'function'`` (:class:`str`): name of the underlying C function used + to get the clock value + * ``'is_monotonic'`` (:class:`bool`): ``True`` if the clock cannot go + backward, ``False`` otherwise + * ``'resolution'`` (:class:`float`): Resolution of the clock in seconds + + * Optional keys + + * ``'precision'`` (:class:`float`): Precision of the clock in seconds + * ``'is_adjusted'`` (:class:`bool`): ``True`` if the clock can be + adjusted (e.g. by a NTP daemon) + + .. function:: gmtime([secs]) Convert a time expressed in seconds since the epoch to a :class:`struct_time` in @@ -249,20 +276,35 @@ The module defines the following functio The earliest date for which it can generate a time is platform-dependent. -.. function:: steady(strict=False) +.. function:: monotonic() - .. index:: - single: benchmarking + Monotonic clock, cannot go backward. Return the current time as a floating + point number expressed in seconds. The reference point of the returned + value is undefined so only the difference of consecutive calls is valid. - Return the current time as a floating point number expressed in seconds. - This clock advances at a steady rate relative to real time and it may not be - adjusted. The reference point of the returned value is undefined so only the - difference of consecutive calls is valid. + Availability: Windows, Mac OS X, Unix. - If available, a monotonic clock is used. By default, - the function falls back to another clock if the monotonic clock failed or is - not available. If *strict* is True, raise an :exc:`OSError` on error or - :exc:`NotImplementedError` if no monotonic clock is available. + .. versionadded:: 3.3 + + +.. function:: perf_counter() + + Performance counter for benchmarking. It is monotonic, i.e. cannot go + backward. It does include time elapsed during sleep. The reference point of + the returned value is undefined, so that only the difference between the + results of consecutive calls is valid and is number of seconds. + + Availability: Windows, Mac OS X, Unix. + + .. versionadded:: 3.3 + + +.. function:: process_time() + + Process time for profiling: sum of the kernel and user-space CPU time. + It does not include time elapsed during sleep. The reference point of the + returned value is undefined, so that only the difference between the + results of consecutive calls is valid. .. versionadded:: 3.3 diff -r 9db3bedcd86c Include/pytime.h --- a/Include/pytime.h Fri Apr 13 20:51:20 2012 +0100 +++ b/Include/pytime.h Fri Apr 13 23:00:11 2012 +0200 @@ -22,11 +22,28 @@ typedef struct { } _PyTime_timeval; #endif +/* Structure used by time.get_clock_info() */ +typedef struct { + /* mandatory */ + const char *function; + double resolution; + int is_monotonic; + /* optional */ + double precision; /* -1.0 if unknown */ + int is_adjusted; /* -1 if unknown */ +} _Py_clock_info_t; + /* Similar to POSIX gettimeofday but cannot fail. If system gettimeofday * fails or is not available, fall back to lower resolution clocks. */ PyAPI_FUNC(void) _PyTime_gettimeofday(_PyTime_timeval *tp); +/* Similar to _PyTime_gettimeofday() but retrieve also information on the + * clock used to get the current time. */ +PyAPI_FUNC(void) _PyTime_gettimeofday_info( + _PyTime_timeval *tp, + _Py_clock_info_t *info); + #define _PyTime_ADD_SECONDS(tv, interval) \ do { \ tv.tv_usec += (long) (((long) interval - interval) * 1000000); \ diff -r 9db3bedcd86c Lib/queue.py --- a/Lib/queue.py Fri Apr 13 20:51:20 2012 +0100 +++ b/Lib/queue.py Fri Apr 13 23:00:11 2012 +0200 @@ -6,7 +6,10 @@ except ImportError: import dummy_threading as threading from collections import deque from heapq import heappush, heappop -from time import steady as time +try: + from time import monotonic as time +except ImportError: + from time import time __all__ = ['Empty', 'Full', 'Queue', 'PriorityQueue', 'LifoQueue'] diff -r 9db3bedcd86c Lib/test/test_time.py --- a/Lib/test/test_time.py Fri Apr 13 20:51:20 2012 +0100 +++ b/Lib/test/test_time.py Fri Apr 13 23:00:11 2012 +0200 @@ -342,23 +342,43 @@ class TimeTestCase(unittest.TestCase): pass self.assertEqual(time.strftime('%Z', tt), tzname) - def test_steady(self): - t1 = time.steady() + @unittest.skipUnless(hasattr(time, 'monotonic'), + 'need time.monotonic') + def test_monotonic(self): + t1 = time.monotonic() time.sleep(0.1) - t2 = time.steady() + t2 = time.monotonic() dt = t2 - t1 - # may fail if the system clock was changed self.assertGreater(t2, t1) self.assertAlmostEqual(dt, 0.1, delta=0.2) - def test_steady_strict(self): + info = time.get_clock_info('monotonic') + self.assertEqual(info['is_monotonic'], True) + + def test_perf_counter(self): + time.perf_counter() + + def test_process_time(self): + start = time.process_time() + time.sleep(0.1) + stop = time.process_time() + self.assertLess(stop - start, 0.01) + + @unittest.skipUnless(hasattr(time, 'monotonic'), + 'need time.monotonic') + @unittest.skipUnless(hasattr(time, 'clock_settime'), + 'need time.clock_settime') + def test_monotonic_settime(self): + t1 = time.monotonic() + realtime = time.clock_gettime(time.CLOCK_REALTIME) + # jump backward with an offset of 1 hour try: - t1 = time.steady(strict=True) - except OSError as err: - self.skipTest("the monotonic clock failed: %s" % err) - except NotImplementedError: - self.skipTest("no monotonic clock available") - t2 = time.steady(strict=True) + time.clock_settime(time.CLOCK_REALTIME, realtime - 3600) + except PermissionError as err: + self.skipTest(err) + t2 = time.monotonic() + time.clock_settime(time.CLOCK_REALTIME, realtime) + # monotonic must not be affected by system clock updates self.assertGreaterEqual(t2, t1) def test_localtime_failure(self): @@ -378,6 +398,32 @@ class TimeTestCase(unittest.TestCase): self.assertRaises(OSError, time.localtime, invalid_time_t) self.assertRaises(OSError, time.ctime, invalid_time_t) + def test_get_clock_info(self): + clocks = ['clock', 'perf_counter', 'process_time', 'time'] + if hasattr(time, 'monotonic'): + clocks.append('monotonic') + + for name in clocks: + info = time.get_clock_info(name) + self.assertIsInstance(info, dict) + self.assertIsInstance(info['implementation'], str) + self.assertNotEqual(info['implementation'], '') + self.assertIsInstance(info['is_monotonic'], bool) + self.assertIsInstance(info['resolution'], float) + # 0 < resolution <= 1.0 + self.assertGreater(info['resolution'], 0) + self.assertLessEqual(info['resolution'], 1) + if 'accuracy' in info: + self.assertIsInstance(info['accuracy'], float) + # 0 < accuracy <= 1.0 + self.assertGreater(info['accuracy'], 0) + self.assertLessEqual(info['accuracy'], 1) + if 'is_adjusted' in info: + self.assertIsInstance(info['is_adjusted'], bool) + + self.assertRaises(ValueError, time.get_clock_info, 'xxx') + + class TestLocale(unittest.TestCase): def setUp(self): self.oldloc = locale.setlocale(locale.LC_ALL) diff -r 9db3bedcd86c Lib/threading.py --- a/Lib/threading.py Fri Apr 13 20:51:20 2012 +0100 +++ b/Lib/threading.py Fri Apr 13 23:00:11 2012 +0200 @@ -3,7 +3,11 @@ import sys as _sys import _thread -from time import steady as _time, sleep as _sleep +from time import sleep as _sleep +try: + from time import monotonic as _time +except ImportError: + from time import time as _time from traceback import format_exc as _format_exc from _weakrefset import WeakSet diff -r 9db3bedcd86c Modules/timemodule.c --- a/Modules/timemodule.c Fri Apr 13 20:51:20 2012 +0100 +++ b/Modules/timemodule.c Fri Apr 13 23:00:11 2012 +0200 @@ -4,9 +4,17 @@ #include +#ifdef HAVE_SYS_TIMES_H +#include +#endif + #ifdef HAVE_SYS_TYPES_H #include -#endif /* HAVE_SYS_TYPES_H */ +#endif + +#if defined(HAVE_SYS_RESOURCE_H) +#include +#endif #ifdef QUICKWIN #include @@ -45,12 +53,16 @@ /* Forward declarations */ static int floatsleep(double); -static PyObject* floattime(void); +static PyObject* floattime(_Py_clock_info_t *info); + +#ifdef MS_WINDOWS +static OSVERSIONINFOEX winver; +#endif static PyObject * time_time(PyObject *self, PyObject *unused) { - return floattime(); + return floattime(NULL); } PyDoc_STRVAR(time_doc, @@ -70,7 +82,7 @@ Fractions of a second may be present if #endif static PyObject * -pyclock(void) +floatclock(_Py_clock_info_t *info) { clock_t value; value = clock(); @@ -80,15 +92,23 @@ pyclock(void) "or its value cannot be represented"); return NULL; } + if (info) { + info->function = "clock()"; + info->resolution = 1.0 / (double)CLOCKS_PER_SEC; + info->precision = info->resolution; + info->is_monotonic = 1; + info->is_adjusted = 0; + } return PyFloat_FromDouble((double)value / CLOCKS_PER_SEC); } #endif /* HAVE_CLOCK */ #if defined(MS_WINDOWS) && !defined(__BORLANDC__) +#define WIN32_PERF_COUNTER /* Win32 has better clock replacement; we have our own version, due to Mark Hammond and Tim Peters */ -static PyObject * -win32_clock(int fallback) +static int +win32_perf_counter(_Py_clock_info_t *info, PyObject **result) { static LONGLONG cpu_frequency = 0; static LONGLONG ctrStart; @@ -102,28 +122,42 @@ win32_clock(int fallback) if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0) { /* Unlikely to happen - this works on all intel machines at least! Revert to clock() */ - if (fallback) - return pyclock(); - else - return PyErr_SetFromWindowsErr(0); + *result = NULL; + return -1; } cpu_frequency = freq.QuadPart; } QueryPerformanceCounter(&now); diff = (double)(now.QuadPart - ctrStart); - return PyFloat_FromDouble(diff / (double)cpu_frequency); + if (info) { + info->function = "QueryPerformanceCounter()"; + info->resolution = 1.0 / (double)cpu_frequency; + info->precision = info->resolution; + info->is_monotonic = 1; + info->is_adjusted = 0; + } + *result = PyFloat_FromDouble(diff / (double)cpu_frequency); + return 0; } #endif -#if (defined(MS_WINDOWS) && !defined(__BORLANDC__)) || defined(HAVE_CLOCK) +#if defined(WIN32_PERF_COUNTER) || defined(HAVE_CLOCK) +#define PYCLOCK +static PyObject* +pyclock(_Py_clock_info_t *info) +{ +#ifdef WIN32_PERF_COUNTER + PyObject *res; + if (win32_perf_counter(info, &res) == 0) + return res; +#endif + return floatclock(info); +} + static PyObject * time_clock(PyObject *self, PyObject *unused) { -#if defined(MS_WINDOWS) && !defined(__BORLANDC__) - return win32_clock(1); -#else - return pyclock(); -#endif + return pyclock(NULL); } PyDoc_STRVAR(clock_doc, @@ -138,8 +172,8 @@ records."); static PyObject * time_clock_gettime(PyObject *self, PyObject *args) { + int clk_id; int ret; - clockid_t clk_id; struct timespec tp; if (!PyArg_ParseTuple(args, "i:clock_gettime", &clk_id)) @@ -150,7 +184,6 @@ time_clock_gettime(PyObject *self, PyObj PyErr_SetFromErrno(PyExc_IOError); return NULL; } - return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9); } @@ -162,7 +195,7 @@ Return the time of the specified clock c static PyObject * time_clock_settime(PyObject *self, PyObject *args) { - clockid_t clk_id; + int clk_id; PyObject *obj; struct timespec tp; int ret; @@ -185,14 +218,12 @@ PyDoc_STRVAR(clock_settime_doc, "clock_settime(clk_id, time)\n\ \n\ Set the time of the specified clock clk_id."); -#endif -#ifdef HAVE_CLOCK_GETRES static PyObject * time_clock_getres(PyObject *self, PyObject *args) { int ret; - clockid_t clk_id; + int clk_id; struct timespec tp; if (!PyArg_ParseTuple(args, "i:clock_getres", &clk_id)) @@ -211,7 +242,7 @@ PyDoc_STRVAR(clock_getres_doc, "clock_getres(clk_id) -> floating point number\n\ \n\ Return the resolution (precision) of the specified clock clk_id."); -#endif +#endif /* HAVE_CLOCK_GETTIME */ static PyObject * time_sleep(PyObject *self, PyObject *args) @@ -789,11 +820,71 @@ the local timezone used by methods such should not be relied on."); #endif /* HAVE_WORKING_TZSET */ +#if defined(MS_WINDOWS) || defined(__APPLE__) || defined(HAVE_CLOCK_GETTIME) +#define PYMONOTONIC + static PyObject* -steady_clock(int strict) +pymonotonic(_Py_clock_info_t *info) { -#if defined(MS_WINDOWS) && !defined(__BORLANDC__) - return win32_clock(!strict); +#if defined(MS_WINDOWS) + static ULONGLONG (*GetTickCount64) (void) = NULL; + static ULONGLONG (CALLBACK *Py_GetTickCount64)(void); + static int has_getickcount64 = 0; + double result; + + if (has_getickcount64 == -1) { + /* GetTickCount64() was added to Windows Vista */ + if (winver.dwMajorVersion >= 6) { + HINSTANCE hKernel32; + hKernel32 = GetModuleHandleW(L"KERNEL32"); + *(FARPROC*)&Py_GetTickCount64 = GetProcAddress(hKernel32, + "GetTickCount64"); + has_getickcount64 = (Py_GetTickCount64 != NULL); + } + else + has_getickcount64 = 0; + } + + if (has_getickcount64) { + ULONGLONG ticks; + ticks = Py_GetTickCount64(); + result = (double)ticks * 1e-3 + } + else { + static DWORD last_ticks = 0; + static DWORD n_overflow = 0; + DWORD ticks; + + ticks = GetTickCount(); + if (ticks < last_ticks) + n_overflow++; + last_ticks = ticks; + + result = ldexp(n_overflow, 32); + result += ticks; + result *= 1e-3; + } + + if (info) { + DWORD timeAdjustment, timeIncrement; + BOOL isTimeAdjustmentDisabled, ok; + if (has_getickcount64) + info->function = "GetTickCount64()"; + else + info->function = "GetTickCount()"; + info->is_monotonic = 1; + info->resolution = 1e-3; + ok = GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement, + &isTimeAdjustmentDisabled); + if (!ok) { + PyErr_SetFromWindowsErr(0); + return NULL; + } + info->precision = timeIncrement * 1e-7; + info->is_adjusted = 0; + } + return PyFloat_FromDouble(result); + #elif defined(__APPLE__) static mach_timebase_info_data_t timebase; uint64_t time; @@ -807,88 +898,331 @@ steady_clock(int strict) time = mach_absolute_time(); secs = (double)time * timebase.numer / timebase.denom * 1e-9; + if (info) { + info->function = "mach_absolute_time()"; + info->resolution = 1e-9; + info->is_monotonic = 1; + info->is_adjusted = 0; + } + return PyFloat_FromDouble(secs); - return PyFloat_FromDouble(secs); #elif defined(HAVE_CLOCK_GETTIME) - static int steady_clk_index = 0; - static int monotonic_clk_index = 0; - int *clk_index; - clockid_t steady_clk_ids[] = { -#ifdef CLOCK_MONOTONIC_RAW - CLOCK_MONOTONIC_RAW, -#endif - CLOCK_MONOTONIC, - CLOCK_REALTIME - }; - clockid_t monotonic_clk_ids[] = { -#ifdef CLOCK_MONOTONIC_RAW - CLOCK_MONOTONIC_RAW, + static int clk_index = 0; + clockid_t clk_ids[] = { +#ifdef CLOCK_HIGHRES + CLOCK_HIGHRES, #endif CLOCK_MONOTONIC }; - clockid_t *clk_ids; - int clk_ids_len; int ret; struct timespec tp; - if (strict) { - clk_index = &monotonic_clk_index; - clk_ids = monotonic_clk_ids; - clk_ids_len = Py_ARRAY_LENGTH(monotonic_clk_ids); + while (1) { + clockid_t clk_id = clk_ids[clk_index]; + ret = clock_gettime(clk_id, &tp); + if (ret == 0) { + if (info) { + struct timespec res; +#ifdef CLOCK_HIGHRES + if (clk_id == CLOCK_HIGHRES) { + info->function = "clock_gettime(CLOCK_HIGHRES)"; + info->is_monotonic = 1; + info->is_adjusted = 0; + } + else +#endif + { + assert(clk_id == CLOCK_MONOTONIC); + info->function = "clock_gettime(CLOCK_MONOTONIC)"; + info->is_monotonic = 1; +#if defined(linux) || defined(__linux) || defined(__linux__) + info->is_adjusted = 1; +#endif + } + info->resolution = 1e-9; + if (clock_getres(clk_id, &res) != 0) { + PyErr_SetFromErrno(PyExc_IOError); + return NULL; + } + info->precision = res.tv_sec + res.tv_nsec * 1e-9; + } + return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9); + } + + if (clk_index == Py_ARRAY_LENGTH(clk_ids)-1) + break; + clk_index++; } - else { - clk_index = &steady_clk_index; - clk_ids = steady_clk_ids; - clk_ids_len = Py_ARRAY_LENGTH(steady_clk_ids); - } - - while (0 <= *clk_index) { - clockid_t clk_id = clk_ids[*clk_index]; - ret = clock_gettime(clk_id, &tp); - if (ret == 0) - return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9); - - (*clk_index)++; - if (clk_ids_len <= *clk_index) - (*clk_index) = -1; - } - if (strict) { - PyErr_SetFromErrno(PyExc_OSError); - return NULL; - } - return floattime(); -#else - if (strict) { - PyErr_SetString(PyExc_NotImplementedError, - "no steady clock available on your platform"); - return NULL; - } - return floattime(); + PyErr_SetFromErrno(PyExc_OSError); + return NULL; #endif } static PyObject * -time_steady(PyObject *self, PyObject *args, PyObject *kwargs) +time_monotonic(PyObject *self, PyObject *unused) { - static char *kwlist[] = {"strict", NULL}; - int strict = 0; + return pymonotonic(NULL); +} - if (!PyArg_ParseTupleAndKeywords( - args, kwargs, "|i:steady", kwlist, - &strict)) +PyDoc_STRVAR(monotonic_doc, +"monotonic() -> float\n\ +\n\ +Monotonic clock, cannot go backward."); +#endif /* PYMONOTONIC */ + +static PyObject* +perf_counter(_Py_clock_info_t *info) +{ + PyObject *res; +#ifdef PYMONOTONIC + static int use_monotonic = 1; +#endif + +#ifdef WIN32_PERF_COUNTER + PyObject *res; + if (win32_perf_counter(info, &res) == 0) + return res; +#endif + +#ifdef PYMONOTONIC + if (use_monotonic) { + res = pymonotonic(info); + if (res != NULL) + return res; + use_monotonic = 0; + PyErr_Clear(); + } +#endif + + return floattime(info); +} + +static PyObject * +time_perf_counter(PyObject *self, PyObject *unused) +{ + return perf_counter(NULL); +} + +PyDoc_STRVAR(perf_counter_doc, +"perf_counter() -> float\n\ +\n\ +Performance counter for benchmarking."); + +static PyObject* +py_process_time(_Py_clock_info_t *info) +{ +#if defined(MS_WINDOWS) + HANDLE process; + FILETIME creation_time, exit_time, kernel_time, user_time; + ULARGE_INTEGER large; + double total; + BOOL ok; + + process = GetCurrentProcess(); + ok = GetProcessTimes(process, &creation_time, &exit_time, &kernel_time, &user_time); + if (!ok) + return PyErr_SetFromWindowsErr(0); + + large.u.LowPart = kernel_time.dwLowDateTime; + large.u.HighPart = kernel_time.dwHighDateTime; + total = large.QuadPart; + large.u.LowPart = user_time.dwLowDateTime; + large.u.HighPart = user_time.dwHighDateTime; + total += large.QuadPart; + if (info) { + info->function = "GetProcessTimes()"; + info->resolution = 1e-7; + info->is_monotonic = 1; + info->is_adjusted = 0; + } + return PyFloat_FromDouble(total * 1e-7); +#else + +#if defined(HAVE_SYS_RESOURCE_H) + struct rusage ru; +#endif +#ifdef HAVE_TIMES + struct tms t; + static long ticks_per_second = -1; +#endif + +#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_PROCESS_CPUTIME_ID) + struct timespec tp; + + if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp) == 0) { + if (info) { + struct timespec precision; + info->function = "clock_gettime(CLOCK_PROCESS_CPUTIME_ID)"; + info->is_monotonic = 1; + info->is_adjusted = 0; + info->resolution = 1e-9; + if (clock_getres(CLOCK_PROCESS_CPUTIME_ID, &precision) != 0) { + PyErr_SetFromErrno(PyExc_IOError); + return NULL; + } + info->precision = precision.tv_sec + precision.tv_nsec * 1e-9; + } + return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9); + } +#endif + +#if defined(HAVE_SYS_RESOURCE_H) + if (getrusage(RUSAGE_SELF, &ru) == 0) { + double total; + total = ru.ru_utime.tv_sec + ru.ru_utime.tv_usec * 1e-6; + total += ru.ru_stime.tv_sec + ru.ru_stime.tv_usec * 1e-6; + if (info) { + info->function = "getrusage(RUSAGE_SELF)"; + info->is_monotonic = 1; + info->is_adjusted = 0; + info->resolution = 1e-6; + } + return PyFloat_FromDouble(total); + } +#endif + +#ifdef HAVE_TIMES + if (times(&t) != (clock_t)-1) { + double total; + + if (ticks_per_second == -1) { +#if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) + ticks_per_second = sysconf(_SC_CLK_TCK); + if (ticks_per_second < 1) + ticks_per_second = -1; +#elif defined(HZ) + ticks_per_second = HZ; +#else + ticks_per_second = 60; /* magic fallback value; may be bogus */ +#endif + } + + if (ticks_per_second != -1) { + total = (double)t.tms_utime / ticks_per_second; + total += (double)t.tms_stime / ticks_per_second; + if (info) { + info->function = "times()"; + info->is_monotonic = 1; + info->is_adjusted = 0; + info->resolution = 1.0 / ticks_per_second; + info->precision = info->resolution; + } + return PyFloat_FromDouble(total); + } + } +#endif + + return pyclock(info); +#endif +} + +static PyObject * +time_process_time(PyObject *self, PyObject *unused) +{ + return py_process_time(NULL); +} + +PyDoc_STRVAR(process_time_doc, +"process_time() -> float\n\ +\n\ +Process time for profiling: sum of the kernel and user-space CPU time."); + + +static PyObject * +time_get_clock_info(PyObject *self, PyObject *args) +{ + char *name; + PyObject *dict, *obj; + int err; + _Py_clock_info_t info; + + if (!PyArg_ParseTuple(args, "s:get_clock_info", &name)) return NULL; - return steady_clock(strict); + info.precision = -1.0; + info.is_adjusted = -1; + + if (strcmp(name, "time") == 0) + obj = floattime(&info); +#ifdef PYCLOCK + else if (strcmp(name, "clock") == 0) + obj = pyclock(&info); +#endif +#ifdef PYMONOTONIC + else if (strcmp(name, "monotonic") == 0) + obj = pymonotonic(&info); +#endif + else if (strcmp(name, "perf_counter") == 0) + obj = perf_counter(&info); + else if (strcmp(name, "process_time") == 0) + obj = py_process_time(&info); + else { + PyErr_SetString(PyExc_ValueError, "unknown clock"); + return NULL; + } + if (obj == NULL) + return NULL; + Py_DECREF(obj); + + dict = PyDict_New(); + if (dict == NULL) + return NULL; + + obj = PyUnicode_FromString(info.function); + if (obj == NULL) + goto error; + err = PyDict_SetItemString(dict, "implementation", obj); + Py_DECREF(obj); + if (err) + goto error; + + obj = PyFloat_FromDouble(info.resolution); + if (obj == NULL) + goto error; + err = PyDict_SetItemString(dict, "resolution", obj); + Py_DECREF(obj); + if (err) + goto error; + + obj = PyBool_FromLong(info.is_monotonic); + if (obj == NULL) + goto error; + err = PyDict_SetItemString(dict, "is_monotonic", obj); + Py_DECREF(obj); + if (err) + goto error; + + if (info.precision != -1.0) { + obj = PyFloat_FromDouble(info.precision); + if (obj == NULL) + goto error; + err = PyDict_SetItemString(dict, "precision", obj); + Py_DECREF(obj); + if (err) + goto error; + } + + if (info.is_adjusted != -1) { + obj = PyBool_FromLong(info.is_adjusted); + if (obj == NULL) + goto error; + err = PyDict_SetItemString(dict, "is_adjusted", obj); + Py_DECREF(obj); + if (err) + goto error; + } + + return dict; + +error: + Py_DECREF(dict); + return NULL; } -PyDoc_STRVAR(steady_doc, -"steady(strict=False) -> float\n\ +PyDoc_STRVAR(get_clock_info_doc, +"get_clock_info(name: str) -> dict\n\ \n\ -Return the current time as a floating point number expressed in seconds.\n\ -This clock advances at a steady rate relative to real time and it may not\n\ -be adjusted. The reference point of the returned value is undefined so only\n\ -the difference of consecutive calls is valid."); - +Get information of the specified clock."); static void PyInit_timezone(PyObject *m) { @@ -979,13 +1313,9 @@ PyInit_timezone(PyObject *m) { #endif /* __CYGWIN__ */ #endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/ -#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_CLOCK_GETRES) -#ifdef CLOCK_REALTIME +#if defined(HAVE_CLOCK_GETTIME) PyModule_AddIntMacro(m, CLOCK_REALTIME); -#endif -#ifdef CLOCK_MONOTONIC PyModule_AddIntMacro(m, CLOCK_MONOTONIC); -#endif #ifdef CLOCK_MONOTONIC_RAW PyModule_AddIntMacro(m, CLOCK_MONOTONIC_RAW); #endif @@ -1004,18 +1334,14 @@ PyInit_timezone(PyObject *m) { static PyMethodDef time_methods[] = { {"time", time_time, METH_NOARGS, time_doc}, -#if (defined(MS_WINDOWS) && !defined(__BORLANDC__)) || defined(HAVE_CLOCK) +#ifdef PYCLOCK {"clock", time_clock, METH_NOARGS, clock_doc}, #endif #ifdef HAVE_CLOCK_GETTIME {"clock_gettime", time_clock_gettime, METH_VARARGS, clock_gettime_doc}, -#endif -#ifdef HAVE_CLOCK_GETTIME + {"clock_getres", time_clock_getres, METH_VARARGS, clock_getres_doc}, {"clock_settime", time_clock_settime, METH_VARARGS, clock_settime_doc}, #endif -#ifdef HAVE_CLOCK_GETRES - {"clock_getres", time_clock_getres, METH_VARARGS, clock_getres_doc}, -#endif {"sleep", time_sleep, METH_VARARGS, sleep_doc}, {"gmtime", time_gmtime, METH_VARARGS, gmtime_doc}, {"localtime", time_localtime, METH_VARARGS, localtime_doc}, @@ -1024,8 +1350,6 @@ static PyMethodDef time_methods[] = { #ifdef HAVE_MKTIME {"mktime", time_mktime, METH_O, mktime_doc}, #endif - {"steady", (PyCFunction)time_steady, METH_VARARGS|METH_KEYWORDS, - steady_doc}, #ifdef HAVE_STRFTIME {"strftime", time_strftime, METH_VARARGS, strftime_doc}, #endif @@ -1033,6 +1357,12 @@ static PyMethodDef time_methods[] = { #ifdef HAVE_WORKING_TZSET {"tzset", time_tzset, METH_NOARGS, tzset_doc}, #endif +#ifdef PYMONOTONIC + {"monotonic", time_monotonic, METH_NOARGS, monotonic_doc}, +#endif + {"process_time", time_process_time, METH_NOARGS, process_time_doc}, + {"perf_counter", time_perf_counter, METH_NOARGS, perf_counter_doc}, + {"get_clock_info", time_get_clock_info, METH_VARARGS, get_clock_info_doc}, {NULL, NULL} /* sentinel */ }; @@ -1110,6 +1440,15 @@ PyInit_time(void) if (!initialized) { PyStructSequence_InitType(&StructTimeType, &struct_time_type_desc); + +#ifdef MS_WINDOWS + winver.dwOSVersionInfoSize = sizeof(winver); + if (!GetVersionEx((OSVERSIONINFO*)&winver)) { + Py_DECREF(m); + PyErr_SetFromWindowsErr(0); + return NULL; + } +#endif } Py_INCREF(&StructTimeType); PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType); @@ -1118,7 +1457,7 @@ PyInit_time(void) } static PyObject* -floattime(void) +floattime(_Py_clock_info_t *info) { _PyTime_timeval t; #ifdef HAVE_CLOCK_GETTIME @@ -1129,10 +1468,23 @@ floattime(void) because it would require to link Python to the rt (real-time) library, at least on Linux */ ret = clock_gettime(CLOCK_REALTIME, &tp); - if (ret == 0) + if (ret == 0) { + if (info) { + struct timespec res; + info->function = "clock_gettime(CLOCK_REALTIME)"; + info->is_monotonic = 0; + info->is_adjusted = 1; + info->resolution = 1e-9; + if (clock_getres(CLOCK_REALTIME, &res) != 0) { + PyErr_SetFromErrno(PyExc_IOError); + return NULL; + } + info->precision = res.tv_sec + res.tv_nsec * 1e-9; + } return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9); + } #endif - _PyTime_gettimeofday(&t); + _PyTime_gettimeofday_info(&t, info); return PyFloat_FromDouble((double)t.tv_sec + t.tv_usec * 1e-6); } diff -r 9db3bedcd86c Python/pytime.c --- a/Python/pytime.c Fri Apr 13 20:51:20 2012 +0100 +++ b/Python/pytime.c Fri Apr 13 23:00:11 2012 +0200 @@ -18,8 +18,8 @@ extern int ftime(struct timeb *); #endif -void -_PyTime_gettimeofday(_PyTime_timeval *tp) +static void +pygettimeofday(_PyTime_timeval *tp, _Py_clock_info_t *info) { #ifdef MS_WINDOWS FILETIME system_time; @@ -35,6 +35,21 @@ _PyTime_gettimeofday(_PyTime_timeval *tp microseconds = large.QuadPart / 10 - 11644473600000000; tp->tv_sec = microseconds / 1000000; tp->tv_usec = microseconds % 1000000; + if (info) { + DWORD timeAdjustment, timeIncrement; + BOOL isTimeAdjustmentDisabled; + + info->function = "GetSystemTimeAsFileTime()"; + info->resolution = 1e-7; + info->is_monotonic = 0; + (void) GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement, + &isTimeAdjustmentDisabled); + info->accuracy = timeIncrement * 1e-7; + if (isTimeAdjustmentDisabled) + info->is_adjusted = 0; + else + info->is_adjusted = 1; + } #else /* There are three ways to get the time: (1) gettimeofday() -- resolution in microseconds @@ -46,14 +61,22 @@ _PyTime_gettimeofday(_PyTime_timeval *tp Note: clock resolution does not imply clock accuracy! */ #ifdef HAVE_GETTIMEOFDAY + int err; #ifdef GETTIMEOFDAY_NO_TZ - if (gettimeofday(tp) == 0) + err = gettimeofday(tp); +#else + err = gettimeofday(tp, (struct timezone *)NULL); +#endif + if (err == 0) { + if (info) { + info->function = "gettimeofday()"; + info->resolution = 1e-6; + info->is_monotonic = 0; + info->is_adjusted = 1; + } return; -#else /* !GETTIMEOFDAY_NO_TZ */ - if (gettimeofday(tp, (struct timezone *)NULL) == 0) - return; -#endif /* !GETTIMEOFDAY_NO_TZ */ -#endif /* !HAVE_GETTIMEOFDAY */ + } +#endif /* HAVE_GETTIMEOFDAY */ #if defined(HAVE_FTIME) { @@ -61,15 +84,39 @@ _PyTime_gettimeofday(_PyTime_timeval *tp ftime(&t); tp->tv_sec = t.time; tp->tv_usec = t.millitm * 1000; + if (info) { + info->function = "ftime()"; + info->resolution = 1e-3; + info->is_monotonic = 0; + info->is_adjusted = 1; + } } #else /* !HAVE_FTIME */ tp->tv_sec = time(NULL); tp->tv_usec = 0; + if (info) { + info->function = "time()"; + info->resolution = 1; + info->is_monotonic = 0; + info->is_adjusted = 1; + } #endif /* !HAVE_FTIME */ #endif /* MS_WINDOWS */ } +void +_PyTime_gettimeofday(_PyTime_timeval *tp) +{ + pygettimeofday(tp, NULL); +} + +void +_PyTime_gettimeofday_info(_PyTime_timeval *tp, _Py_clock_info_t *info) +{ + pygettimeofday(tp, info); +} + static void error_time_t_overflow(void) {