From b94f701db8a22f7d640d5aeb3f24022fea35d978 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=A9rence=20Clastres?= Date: Sat, 20 Jun 2020 16:45:23 +0200 Subject: [PATCH] Fix high CPU usage when running the SDK server It was coming from `recv_select()` `nd accept_select()` The timeout for select() is only when waiting for the file descriptor to be ready for reading/writing so when the FD is ready AND there is nothing to read, it will not return and instead keep executing the while loop with no timeout. Fix this by adding a 100ms timeout when there is nothing to read. --- NetworkClient.cpp | 2 ++ NetworkServer.cpp | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/NetworkClient.cpp b/NetworkClient.cpp index 3ad996d5..79826896 100644 --- a/NetworkClient.cpp +++ b/NetworkClient.cpp @@ -246,6 +246,7 @@ int NetworkClient::recv_select(SOCKET s, char *buf, int len, int flags) } else if(rv == 0) { + std::this_thread::sleep_for(100ms); continue; } else @@ -253,6 +254,7 @@ int NetworkClient::recv_select(SOCKET s, char *buf, int len, int flags) // socket has something to read return(recv(s, buf, len, flags)); } + } } diff --git a/NetworkServer.cpp b/NetworkServer.cpp index a85b56de..68a205d6 100644 --- a/NetworkServer.cpp +++ b/NetworkServer.cpp @@ -30,6 +30,9 @@ const char yes = 1; #include #endif +using namespace std::chrono_literals; + + NetworkServer::NetworkServer(std::vector& control) : controllers(control) { port_num = OPENRGB_SDK_PORT; @@ -280,6 +283,7 @@ int NetworkServer::accept_select(int sockfd, struct sockaddr *addr, socklen_t *a } else if(rv == 0) { + std::this_thread::sleep_for(100ms); continue; } else @@ -310,6 +314,7 @@ int NetworkServer::recv_select(SOCKET s, char *buf, int len, int flags) } else if(rv == 0) { + std::this_thread::sleep_for(100ms); continue; } else