-
Notifications
You must be signed in to change notification settings - Fork 39
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
Busy loop in cpu_backend/task_queue.cpp keeps 1 thread at 100% CPU when queue is empty #80
Comments
You are right, this problem should be fixed. I tried using A better practice, as you mentioned, is to use short sleep to yield the core when idle waiting is detected. The modified code is as follows. It will be integrated into the repository. void TaskQueue::processTasks() {
auto start = std::chrono::steady_clock::now();
while (true) {
mutex.lock();
if (tasks.empty()) {
if (exit_flag.load(std::memory_order_seq_cst)) {
return;
}
mutex.unlock();
auto now = std::chrono::steady_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(now - start).count();
if (duration > 50) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
continue;
}
...
start = std::chrono::steady_clock::now();
}
} |
Actually I have been running a build with cond var for a few days (see sayap@9c81098), and there is no performance degradation during prefill/decode. This is on kernel 6.10. |
I tried your code, and indeed there is no performance degradation; there might have been a mistake in my previous testing method. It would be great if you could submit a Pull Request to our project. By the way, in cpu_backend/backend.cpp, there is a group of threads ( |
Cool, I created pull request #83. Haven't tested it on Windows though. |
Ah I see. When I searched for what was causing the 100% CPU, I did see a lot of 1ms sleeps in |
Hi, I notice that the busy loop in cpu_backend/task_queue.cpp will keep 1 thread at 100% CPU when the queue is empty:
On a laptop, this busy thread will keep the fans on all the time.
If I change the code to use a
std::condition_variable
, then all the threads will be at near 0% CPU when the queue is empty.I suppose
std::condition_variable
also works on Windows, but I don't have a machine ready to test that. If it doesn't work on Windows, then we can add a short sleep beforecontinue
as a workaround. From my testing, a short sleep of 0.1ms can keep the threads at near 0% CPU without impacting the queue processing speed. However, this feels much less elegant compared to using a conditional variable 😅The text was updated successfully, but these errors were encountered: