Skip to content

posix: implement clock_getres() #60187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/zephyr/posix/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ int clock_gettime(clockid_t clock_id, struct timespec *ts);
__syscall int clock_gettime(clockid_t clock_id, struct timespec *ts);
#endif /* CONFIG_ARCH_POSIX */
int clock_settime(clockid_t clock_id, const struct timespec *ts);
int clock_getres(clockid_t clock_id, struct timespec *res);
/* Timer APIs */
int timer_create(clockid_t clockId, struct sigevent *evp, timer_t *timerid);
int timer_delete(timer_t timerid);
Expand Down
35 changes: 35 additions & 0 deletions lib/posix/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,41 @@ int clock_settime(clockid_t clock_id, const struct timespec *tp)
return 0;
}

/**
* @brief Get clock resolution.
*
* See IEEE 1003.1
*/
int clock_getres(clockid_t clock_id, struct timespec *res)
{
switch (clock_id) {
case CLOCK_MONOTONIC:
res->tv_sec = 0;
res->tv_nsec = NSEC_PER_SEC / CONFIG_SYS_CLOCK_TICKS_PER_SEC;
break;

case CLOCK_REALTIME:
uint64_t tick_1 = k_uptime_ticks();
uint64_t tick_2 = tick_1;

while (tick_1 == tick_2) {
tick_2 = k_uptime_ticks();
}

int tick_res = tick_2 - tick_1;
Copy link
Member

Choose a reason for hiding this comment

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

tv_nsec is in long, this prob can use long as well?


res->tv_sec = 0;
res->tv_nsec = NSEC_PER_SEC / tick_res;
break;

default:
errno = EINVAL;
return -1;
}

return 0;
}

/**
* @brief Get current real time.
*
Expand Down
12 changes: 12 additions & 0 deletions tests/posix/common/src/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

ZTEST(posix_apis, test_clock)
{
int ret;
int64_t nsecs_elapsed, secs_elapsed;
struct timespec ts, te;

Expand All @@ -36,6 +37,12 @@ ZTEST(posix_apis, test_clock)
secs_elapsed = (te.tv_sec - ts.tv_sec - 1);
}

ret = clock_getres(CLOCK_MONOTONIC, &te);
zassert_equal(ret, 0, "Fail to get monotonic clock resolution");
Copy link
Member

Choose a reason for hiding this comment

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

can use zassert_ok to avoid declaring ret

zassert_equal(te.tv_sec, 0, "Monotonic clock resolution should be in ns");
zassert_equal(te.tv_nsec, NSEC_PER_SEC / CONFIG_SYS_CLOCK_TICKS_PER_SEC,
"Monotonic clock resolution should be in ns");

/*TESTPOINT: Check if POSIX clock API test passes*/
zassert_equal(secs_elapsed, SLEEP_SECONDS,
"POSIX clock API test failed");
Expand Down Expand Up @@ -122,4 +129,9 @@ ZTEST(posix_apis, test_realtime)
" provide correct result");
zassert_true(rts.tv_nsec >= tv.tv_usec * NSEC_PER_USEC,
"gettimeofday didn't provide correct result");

ret = clock_getres(CLOCK_REALTIME, &nts);
zassert_equal(ret, 0, "Fail to get realtime clock resolution");
zassert_equal(nts.tv_sec, 0, "Realtime clock resolution should be in ns");
zassert_not_equal(nts.tv_nsec, 0, "Invalid realtime clock resolution");
}