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

Workaround for 100% CPU usage problem on the virtual device #1167

Merged
merged 2 commits into from Nov 24, 2016

Conversation

@sergeuz
Copy link
Member

commented Nov 11, 2016

This PR fixes #1036 by suspending main thread for some minimum time on every Nth iteration of the application loop. That's probably not the best solution possible, but it allowed to reduce CPU usage from 100 to 10-15% on my machine (Intel Core i7), while keeping reasonable performance comparing to the real devices.

Note that calling std::this_thread::yield() has appeared to be insufficient to workaround the problem, also a simple unconditional sleep on each iteration makes an application significantly slower comparing to the same application running on the real device (at least 7 times slower in my tests).

Below is the simple test application that can be used to check how many times application loop is being invoked per second:

#include "application.h"

SYSTEM_MODE(MANUAL)

SerialLogHandler logHandler(LOG_LEVEL_WARN, {
    { "app", LOG_LEVEL_ALL }
});

uint32_t t = 0;
unsigned n = 0;

void setup() {
    t = millis();
}

void loop() {
    ++n;
    if (millis() - t >= 1000) {
        Log.info("%u", n);
        t = millis();
        n = 0;
    }
}

Doneness:

  • Contributor has signed CLA
  • Problem and Solution clearly stated
  • Code peer reviewed
  • API tests compiled
  • Run unit/integration/application tests on device
  • Add documentation
  • Add to CHANGELOG.md after merging (add links to docs and issues)

Internal

@sergeuz sergeuz added this to the 0.7.x milestone Nov 11, 2016

@monkbroc
Copy link
Member

left a comment

Nice solution for this.

@m-mcgowan

This comment has been minimized.

Copy link
Contributor

commented Nov 12, 2016

Great writeup and good work around for the time being. 👍

So...can we remove the hard-coded value of 25 loops and push that into a define? then we have a way of tweaking the number for different use cases.

@m-mcgowan m-mcgowan merged commit a31ec9f into develop Nov 24, 2016

2 checks passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
continuous-integration/travis-ci/push The Travis CI build passed
Details

@technobly technobly modified the milestones: 0.7.x, 0.6.1 Nov 29, 2016

@joegoggins joegoggins referenced this pull request Jun 28, 2019
3 of 3 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.