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

Improve RTOS behavior with deep sleep #8223

Merged
merged 1 commit into from
Oct 27, 2018

Conversation

c1728p9
Copy link
Contributor

@c1728p9 c1728p9 commented Sep 21, 2018

Description

Only deep sleep when the wakeup time is more than 10ms in the future. This ensures that RTOS events are handled at the correct time. Note - when deep sleep is allow interrupt latency may still be as high as 10ms.

Pull request type

[ ] Fix
[ ] Refactor
[ ] Target update
[x] Functionality change
[ ] Breaking change

@c1728p9
Copy link
Contributor Author

c1728p9 commented Sep 21, 2018

This may negatively increase power consumption so I'm marking this as do not merge until further investigation can be done

@jeromecoutant
Copy link
Collaborator

Hi

Proposition in targets.json file:

"Target": {
    "config": {
        "deep-sleep-latency": {
            "help": "time in ms required to go to and wake up from deep sleep (max 10)",
            "value": 0
        },

Then, it is up to each target to define it ?

@kjbracey
Copy link
Contributor

Seems like something like this is needed, given the HAL sleep spec. Implementation seems reasonable to me.

Next possible refinement would be to allow a target to specify its number.

Copy link
Collaborator

@jeromecoutant jeromecoutant left a comment

Choose a reason for hiding this comment

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

Verified with tests
(note that this current patch version doesn't compile :-))

@c1728p9
Copy link
Contributor Author

c1728p9 commented Oct 3, 2018

Rebased on to master and added config value with a default of 0. Removing "do not merge" since this PR should not effect power consumption in the default configuration.

@kjbracey
Copy link
Contributor

kjbracey commented Oct 4, 2018

GitHub description needs update from commit message

@jeromecoutant
Copy link
Collaborator

@c1728p9 it seems there is some conflict
@ARMmbed/mbed-os-maintainers can you start CI ?
Thx

@cmonr
Copy link
Contributor

cmonr commented Oct 9, 2018

@jeromecoutant It doesn't makes sense to start CI if the PR needs to be rebased, which will require another run of CI anyways.

if (block_deep_sleep) {
sleep_manager_lock_deep_sleep();
} else {
ticks_to_sleep -= MBED_CONF_TARGET_DEEP_SLEEP_LATENCY;
Copy link
Contributor

Choose a reason for hiding this comment

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

Technically this subtraction should be just "wake latency".

If sleep time was 3ms and wake time was 5ms, you'd want

7ms: normal sleep, 7ms timer
8ms: normal sleep, 8ms timer
9ms: deep sleep, 4ms timer
10ms: deep sleep, 5ms timer

I think you might want separate "sleep" and "wake" numbers to do that.

Copy link
Contributor

Choose a reason for hiding this comment

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

But then, is 1ms in deep sleep better than 9ms in normal sleep? Scope for another fudge factor... Maybe you'd want to not go deep until "actual deep sleep time >= 50% of desired total" or something.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @kjbracey-arm the intent here was to reduce typical latency by using the requirement defined in the sleep specification - https://github.com/ARMmbed/mbed-os/blob/master/hal/sleep_api.h#L42

This is an optimization for latency not for power savings which is why I didn't add additional logic to determine if it would be optimal for power consumption to go into deep sleep. This could definitely be done, but is outside the scope of this PR.

I think you might want separate "sleep" and "wake" numbers to do that.

Have you seen devices which take a large amount of time to enter deep sleep? I'm use to a wakeup delay since crystals and plls need time to stabalize and lock, but I don't think I have seen a case where the time to enter sleep was a concern.

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay, I think that answers my main point - I was thinking sleep and wake time might be comparable, which but if it's typically almost all wake, this is fine.

The power saving thing is something else, indeed.

The 10ms latency is something I think we'll have to be occasionally conscious of; I'd not really registered the implications. There may be external devices that keep working perfectly fine in deep sleep, except that they wouldn't cope with 10ms IRQ response latency from the CPU, so drivers may need to hold the lock purely for that reason.

@jeromecoutant
Copy link
Collaborator

bump...

@cmonr
Copy link
Contributor

cmonr commented Oct 17, 2018

@c1728p9 Could you address @kjbracey-arm's comments, and rebase so that we can get CI going?

@@ -20,6 +20,10 @@
"network-default-interface-type": {
"help": "Default network interface type. Typical options: null, ETHERNET, WIFI, CELLULAR, MESH",
"value": null
},
"deep-sleep-latency": {
"help": "Time in ms required to go to and wake up from deep sleep (max 10)",
Copy link
Contributor

Choose a reason for hiding this comment

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

I know it's a nitpick but I think you've got some tabs in this change. The only 2 tabs in the file.

Only deep sleep when the wakeup time is more than
MBED_CONF_TARGET_DEEP_SLEEP_LATENCY ms in the future.
This ensures that RTOS events are handled at the correct time. Note -
when deep sleep is allow interrupt latency may still be as high as
10ms.
@c1728p9
Copy link
Contributor Author

c1728p9 commented Oct 24, 2018

Fixed witespace and rebased to master

@cmonr
Copy link
Contributor

cmonr commented Oct 25, 2018

@mattbrown015 @LMESTM Please feel free to take another look at this PR.

/morph build

@mbed-ci
Copy link

mbed-ci commented Oct 25, 2018

Build : SUCCESS

Build number : 3456
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/8223/

Triggering tests

/morph test
/morph export-build
/morph mbed2-build

@mbed-ci
Copy link

mbed-ci commented Oct 25, 2018

@mbed-ci
Copy link

mbed-ci commented Oct 25, 2018

@kjbracey
Copy link
Contributor

I still approve this, but would welcome @c1728p9's thoughts on my comments about separate wake/sleep times.

@@ -21,6 +21,10 @@
"help": "Default network interface type. Typical options: null, ETHERNET, WIFI, CELLULAR, MESH",
"value": null
},
"deep-sleep-latency": {
"help": "Time in ms required to go to and wake up from deep sleep (max 10)",
Copy link
Contributor

Choose a reason for hiding this comment

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

I assume MBED_CONF_TARGET_DEEP_SLEEP_LATENCY is the worst case scenario (the main depending factor here are the clocks that are running and need to be reenabled - can change in the runtime - in the most cases now it wont).

Max number is defined in the help text as 10, but not checked anywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@0xc0170 the max recommended value is 10 because deep sleep shouldn't take longer than 10ms to wake - https://github.com/ARMmbed/mbed-os/blob/master/hal/sleep_api.h#L42

Setting it to a value higher than 10 will cause higher power consumption but won't negatively effect the system otherwise.

Copy link
Contributor

Choose a reason for hiding this comment

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

I understood, just checking if we set it to 11 , is this allowed and just impacts the system (no compile time error). I believe so.

@0xc0170
Copy link
Contributor

0xc0170 commented Oct 26, 2018

/morph build

@mbed-ci
Copy link

mbed-ci commented Oct 26, 2018

Build : SUCCESS

Build number : 3479
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/8223/

Triggering tests

/morph test
/morph export-build
/morph mbed2-build

@mbed-ci
Copy link

mbed-ci commented Oct 26, 2018

@mbed-ci
Copy link

mbed-ci commented Oct 26, 2018

@cmonr cmonr merged commit 4798a91 into ARMmbed:master Oct 27, 2018
@cmonr cmonr removed the needs: CI label Oct 27, 2018
@mattbrown015
Copy link
Contributor

mattbrown015 commented Oct 29, 2018

Hi @jeromecoutant,

Would you recommend changing this to something other than 0 for my STM32L4?

The STM32L4 datasheet says it will wake from Stop 2 mode in microseconds. In another thread you said you were setting this to 5 ms but is that because you were using a difference device that's not as good at waking up?

Or, is it because the wake up time in the datasheet doesn't include all the overhead required to really get mbed-os running again (enabling clocks, etc.)?

Thanks,
Matt

@jeromecoutant
Copy link
Collaborator

Hi

ForceClockOutofDeepSleep();

After STOP2 mode, we need to reconfigure PLL, this takes time.
As we got several issues around this, we added some clock initialisation code and some tempo, this takes also time...

About the value to apply, tests are on going on my side...
About power consumption argument, again, I am not sure... When you know that deepsleep takes time, and then power to update registers, maybe it is not so bad to stay in sleep for small delay ?

And finally, maybe this fix is no more useful after #8189 ...?
@c1728p9 what is your thinking about this PR and #8189?

@jeromecoutant
Copy link
Collaborator

@LMESTM

@c1728p9
Copy link
Contributor Author

c1728p9 commented Nov 8, 2018

And finally, maybe this fix is no more useful after #8189 ...?

@jeromecoutant this PR should still improve average wakeup latency even if it doesn't help with worst case latency.

if (block_deep_sleep) {
sleep_manager_lock_deep_sleep();
} else {
ticks_to_sleep -= MBED_CONF_TARGET_DEEP_SLEEP_LATENCY;
Copy link
Contributor

Choose a reason for hiding this comment

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

@c1728p9 - was discussing this with @sg-, and it occurred to me that this is being unduly pessimistic - you're subtracting the deep sleep latency from the wakeup time even if you're not going to be deep sleeping anyway, so in shallow operation there will be lots of unnecessary double firing.

May have to make the critical section wrapper bigger to synchronise a read of sleep_manager_can_deep_sleep. Maybe we could partially compensate by stripping the sleep_manager_lock_deep_sleep_internal() down - it doesn't need its critical section.

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.

7 participants