Move time facility to OS layer

Migrates the Time facility to the OS layer.
This commit is contained in:
guangli-dai 2026-07-27 16:30:29 -07:00 committed by Guangli Dai
parent abb0a8a05b
commit c5b082c470
5 changed files with 132 additions and 75 deletions

View file

@ -31,4 +31,7 @@
/* File I/O */
#include "jemalloc/internal/os/file.h"
/* Time */
#include "jemalloc/internal/os/time.h"
#endif /* JEMALLOC_INTERNAL_OS_H */

View file

@ -0,0 +1,68 @@
#ifndef JEMALLOC_INTERNAL_OS_POSIX_TIME_H
#define JEMALLOC_INTERNAL_OS_POSIX_TIME_H
/*
* POSIX time backend: picks the best available (ideally monotonic) clock,
* falling back to gettimeofday() if nothing better is available.
*/
#include "jemalloc/internal/jemalloc_preamble.h"
#if defined(JEMALLOC_HAVE_CLOCK_MONOTONIC_COARSE)
# define OS_TIME_MONOTONIC 1
JEMALLOC_ALWAYS_INLINE void
os_time_get(nstime_t *time) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC_COARSE, &ts);
nstime_init2(time, ts.tv_sec, ts.tv_nsec);
}
#elif defined(JEMALLOC_HAVE_CLOCK_MONOTONIC)
# define OS_TIME_MONOTONIC 1
JEMALLOC_ALWAYS_INLINE void
os_time_get(nstime_t *time) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
nstime_init2(time, ts.tv_sec, ts.tv_nsec);
}
#elif defined(JEMALLOC_HAVE_CLOCK_GETTIME_NSEC_NP)
# define OS_TIME_MONOTONIC 1
JEMALLOC_ALWAYS_INLINE void
os_time_get(nstime_t *time) {
nstime_init(time, clock_gettime_nsec_np(CLOCK_UPTIME_RAW));
}
#elif defined(JEMALLOC_HAVE_MACH_ABSOLUTE_TIME)
# define OS_TIME_MONOTONIC 1
JEMALLOC_ALWAYS_INLINE void
os_time_get(nstime_t *time) {
static mach_timebase_info_data_t sTimebaseInfo;
if (sTimebaseInfo.denom == 0) {
(void)mach_timebase_info(&sTimebaseInfo);
}
nstime_init(time,
mach_absolute_time() * sTimebaseInfo.numer / sTimebaseInfo.denom);
}
#else
# define OS_TIME_MONOTONIC 0
JEMALLOC_ALWAYS_INLINE void
os_time_get(nstime_t *time) {
struct timeval tv;
gettimeofday(&tv, NULL);
nstime_init2(time, tv.tv_sec, tv.tv_usec * 1000);
}
#endif
JEMALLOC_ALWAYS_INLINE void
os_time_get_realtime(nstime_t *time) {
#if defined(JEMALLOC_HAVE_CLOCK_REALTIME)
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
nstime_init2(time, ts.tv_sec, ts.tv_nsec);
#else
unreachable();
#endif
}
#endif /* JEMALLOC_INTERNAL_OS_POSIX_TIME_H */

View file

@ -0,0 +1,29 @@
#ifndef JEMALLOC_INTERNAL_OS_TIME_H
#define JEMALLOC_INTERNAL_OS_TIME_H
#include "jemalloc/internal/jemalloc_preamble.h"
#include "jemalloc/internal/os/detect.h"
#include "jemalloc/internal/nstime.h"
/*
* Time interface.
* Default: posix/. Override: Windows (GetSystemTimeAsFileTime).
*
* Capability macro: OS_TIME_MONOTONIC - 1 iff os_time_get()'s clock never
* goes backwards, 0 otherwise. Defined by whichever backend is selected
* below.
*/
/* Functions required for implementation in each backend. */
JEMALLOC_ALWAYS_INLINE void os_time_get(nstime_t *time);
JEMALLOC_ALWAYS_INLINE void os_time_get_realtime(nstime_t *time);
#if defined(_WIN32)
# include "jemalloc/internal/os/windows/time.h"
#elif defined(JEMALLOC_OS_POSIX)
# include "jemalloc/internal/os/posix/time.h"
#else
# error "OS layer: no time backend for this platform; add os/<os>/time.h"
#endif
#endif /* JEMALLOC_INTERNAL_OS_TIME_H */

View file

@ -0,0 +1,27 @@
#ifndef JEMALLOC_INTERNAL_OS_WINDOWS_TIME_H
#define JEMALLOC_INTERNAL_OS_WINDOWS_TIME_H
/*
* Windows time backend, via GetSystemTimeAsFileTime(). Not monotonic.
*/
#include "jemalloc/internal/jemalloc_preamble.h"
#define OS_TIME_MONOTONIC 0
JEMALLOC_ALWAYS_INLINE void
os_time_get(nstime_t *time) {
FILETIME ft;
uint64_t ticks_100ns;
GetSystemTimeAsFileTime(&ft);
ticks_100ns = (((uint64_t)ft.dwHighDateTime) << 32) | ft.dwLowDateTime;
nstime_init(time, ticks_100ns * 100);
}
JEMALLOC_ALWAYS_INLINE void
os_time_get_realtime(nstime_t *time) {
unreachable();
}
#endif /* JEMALLOC_INTERNAL_OS_WINDOWS_TIME_H */