4

I want to know the number of active threads (iocp and worker) in threadpool. I use this technique:

ThreadPool.GetAvailableThreads(out var workerAvailable, out var iocpAvailable);
ThreadPool.GetMinThreads(out var workerMin, out var iocpMin);
ThreadPool.GetMaxThreads(out var workerMax, out var iocpMax);

var total = Process.GetCurrentProcess().Threads.Count;
var workerCurrent = workerMax - workerAvailable;
var iocpCurrent = iocpMax - iocpAvailable;
var other = total - worker - iocp;

There are quite strange numbers in logs: 8 worker, 3 iocp and 150 other threads.

I made a dump using procdump tool and inspected it using ClrMd (ClrThread.IsThreadpoolCompletionPort and ClrThread.IsThreadpoolWorker properties). Finally I got a different result from the dump: 99 worker and 14 iocp threads.

Why does the first approach return such strange result?

UPD: I suppose ThreadPool.GetAvailableThreads returns max minus currently active (not idle) threads.

mtkachenko
  • 5,389
  • 9
  • 38
  • 68
  • More to the point, why do you need this information? – Ian Kemp Feb 02 '19 at 20:00
  • @IanKemp I'm working on application which use threads and tasks a lot. And I want to monitor how it's going internally. – mtkachenko Feb 03 '19 at 07:25
  • 2
    The max values are pointless because these are upper limits which at least for IOCP thread are never hit. The numbers of Threadpool.GetAvailable thread are the Idle threads which wait for work. The internal state of the Threadpool is more complex. You can use ETW to track that data, or you can automate Windbg and use sos.dll to get the data you are after (see e.g. https://archive.codeplex.com/?p=wmemoryprofiler how this can be done). – Alois Kraus Feb 05 '19 at 13:58

0 Answers0