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

Reduce Host emulator CPU load #2472

Merged
merged 12 commits into from
Jan 26, 2022
Merged

Conversation

mikee47
Copy link
Contributor

@mikee47 mikee47 commented Jan 24, 2022

This PR improves Host performance by using efficient waiting for tasks & timers rather than just looping.
Running the HttpServer_ConfigNetwork example sees a drop from 100% CPU usage to < 2% running in linux. Difference in Windows is less pronounced as the lwip layer incorporates a 1ms wait.

LWIP is now serviced using an event timer. The interval is adjusted dynamically which allows good file transfer performance and reduced CPU usage when idle.

Simplify build by using cmake for lwip only, sming glue built as regular sming library
Pull out common linux/Windows code
Service using event timer rather than polled timer
Remove LWIP_SERVICE_INTERVAL and adjust interval dynamically
Code required by both CMake and sming library builds
Time is absolute so must base on system clock.
Because this wasn't done will always succeed.

Change parameter to microseconds.
@slaff slaff added this to the 4.6.0 milestone Jan 24, 2022
return false;
}

extern CSemaphore host_main_loop_semaphore;
Copy link
Contributor

Choose a reason for hiding this comment

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

@mikee47 Can you move this line to the beginning of the file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll need to rewrite this using functions anyway.
As you know from CI the Windows run keeps stalling. It happens because the Windows pthreads library uses a mutex to guard semaphores. If that mutex is locked when the main thread is suspended, the next call to sem_post() results in a deadlock. Oh, the joys of multithreading.

@@ -271,6 +271,7 @@ int main(int argc, char* argv[])
return 1;
}

extern void host_init_bootloader();
Copy link
Contributor

Choose a reason for hiding this comment

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

@mikee47 Can you move this line to the beginning of the file or a separate header file?

lwip_initialised = host_lwip_init(&config.lwip);
if(lwip_initialised) {
if(host_lwip_init(config.lwip)) {
extern void host_wifi_lwip_init_complete();
Copy link
Contributor

Choose a reason for hiding this comment

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

@mikee47 Can you move this line to the beginning of the file or a separate header file?

Time structure same as sem_timedwait
Consistent with example code
Investigating cause of Windows deadlock.
May have other uses.
Replace global semaphore with `host_thread_wait` and `host_thread_kick`
@slaff
Copy link
Contributor

slaff commented Jan 26, 2022

@mikee47 this PR looks much better now. Shall I merge it or you would like to add something?

@mikee47
Copy link
Contributor Author

mikee47 commented Jan 26, 2022

@slaff All done thanks

@slaff slaff merged commit 90886ad into SmingHub:develop Jan 26, 2022
@slaff slaff mentioned this pull request Jan 26, 2022
5 tasks
@mikee47 mikee47 deleted the feature/host-events branch January 26, 2022 10:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants