Skip to content
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

posix: clock_settime calculates the base time incorrectly #16143

Closed
alexanderendian opened this issue May 14, 2019 · 2 comments · Fixed by #16040
Closed

posix: clock_settime calculates the base time incorrectly #16143

alexanderendian opened this issue May 14, 2019 · 2 comments · Fixed by #16040
Assignees
Labels
area: POSIX POSIX API Library area: Timer Timer bug The issue is a bug, or the PR is fixing a bug priority: medium Medium impact/importance bug

Comments

@alexanderendian
Copy link
Contributor

If you call clock_settime twice with the same non-zero value appears to reset the clock back to 0. I've that disovered that it's caused by clock_settime performing its internal calculation incorrectly when updating the clock base. I have submitted a pull request which contains some further explanation and a fix #16040.

To Reproduce

  1. Build and run the following program in QEMU (although it should affect any target). Disclaimer: I don't have a development environment nearby to test it, so it may contain a typo.
#include <zephyr.h>
#include <posix/time.h>
#include <misc/printk.h>

void main(void)
{
	struct timespec target;
	struct timespec actual;

	target.tv_sec = 1000000;
	target.tv_nsec = 0;

	printk("clock_settime to %d\n", target->tv_sec);
	clock_settime(CLOCK_REALTIME, &target);
	clock_gettime(CLOCK_REALTIME, &actual);
	printk("clock_gettime returned %d\n", actual->tv_sec);

	printk("clock_settime to %d\n", target->tv_sec);
	clock_settime(CLOCK_REALTIME, &target);
	clock_gettime(CLOCK_REALTIME, &actual);
	printk("clock_gettime returned %d\n", actual->tv_sec);
}
  1. Observe that the last printed line shows a time close to 0 when something close to the target of 1000000 is expected.
@alexanderendian alexanderendian added the bug The issue is a bug, or the PR is fixing a bug label May 14, 2019
@galak
Copy link
Collaborator

galak commented May 14, 2019

@alexanderendian thanks for submitting this issue.

@carlescufi carlescufi added area: POSIX POSIX API Library area: Timer Timer priority: medium Medium impact/importance bug labels May 14, 2019
@jbrzozoski
Copy link
Contributor

jbrzozoski commented May 17, 2019

I just bumped into this, too. Fix is just to use CLOCK_MONOTONIC when getting the base reference inside clock_settime.
EDIT: I like your fix in the pull request better. Cleaner to just get the time reference directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: POSIX POSIX API Library area: Timer Timer bug The issue is a bug, or the PR is fixing a bug priority: medium Medium impact/importance bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants