-
Notifications
You must be signed in to change notification settings - Fork 5.4k
SunOS process and thread support #105403
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
Merged
SunOS process and thread support #105403
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
74 changes: 74 additions & 0 deletions
74
src/libraries/Common/src/Interop/SunOS/procfs/Interop.ProcFs.GetProcessInfoById.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,74 @@ | ||
| // 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.IO; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| internal static partial class Interop | ||
| { | ||
| internal static partial class @procfs | ||
| { | ||
| // Constants from sys/procfs.h | ||
| private const int PRARGSZ = 80; | ||
|
|
||
| // Output type for GetProcessInfoById() | ||
| // Keep in sync with pal_io.h ProcessInfo | ||
| [StructLayout(LayoutKind.Sequential)] | ||
| internal struct ProcessInfo | ||
| { | ||
| internal ulong VirtualSize; | ||
| internal ulong ResidentSetSize; | ||
| internal long StartTime; | ||
| internal long StartTimeNsec; | ||
| internal long CpuTotalTime; | ||
| internal long CpuTotalTimeNsec; | ||
| internal int Pid; | ||
| internal int ParentPid; | ||
| internal int SessionId; | ||
| internal int Priority; | ||
| internal int NiceVal; | ||
| } | ||
|
|
||
| [LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_ReadProcessInfo", SetLastError = true)] | ||
| private static unsafe partial int ReadProcessInfo(int pid, ProcessInfo* processInfo, byte* argBuf, int argBufSize); | ||
|
|
||
| /// <summary> | ||
| /// Attempts to get status info for the specified process ID. | ||
| /// </summary> | ||
| /// <param name="pid">PID of the process to read status info for.</param> | ||
| /// <param name="processInfo">The pointer to ProcessInfo instance.</param> | ||
| /// <returns> | ||
| /// true if the process status was read; otherwise, false. | ||
| /// </returns> | ||
| internal static unsafe bool GetProcessInfoById(int pid, out ProcessInfo processInfo) | ||
|
gwr marked this conversation as resolved.
|
||
| { | ||
| fixed (ProcessInfo* pProcessInfo = &processInfo) | ||
| { | ||
| if (ReadProcessInfo(pid, pProcessInfo, null, 0) < 0) | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
|
gwr marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // Variant that also gets the arg string. | ||
| internal static unsafe bool GetProcessInfoById(int pid, out ProcessInfo processInfo, out string argString) | ||
| { | ||
| byte* argBuf = stackalloc byte[PRARGSZ]; | ||
| fixed (ProcessInfo* pProcessInfo = &processInfo) | ||
| { | ||
| if (ReadProcessInfo(pid, pProcessInfo, argBuf, PRARGSZ) < 0) | ||
| { | ||
| argString = ""; | ||
| return false; | ||
| } | ||
| } | ||
| argString = Marshal.PtrToStringUTF8((IntPtr)argBuf)!; | ||
| return true; | ||
|
gwr marked this conversation as resolved.
|
||
| } | ||
| } | ||
| } | ||
55 changes: 55 additions & 0 deletions
55
src/libraries/Common/src/Interop/SunOS/procfs/Interop.ProcFs.GetThreadInfoById.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,55 @@ | ||
| // 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.IO; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| internal static partial class Interop | ||
| { | ||
| internal static partial class @procfs | ||
| { | ||
| // Output type for GetThreadInfoById() | ||
| // Keep in sync with pal_io.h ThreadInfo | ||
| [StructLayout(LayoutKind.Sequential)] | ||
| internal struct ThreadInfo | ||
| { | ||
| internal long StartTime; | ||
| internal long StartTimeNsec; | ||
| internal long CpuTotalTime; // user+sys | ||
| internal long CpuTotalTimeNsec; | ||
| internal int Tid; | ||
| internal int Priority; | ||
| internal int NiceVal; | ||
| internal char StatusCode; | ||
| } | ||
|
|
||
| // See caller: ProcessManager.SunOS.cs | ||
|
|
||
|
jkotas marked this conversation as resolved.
Outdated
|
||
| [LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_ReadThreadInfo", SetLastError = true)] | ||
| private static unsafe partial int ReadThreadInfo(int pid, int tid, ThreadInfo* threadInfo); | ||
|
|
||
| /// <summary> | ||
| /// Attempts to get status info for the specified thread ID. | ||
| /// </summary> | ||
| /// <param name="pid">PID of the process to read status info for.</param> | ||
| /// <param name="tid">TID of the thread to read status info for.</param> | ||
| /// <param name="threadInfo">The pointer to ThreadInfo instance.</param> | ||
| /// <returns> | ||
| /// true if the process status was read; otherwise, false. | ||
| /// </returns> | ||
| internal static unsafe bool GetThreadInfoById(int pid, int tid, out ThreadInfo threadInfo) | ||
| { | ||
| fixed (ThreadInfo* pThreadInfo = &threadInfo) | ||
| { | ||
| if (ReadThreadInfo(pid, tid, pThreadInfo) < 0) | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
37 changes: 0 additions & 37 deletions
37
src/libraries/Common/src/Interop/SunOS/procfs/Interop.ProcFsStat.TryReadProcessStatusInfo.cs
This file was deleted.
Oops, something went wrong.
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
136 changes: 136 additions & 0 deletions
136
src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.SunOS.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,136 @@ | ||
| // 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.Buffers; | ||
| using System.Collections.Generic; | ||
| using System.ComponentModel; | ||
| using System.Globalization; | ||
| using System.IO; | ||
| using System.Runtime.InteropServices; | ||
| using System.Runtime.Versioning; | ||
| using System.Text; | ||
| using System.Threading; | ||
|
|
||
| namespace System.Diagnostics | ||
| { | ||
| public partial class Process : IDisposable | ||
| { | ||
| /// <summary>Gets the time the associated process was started.</summary> | ||
| internal DateTime StartTimeCore | ||
| { | ||
| get | ||
| { | ||
| Interop.procfs.ProcessInfo processInfo = GetProcInfo(); | ||
|
|
||
| DateTime startTime = DateTime.UnixEpoch + | ||
| TimeSpan.FromSeconds(processInfo.StartTime) + | ||
| TimeSpan.FromMicroseconds(processInfo.StartTimeNsec / 1000); | ||
|
|
||
| // The return value is expected to be in the local time zone. | ||
| return startTime.ToLocalTime(); | ||
| } | ||
| } | ||
|
|
||
| /// <summary>Gets the parent process ID</summary> | ||
| private int ParentProcessId => GetProcInfo().ParentPid; | ||
|
|
||
| /// <summary>Gets execution path</summary> | ||
| private static string? GetPathToOpenFile() | ||
| { | ||
| return FindProgramInPath("xdg-open"); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the amount of time the associated process has spent utilizing the CPU. | ||
| /// It is the sum of the <see cref='System.Diagnostics.Process.UserProcessorTime'/> and | ||
| /// <see cref='System.Diagnostics.Process.PrivilegedProcessorTime'/>. | ||
| /// </summary> | ||
| [UnsupportedOSPlatform("ios")] | ||
| [UnsupportedOSPlatform("tvos")] | ||
| [SupportedOSPlatform("maccatalyst")] | ||
| public TimeSpan TotalProcessorTime | ||
| { | ||
| get | ||
| { | ||
| // a.k.a. "user" + "system" time | ||
| Interop.procfs.ProcessInfo processInfo = GetProcInfo(); | ||
| TimeSpan ts = TimeSpan.FromSeconds(processInfo.CpuTotalTime) + | ||
| TimeSpan.FromMicroseconds(processInfo.CpuTotalTimeNsec / 1000); | ||
| return ts; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the amount of time the associated process has spent running code | ||
| /// inside the application portion of the process (not the operating system core). | ||
| /// </summary> | ||
| [UnsupportedOSPlatform("ios")] | ||
| [UnsupportedOSPlatform("tvos")] | ||
| [SupportedOSPlatform("maccatalyst")] | ||
| public TimeSpan UserProcessorTime | ||
| { | ||
| get | ||
| { | ||
| // a.k.a. "user" time | ||
| // Could get this from /proc/$pid/status | ||
| // Just say it's all user time for now | ||
| return TotalProcessorTime; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the amount of time the process has spent running code inside the operating | ||
| /// system core. | ||
| /// </summary> | ||
| [UnsupportedOSPlatform("ios")] | ||
| [UnsupportedOSPlatform("tvos")] | ||
| [SupportedOSPlatform("maccatalyst")] | ||
| public TimeSpan PrivilegedProcessorTime | ||
| { | ||
| get | ||
| { | ||
| // a.k.a. "system" time | ||
| // Could get this from /proc/$pid/status | ||
| // Just say it's all user time for now | ||
| EnsureState(State.HaveNonExitedId); | ||
| return TimeSpan.Zero; | ||
| } | ||
| } | ||
|
|
||
| // ---------------------------------- | ||
| // ---- Unix PAL layer ends here ---- | ||
| // ---------------------------------- | ||
|
|
||
| /// <summary>Gets the name that was used to start the process, or null if it could not be retrieved.</summary> | ||
| internal static string? GetUntruncatedProcessName(ref Interop.procfs.ProcessInfo processInfo, ref string argString) | ||
| { | ||
| // This assumes the process name is the first part of the Args string | ||
| // ending at the first space. That seems to work well enough for now. | ||
| // If someday this need to support a process name containing spaces, | ||
| // this could call a new Interop function that reads /proc/$pid/auxv | ||
| // (sys/auxv.h) and gets the AT_SUN_EXECNAME string from that file. | ||
| if (processInfo.Pid != 0 && !string.IsNullOrEmpty(argString)) | ||
| { | ||
| string[] argv = argString.Split(' ', 2); | ||
| if (!string.IsNullOrEmpty(argv[0])) | ||
| { | ||
| return Path.GetFileName(argv[0]); | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /// <summary>Reads the information for this process from the procfs file system.</summary> | ||
| private Interop.procfs.ProcessInfo GetProcInfo() | ||
| { | ||
| EnsureState(State.HaveNonExitedId); | ||
| Interop.procfs.ProcessInfo processInfo; | ||
| if (!Interop.procfs.GetProcessInfoById(_processId, out processInfo)) | ||
| { | ||
| throw new Win32Exception(SR.ProcessInformationUnavailable); | ||
| } | ||
| return processInfo; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.