Skip to content

Introduce new time retrieval Zend-API#21370

Open
marc-mabe wants to merge 1 commit intophp:masterfrom
marc-mabe:new-time-retrieval-api
Open

Introduce new time retrieval Zend-API#21370
marc-mabe wants to merge 1 commit intophp:masterfrom
marc-mabe:new-time-retrieval-api

Conversation

@marc-mabe
Copy link
Contributor

This is split out from #19202 to first introduce the new time retrieval API. Later PRs will follow to refactor time retrieval usages.

Key Changes

  • Introduced zend_time.h
    A new internal header that abstracts system time functions, replacing direct usage of <time.h> and related platform-specific APIs.

  • Added zend_time_real_spec
    A unified wrapper around clock_gettime(), timespec_get(), gettimeofday(), and time() using the real/wall clock.
    Returns a timespec structure with up-to nanosecond precision.

  • Added zend_time_real_sec
    A lightweight wrapper around time(NULL) for simple, low-resolution time retrieval in seconds.

  • Added zend_time_mono_fallback_nsec
    A wrapper for zend_hrtime or falls back to zend_time_real_spec.
    Useful for time measurements (e.g. timeout handling) where monotonic time is preferred, but wall time is an acceptable fallback.

  • Added helper functions
    Added utilities to simplify usage of timeval and timespec structures.

The time retrieval functions are not inlined to make it possible to be mocked in unit tests.

Copy link
Member

@TimWolla TimWolla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The time retrieval functions are not inlined to make it possible to be mocked in unit tests.

Is it really necessary to mock them rather than mocking their implementation default? Not allowing them to be inlined is likely going to make them expensive for basically only being a wrapper.

}

/* Assign timeval to timespec */
static zend_always_inline void zend_time_val2spec(struct timeval tv, struct timespec *ts) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taking the tv by value and ts by pointer looks odd. Personally I find taking and returning by value more convenient, so I wonder if we should just do this for all the APIs:

Suggested change
static zend_always_inline void zend_time_val2spec(struct timeval tv, struct timespec *ts) {
static zend_always_inline struct timespec zend_time_val2spec(struct timeval tv) {

On Linux both struct timespec and struct timeval are 16 bytes, thus they can be passed and returned in a register pair.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is also specifically intended for the other reviewers to discuss.


/* Assign timeval to timespec */
static zend_always_inline void zend_time_val2spec(struct timeval tv, struct timespec *ts) {
ts->tv_sec = (time_t) tv.tv_sec;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ts->tv_sec = (time_t) tv.tv_sec;
ts->tv_sec = tv.tv_sec;

tv.tv_sec should already be time_t.

Comment on lines +40 to +41
#define ZEND_MILLI_IN_SEC 1000U
#define ZEND_MICRO_IN_SEC 1000000U
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#define ZEND_MILLI_IN_SEC 1000U
#define ZEND_MICRO_IN_SEC 1000000U
#define ZEND_TIME_MILLI_IN_SEC 1000U
#define ZEND_TIME_MICRO_IN_SEC 1000000U

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants