-
Notifications
You must be signed in to change notification settings - Fork 5.4k
CPU utilization computation fixes #2196
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,35 +9,27 @@ namespace System.Threading | |
| { | ||
| internal partial class PortableThreadPool | ||
| { | ||
| private class CpuUtilizationReader | ||
| private struct CpuUtilizationReader | ||
| { | ||
| private struct ProcessCpuInformation | ||
| { | ||
| public long idleTime; | ||
| public long kernelTime; | ||
| public long userTime; | ||
| } | ||
|
|
||
| private ProcessCpuInformation _processCpuInfo = new ProcessCpuInformation(); | ||
| public long _idleTime; | ||
| public long _kernelTime; | ||
| public long _userTime; | ||
|
|
||
| public int CurrentUtilization | ||
| { | ||
| get | ||
| { | ||
| if (!Interop.Kernel32.GetSystemTimes(out var idleTime, out var kernelTime, out var userTime)) | ||
| { | ||
| int error = Marshal.GetLastWin32Error(); | ||
| var exception = new OutOfMemoryException(); | ||
| exception.HResult = error; | ||
| throw exception; | ||
| return 0; | ||
| } | ||
|
|
||
| long cpuTotalTime = ((long)userTime - _processCpuInfo.userTime) + ((long)kernelTime - _processCpuInfo.kernelTime); | ||
| long cpuBusyTime = cpuTotalTime - ((long)idleTime - _processCpuInfo.idleTime); | ||
| long cpuTotalTime = ((long)userTime - _userTime) + ((long)kernelTime - _kernelTime); | ||
| long cpuBusyTime = cpuTotalTime - ((long)idleTime - _idleTime); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have any tests that would fall if we got something wrong here?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently, this is only used for Mono on Windows that gets a very light testing. If we got this wrong, this would manifest itself as a performance bug in thread pool scaling. Hopefully, we will find time to switch to the managed threadpool for CoreCLR too. Validating details like this would be the bulk of the work for that. |
||
|
|
||
| _processCpuInfo.kernelTime = (long)kernelTime; | ||
| _processCpuInfo.userTime = (long)userTime; | ||
| _processCpuInfo.idleTime = (long)idleTime; | ||
| _kernelTime = (long)kernelTime; | ||
| _userTime = (long)userTime; | ||
| _idleTime = (long)idleTime; | ||
|
|
||
| if (cpuTotalTime > 0 && cpuBusyTime > 0) | ||
| { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a little confused by removing the multiplication by number of processors, but then this saying it's larger for more processors. I'm sure you're right, but what's my misunderstanding?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
System.Native shim does not have easy access to the process count adjusted for container limits. The processor count on managed size is adjusted and thus it is the right number to use to compute how much spare cycles may extra threads take advantage of.
In other words, this change should get us equivalent of this piece of logic from CoreCLR PAL.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's essentially removing division, not multiplication. It's not obvious from reading the unified diff because GitHub hides the
cpuUtilization = (int32_t)(cpuBusyTime * 100 / cpuTotalTime);line. The multiplication incpuTotalTimeis affecting the divisor.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yeah, I missed that, thanks.