Excessive context switching in FreeBSD dotnet ThreadPool worker threads #108079
Labels
area-System.Threading
os-freebsd
FreeBSD OS
tenet-performance
Performance related issue
untriaged
New issue has not been triaged by the area owner
Description
On FreeBSD, ThreadPool worker threads (identified by ".NET TP Worker") cause an excessive (as compared to Linux) number of voluntary context switches. This consumes excessive system and user CPU resources even when there's not a lot of work being done in the thread pools.
Here is a minimal example that demonstrates the problem:
This program submits
Sleep(1)
jobs to the thread pool workers about 200 times per second. If you comment out theThreadPool.QueueUserWorkItem
, this program generates about ~200 voluntary context switches per second, since it sleeps for 5 ms between successive iterations of the loop. However, with the ThreadPool, the program starts generating in excess of 50000 voluntary context switches per second.I tested the same program on Linux, and it doesn't generate nearly as many context switches.
Configuration
Tested with similar results on:
Architectures:
And dotnet SDKs:
Regression?
Not Known
Data
On FreeBSD,
top
reports context switches when run astop -m io -H
:Analysis
ThreadPoolWorkQueue.cs
appears to use something calledSpinLock
which in turn usesSpinWait
which alternates calls toThread.Yield()
,Thread.Sleep(0)
, andThread.Sleep(1)
. Perhaps these things behave slightly differently on FreeBSD, and need changing on this platform?The text was updated successfully, but these errors were encountered: