Skip to content

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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 ProcessStatus
[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;
// add more fields when needed.
}

// Output type for TryGetThreadInfoById()
// Keep in sync with pal_io.h ThreadStatus
[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;
// add more fields when needed.
}

internal static string GetInfoFilePathForProcess(int pid) =>
$"{RootPath}{(uint)pid}{psinfoFileName}";

internal static string GetLwpDirForProcess(int pid) =>
$"{RootPath}{(uint)pid}{lwpDirName}";

internal static string GetInfoFilePathForThread(int pid, int tid) =>
$"{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_ReadProcessStatusInfo", SetLastError = true)]
private static unsafe partial int ReadProcessStatusInfo(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.
/// </returns>
internal static unsafe bool TryGetProcessInfoById(int pid, out ProcessInfo processInfo)
{
ProcessInfo info = default;
if (ReadProcessStatusInfo(pid, &info, null, 0) < 0)
{
Interop.ErrorInfo errorInfo = Sys.GetLastErrorInfo();
throw new IOException(errorInfo.GetErrorMessage(), errorInfo.RawErrno);
}
processInfo = info;

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 (ReadProcessStatusInfo(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;
}


}
}
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_ReadProcessLwpInfo", SetLastError = true)]
internal static unsafe partial int ReadProcessLwpInfo(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)
{
ThreadInfo info = default;
if (ReadProcessLwpInfo(pid, tid, &info) < 0)
{
Interop.ErrorInfo errorInfo = Sys.GetLastErrorInfo();
throw new IOException(errorInfo.GetErrorMessage(), errorInfo.RawErrno);
}
threadInfo = info;

return true;
}

}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public static partial class PlatformDetection
public static bool IsNotMacCatalyst => !IsMacCatalyst;
public static bool Isillumos => RuntimeInformation.IsOSPlatform(OSPlatform.Create("ILLUMOS"));
public static bool IsSolaris => RuntimeInformation.IsOSPlatform(OSPlatform.Create("SOLARIS"));
public static bool IsSunOS => Isillumos || IsSolaris;
public static bool IsBrowser => RuntimeInformation.IsOSPlatform(OSPlatform.Create("BROWSER"));
public static bool IsWasi => RuntimeInformation.IsOSPlatform(OSPlatform.Create("WASI"));
public static bool IsNotBrowser => !IsBrowser;
Expand Down
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>
Expand Down Expand Up @@ -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" />
<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" />
Expand Down
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
{

/// <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))
{
throw new Win32Exception(SR.ProcessInformationUnavailable);
}
return iinfo;
}
}
}
Loading
Loading