Skip to content
This repository was archived by the owner on Nov 13, 2026. It is now read-only.

Enable CLR Thread Pool via a MSBuild flag (default enabled on Unix) - #4124

Closed
jkoritzinsky wants to merge 44 commits into
dotnet:masterfrom
jkoritzinsky:EnableClrThreadPool
Closed

jkoritzinsky wants to merge 44 commits into
dotnet:masterfrom
jkoritzinsky:EnableClrThreadPool

Conversation

@jkoritzinsky

@jkoritzinsky jkoritzinsky commented Jul 10, 2017 •

Copy link
Copy Markdown
Member

This PR adds tests for the CLR Thread Pool. It also adds Windows implementations for the low level primitives used in the CLR Thread Pool.

The thread pool is enabled by setting the MSBuild property FeaturePortableThreadPool to true, which is the default on Unix.

Supersedes #4039.

@kouvel @sergiy-k @AntonLapounov @jkotas

Note: This is tagged * NO MERGE * because it intermittently exposes a GC crash in root scan during thread setup. That bug is being tracked by #4214.

@dnfclas

dnfclas commented Jul 10, 2017

Copy link
Copy Markdown

@jkoritzinsky,
Thanks for having already signed the Contribution License Agreement. Your agreement was validated by .NET Foundation. We will now review your pull request.
Thanks,
.NET Foundation Pull Request Bot

internal struct SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION
{
// These fields are all assigned by the native call.
#pragma warning disable CS0649

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mark the struct as [StructLayout(LayoutKind.Sequential)]. It will disable the warning as well, but it looks better.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know that would disable the warning. Good to know!

public struct CRITICAL_SECTION
{
    IntPtr _debugInfo;
    long _lockCount;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LONG in Windows C++ does not map to long in C#. LONG in Windows C++ is 32-bit integer, and thus it should be int in C#.

{
if (s_frequency == 0)
{
Interop.Kernel32.QueryPerformanceFrequency(out s_frequency);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not thread-safe for multiple reasons:

  • Assignment of 64-bit numbers is not atomic on 32-bit platforms
  • You do not know whether QueryPerformanceFrequency assigns the result just once.

Instead, you should assign it via before field init constructor. Something like:

private static readonly ulong s_frequency = GetFrequency();

It will guarantee atomicity.

@jkoritzinsky
jkoritzinsky force-pushed the EnableClrThreadPool branch from e815906 to 03aac74 Compare July 12, 2017 20:54
{
if(s_querySystemInformation == null)
{
IntPtr ntDll = LoadLibraryEx("NtDll.dll", IntPtr.Zero, 0);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should pass LOAD_LIBRARY_SEARCH_SYSTEM32 flag instead of 0 - we do not want to be loading any random ntdll.dll laying around.

_cpuInfo.numberOfProcessors = ThreadPoolGlobals.processorCount;

_cpuInfo.usageBuffer = new Interop.mincore.SystemProcessorPerformanceInformation[ThreadPoolGlobals.processorCount];
GetCpuUtilization(); // Call once to initialize the usage buffer

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think this is necessary anymore for the purpose noted. It would be better to initialize it in GateThreadStart before the loop, such that every calculation of the CPU utilization would be the value over a 500 ms interval instead of some arbitrary interval.

}
s_requested = false;

ThreadPoolInstance._cpuUtilization = s_cpu.CurrentUtilization;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be done on every iteration of the loop even if there are no pending requests since hill climbing also uses the cpu utilization to make decisions.

_cpuInfo.userTime = userTime;
_cpuInfo.idleTime = idleTime;

if (cpuTotalTime > 0)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check would have caught negative values before when it was signed. Could make cpuTotalTime and cpuBusyTime signed. Should also check that cpuBusyTime >= 0.


if (cpuTotalTime > 0)
{
ulong reading = cpuBusyTime * 100 / cpuTotalTime;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if the values returned by GetSystemTimes are monotonic, but we shouldn't allow values greater than 100, if cpuBusyTime >= cpuTotalTime, reading could be set to 100 and changed to signed long as well.

if (cpuTotalTime > 0)
{
ulong reading = cpuBusyTime * 100 / cpuTotalTime;
Debug.Assert(0 <= reading && reading <= int.MaxValue);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then this assert could say <= 100


public CpuUtilizationReader()
{
GetCpuUtilization(); // Call once to initialize the usage buffer

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think this is necessary anymore for the reason indicated. It would be better to initialize this at the top of GateThreadStart before the loop, such that every calculated CPU utilization is a value taken over a 500 ms interval rather than over an arbitrary interval.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still need to do this initial read to initialize our data structure. Without it, the first read would give us the CPU utilization since system startup I believe, which is not what we want.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just suggesting to do the first read in GateThreadStart, since that function is the only one that takes any readings. That's sort of what CoreCLR is doing, to ensure that every stored reading is over a 500 ms interval. Otherwise, the first stored reading would give us CPU utilization between process start time and first use of the thread pool.

// TODO: CoreCLR: Worker Tracking in CoreCLR? (Config name: ThreadPool_EnableWorkerTracking)
private static void GateThreadStart()
{
CpuUtilizationReader cpu = new CpuUtilizationReader();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could OOM, may need to precreate and call some Initialize method

@kouvel

kouvel commented Jul 19, 2017 •

Copy link
Copy Markdown
Contributor

I'm told there are occasional crashes during GC root scan during thread startup, marked as no-merge until that is resolved.

@jkotas

jkotas commented Sep 28, 2017

Copy link
Copy Markdown
Member

@dotnet-bot test this please

@jkotas jkotas closed this Sep 28, 2017
@jkotas jkotas reopened this Sep 28, 2017
@dnfclas

dnfclas commented Sep 28, 2017

Copy link
Copy Markdown

@jkoritzinsky,
Thanks for having already signed the Contribution License Agreement. Your agreement was validated by .NET Foundation. We will now review your pull request.
Thanks,
.NET Foundation Pull Request Bot

@jkotas

jkotas commented Sep 28, 2017

Copy link
Copy Markdown
Member

@dotnet-bot test this please

@kouvel

kouvel commented Sep 28, 2017

Copy link
Copy Markdown
Contributor

I'll rebase and squash this into a new PR, I'll need to revert a change from CoreCLR that was merged into this change as well, I'll do that in a separate commit. Closing for now.

@kouvel kouvel closed this Sep 28, 2017
jkotas pushed a commit that referenced this pull request Sep 29, 2017
This PR completes, adds tests for, and enables the CLR Thread Pool on Unix. It also adds Windows implementations for the low level primitives used in the CLR Thread Pool.

The thread pool is enabled by setting the MSBuild property FeaturePortableThreadPool to true, which is the default on Unix.

Supersedes #4039.
Rebased and squashed replacement for original PR #4124.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants