Debian Codegraph @

mypyc/lib-rt/time/librt_time.c

raw
1 #define PY_SSIZE_T_CLEAN
2 #include <Python.h>
3 #include <time.h>
4 #include <stdint.h>
5 #include "librt_time.h"
6 #include "pythoncapi_compat.h"
7 #include "mypyc_util.h"
8
9 #ifdef _WIN32
10 #include <windows.h>
11 #else
12 #include <sys/time.h>
13 #endif
14
15 // Internal function that returns a C double for mypyc primitives
16 // Returns high-precision time in seconds (like time.time())
17 static double
18 time_time_internal(void) {
19 #ifdef _WIN32
20 // Windows: Use GetSystemTimePreciseAsFileTime for ~100ns precision
21 FILETIME ft;
22 ULARGE_INTEGER large;
23
24 GetSystemTimePreciseAsFileTime(&ft);
25 large.LowPart = ft.dwLowDateTime;
26 large.HighPart = ft.dwHighDateTime;
27
28 // Windows FILETIME is 100-nanosecond intervals since January 1, 1601
29 // 116444736000000000 = number of 100-ns intervals between 1601 and 1970
30 // Convert directly to seconds: 100ns * 1e-9 = 1e-7
31 int64_t intervals = large.QuadPart - 116444736000000000LL;
32 return (double)intervals * 1e-7;
33
34 #else // Unix-like systems (Linux, macOS, BSD, etc.)
35
36 // Try clock_gettime(CLOCK_REALTIME) for nanosecond precision
37 // This is available on POSIX.1-2001 and later (widely available on modern systems)
38 #if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0
39 struct timespec ts;
40 if (clock_gettime(CLOCK_REALTIME, &ts) == 0) {
41 // Convert seconds and nanoseconds separately to avoid large integer operations
42 return (double)ts.tv_sec + (double)ts.tv_nsec * 1e-9;
43 }
44 // Fall through to gettimeofday if clock_gettime failed
45 #endif
46
47 // Fallback: gettimeofday for microsecond precision
48 // This is widely available (POSIX.1-2001, BSD, etc.)
49 struct timeval tv;
50 if (unlikely(gettimeofday(&tv, NULL) != 0)) {
51 PyErr_SetFromErrno(PyExc_OSError);
52 return CPY_FLOAT_ERROR;
53 }
54
55 // Convert seconds and microseconds separately to avoid large integer operations
56 return (double)tv.tv_sec + (double)tv.tv_usec * 1e-6;
57 #endif
58 }
59
60 // Wrapper function for normal Python extension usage
61 static PyObject*
62 time_time(PyObject *self, PyObject *const *args, size_t nargs) {
63 if (nargs != 0) {
64 PyErr_SetString(PyExc_TypeError, "time() takes no arguments");
65 return NULL;
66 }
67
68 double result = time_time_internal();
69 if (result == CPY_FLOAT_ERROR) {
70 return NULL;
71 }
72 return PyFloat_FromDouble(result);
73 }
74
75 static PyMethodDef librt_time_module_methods[] = {
76 {"time", (PyCFunction)time_time, METH_FASTCALL,
77 PyDoc_STR("Return the current time in seconds since the Unix epoch as a floating point number.")},
78 {NULL, NULL, 0, NULL}
79 };
80
81 static int
82 time_abi_version(void) {
83 return LIBRT_TIME_ABI_VERSION;
84 }
85
86 static int
87 time_api_version(void) {
88 return LIBRT_TIME_API_VERSION;
89 }
90
91 static int
92 librt_time_module_exec(PyObject *m)
93 {
94 // Export mypyc internal C API via capsule
95 static void *time_api[LIBRT_TIME_API_LEN] = {
96 (void *)time_abi_version,
97 (void *)time_api_version,
98 (void *)time_time_internal,
99 };
100 PyObject *c_api_object = PyCapsule_New((void *)time_api, "librt.time._C_API", NULL);
101 if (PyModule_Add(m, "_C_API", c_api_object) < 0) {
102 return -1;
103 }
104 return 0;
105 }
106
107 static PyModuleDef_Slot librt_time_module_slots[] = {
108 {Py_mod_exec, librt_time_module_exec},
109 #ifdef Py_MOD_GIL_NOT_USED
110 {Py_mod_gil, Py_MOD_GIL_NOT_USED},
111 #endif
112 {0, NULL}
113 };
114
115 static PyModuleDef librt_time_module = {
116 .m_base = PyModuleDef_HEAD_INIT,
117 .m_name = "time",
118 .m_doc = "Fast time() function optimized for mypyc",
119 .m_size = 0,
120 .m_methods = librt_time_module_methods,
121 .m_slots = librt_time_module_slots,
122 };
123
124 PyMODINIT_FUNC
125 PyInit_time(void)
126 {
127 return PyModuleDef_Init(&librt_time_module);
128 }