cpenny42 / juce_mongoose

A JUCE Module containing C++ Bindings for the popular mongoose lightweight web server
4 stars 2 forks source link

MacOS very high CPU usage #2

Open benkuper opened 5 years ago

benkuper commented 5 years ago

Hello, I couldn't test that myself, but I've had reports of MacOS users seeing very high CPU usage (more than 100% sometime) when using mongoose. Commenting the code using mongoose instantly dropped the CPU to less than 10%. If I have time I'll look into it, there may be a thread loop not handled correctly somewhere ?

old247 commented 5 years ago

Hello, same problem => GUI blocked on OSX due to mongoose.

cpenny42 commented 5 years ago

This definitely could be the case, I am not sure. This library is just a simple C++ wrapper for the mongoose library that can be conveniently added to JUCE projects (JUCE is a cross-platform application framework geared towards audio programming).

I also essentially took the inspiration behind this project: https://github.com/Gregwar/mongoose-cpp and did a similar thing, but wrapped in a JUCE module. The original Mongoose project is here: https://github.com/cesanta/mongoose

Any underlying issues with Mongoose should be reported to cesanta, and any issues with the C++ wrapper should be reported to Gregwar.

I'll do some testing though to see if there's any graceful way to handle GUI blocking on the JUCE-wrapper side.

benkuper commented 4 years ago

I found some dirty workaround, but at least it drops the CPU usage from 16% (on an hexacore !) to around 0.01% . Pages are not served instantly on localhost but the tradeoff is worth it, considering that it's totally blocking the UI on OSX and even on Windows it's just too much process for serving basic webpages.

Adding some sleep once in a while in mongoose_Server.cpp, line 110

    void Server::poll()
    {
#ifndef NO_WEBSOCKET
        unsigned int current_timer = 0;
#endif
        int index = 0;
        while (!stopped) {
            mg_poll_server(server, 1000);
#ifndef NO_WEBSOCKET
            mg_iterate_over_connections(server, iterate_callback, &current_timer);
#endif
            if(index == 0) sleep(1); //to avoid taking 100% of the cpu
            index = (index + 1) % 1000;
        }

        mg_destroy_server(&server);
        destroyed = true;
    }