From 844788989a46eb649c4f1f243bfb5d99f906b3bd Mon Sep 17 00:00:00 2001 From: Sung Yoon Whang Date: Thu, 29 Aug 2019 01:22:31 -0700 Subject: [PATCH 01/12] Use win32 api directly for workingset counter --- .../Kernel32/Interop.GetProcessMemoryInfo.cs | 31 +++++++++++++++++++ .../Interop.GetProcessWorkingSetSize.cs | 15 +++++++++ .../System.Private.CoreLib.Shared.projitems | 2 ++ .../Eventing/RuntimeEventSource.cs | 7 ++++- .../Eventing/RuntimeEventSourceHelper.Unix.cs | 7 +++++ .../RuntimeEventSourceHelper.Windows.cs | 17 ++++++++++ 6 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessMemoryInfo.cs create mode 100644 src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessWorkingSetSize.cs diff --git a/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessMemoryInfo.cs b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessMemoryInfo.cs new file mode 100644 index 000000000000..382bfafeb593 --- /dev/null +++ b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessMemoryInfo.cs @@ -0,0 +1,31 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + // Used for retrieving working set information directly via pinvoke. + [StructLayout(LayoutKind.Sequential, Size=72)] + internal struct PROCESS_MEMORY_COUNTERS + { + public uint cb; + public uint PageFaultCount; + public UInt64 PeakWorkingSetSize; + public UInt64 WorkingSetSize; + public UInt64 QuotaPeakPagedPoolUsage; + public UInt64 QuotaPagedPoolUsage; + public UInt64 QuotaPeakNonPagedPoolUsage; + public UInt64 QuotaNonPagedPoolUsage; + public UInt64 PagefileUsage; + public UInt64 PeakPagefileUsage; + } + + internal partial class Kernel32 + { + [DllImport("psapi.dll", SetLastError = true)] + internal static extern bool GetProcessMemoryInfo(IntPtr handleProcess, out PROCESS_MEMORY_COUNTERS pmCounter, uint cb); + } +} diff --git a/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessWorkingSetSize.cs b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessWorkingSetSize.cs new file mode 100644 index 000000000000..9eb7bf283f82 --- /dev/null +++ b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessWorkingSetSize.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class Kernel32 + { + [DllImport(Libraries.Kernel32, SetLastError = true)] + internal static extern bool GetProcessWorkingSetSize(IntPtr handleProcess, out long minWS, out long maxWS); + } +} diff --git a/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems b/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems index 65c68d8a9f01..6134174cdf64 100644 --- a/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems +++ b/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems @@ -1048,6 +1048,8 @@ + + diff --git a/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSource.cs b/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSource.cs index 805b3e6139b5..959ad28f6129 100644 --- a/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSource.cs +++ b/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSource.cs @@ -20,6 +20,9 @@ internal sealed class RuntimeEventSource : EventSource private IncrementingPollingCounter? _exceptionCounter; private PollingCounter? _cpuTimeCounter; private PollingCounter? _workingSetCounter; + + private PollingCounter? _eworkingSetCounter; + private PollingCounter? _threadPoolThreadCounter; private IncrementingPollingCounter? _monitorContentionCounter; private PollingCounter? _threadPoolQueueCounter; @@ -52,7 +55,9 @@ protected override void OnEventCommand(EventCommandEventArgs command) // On disable, PollingCounters will stop polling for values so it should be fine to leave them around. _cpuTimeCounter = _cpuTimeCounter ?? new PollingCounter("cpu-usage", this, () => RuntimeEventSourceHelper.GetCpuUsage()) { DisplayName = "CPU Usage", DisplayUnits = "%" }; - _workingSetCounter = _workingSetCounter ?? new PollingCounter("working-set", this, () => (double)(Environment.WorkingSet / 1000000)) { DisplayName = "Working Set", DisplayUnits = "MB" }; + _workingSetCounter = _workingSetCounter ?? new PollingCounter("working-set", this, () => (double)(RuntimeEventSourceHelper.GetWorkingSet())) { DisplayName = "Working Set", DisplayUnits = "MB" }; + _eworkingSetCounter = _eworkingSetCounter ?? new PollingCounter("e-working-set", this, () => (double)(Environment.WorkingSet)) { DisplayName = "Env Working Set", DisplayUnits = "MB" }; + _gcHeapSizeCounter = _gcHeapSizeCounter ?? new PollingCounter("gc-heap-size", this, () => (double)(GC.GetTotalMemory(false) / 1000000)) { DisplayName = "GC Heap Size", DisplayUnits = "MB" }; _gen0GCCounter = _gen0GCCounter ?? new IncrementingPollingCounter("gen-0-gc-count", this, () => GC.CollectionCount(0)) { DisplayName = "Gen 0 GC Count", DisplayRateTimeScale = new TimeSpan(0, 1, 0) }; _gen1GCCounter = _gen1GCCounter ?? new IncrementingPollingCounter("gen-1-gc-count", this, () => GC.CollectionCount(1)) { DisplayName = "Gen 1 GC Count", DisplayRateTimeScale = new TimeSpan(0, 1, 0) }; diff --git a/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Unix.cs b/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Unix.cs index 776ad5d3b692..9344414fc6a5 100644 --- a/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Unix.cs +++ b/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Unix.cs @@ -14,5 +14,12 @@ internal static int GetCpuUsage() { return Interop.Sys.GetCpuUtilization(ref cpuInfo); } + + + internal static long GetWorkingSet() + { + // Returns the current processs' WorkingSet + return 0; + } } } diff --git a/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Windows.cs b/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Windows.cs index 6a74c7f9f498..45635d18d7cb 100644 --- a/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Windows.cs +++ b/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Windows.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Runtime.InteropServices; + namespace System.Diagnostics.Tracing { internal sealed class RuntimeEventSourceHelper @@ -45,5 +47,20 @@ internal static int GetCpuUsage() return cpuUsage; } + + internal static long GetWorkingSet() + { + + Interop.PROCESS_MEMORY_COUNTERS memoryCounters; + memoryCounters.cb = (uint)(Marshal.SizeOf(typeof(Interop.PROCESS_MEMORY_COUNTERS))); + + // Returns the current processs' WorkingSet + if (!Interop.Kernel32.GetProcessMemoryInfo(Interop.Kernel32.GetCurrentProcess(), out memoryCounters, memoryCounters.cb)) + { + Debug.WriteLine("Failed: GetProcessMemoryInfo returned false"); + return 0; + } + return (long)memoryCounters.WorkingSetSize; + } } } From 37e3d4d026eaac97b1fe1f4bdd2a3afcf8d22e58 Mon Sep 17 00:00:00 2001 From: Sung Yoon Whang Date: Wed, 4 Sep 2019 17:33:18 -0700 Subject: [PATCH 02/12] Fix build warnings --- .../Kernel32/Interop.GetProcessMemoryInfo.cs | 30 +++++++++---------- .../RuntimeEventSourceHelper.Windows.cs | 2 +- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessMemoryInfo.cs b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessMemoryInfo.cs index 382bfafeb593..5a37633e72c0 100644 --- a/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessMemoryInfo.cs +++ b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessMemoryInfo.cs @@ -7,21 +7,21 @@ internal partial class Interop { - // Used for retrieving working set information directly via pinvoke. - [StructLayout(LayoutKind.Sequential, Size=72)] - internal struct PROCESS_MEMORY_COUNTERS - { - public uint cb; - public uint PageFaultCount; - public UInt64 PeakWorkingSetSize; - public UInt64 WorkingSetSize; - public UInt64 QuotaPeakPagedPoolUsage; - public UInt64 QuotaPagedPoolUsage; - public UInt64 QuotaPeakNonPagedPoolUsage; - public UInt64 QuotaNonPagedPoolUsage; - public UInt64 PagefileUsage; - public UInt64 PeakPagefileUsage; - } + // Used for retrieving working set information directly via pinvoke. + [StructLayout(LayoutKind.Sequential, Size=72)] + internal struct PROCESS_MEMORY_COUNTERS + { + public uint cb; + public uint PageFaultCount; + public ulong PeakWorkingSetSize; + public ulong WorkingSetSize; + public ulong QuotaPeakPagedPoolUsage; + public ulong QuotaPagedPoolUsage; + public ulong QuotaPeakNonPagedPoolUsage; + public ulong QuotaNonPagedPoolUsage; + public ulong PagefileUsage; + public ulong PeakPagefileUsage; + } internal partial class Kernel32 { diff --git a/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Windows.cs b/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Windows.cs index 45635d18d7cb..4c5483c22426 100644 --- a/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Windows.cs +++ b/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Windows.cs @@ -53,7 +53,7 @@ internal static long GetWorkingSet() Interop.PROCESS_MEMORY_COUNTERS memoryCounters; memoryCounters.cb = (uint)(Marshal.SizeOf(typeof(Interop.PROCESS_MEMORY_COUNTERS))); - + // Returns the current processs' WorkingSet if (!Interop.Kernel32.GetProcessMemoryInfo(Interop.Kernel32.GetCurrentProcess(), out memoryCounters, memoryCounters.cb)) { From b92c1a6a50f8eeae709ce6954e0d4716e71bfb73 Mon Sep 17 00:00:00 2001 From: Sung Yoon Whang Date: Wed, 4 Sep 2019 17:41:10 -0700 Subject: [PATCH 03/12] Removing useless code --- .../Interop.GetProcessWorkingSetSize.cs | 15 ------ .../System.Private.CoreLib.Shared.projitems | 1 - .../Eventing/RuntimeEventSource.cs | 46 ++++++++----------- .../Eventing/RuntimeEventSourceHelper.Unix.cs | 2 +- 4 files changed, 21 insertions(+), 43 deletions(-) delete mode 100644 src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessWorkingSetSize.cs diff --git a/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessWorkingSetSize.cs b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessWorkingSetSize.cs deleted file mode 100644 index 9eb7bf283f82..000000000000 --- a/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessWorkingSetSize.cs +++ /dev/null @@ -1,15 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Runtime.InteropServices; - -internal partial class Interop -{ - internal partial class Kernel32 - { - [DllImport(Libraries.Kernel32, SetLastError = true)] - internal static extern bool GetProcessWorkingSetSize(IntPtr handleProcess, out long minWS, out long maxWS); - } -} diff --git a/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems b/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems index 83aeab25ceb1..c53a5b450628 100644 --- a/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems +++ b/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems @@ -1055,7 +1055,6 @@ - diff --git a/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSource.cs b/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSource.cs index aafaffa96aca..84b7e67a457e 100644 --- a/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSource.cs +++ b/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSource.cs @@ -20,9 +20,6 @@ internal sealed class RuntimeEventSource : EventSource private IncrementingPollingCounter? _exceptionCounter; private PollingCounter? _cpuTimeCounter; private PollingCounter? _workingSetCounter; - - private PollingCounter? _eworkingSetCounter; - private PollingCounter? _threadPoolThreadCounter; private IncrementingPollingCounter? _monitorContentionCounter; private PollingCounter? _threadPoolQueueCounter; @@ -54,29 +51,26 @@ protected override void OnEventCommand(EventCommandEventArgs command) // overhead by at all times even when counters aren't enabled. // On disable, PollingCounters will stop polling for values so it should be fine to leave them around. - _cpuTimeCounter = _cpuTimeCounter ?? new PollingCounter("cpu-usage", this, () => RuntimeEventSourceHelper.GetCpuUsage()) { DisplayName = "CPU Usage", DisplayUnits = "%" }; - _workingSetCounter = _workingSetCounter ?? new PollingCounter("working-set", this, () => (double)(RuntimeEventSourceHelper.GetWorkingSet())) { DisplayName = "Working Set", DisplayUnits = "MB" }; - _eworkingSetCounter = _eworkingSetCounter ?? new PollingCounter("e-working-set", this, () => (double)(Environment.WorkingSet)) { DisplayName = "Env Working Set", DisplayUnits = "MB" }; - - _gcHeapSizeCounter = _gcHeapSizeCounter ?? new PollingCounter("gc-heap-size", this, () => (double)(GC.GetTotalMemory(false) / 1000000)) { DisplayName = "GC Heap Size", DisplayUnits = "MB" }; - _gen0GCCounter = _gen0GCCounter ?? new IncrementingPollingCounter("gen-0-gc-count", this, () => GC.CollectionCount(0)) { DisplayName = "Gen 0 GC Count", DisplayRateTimeScale = new TimeSpan(0, 1, 0) }; - _gen1GCCounter = _gen1GCCounter ?? new IncrementingPollingCounter("gen-1-gc-count", this, () => GC.CollectionCount(1)) { DisplayName = "Gen 1 GC Count", DisplayRateTimeScale = new TimeSpan(0, 1, 0) }; - _gen2GCCounter = _gen2GCCounter ?? new IncrementingPollingCounter("gen-2-gc-count", this, () => GC.CollectionCount(2)) { DisplayName = "Gen 2 GC Count", DisplayRateTimeScale = new TimeSpan(0, 1, 0) }; - _exceptionCounter = _exceptionCounter ?? new IncrementingPollingCounter("exception-count", this, () => Exception.GetExceptionCount()) { DisplayName = "Exception Count", DisplayRateTimeScale = new TimeSpan(0, 0, 1) }; - _threadPoolThreadCounter = _threadPoolThreadCounter ?? new PollingCounter("threadpool-thread-count", this, () => ThreadPool.ThreadCount) { DisplayName = "ThreadPool Thread Count" }; - _monitorContentionCounter = _monitorContentionCounter ?? new IncrementingPollingCounter("monitor-lock-contention-count", this, () => Monitor.LockContentionCount) { DisplayName = "Monitor Lock Contention Count", DisplayRateTimeScale = new TimeSpan(0, 0, 1) }; - _threadPoolQueueCounter = _threadPoolQueueCounter ?? new PollingCounter("threadpool-queue-length", this, () => ThreadPool.PendingWorkItemCount) { DisplayName = "ThreadPool Queue Length" }; - _completedItemsCounter = _completedItemsCounter ?? new IncrementingPollingCounter("threadpool-completed-items-count", this, () => ThreadPool.CompletedWorkItemCount) { DisplayName = "ThreadPool Completed Work Item Count", DisplayRateTimeScale = new TimeSpan(0, 0, 1) }; - _gcTimeCounter = _gcTimeCounter ?? new PollingCounter("time-in-gc", this, () => GC.GetLastGCPercentTimeInGC()) { DisplayName = "% Time in GC since last GC", DisplayUnits = "%" }; - _gen0SizeCounter = _gen0SizeCounter ?? new PollingCounter("gen-0-size", this, () => GC.GetGenerationSize(0)) { DisplayName = "Gen 0 Size", DisplayUnits="B" }; - _gen1SizeCounter = _gen1SizeCounter ?? new PollingCounter("gen-1-size", this, () => GC.GetGenerationSize(1)) { DisplayName = "Gen 1 Size", DisplayUnits="B" }; - _gen2SizeCounter = _gen2SizeCounter ?? new PollingCounter("gen-2-size", this, () => GC.GetGenerationSize(2)) { DisplayName = "Gen 2 Size", DisplayUnits="B" }; - _lohSizeCounter = _lohSizeCounter ?? new PollingCounter("loh-size", this, () => GC.GetGenerationSize(3)) { DisplayName = "LOH Size", DisplayUnits="B" }; - _allocRateCounter = _allocRateCounter ?? new IncrementingPollingCounter("alloc-rate", this, () => GC.GetTotalAllocatedBytes()) { DisplayName = "Allocation Rate", DisplayUnits="B", DisplayRateTimeScale = new TimeSpan(0, 0, 1) }; - _assemblyCounter = _assemblyCounter ?? new PollingCounter("assembly-count", this, () => System.Reflection.Assembly.GetAssemblyCount()) { DisplayName = "Number of Assemblies Loaded" }; - _timerCounter = _timerCounter ?? new PollingCounter("active-timer-count", this, () => Timer.ActiveCount) { DisplayName = "Number of Active Timers" }; - + _cpuTimeCounter ??= new PollingCounter("cpu-usage", this, () => RuntimeEventSourceHelper.GetCpuUsage()) { DisplayName = "CPU Usage", DisplayUnits = "%" }; + _workingSetCounter ??= new PollingCounter("working-set", this, () => (double)(RuntimeEventSourceHelper.GetWorkingSet() / 1000000)) { DisplayName = "Working Set", DisplayUnits = "MB" }; + _gcHeapSizeCounter ??= new PollingCounter("gc-heap-size", this, () => (double)(GC.GetTotalMemory(false) / 1000000)) { DisplayName = "GC Heap Size", DisplayUnits = "MB" }; + _gen0GCCounter ??= new IncrementingPollingCounter("gen-0-gc-count", this, () => GC.CollectionCount(0)) { DisplayName = "Gen 0 GC Count", DisplayRateTimeScale = new TimeSpan(0, 1, 0) }; + _gen1GCCounter ??= new IncrementingPollingCounter("gen-1-gc-count", this, () => GC.CollectionCount(1)) { DisplayName = "Gen 1 GC Count", DisplayRateTimeScale = new TimeSpan(0, 1, 0) }; + _gen2GCCounter ??= new IncrementingPollingCounter("gen-2-gc-count", this, () => GC.CollectionCount(2)) { DisplayName = "Gen 2 GC Count", DisplayRateTimeScale = new TimeSpan(0, 1, 0) }; + _exceptionCounter ??= new IncrementingPollingCounter("exception-count", this, () => Exception.GetExceptionCount()) { DisplayName = "Exception Count", DisplayRateTimeScale = new TimeSpan(0, 0, 1) }; + _threadPoolThreadCounter ??= new PollingCounter("threadpool-thread-count", this, () => ThreadPool.ThreadCount) { DisplayName = "ThreadPool Thread Count" }; + _monitorContentionCounter ??= new IncrementingPollingCounter("monitor-lock-contention-count", this, () => Monitor.LockContentionCount) { DisplayName = "Monitor Lock Contention Count", DisplayRateTimeScale = new TimeSpan(0, 0, 1) }; + _threadPoolQueueCounter ??= new PollingCounter("threadpool-queue-length", this, () => ThreadPool.PendingWorkItemCount) { DisplayName = "ThreadPool Queue Length" }; + _completedItemsCounter ??= new IncrementingPollingCounter("threadpool-completed-items-count", this, () => ThreadPool.CompletedWorkItemCount) { DisplayName = "ThreadPool Completed Work Item Count", DisplayRateTimeScale = new TimeSpan(0, 0, 1) }; + _gcTimeCounter ??= new PollingCounter("time-in-gc", this, () => GC.GetLastGCPercentTimeInGC()) { DisplayName = "% Time in GC since last GC", DisplayUnits = "%" }; + _gen0SizeCounter ??= new PollingCounter("gen-0-size", this, () => GC.GetGenerationSize(0)) { DisplayName = "Gen 0 Size", DisplayUnits = "B" }; + _gen1SizeCounter ??= new PollingCounter("gen-1-size", this, () => GC.GetGenerationSize(1)) { DisplayName = "Gen 1 Size", DisplayUnits = "B" }; + _gen2SizeCounter ??= new PollingCounter("gen-2-size", this, () => GC.GetGenerationSize(2)) { DisplayName = "Gen 2 Size", DisplayUnits = "B" }; + _lohSizeCounter ??= new PollingCounter("loh-size", this, () => GC.GetGenerationSize(3)) { DisplayName = "LOH Size", DisplayUnits = "B" }; + _allocRateCounter ??= new IncrementingPollingCounter("alloc-rate", this, () => GC.GetTotalAllocatedBytes()) { DisplayName = "Allocation Rate", DisplayUnits = "B", DisplayRateTimeScale = new TimeSpan(0, 0, 1) }; + _assemblyCounter ??= new PollingCounter("assembly-count", this, () => System.Reflection.Assembly.GetAssemblyCount()) { DisplayName = "Number of Assemblies Loaded" }; + _timerCounter ??= new PollingCounter("active-timer-count", this, () => Timer.ActiveCount) { DisplayName = "Number of Active Timers" }; } } } -} +} \ No newline at end of file diff --git a/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Unix.cs b/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Unix.cs index 9344414fc6a5..5a607e3fae71 100644 --- a/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Unix.cs +++ b/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Unix.cs @@ -19,7 +19,7 @@ internal static int GetCpuUsage() internal static long GetWorkingSet() { // Returns the current processs' WorkingSet - return 0; + return Environment.WorkingSet; } } } From a1d5f95a5cd542300fe38031e001bd12a920beaa Mon Sep 17 00:00:00 2001 From: Sung Yoon Whang Date: Wed, 4 Sep 2019 22:07:26 -0700 Subject: [PATCH 04/12] more cleanup --- .../Windows/Kernel32/Interop.GetProcessMemoryInfo.cs | 4 ++-- .../Diagnostics/Eventing/RuntimeEventSourceHelper.Unix.cs | 2 -- .../Eventing/RuntimeEventSourceHelper.Windows.cs | 7 ++----- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessMemoryInfo.cs b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessMemoryInfo.cs index 5a37633e72c0..db6b25691d7b 100644 --- a/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessMemoryInfo.cs +++ b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessMemoryInfo.cs @@ -9,7 +9,7 @@ internal partial class Interop { // Used for retrieving working set information directly via pinvoke. [StructLayout(LayoutKind.Sequential, Size=72)] - internal struct PROCESS_MEMORY_COUNTERS + internal struct ProcessMemoryCounters { public uint cb; public uint PageFaultCount; @@ -26,6 +26,6 @@ internal struct PROCESS_MEMORY_COUNTERS internal partial class Kernel32 { [DllImport("psapi.dll", SetLastError = true)] - internal static extern bool GetProcessMemoryInfo(IntPtr handleProcess, out PROCESS_MEMORY_COUNTERS pmCounter, uint cb); + internal static extern bool GetProcessMemoryInfo(IntPtr handleProcess, out ProcessMemoryCounters pmCounter, uint cb); } } diff --git a/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Unix.cs b/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Unix.cs index 5a607e3fae71..5ac8033a2433 100644 --- a/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Unix.cs +++ b/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Unix.cs @@ -15,10 +15,8 @@ internal static int GetCpuUsage() return Interop.Sys.GetCpuUtilization(ref cpuInfo); } - internal static long GetWorkingSet() { - // Returns the current processs' WorkingSet return Environment.WorkingSet; } } diff --git a/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Windows.cs b/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Windows.cs index 4c5483c22426..7cd63c33f225 100644 --- a/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Windows.cs +++ b/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Windows.cs @@ -50,14 +50,11 @@ internal static int GetCpuUsage() internal static long GetWorkingSet() { + Interop.ProcessMemoryCounters memoryCounters; + memoryCounters.cb = (uint)(Marshal.SizeOf(typeof(Interop.ProcessMemoryCounters))); - Interop.PROCESS_MEMORY_COUNTERS memoryCounters; - memoryCounters.cb = (uint)(Marshal.SizeOf(typeof(Interop.PROCESS_MEMORY_COUNTERS))); - - // Returns the current processs' WorkingSet if (!Interop.Kernel32.GetProcessMemoryInfo(Interop.Kernel32.GetCurrentProcess(), out memoryCounters, memoryCounters.cb)) { - Debug.WriteLine("Failed: GetProcessMemoryInfo returned false"); return 0; } return (long)memoryCounters.WorkingSetSize; From b2e3c9ed9ede7fc5b5483b5cc5602ad127911223 Mon Sep 17 00:00:00 2001 From: Sung Yoon Whang Date: Wed, 4 Sep 2019 22:35:57 -0700 Subject: [PATCH 05/12] remove size annotation --- .../Interop/Windows/Kernel32/Interop.GetProcessMemoryInfo.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessMemoryInfo.cs b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessMemoryInfo.cs index db6b25691d7b..b88e9d05e6f0 100644 --- a/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessMemoryInfo.cs +++ b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessMemoryInfo.cs @@ -8,7 +8,7 @@ internal partial class Interop { // Used for retrieving working set information directly via pinvoke. - [StructLayout(LayoutKind.Sequential, Size=72)] + [StructLayout(LayoutKind.Sequential)] internal struct ProcessMemoryCounters { public uint cb; From 353a9c9191ce60cb91721f57fe3c89f0dfc6c96d Mon Sep 17 00:00:00 2001 From: Sung Yoon Whang Date: Wed, 4 Sep 2019 23:05:46 -0700 Subject: [PATCH 06/12] remove useless comment --- .../Interop/Windows/Kernel32/Interop.GetProcessMemoryInfo.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessMemoryInfo.cs b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessMemoryInfo.cs index b88e9d05e6f0..42e191d5d783 100644 --- a/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessMemoryInfo.cs +++ b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessMemoryInfo.cs @@ -7,7 +7,6 @@ internal partial class Interop { - // Used for retrieving working set information directly via pinvoke. [StructLayout(LayoutKind.Sequential)] internal struct ProcessMemoryCounters { From b9d389c73a2f74bd8f81f61e20b63a32bae06353 Mon Sep 17 00:00:00 2001 From: Sung Yoon Whang Date: Thu, 5 Sep 2019 13:06:57 -0700 Subject: [PATCH 07/12] Move all the changes to Environment.WorkingSet and remove it from RuntimeEventSourceHelper --- .../shared/System/Environment.Unix.cs | 19 +++++++++++++++ .../shared/System/Environment.Windows.cs | 15 ++++++++++++ .../shared/System/Environment.cs | 24 +------------------ .../Eventing/RuntimeEventSource.cs | 2 +- .../Eventing/RuntimeEventSourceHelper.Unix.cs | 5 ---- .../RuntimeEventSourceHelper.Windows.cs | 12 ---------- 6 files changed, 36 insertions(+), 41 deletions(-) diff --git a/src/System.Private.CoreLib/shared/System/Environment.Unix.cs b/src/System.Private.CoreLib/shared/System/Environment.Unix.cs index 9a2af4dfb80e..47899cac108b 100644 --- a/src/System.Private.CoreLib/shared/System/Environment.Unix.cs +++ b/src/System.Private.CoreLib/shared/System/Environment.Unix.cs @@ -444,5 +444,24 @@ private static int CheckedSysConf(Interop.Sys.SysConfName name) } return (int)result; } + + public static long WorkingSet + { + get + { + Type? processType = Type.GetType("System.Diagnostics.Process, System.Diagnostics.Process, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", throwOnError: false); + if (processType?.GetMethod("GetCurrentProcess")?.Invoke(null, BindingFlags.DoNotWrapExceptions, null, null, null) is IDisposable currentProcess) + { + using (currentProcess) + { + object? result = processType!.GetMethod("get_WorkingSet64")?.Invoke(currentProcess, BindingFlags.DoNotWrapExceptions, null, null, null); + if (result is long) return (long)result; + } + } + + // Could not get the current working set. + return 0; + } + } } } diff --git a/src/System.Private.CoreLib/shared/System/Environment.Windows.cs b/src/System.Private.CoreLib/shared/System/Environment.Windows.cs index 957d1894a2a8..e252b7a6937f 100644 --- a/src/System.Private.CoreLib/shared/System/Environment.Windows.cs +++ b/src/System.Private.CoreLib/shared/System/Environment.Windows.cs @@ -119,5 +119,20 @@ public static string SystemDirectory return builder.ToString(); } } + + public static unsafe long WorkingSet + { + get + { + Interop.ProcessMemoryCounters memoryCounters; + memoryCounters.cb = (uint)(sizeof(Interop.ProcessMemoryCounters)); + + if (!Interop.Kernel32.GetProcessMemoryInfo(Interop.Kernel32.GetCurrentProcess(), out memoryCounters, memoryCounters.cb)) + { + return 0; + } + return (long)memoryCounters.WorkingSetSize; + } + } } } diff --git a/src/System.Private.CoreLib/shared/System/Environment.cs b/src/System.Private.CoreLib/shared/System/Environment.cs index 0f602030c528..7fdd0285eeb2 100644 --- a/src/System.Private.CoreLib/shared/System/Environment.cs +++ b/src/System.Private.CoreLib/shared/System/Environment.cs @@ -6,6 +6,7 @@ using System.Diagnostics; using System.Reflection; using System.Threading; +using System.Runtime.InteropServices; namespace System { @@ -153,29 +154,6 @@ public static Version Version } } - public static long WorkingSet - { - get - { - // Use reflection to access the implementation in System.Diagnostics.Process.dll. While far from ideal, - // we do this to avoid duplicating the Windows, Linux, macOS, and potentially other platform-specific implementations - // present in Process. If it proves important, we could look at separating that functionality out of Process into - // Common files which could also be included here. - Type? processType = Type.GetType("System.Diagnostics.Process, System.Diagnostics.Process, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", throwOnError: false); - if (processType?.GetMethod("GetCurrentProcess")?.Invoke(null, BindingFlags.DoNotWrapExceptions, null, null, null) is IDisposable currentProcess) - { - using (currentProcess) - { - object? result = processType!.GetMethod("get_WorkingSet64")?.Invoke(currentProcess, BindingFlags.DoNotWrapExceptions, null, null, null); - if (result is long) return (long)result; - } - } - - // Could not get the current working set. - return 0; - } - } - private static bool ValidateAndConvertRegistryTarget(EnvironmentVariableTarget target) { Debug.Assert(target != EnvironmentVariableTarget.Process); diff --git a/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSource.cs b/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSource.cs index 84b7e67a457e..7abed9dd5729 100644 --- a/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSource.cs +++ b/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSource.cs @@ -52,7 +52,7 @@ protected override void OnEventCommand(EventCommandEventArgs command) // On disable, PollingCounters will stop polling for values so it should be fine to leave them around. _cpuTimeCounter ??= new PollingCounter("cpu-usage", this, () => RuntimeEventSourceHelper.GetCpuUsage()) { DisplayName = "CPU Usage", DisplayUnits = "%" }; - _workingSetCounter ??= new PollingCounter("working-set", this, () => (double)(RuntimeEventSourceHelper.GetWorkingSet() / 1000000)) { DisplayName = "Working Set", DisplayUnits = "MB" }; + _workingSetCounter ??= new PollingCounter("working-set", this, () => (double)(Environment.WorkingSet / 1000000)) { DisplayName = "Working Set", DisplayUnits = "MB" }; _gcHeapSizeCounter ??= new PollingCounter("gc-heap-size", this, () => (double)(GC.GetTotalMemory(false) / 1000000)) { DisplayName = "GC Heap Size", DisplayUnits = "MB" }; _gen0GCCounter ??= new IncrementingPollingCounter("gen-0-gc-count", this, () => GC.CollectionCount(0)) { DisplayName = "Gen 0 GC Count", DisplayRateTimeScale = new TimeSpan(0, 1, 0) }; _gen1GCCounter ??= new IncrementingPollingCounter("gen-1-gc-count", this, () => GC.CollectionCount(1)) { DisplayName = "Gen 1 GC Count", DisplayRateTimeScale = new TimeSpan(0, 1, 0) }; diff --git a/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Unix.cs b/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Unix.cs index 5ac8033a2433..776ad5d3b692 100644 --- a/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Unix.cs +++ b/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Unix.cs @@ -14,10 +14,5 @@ internal static int GetCpuUsage() { return Interop.Sys.GetCpuUtilization(ref cpuInfo); } - - internal static long GetWorkingSet() - { - return Environment.WorkingSet; - } } } diff --git a/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Windows.cs b/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Windows.cs index 7cd63c33f225..761b8dc5391e 100644 --- a/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Windows.cs +++ b/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Windows.cs @@ -47,17 +47,5 @@ internal static int GetCpuUsage() return cpuUsage; } - - internal static long GetWorkingSet() - { - Interop.ProcessMemoryCounters memoryCounters; - memoryCounters.cb = (uint)(Marshal.SizeOf(typeof(Interop.ProcessMemoryCounters))); - - if (!Interop.Kernel32.GetProcessMemoryInfo(Interop.Kernel32.GetCurrentProcess(), out memoryCounters, memoryCounters.cb)) - { - return 0; - } - return (long)memoryCounters.WorkingSetSize; - } } } From abc459272d25b9de69eeb759f30681bde544aa1d Mon Sep 17 00:00:00 2001 From: Sung Yoon Whang Date: Thu, 5 Sep 2019 13:08:28 -0700 Subject: [PATCH 08/12] removing useless usings --- src/System.Private.CoreLib/shared/System/Environment.cs | 1 - .../Diagnostics/Eventing/RuntimeEventSourceHelper.Windows.cs | 1 - 2 files changed, 2 deletions(-) diff --git a/src/System.Private.CoreLib/shared/System/Environment.cs b/src/System.Private.CoreLib/shared/System/Environment.cs index 7fdd0285eeb2..edf1eeeabe33 100644 --- a/src/System.Private.CoreLib/shared/System/Environment.cs +++ b/src/System.Private.CoreLib/shared/System/Environment.cs @@ -6,7 +6,6 @@ using System.Diagnostics; using System.Reflection; using System.Threading; -using System.Runtime.InteropServices; namespace System { diff --git a/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Windows.cs b/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Windows.cs index 761b8dc5391e..1a05715da6e8 100644 --- a/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Windows.cs +++ b/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Windows.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System.Runtime.InteropServices; namespace System.Diagnostics.Tracing { From c253eb9d154713949c3a33a2bb8dd38892bb8307 Mon Sep 17 00:00:00 2001 From: Sung Yoon Whang Date: Thu, 5 Sep 2019 13:18:29 -0700 Subject: [PATCH 09/12] Use kernel32.dll instead of psapi.dll --- .../Interop/Windows/Kernel32/Interop.GetProcessMemoryInfo.cs | 4 ++-- .../shared/System/Environment.Windows.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessMemoryInfo.cs b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessMemoryInfo.cs index 42e191d5d783..45c8b3b5a6c2 100644 --- a/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessMemoryInfo.cs +++ b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessMemoryInfo.cs @@ -24,7 +24,7 @@ internal struct ProcessMemoryCounters internal partial class Kernel32 { - [DllImport("psapi.dll", SetLastError = true)] - internal static extern bool GetProcessMemoryInfo(IntPtr handleProcess, out ProcessMemoryCounters pmCounter, uint cb); + [DllImport(Libraries.Kernel32, SetLastError = true)] + internal static extern bool K32GetProcessMemoryInfo(IntPtr handleProcess, out ProcessMemoryCounters pmCounter, uint cb); } } diff --git a/src/System.Private.CoreLib/shared/System/Environment.Windows.cs b/src/System.Private.CoreLib/shared/System/Environment.Windows.cs index e252b7a6937f..6cb29eea4b65 100644 --- a/src/System.Private.CoreLib/shared/System/Environment.Windows.cs +++ b/src/System.Private.CoreLib/shared/System/Environment.Windows.cs @@ -127,7 +127,7 @@ public static unsafe long WorkingSet Interop.ProcessMemoryCounters memoryCounters; memoryCounters.cb = (uint)(sizeof(Interop.ProcessMemoryCounters)); - if (!Interop.Kernel32.GetProcessMemoryInfo(Interop.Kernel32.GetCurrentProcess(), out memoryCounters, memoryCounters.cb)) + if (!Interop.Kernel32.K32GetProcessMemoryInfo(Interop.Kernel32.GetCurrentProcess(), out memoryCounters, memoryCounters.cb)) { return 0; } From 176f01ee495d47a7cec6182b0ffcea7bd821b3d7 Mon Sep 17 00:00:00 2001 From: Sung Yoon Whang Date: Thu, 5 Sep 2019 16:17:55 -0700 Subject: [PATCH 10/12] Code review feedback --- .../Kernel32/Interop.GetProcessMemoryInfo.cs | 22 +++++++++---------- .../System.Private.CoreLib.Shared.projitems | 2 +- .../shared/System/Environment.Windows.cs | 6 ++--- .../Eventing/RuntimeEventSource.cs | 2 +- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessMemoryInfo.cs b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessMemoryInfo.cs index 45c8b3b5a6c2..abeedacd4c4f 100644 --- a/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessMemoryInfo.cs +++ b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessMemoryInfo.cs @@ -8,23 +8,23 @@ internal partial class Interop { [StructLayout(LayoutKind.Sequential)] - internal struct ProcessMemoryCounters + internal struct PROCESS_MEMORY_COUNTERS { public uint cb; public uint PageFaultCount; - public ulong PeakWorkingSetSize; - public ulong WorkingSetSize; - public ulong QuotaPeakPagedPoolUsage; - public ulong QuotaPagedPoolUsage; - public ulong QuotaPeakNonPagedPoolUsage; - public ulong QuotaNonPagedPoolUsage; - public ulong PagefileUsage; - public ulong PeakPagefileUsage; + public UIntPtr PeakWorkingSetSize; + public UIntPtr WorkingSetSize; + public UIntPtr QuotaPeakPagedPoolUsage; + public UIntPtr QuotaPagedPoolUsage; + public UIntPtr QuotaPeakNonPagedPoolUsage; + public UIntPtr QuotaNonPagedPoolUsage; + public UIntPtr PagefileUsage; + public UIntPtr PeakPagefileUsage; } internal partial class Kernel32 { - [DllImport(Libraries.Kernel32, SetLastError = true)] - internal static extern bool K32GetProcessMemoryInfo(IntPtr handleProcess, out ProcessMemoryCounters pmCounter, uint cb); + [DllImport(Libraries.Kernel32)] + internal static extern bool K32GetProcessMemoryInfo(IntPtr Process, ref PROCESS_MEMORY_COUNTERS ppsmemCounters, uint cb); } } diff --git a/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems b/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems index c53a5b450628..0eea0d308665 100644 --- a/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems +++ b/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems @@ -1053,8 +1053,8 @@ - + diff --git a/src/System.Private.CoreLib/shared/System/Environment.Windows.cs b/src/System.Private.CoreLib/shared/System/Environment.Windows.cs index 6cb29eea4b65..68a2071cf36c 100644 --- a/src/System.Private.CoreLib/shared/System/Environment.Windows.cs +++ b/src/System.Private.CoreLib/shared/System/Environment.Windows.cs @@ -124,10 +124,10 @@ public static unsafe long WorkingSet { get { - Interop.ProcessMemoryCounters memoryCounters; - memoryCounters.cb = (uint)(sizeof(Interop.ProcessMemoryCounters)); + Interop.PROCESS_MEMORY_COUNTERS memoryCounters = default; + memoryCounters.cb = (uint)(sizeof(Interop.PROCESS_MEMORY_COUNTERS)); - if (!Interop.Kernel32.K32GetProcessMemoryInfo(Interop.Kernel32.GetCurrentProcess(), out memoryCounters, memoryCounters.cb)) + if (!Interop.Kernel32.K32GetProcessMemoryInfo(Interop.Kernel32.GetCurrentProcess(), ref memoryCounters, memoryCounters.cb)) { return 0; } diff --git a/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSource.cs b/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSource.cs index 7abed9dd5729..e7edc85d1917 100644 --- a/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSource.cs +++ b/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSource.cs @@ -73,4 +73,4 @@ protected override void OnEventCommand(EventCommandEventArgs command) } } } -} \ No newline at end of file +} From 943e87e31de1115fb221cf0b5bd3e8520945bd52 Mon Sep 17 00:00:00 2001 From: Sung Yoon Whang Date: Fri, 6 Sep 2019 15:24:55 -0700 Subject: [PATCH 11/12] Remove newline change --- .../Diagnostics/Eventing/RuntimeEventSourceHelper.Windows.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Windows.cs b/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Windows.cs index 1a05715da6e8..6a74c7f9f498 100644 --- a/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Windows.cs +++ b/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSourceHelper.Windows.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. - namespace System.Diagnostics.Tracing { internal sealed class RuntimeEventSourceHelper From 3b9c37d9a6d5f8020d6957e9e967c0670f4bc4b5 Mon Sep 17 00:00:00 2001 From: Sung Yoon Whang Date: Wed, 11 Sep 2019 15:56:57 -0700 Subject: [PATCH 12/12] More code review nits --- .../Kernel32/Interop.GetProcessMemoryInfo.cs | 34 +++++++++---------- .../shared/System/Environment.Windows.cs | 6 ++-- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessMemoryInfo.cs b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessMemoryInfo.cs index abeedacd4c4f..98234b8e6e08 100644 --- a/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessMemoryInfo.cs +++ b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetProcessMemoryInfo.cs @@ -7,24 +7,24 @@ internal partial class Interop { - [StructLayout(LayoutKind.Sequential)] - internal struct PROCESS_MEMORY_COUNTERS - { - public uint cb; - public uint PageFaultCount; - public UIntPtr PeakWorkingSetSize; - public UIntPtr WorkingSetSize; - public UIntPtr QuotaPeakPagedPoolUsage; - public UIntPtr QuotaPagedPoolUsage; - public UIntPtr QuotaPeakNonPagedPoolUsage; - public UIntPtr QuotaNonPagedPoolUsage; - public UIntPtr PagefileUsage; - public UIntPtr PeakPagefileUsage; - } - internal partial class Kernel32 { - [DllImport(Libraries.Kernel32)] - internal static extern bool K32GetProcessMemoryInfo(IntPtr Process, ref PROCESS_MEMORY_COUNTERS ppsmemCounters, uint cb); + [StructLayout(LayoutKind.Sequential)] + internal struct PROCESS_MEMORY_COUNTERS + { + public uint cb; + public uint PageFaultCount; + public UIntPtr PeakWorkingSetSize; + public UIntPtr WorkingSetSize; + public UIntPtr QuotaPeakPagedPoolUsage; + public UIntPtr QuotaPagedPoolUsage; + public UIntPtr QuotaPeakNonPagedPoolUsage; + public UIntPtr QuotaNonPagedPoolUsage; + public UIntPtr PagefileUsage; + public UIntPtr PeakPagefileUsage; + } + + [DllImport(Libraries.Kernel32, EntryPoint="K32GetProcessMemoryInfo")] + internal static extern bool GetProcessMemoryInfo(IntPtr Process, ref PROCESS_MEMORY_COUNTERS ppsmemCounters, uint cb); } } diff --git a/src/System.Private.CoreLib/shared/System/Environment.Windows.cs b/src/System.Private.CoreLib/shared/System/Environment.Windows.cs index 68a2071cf36c..e2b0918553db 100644 --- a/src/System.Private.CoreLib/shared/System/Environment.Windows.cs +++ b/src/System.Private.CoreLib/shared/System/Environment.Windows.cs @@ -124,10 +124,10 @@ public static unsafe long WorkingSet { get { - Interop.PROCESS_MEMORY_COUNTERS memoryCounters = default; - memoryCounters.cb = (uint)(sizeof(Interop.PROCESS_MEMORY_COUNTERS)); + Interop.Kernel32.PROCESS_MEMORY_COUNTERS memoryCounters = default; + memoryCounters.cb = (uint)(sizeof(Interop.Kernel32.PROCESS_MEMORY_COUNTERS)); - if (!Interop.Kernel32.K32GetProcessMemoryInfo(Interop.Kernel32.GetCurrentProcess(), ref memoryCounters, memoryCounters.cb)) + if (!Interop.Kernel32.GetProcessMemoryInfo(Interop.Kernel32.GetCurrentProcess(), ref memoryCounters, memoryCounters.cb)) { return 0; }