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

xtimer: introduce xtimer_msleep() #15076

Merged
merged 1 commit into from Nov 4, 2020
Merged

Conversation

benpicco
Copy link
Contributor

Contribution description

Add a function that sleeps for ms instead of µs.
This will make the conversion to ztimer easier for users as now there is a direct mapping to ZTIMER_MSEC.

Testing procedure

Issues/PRs references

@benpicco benpicco added Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation Discussion: RFC The issue/PR is used as a discussion starting point about the item of the issue/PR labels Sep 24, 2020
@benpicco benpicco force-pushed the xtimer_msleep branch 2 times, most recently from cb7ebc0 to c11e7c9 Compare September 24, 2020 12:18
@kaspar030
Copy link
Contributor

This will make the conversion to ztimer easier for users as now there is a direct mapping to ZTIMER_MSEC.

btw, there is a coccinelle script that can recognize "xtimer_(, ..., foo * MSEC_IN_USEC)" and convert that to "ztimer_(ZTIMER_MSEC, ...)".

@benpicco
Copy link
Contributor Author

benpicco commented Sep 24, 2020

btw.: what was the rationale for having ztimer_sleep(ZTIMER_USEC, …) instead of ztimer_usleep(…), ztimer_msleep()?

@maribu
Copy link
Member

maribu commented Sep 24, 2020

btw.: what was the rationale for having ztimer_sleep(ZTIMER_USEC, …) instead of ztimer_usleep(…), ztimer_msleep()?

I cannot answer what the rationale was, but I can point out some advantage of this decision: This decouples the choice of the hardware to run on from the time unit used.

E.g. let's say I have a PTP clock an a periph_timer. Both could implement ZTIMER_USEC. But I might want to provide both. E.g. setting a 1 second timeout on the periph_timer is guaranteed to actually trigger in 1 second (+- jitter, overhead, clock drift, ...). But if I set on on the PTP clock, it might trigger on something completely different due to clock synchronization.

E.g. in a sensor netowrk I might indeed want measurements to be performed completely synchronized, so in terms of the synchronized PTP clock - even if the period changes whenever the clock is adjusted.

@kaspar030
Copy link
Contributor

btw.: what was the rationale for having ztimer_sleep(ZTIMER_USEC, …) instead of ztimer_usleep(…), ztimer_msleep()?

What @maribu said. ZTIMER_USEC is just a name (ptr) that by convention points to a ztimer clock that provides usec ticks.
While adding ztimer_usleep(val){ztimer_sleep(ZTIMER_USEC, val);} would be trivial to add, IMO as is this provides a lot of flexibility. E.g., all the ztimer tests can trivially be used to test any ztimer clock, because the clock argument is just a variable.

@benpicco benpicco added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Sep 28, 2020
@bergzand bergzand added this to Backlog / Proposals in RIOT Sprint Day via automation Sep 29, 2020
@maribu
Copy link
Member

maribu commented Oct 8, 2020

Being honest, I think that

xtimer_msleep(3);

is simply more readable than

xtimer_usleep(3 * US_PER_MS);

Just for that reason, we should IMO get this in.

That this will make people using ztimer_xtimer_compat more happy is of course also nice :-)

@fjmolinas
Copy link
Contributor

Just for that reason, we should IMO get this in.

+1

1 similar comment
@jia200x
Copy link
Member

jia200x commented Oct 8, 2020

Just for that reason, we should IMO get this in.

+1

@benpicco benpicco removed the Discussion: RFC The issue/PR is used as a discussion starting point about the item of the issue/PR label Oct 8, 2020
@benpicco benpicco removed the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Oct 8, 2020
Comment on lines 89 to 99
static inline void xtimer_msleep(uint32_t milliseconds)
{
if (IS_ACTIVE(MODULE_ZTIMER_MSEC)) {
ztimer_sleep(ZTIMER_MSEC, milliseconds);
} else {
while (milliseconds > (UINT32_MAX / 1000LU)) {
ztimer_sleep(ZTIMER_USEC, UINT32_MAX);
seconds -= UINT32_MAX / 1000LU;
}
ztimer_sleep(ZTIMER_USEC, milliseconds * 1000LU);
}
Copy link
Member

Choose a reason for hiding this comment

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

This would IMO now justify an ztimer_msleep() as helper - for when the user really doesn't care on which hardware the timer is set. But that should be another discussion in another PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

why not just depend on ztimer msec?

Copy link
Contributor

Choose a reason for hiding this comment

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

the helper would do the intermediate wakeups N times for N ztimer_msleep instances. ztimer_sleep(ZTIMER_MSEC, ... would mitigate that.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, is there a use case for using ztimer without ztimer_msec?

Copy link
Contributor

Choose a reason for hiding this comment

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

well, if nothing uses it, no need to include it. but that's not the point here, there's no case (or there should not be one) where ztime_msec is not available.

@benpicco benpicco added the Area: timers Area: timer subsystems label Oct 11, 2020
Add a function that sleeps for ms instead of µs.
This will make the conversion to ztimer easier for users as now there is
a direct mapping to ZTIMER_MSEC.
@benpicco benpicco added CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR and removed CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Oct 24, 2020
@benpicco benpicco added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Oct 24, 2020
@benpicco benpicco requested a review from aabadie October 27, 2020 09:24
Copy link
Member

@maribu maribu left a comment

Choose a reason for hiding this comment

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

ACK. I really like _ztimer_sleep_scale(), which should improve both readability and the opportunities of the compiler to optimize the code.

RIOT Sprint Day automation moved this from Backlog / Proposals to Waiting For Murdock Nov 4, 2020
@benpicco benpicco merged commit 010925d into RIOT-OS:master Nov 4, 2020
RIOT Sprint Day automation moved this from Waiting For Murdock to Done Nov 4, 2020
@benpicco benpicco deleted the xtimer_msleep branch November 4, 2020 10:09
@kaspar030
Copy link
Contributor

I really like _ztimer_sleep_scale(), which should improve both readability and the opportunities of the compiler to optimize the code.

yeah. maybe move it to be a public ztimer function?

@@ -172,6 +172,11 @@ static inline void xtimer_spin(xtimer_ticks32_t ticks) {
_xtimer_spin(ticks.ticks32);
}

static inline void xtimer_msleep(uint32_t milliseconds)
{
_xtimer_tsleep64(_xtimer_ticks_from_usec64(milliseconds * US_PER_MS));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm now I wonder: will this now have a worse overhead than calling xtimer_usleep(ms * US_PER_MS) because here 64 bit arithmetic will be involved?

Copy link
Member

Choose a reason for hiding this comment

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

Rather than worrying about xtimer, let's push ztimer forward :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: timers Area: timer subsystems CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation
Projects
Development

Successfully merging this pull request may close these issues.

None yet

5 participants