-
Notifications
You must be signed in to change notification settings - Fork 763
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use sum of memory consumptions of all processes of a container (#5301)
- Loading branch information
1 parent
74f689e
commit b5e5452
Showing
5 changed files
with
44 additions
and
114 deletions.
There are no files selected for viewing
10 changes: 4 additions & 6 deletions
10
...aries/Microsoft.Extensions.Diagnostics.ResourceMonitoring/Windows/Interop/IProcessInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,16 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using static Microsoft.Extensions.Diagnostics.ResourceMonitoring.Windows.Interop.ProcessInfo; | ||
|
||
namespace Microsoft.Extensions.Diagnostics.ResourceMonitoring.Windows.Interop; | ||
|
||
/// <summary> | ||
/// An interface to enable the mocking of process information retrieval. | ||
/// An interface to enable the mocking of memory usage information retrieval. | ||
/// </summary> | ||
internal interface IProcessInfo | ||
{ | ||
/// <summary> | ||
/// Retrieve the current application memory information. | ||
/// Retrieve the memory usage of a system. | ||
/// </summary> | ||
/// <returns>An appropriate memory data structure.</returns> | ||
APP_MEMORY_INFORMATION GetCurrentAppMemoryInfo(); | ||
/// <returns>Memory usage amount in bytes.</returns> | ||
ulong GetMemoryUsage(); | ||
} |
97 changes: 25 additions & 72 deletions
97
...raries/Microsoft.Extensions.Diagnostics.ResourceMonitoring/Windows/Interop/ProcessInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,41 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Microsoft.Extensions.Diagnostics.ResourceMonitoring.Windows.Interop; | ||
|
||
/// <summary> | ||
/// Process native methods class. | ||
/// </summary> | ||
/// <remarks>This will not be covered by UTs, as those | ||
/// classes have insufficient and inconsistent privileges, | ||
/// depending on runtime environment.</remarks> | ||
[ExcludeFromCodeCoverage] | ||
internal static class ProcessInfo | ||
internal sealed class ProcessInfo : IProcessInfo | ||
{ | ||
private enum PROCESS_INFORMATION_CLASS | ||
public ulong GetMemoryUsage() | ||
{ | ||
ProcessAppMemoryInfo = 2 | ||
} | ||
|
||
/// <summary> | ||
/// The APP_MEMORY_INFORMATION structure. | ||
/// </summary> | ||
[StructLayout(LayoutKind.Sequential)] | ||
internal struct APP_MEMORY_INFORMATION | ||
{ | ||
public ulong AvailableCommit; | ||
public ulong PrivateCommitUsage; | ||
public ulong PeakPrivateCommitUsage; | ||
public ulong TotalCommitUsage; | ||
} | ||
|
||
/// <summary> | ||
/// Retrieve the current application memory information. | ||
/// </summary> | ||
/// <returns>An appropriate memory data structure.</returns> | ||
public static APP_MEMORY_INFORMATION GetCurrentAppMemoryInfo() | ||
{ | ||
unsafe | ||
{ | ||
APP_MEMORY_INFORMATION info = default; | ||
void* buffer = &info; | ||
using var currentProcess = Process.GetCurrentProcess(); | ||
NtGetProcessInformation( | ||
currentProcess.Handle, | ||
PROCESS_INFORMATION_CLASS.ProcessAppMemoryInfo, | ||
buffer, | ||
sizeof(APP_MEMORY_INFORMATION)); | ||
|
||
return info; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Get process information. | ||
/// </summary> | ||
/// <param name="handle">The handle of the object to query.</param> | ||
/// <param name="infoClass">Process info class.</param> | ||
/// <param name="buffer">Buffer containing the limit.</param> | ||
/// <param name="size">Buffer size.</param> | ||
private static unsafe void NtGetProcessInformation(IntPtr handle, PROCESS_INFORMATION_CLASS infoClass, void* buffer, int size) | ||
{ | ||
if (!UnsafeNativeMethods.GetProcessInformation( | ||
handle, | ||
infoClass, | ||
buffer, | ||
size)) | ||
ulong memoryUsage = 0; | ||
var processes = Process.GetProcesses(); | ||
foreach (var process in processes) | ||
{ | ||
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); | ||
try | ||
{ | ||
memoryUsage += (ulong)process.WorkingSet64; | ||
} | ||
#pragma warning disable CA1031 // Do not catch general exception types | ||
catch | ||
#pragma warning restore CA1031 // Do not catch general exception types | ||
{ | ||
// Ignore various exceptions including, but not limited: | ||
// AccessDenied (from kernel processes), | ||
// InvalidOperation (process does not exist anymore) | ||
// and silently continue to the next process. | ||
} | ||
finally | ||
{ | ||
#pragma warning disable EA0011 // Consider removing unnecessary conditional access operator (?) | ||
process?.Dispose(); | ||
#pragma warning restore EA0011 // Consider removing unnecessary conditional access operator (?) | ||
} | ||
} | ||
} | ||
|
||
private static class UnsafeNativeMethods | ||
{ | ||
[DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true)] | ||
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)] | ||
[return: MarshalAs(UnmanagedType.Bool)] | ||
public static unsafe extern bool GetProcessInformation( | ||
IntPtr processHandle, | ||
PROCESS_INFORMATION_CLASS processInformationClass, | ||
void* processInformation, | ||
int processInformationSize); | ||
return memoryUsage; | ||
} | ||
} |
15 changes: 0 additions & 15 deletions
15
...Microsoft.Extensions.Diagnostics.ResourceMonitoring/Windows/Interop/ProcessInfoWrapper.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters