-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Issue 55685 processname optimisation #59672
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
Merged
adamsitnik
merged 21 commits into
dotnet:main
from
SteveDunn:issue-55685-processname-optimisation
Dec 7, 2021
Merged
Changes from 17 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
dd7f67b
Implementation
SteveDunn 41e0197
Merge branch 'dotnet:main' into issue-55685-processname-optimisation
SteveDunn 19b9126
Get short name after calling Kernel32.GetProcessName
SteveDunn 8955b32
PR feedback
SteveDunn 901e8f3
PR feedback
SteveDunn 47262da
PR feedback
SteveDunn 8689265
Removed commented out code
SteveDunn ac70497
PR feedback
SteveDunn c5a2790
If processinfo is already available, use that, otherwise use the quic…
SteveDunn 9f69e34
PR feedback
SteveDunn 38e3d14
PR feedback
SteveDunn 3227f87
Update src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetP…
SteveDunn f07a0bc
Update src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetP…
SteveDunn 3728a1f
Update src/libraries/System.Diagnostics.Process/src/System/Diagnostic…
SteveDunn fcb47a9
Update src/libraries/System.Diagnostics.Process/src/System/Diagnostic…
SteveDunn 23cd323
Update src/libraries/System.Diagnostics.Process/src/System/Diagnostic…
SteveDunn d4c2e86
Update src/libraries/System.Diagnostics.Process/src/System/Diagnostic…
SteveDunn 96b88b4
Apply suggestions from code review
adamsitnik 67732e4
handle ERROR_INSUFFICIENT_BUFFER
adamsitnik 6676175
discard process name on Refresh
adamsitnik 08ec6d0
Merge remote-tracking branch 'upstream/main' into issue-55685-process…
adamsitnik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetProcessName.cs
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // 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.Runtime.InteropServices; | ||
| using System.Text; | ||
| using Microsoft.Win32.SafeHandles; | ||
|
|
||
| internal static partial class Interop | ||
| { | ||
| internal static partial class Kernel32 | ||
| { | ||
| [DllImport(Libraries.Kernel32, CharSet = CharSet.Unicode, EntryPoint = "QueryFullProcessImageNameW", ExactSpelling = true)] | ||
| private static extern bool QueryFullProcessImageName( | ||
| SafeHandle hProcess, | ||
| uint dwFlags, | ||
| ref char lpBuffer, | ||
| ref uint lpdwSize); | ||
|
|
||
| private const int MAX_PROCESSNAME_LENGTH = 1024; | ||
|
|
||
| internal static string? GetProcessName(uint processId) | ||
| { | ||
| using (SafeProcessHandle h = Interop.Kernel32.OpenProcess(Interop.Advapi32.ProcessOptions.PROCESS_QUERY_LIMITED_INFORMATION, false, (int)processId)) | ||
adamsitnik marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| Span<char> buffer = stackalloc char[MAX_PROCESSNAME_LENGTH + 1]; | ||
adamsitnik marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| uint length = (uint)buffer.Length; | ||
|
|
||
| bool queried = QueryFullProcessImageName( | ||
| h, | ||
| 0, | ||
| ref MemoryMarshal.GetReference(buffer), | ||
| ref length); | ||
|
|
||
| return queried ? buffer.Slice(0, (int)length).ToString() : null; | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -17,6 +17,8 @@ public partial class Process : IDisposable | |
| { | ||
| private static readonly object s_createProcessLock = new object(); | ||
|
|
||
| private string? _processName; | ||
|
|
||
| /// <summary> | ||
| /// Creates an array of <see cref="Process"/> components that are associated with process resources on a | ||
| /// remote computer. These process resources share the specified process name. | ||
|
|
@@ -885,5 +887,40 @@ private static string GetEnvironmentVariablesBlock(IDictionary<string, string> s | |
| } | ||
|
|
||
| private static string GetErrorMessage(int error) => Interop.Kernel32.GetMessage(error); | ||
|
|
||
| /// <summary>Gets the friendly name of the process.</summary> | ||
| public string ProcessName | ||
| { | ||
| get | ||
| { | ||
| EnsureState(State.HaveNonExitedId); | ||
|
|
||
| if (_processName == null) | ||
| { | ||
| // If we already have the name via a populated ProcessInfo | ||
adamsitnik marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // then use that one. | ||
| if (_processInfo?.ProcessName != null) | ||
| { | ||
| _processName = _processInfo!.ProcessName; | ||
| } | ||
| else | ||
| { | ||
|
|
||
| // If we don't have a populated ProcessInfo, then create and | ||
| // cache the process name. | ||
| EnsureState(State.HaveId); | ||
|
|
||
| _processName = ProcessManager.GetProcessName(_processId, _machineName); | ||
adamsitnik marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if (_processName == null) | ||
adamsitnik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| throw new InvalidOperationException(SR.NoProcessInfo); | ||
|
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. thank you for keeping the exception consistent with existing implementation! 👍 |
||
| } | ||
| } | ||
| } | ||
|
|
||
| return _processName; | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -85,6 +85,39 @@ public static ProcessInfo[] GetProcessInfos(string machineName) | |||||||
| return null; | ||||||||
| } | ||||||||
|
|
||||||||
| /// <summary>Gets the process name for the specified process ID on the specified machine.</summary> | ||||||||
| /// <param name="processId">The process ID.</param> | ||||||||
| /// <param name="machineName">The machine name.</param> | ||||||||
| /// <returns>The process name for the process if it could be found; otherwise, null.</returns> | ||||||||
| public static string? GetProcessName(int processId, string machineName) | ||||||||
| { | ||||||||
| if (IsRemoteMachine(machineName)) | ||||||||
| { | ||||||||
| // remote case: we take the hit of looping through all results | ||||||||
| ProcessInfo[] processInfos = NtProcessManager.GetProcessInfos(machineName, isRemoteMachine: true); | ||||||||
| foreach (ProcessInfo processInfo in processInfos) | ||||||||
| { | ||||||||
| if (processInfo.ProcessId == processId) | ||||||||
| { | ||||||||
| return processInfo.ProcessName; | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
| else | ||||||||
| { | ||||||||
| // local case: do not use performance counter and also attempt to get the matching (by pid) process only | ||||||||
|
|
||||||||
| string? processName = Interop.Kernel32.GetProcessName((uint)processId); | ||||||||
| if (processName is not null) | ||||||||
| { | ||||||||
| return NtProcessInfoHelper.GetProcessShortName(processName); | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| return null; | ||||||||
| } | ||||||||
|
|
||||||||
|
|
||||||||
|
Comment on lines
+118
to
+120
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. nit: extra empty line
Suggested change
|
||||||||
| /// <summary>Gets the IDs of all processes on the specified machine.</summary> | ||||||||
| /// <param name="machineName">The machine to examine.</param> | ||||||||
| /// <returns>An array of process IDs from the specified machine.</returns> | ||||||||
|
|
||||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.