-
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
base: main
Are you sure you want to change the base?
SunOS process and thread support #105403
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// 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; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class @procfs | ||
{ | ||
internal const string RootPath = "/proc/"; | ||
private const string psinfoFileName = "/psinfo"; | ||
private const string lwpDirName = "/lwp"; | ||
private const string lwpsinfoFileName = "/lwpsinfo"; | ||
|
||
// 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; | ||
} | ||
|
||
// Output type for TryGetThreadInfoById() | ||
// Keep in sync with pal_io.h ThreadInfo | ||
[StructLayout(LayoutKind.Sequential)] | ||
internal struct ThreadInfo | ||
gwr marked this conversation as resolved.
Show resolved
Hide resolved
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. Similar here - it would make sense for this to be next to |
||
{ | ||
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; | ||
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. This is (char is 16-bit in C#, so there is a mismatch between the managed and unmanaged definitions.) |
||
} | ||
|
||
internal static string GetInfoFilePathForProcess(int pid) => | ||
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. This looks unused |
||
$"{RootPath}{(uint)pid}{psinfoFileName}"; | ||
|
||
internal static string GetLwpDirForProcess(int pid) => | ||
$"{RootPath}{(uint)pid}{lwpDirName}"; | ||
|
||
internal static string GetInfoFilePathForThread(int pid, int tid) => | ||
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. This looks unused |
||
$"{RootPath}{(uint)pid}{lwpDirName}/{(uint)tid}{lwpsinfoFileName}"; | ||
|
||
} | ||
} |
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; | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
internal static partial class Interop | ||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||
internal static partial class @procfs | ||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
// See caller: ProcessManager.SunOS.cs | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
[LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_ReadProcessInfo", SetLastError = true)] | ||||||||||||||||||||||||||||||||
private static unsafe partial int ReadProcessInfo(int pid, ProcessInfo* processInfo, byte* argBuf, int argBufSize); | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
// Handy helpers for Environment.SunOS etc. | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
/// <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. | ||||||||||||||||||||||||||||||||
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. The method implementation throws on errors. It never returns false. Should it be |
||||||||||||||||||||||||||||||||
/// </returns> | ||||||||||||||||||||||||||||||||
internal static unsafe bool TryGetProcessInfoById(int pid, out ProcessInfo processInfo) | ||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||
ProcessInfo info = default; | ||||||||||||||||||||||||||||||||
if (ReadProcessInfo(pid, &info, null, 0) < 0) | ||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||
Interop.ErrorInfo errorInfo = Sys.GetLastErrorInfo(); | ||||||||||||||||||||||||||||||||
throw new IOException(errorInfo.GetErrorMessage(), errorInfo.RawErrno); | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
processInfo = info; | ||||||||||||||||||||||||||||||||
Comment on lines
+32
to
+38
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
ProcessInfo is a structure with non-trivial size. It would be better to pin it to avoid unnecessary copying. (dtto for other similar places) |
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
return true; | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
// Variant that also gets the arg string. | ||||||||||||||||||||||||||||||||
internal static unsafe bool TryGetProcessInfoById(int pid, out ProcessInfo processInfo, out string argString) | ||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||
ProcessInfo info = default; | ||||||||||||||||||||||||||||||||
byte* argBuf = stackalloc byte[PRARGSZ]; | ||||||||||||||||||||||||||||||||
if (ReadProcessInfo(pid, &info, argBuf, PRARGSZ) < 0) | ||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||
Interop.ErrorInfo errorInfo = Sys.GetLastErrorInfo(); | ||||||||||||||||||||||||||||||||
throw new IOException(errorInfo.GetErrorMessage(), errorInfo.RawErrno); | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
processInfo = info; | ||||||||||||||||||||||||||||||||
argString = Marshal.PtrToStringUTF8((IntPtr)argBuf)!; | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
return true; | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
Comment on lines
+58
to
+59
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
Nit |
||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,43 @@ | ||||||
// 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 | ||||||
{ | ||||||
|
||||||
// See caller: ProcessManager.SunOS.cs | ||||||
|
||||||
[LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_ReadThreadInfo", SetLastError = true)] | ||||||
internal static unsafe partial int ReadThreadInfo(int pid, int tid, ThreadInfo* threadInfo); | ||||||
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
Can this be private? |
||||||
|
||||||
/// <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) | ||||||
{ | ||||||
ThreadInfo info = default; | ||||||
if (ReadThreadInfo(pid, tid, &info) < 0) | ||||||
{ | ||||||
Interop.ErrorInfo errorInfo = Sys.GetLastErrorInfo(); | ||||||
throw new IOException(errorInfo.GetErrorMessage(), errorInfo.RawErrno); | ||||||
} | ||||||
threadInfo = info; | ||||||
|
||||||
return true; | ||||||
} | ||||||
|
||||||
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
Nit |
||||||
} | ||||||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -1,7 +1,7 @@ | ||||||||||
<Project Sdk="Microsoft.NET.Sdk"> | ||||||||||
|
||||||||||
<PropertyGroup> | ||||||||||
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-freebsd;$(NetCoreAppCurrent)-linux;$(NetCoreAppCurrent)-osx;$(NetCoreAppCurrent)-maccatalyst;$(NetCoreAppCurrent)-ios;$(NetCoreAppCurrent)-tvos;$(NetCoreAppCurrent)</TargetFrameworks> | ||||||||||
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-freebsd;$(NetCoreAppCurrent)-linux;$(NetCoreAppCurrent)-osx;$(NetCoreAppCurrent)-maccatalyst;$(NetCoreAppCurrent)-ios;$(NetCoreAppCurrent)-tvos;$(NetCoreAppCurrent)-illumos;$(NetCoreAppCurrent)-solaris;$(NetCoreAppCurrent)</TargetFrameworks> | ||||||||||
<DefineConstants>$(DefineConstants);FEATURE_REGISTRY</DefineConstants> | ||||||||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||||||||||
<UseCompilerGeneratedDocXmlFile>false</UseCompilerGeneratedDocXmlFile> | ||||||||||
|
@@ -369,6 +369,19 @@ | |||||||||
Link="Common\Interop\FreeBSD\Interop.Process.GetProcInfo.cs" /> | ||||||||||
</ItemGroup> | ||||||||||
|
||||||||||
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'illumos' or '$(TargetPlatformIdentifier)' == 'solaris'"> | ||||||||||
<Compile Include="System\Diagnostics\Process.BSD.cs" /> | ||||||||||
<Compile Include="System\Diagnostics\Process.SunOS.cs" /> | ||||||||||
<Compile Include="System\Diagnostics\ProcessManager.SunOS.cs" /> | ||||||||||
<Compile Include="System\Diagnostics\ProcessThread.SunOS.cs" /> | ||||||||||
<Compile Include="$(CommonPath)Interop\SunOS\procfs\Interop.ProcFs.Definitions.cs" | ||||||||||
Link="Common\Interop\SunOS\procfs\Interop.ProcFs.Definitions.cs" /> | ||||||||||
Comment on lines
+377
to
+378
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
Nit: There is no other |
||||||||||
<Compile Include="$(CommonPath)Interop\SunOS\procfs\Interop.ProcFs.TryGetProcessInfoById.cs" | ||||||||||
Link="Common\Interop\SunOS\procfs\Interop.ProcFs.TryGetProcessInfoById.cs" /> | ||||||||||
<Compile Include="$(CommonPath)Interop\SunOS\procfs\Interop.ProcFs.TryGetThreadInfoById.cs" | ||||||||||
Link="Common\Interop\SunOS\procfs\Interop.ProcFs.TryGetThreadInfoById.cs" /> | ||||||||||
</ItemGroup> | ||||||||||
|
||||||||||
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'ios' or '$(TargetPlatformIdentifier)' == 'tvos'"> | ||||||||||
<Compile Include="System\Diagnostics\Process.iOS.cs" /> | ||||||||||
<Compile Include="System\Diagnostics\ProcessManager.iOS.cs" /> | ||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,137 @@ | ||||||||||
// 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 | ||||||||||
{ | ||||||||||
|
||||||||||
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
|
||||||||||
/// <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
+129
to
+130
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; | ||||||||||
} | ||||||||||
} | ||||||||||
} |
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.
It would make more sense for this to be in Interop.ProcFs.TryGetProcessInfoById.cs. We typically place the structures that support the internal API in the same file as the API, so that one can include the least amount of interop goo a lot of unnecessary goo to use the internal API.