Skip to content
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,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
Copy link
Member

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.

{
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar here - it would make sense for this to be next to ReadThreadInfo.

{
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is uint8_t in the unmanaged view. It should be byte here.

(char is 16-bit in C#, so there is a mismatch between the managed and unmanaged definitions.)

}

internal static string GetInfoFilePathForProcess(int pid) =>
Copy link
Member

Choose a reason for hiding this comment

The 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) =>
Copy link
Member

Choose a reason for hiding this comment

The 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.
Copy link
Member

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)

/// </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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ProcessInfo info = default;
if (ReadProcessInfo(pid, &info, null, 0) < 0)
{
Interop.ErrorInfo errorInfo = Sys.GetLastErrorInfo();
throw new IOException(errorInfo.GetErrorMessage(), errorInfo.RawErrno);
}
processInfo = info;
fixed (ProcessInfo* pProcessInfo = &processInfo)
{
if (ReadProcessInfo(pid, pProcessInfo, null, 0) < 0)
{
Interop.ErrorInfo errorInfo = Sys.GetLastErrorInfo();
throw new IOException(errorInfo.GetErrorMessage(), errorInfo.RawErrno);
}
}

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
Copy link
Member

Choose a reason for hiding this comment

The 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
internal static unsafe partial int ReadThreadInfo(int pid, int tid, ThreadInfo* threadInfo);
private static unsafe partial int ReadThreadInfo(int pid, int tid, ThreadInfo* threadInfo);

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

Copy link
Member

Choose a reason for hiding this comment

The 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
Expand Up @@ -48,6 +48,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" />
Comment on lines +377 to +378
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<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.cs"
Link="Common\Interop\SunOS\procfs\Interop.ProcFs.cs" />

Nit: There is no other *.Definitions.cs file under libraries. To follow the repo conventions, this should be Interop.ProcFs.cs or Interop.ProcFs.Common.cs - if there is anything left in this file after incorporating the other feedback.

<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
{

Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Interop.procfs.ProcessInfo iinfo;
if (!Interop.procfs.TryGetProcessInfoById(_processId, out iinfo))
Interop.procfs.ProcessInfo processInfo;
if (!Interop.procfs.TryGetProcessInfoById(_processId, out processInfo))

Does the i prefix in iinfo mean something?

{
throw new Win32Exception(SR.ProcessInformationUnavailable);
}
return iinfo;
}
}
}
Loading
Loading