Skip to content

Commit 2d0f7fe

Browse files
committed
Jan Kotas review 9/9
Let ThreadInfo.StatusCode be uint16_t (C# char) move structs where they're used Remove Interop.ProcFs.Definitions.cs Let SDP compose /proc strings Use fixed in getinfo funcs kill unused, fix nits
1 parent 8194ac0 commit 2d0f7fe

File tree

9 files changed

+67
-96
lines changed

9 files changed

+67
-96
lines changed

src/libraries/Common/src/Interop/SunOS/procfs/Interop.ProcFs.Definitions.cs

Lines changed: 0 additions & 62 deletions
This file was deleted.

src/libraries/Common/src/Interop/SunOS/procfs/Interop.ProcFs.TryGetProcessInfoById.cs

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,39 @@
77
using System.Runtime.CompilerServices;
88
using System.Runtime.InteropServices;
99

10+
// See callers:
11+
// ProcessManager.SunOS.cs
12+
// Environment.SunOS etc.
13+
1014
internal static partial class Interop
1115
{
1216
internal static partial class @procfs
1317
{
1418

15-
// See caller: ProcessManager.SunOS.cs
19+
// Constants from sys/procfs.h
20+
private const int PRARGSZ = 80;
21+
22+
// Output type for TryGetProcessInfoById()
23+
// Keep in sync with pal_io.h ProcessInfo
24+
[StructLayout(LayoutKind.Sequential)]
25+
internal struct ProcessInfo
26+
{
27+
internal ulong VirtualSize;
28+
internal ulong ResidentSetSize;
29+
internal long StartTime;
30+
internal long StartTimeNsec;
31+
internal long CpuTotalTime;
32+
internal long CpuTotalTimeNsec;
33+
internal int Pid;
34+
internal int ParentPid;
35+
internal int SessionId;
36+
internal int Priority;
37+
internal int NiceVal;
38+
}
1639

1740
[LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_ReadProcessInfo", SetLastError = true)]
1841
private static unsafe partial int ReadProcessInfo(int pid, ProcessInfo* processInfo, byte* argBuf, int argBufSize);
1942

20-
// Handy helpers for Environment.SunOS etc.
21-
2243
/// <summary>
2344
/// Attempts to get status info for the specified process ID.
2445
/// </summary>
@@ -29,33 +50,31 @@ internal static partial class @procfs
2950
/// </returns>
3051
internal static unsafe bool TryGetProcessInfoById(int pid, out ProcessInfo processInfo)
3152
{
32-
ProcessInfo info = default;
33-
if (ReadProcessInfo(pid, &info, null, 0) < 0)
53+
fixed (ProcessInfo* pProcessInfo = &processInfo)
3454
{
35-
Interop.ErrorInfo errorInfo = Sys.GetLastErrorInfo();
36-
throw new IOException(errorInfo.GetErrorMessage(), errorInfo.RawErrno);
55+
if (ReadProcessInfo(pid, pProcessInfo, null, 0) < 0)
56+
{
57+
Interop.ErrorInfo errorInfo = Sys.GetLastErrorInfo();
58+
throw new IOException(errorInfo.GetErrorMessage(), errorInfo.RawErrno);
59+
}
3760
}
38-
processInfo = info;
39-
4061
return true;
4162
}
4263

4364
// Variant that also gets the arg string.
4465
internal static unsafe bool TryGetProcessInfoById(int pid, out ProcessInfo processInfo, out string argString)
4566
{
46-
ProcessInfo info = default;
4767
byte* argBuf = stackalloc byte[PRARGSZ];
48-
if (ReadProcessInfo(pid, &info, argBuf, PRARGSZ) < 0)
68+
fixed (ProcessInfo* pProcessInfo = &processInfo)
4969
{
50-
Interop.ErrorInfo errorInfo = Sys.GetLastErrorInfo();
51-
throw new IOException(errorInfo.GetErrorMessage(), errorInfo.RawErrno);
70+
if (ReadProcessInfo(pid, pProcessInfo, argBuf, PRARGSZ) < 0)
71+
{
72+
Interop.ErrorInfo errorInfo = Sys.GetLastErrorInfo();
73+
throw new IOException(errorInfo.GetErrorMessage(), errorInfo.RawErrno);
74+
}
5275
}
53-
processInfo = info;
5476
argString = Marshal.PtrToStringUTF8((IntPtr)argBuf)!;
55-
5677
return true;
5778
}
58-
59-
6079
}
6180
}

src/libraries/Common/src/Interop/SunOS/procfs/Interop.ProcFs.TryGetThreadInfoById.cs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,34 @@
77
using System.Runtime.CompilerServices;
88
using System.Runtime.InteropServices;
99

10+
// See callers:
11+
// ProcessManager.SunOS.cs
12+
// ProcessThread.SunOS etc.
13+
1014
internal static partial class Interop
1115
{
1216
internal static partial class @procfs
1317
{
1418

19+
// Output type for TryGetThreadInfoById()
20+
// Keep in sync with pal_io.h ThreadInfo
21+
[StructLayout(LayoutKind.Sequential)]
22+
internal struct ThreadInfo
23+
{
24+
internal long StartTime;
25+
internal long StartTimeNsec;
26+
internal long CpuTotalTime; // user+sys
27+
internal long CpuTotalTimeNsec;
28+
internal int Tid;
29+
internal int Priority;
30+
internal int NiceVal;
31+
internal char StatusCode;
32+
}
33+
1534
// See caller: ProcessManager.SunOS.cs
1635

1736
[LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_ReadThreadInfo", SetLastError = true)]
18-
internal static unsafe partial int ReadThreadInfo(int pid, int tid, ThreadInfo* threadInfo);
37+
private static unsafe partial int ReadThreadInfo(int pid, int tid, ThreadInfo* threadInfo);
1938

2039
/// <summary>
2140
/// Attempts to get status info for the specified thread ID.
@@ -28,16 +47,15 @@ internal static partial class @procfs
2847
/// </returns>
2948
internal static unsafe bool TryGetThreadInfoById(int pid, int tid, out ThreadInfo threadInfo)
3049
{
31-
ThreadInfo info = default;
32-
if (ReadThreadInfo(pid, tid, &info) < 0)
50+
fixed (ThreadInfo* pThreadInfo = &threadInfo)
3351
{
34-
Interop.ErrorInfo errorInfo = Sys.GetLastErrorInfo();
35-
throw new IOException(errorInfo.GetErrorMessage(), errorInfo.RawErrno);
52+
if (ReadThreadInfo(pid, tid, pThreadInfo) < 0)
53+
{
54+
Interop.ErrorInfo errorInfo = Sys.GetLastErrorInfo();
55+
throw new IOException(errorInfo.GetErrorMessage(), errorInfo.RawErrno);
56+
}
3657
}
37-
threadInfo = info;
38-
3958
return true;
4059
}
41-
4260
}
4361
}

src/libraries/System.Diagnostics.Process/src/System.Diagnostics.Process.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,6 @@
374374
<Compile Include="System\Diagnostics\Process.SunOS.cs" />
375375
<Compile Include="System\Diagnostics\ProcessManager.SunOS.cs" />
376376
<Compile Include="System\Diagnostics\ProcessThread.SunOS.cs" />
377-
<Compile Include="$(CommonPath)Interop\SunOS\procfs\Interop.ProcFs.Definitions.cs"
378-
Link="Common\Interop\SunOS\procfs\Interop.ProcFs.Definitions.cs" />
379377
<Compile Include="$(CommonPath)Interop\SunOS\procfs\Interop.ProcFs.TryGetProcessInfoById.cs"
380378
Link="Common\Interop\SunOS\procfs\Interop.ProcFs.TryGetProcessInfoById.cs" />
381379
<Compile Include="$(CommonPath)Interop\SunOS\procfs\Interop.ProcFs.TryGetThreadInfoById.cs"

src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.SunOS.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ internal static IEnumerable<int> EnumerateProcessIds()
105105
{
106106
// Parse /proc for any directory that's named with a number. Each such
107107
// directory represents a process.
108-
foreach (string procDir in Directory.EnumerateDirectories(Interop.procfs.RootPath))
108+
const string dir = "/proc";
109+
foreach (string procDir in Directory.EnumerateDirectories(dir))
109110
{
110111
string dirName = Path.GetFileName(procDir);
111112
int pid;
@@ -122,7 +123,7 @@ internal static IEnumerable<int> EnumerateThreadIds(int pid)
122123
{
123124
// Parse /proc/$pid/lwp for any directory that's named with a number.
124125
// Each such directory represents a thread.
125-
string dir = Interop.procfs.GetLwpDirForProcess(pid);
126+
string dir = $"/proc/{(uint)pid}/lwp";
126127
foreach (string lwpDir in Directory.EnumerateDirectories(dir))
127128
{
128129
string dirName = Path.GetFileName(lwpDir);

src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessThread.SunOS.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ namespace System.Diagnostics
77
{
88
public partial class ProcessThread
99
{
10-
1110
/// <summary>Gets the time this thread was started.</summary>
1211
internal DateTime GetStartTime()
1312
{
@@ -128,6 +127,5 @@ private Interop.procfs.ThreadInfo GetThreadInfo()
128127
}
129128
return iinfo;
130129
}
131-
132130
}
133131
}

src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2640,7 +2640,6 @@
26402640
<Compile Include="$(CommonPath)Interop\Linux\procfs\Interop.ProcFsStat.TryReadStatusFile.cs" Condition="'$(TargetsLinux)' == 'true'" Link="Common\Interop\Linux\Interop.ProcFsStat.TryReadStatusFile.cs" />
26412641
<Compile Include="$(CommonPath)System\IO\StringParser.cs" Condition="'$(TargetsLinux)' == 'true'" Link="Common\System\IO\StringParser.cs" />
26422642
<Compile Include="$(CommonPath)Interop\OSX\Interop.libproc.GetProcessInfoById.cs" Condition="'$(TargetsOSX)' == 'true' or '$(TargetsMacCatalyst)' == 'true'" Link="Common\Interop\OSX\Interop.libproc.GetProcessInfoById.cs" />
2643-
<Compile Include="$(CommonPath)Interop\SunOS\procfs\Interop.ProcFs.Definitions.cs" Condition="'$(Targetsillumos)' == 'true' or '$(TargetsSolaris)' == 'true'" Link="Common\Interop\SunOS\Interop.ProcFs.Definitions.cs" />
26442643
<Compile Include="$(CommonPath)Interop\SunOS\procfs\Interop.ProcFs.TryGetProcessInfoById.cs" Condition="'$(Targetsillumos)' == 'true' or '$(TargetsSolaris)' == 'true'" Link="Common\Interop\SunOS\Interop.ProcFs.GetProcessInfoById.cs" />
26452644
<Compile Include="$(CommonPath)Interop\Linux\os-release\Interop.OSReleaseFile.cs" Condition="'$(TargetsBrowser)' != 'true' and '$(TargetsWasi)' != 'true'" />
26462645
<Compile Include="$(MSBuildThisFileDirectory)System\Environment.Unix.cs" />

src/native/libs/System.Native/pal_io.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1840,7 +1840,7 @@ int32_t SystemNative_ReadThreadInfo(int32_t pid, int32_t tid, ThreadInfo* thread
18401840
threadInfo->Priority = pr.pr_pri;
18411841
threadInfo->NiceVal = pr.pr_nice;
18421842
// Status code, a char: ...
1843-
threadInfo->StatusCode = (char)pr.pr_sname;
1843+
threadInfo->StatusCode = (uchar_t)pr.pr_sname;
18441844
// Thread start time and CPU time
18451845
threadInfo->StartTime = pr.pr_start.tv_sec;
18461846
threadInfo->StartTimeNsec = pr.pr_start.tv_nsec;

src/native/libs/System.Native/pal_io.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ typedef struct
6262
int32_t Tid;
6363
int32_t Priority;
6464
int32_t NiceVal;
65-
uint8_t StatusCode; // [ORSTWZ] See ProcFsStateToThreadState()
65+
uint16_t StatusCode; // See ProcFsStateToThreadState()
6666
} ThreadInfo;
6767

6868
// NOTE: the layout of this type is intended to exactly match the layout of a `struct iovec`. There are

0 commit comments

Comments
 (0)