-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
Open
gwr
wants to merge
2
commits into
dotnet:main
Choose a base branch
from
gwr:illumos5
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
SunOS process and thread support #105403
Changes from all commits
Commits
Show all changes
2 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
80 changes: 80 additions & 0 deletions
80
src/libraries/Common/src/Interop/SunOS/procfs/Interop.ProcFs.TryGetProcessInfoById.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,80 @@ | ||
// 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; | ||
|
||
// See callers: | ||
// ProcessManager.SunOS.cs | ||
// Environment.SunOS etc. | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class @procfs | ||
{ | ||
|
||
// Constants from sys/procfs.h | ||
private const int PRARGSZ = 80; | ||
|
||
// Output type for TryGetProcessInfoById() | ||
// 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 TryGetProcessInfoById(int pid, out ProcessInfo processInfo) | ||
{ | ||
fixed (ProcessInfo* pProcessInfo = &processInfo) | ||
{ | ||
if (ReadProcessInfo(pid, pProcessInfo, null, 0) < 0) | ||
{ | ||
Interop.ErrorInfo errorInfo = Sys.GetLastErrorInfo(); | ||
throw new IOException(errorInfo.GetErrorMessage(), errorInfo.RawErrno); | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
// Variant that also gets the arg string. | ||
internal static unsafe bool TryGetProcessInfoById(int pid, out ProcessInfo processInfo, out string argString) | ||
{ | ||
byte* argBuf = stackalloc byte[PRARGSZ]; | ||
fixed (ProcessInfo* pProcessInfo = &processInfo) | ||
{ | ||
if (ReadProcessInfo(pid, pProcessInfo, argBuf, PRARGSZ) < 0) | ||
{ | ||
Interop.ErrorInfo errorInfo = Sys.GetLastErrorInfo(); | ||
throw new IOException(errorInfo.GetErrorMessage(), errorInfo.RawErrno); | ||
} | ||
} | ||
argString = Marshal.PtrToStringUTF8((IntPtr)argBuf)!; | ||
return true; | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/libraries/Common/src/Interop/SunOS/procfs/Interop.ProcFs.TryGetThreadInfoById.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,61 @@ | ||
// 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; | ||
|
||
// See callers: | ||
// ProcessManager.SunOS.cs | ||
// ProcessThread.SunOS etc. | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class @procfs | ||
{ | ||
|
||
// Output type for TryGetThreadInfoById() | ||
// 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 | ||
|
||
[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 TryGetThreadInfoById(int pid, int tid, out ThreadInfo threadInfo) | ||
{ | ||
fixed (ThreadInfo* pThreadInfo = &threadInfo) | ||
{ | ||
if (ReadThreadInfo(pid, tid, pThreadInfo) < 0) | ||
{ | ||
Interop.ErrorInfo errorInfo = Sys.GetLastErrorInfo(); | ||
throw new IOException(errorInfo.GetErrorMessage(), errorInfo.RawErrno); | ||
} | ||
} | ||
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 iinfo = GetProcInfo(); | ||||||||||
|
||||||||||
DateTime startTime = DateTime.UnixEpoch + | ||||||||||
TimeSpan.FromSeconds(iinfo.StartTime) + | ||||||||||
TimeSpan.FromMicroseconds(iinfo.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 iinfo = GetProcInfo(); | ||||||||||
TimeSpan ts = TimeSpan.FromSeconds(iinfo.CpuTotalTime) + | ||||||||||
TimeSpan.FromMicroseconds(iinfo.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 iProcInfo, 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 (iProcInfo.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 iinfo; | ||||||||||
if (!Interop.procfs.TryGetProcessInfoById(_processId, out iinfo)) | ||||||||||
Comment on lines
+128
to
+129
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.
Suggested change
Does the |
||||||||||
{ | ||||||||||
throw new Win32Exception(SR.ProcessInformationUnavailable); | ||||||||||
} | ||||||||||
return iinfo; | ||||||||||
} | ||||||||||
} | ||||||||||
} |
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.
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.
The method implementation throws on errors. It never returns false. Should it be
void GetProcessInfoById
instead? (dtto for other similar Try methods)