-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathInterop.ProcFsStat.TryReadStatusFile.cs
196 lines (179 loc) · 6.98 KB
/
Interop.ProcFsStat.TryReadStatusFile.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#nullable enable
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Text;
internal static partial class Interop
{
internal static partial class ProcFs
{
internal const string RootPath = "/proc/";
private const string StatusFileName = "/status";
internal struct ParsedStatus
{
#if DEBUG
internal int Pid;
#endif
internal ulong VmHWM;
internal ulong VmRSS;
internal ulong VmData;
internal ulong VmSwap;
internal ulong VmSize;
internal ulong VmPeak;
}
internal static string GetStatusFilePathForProcess(int pid)
{
return RootPath + pid.ToString(CultureInfo.InvariantCulture) + StatusFileName;
}
internal static bool TryReadStatusFile(int pid, out ParsedStatus result)
{
bool b = TryParseStatusFile(GetStatusFilePathForProcess(pid), out result);
#if DEBUG
Debug.Assert(!b || result.Pid == pid, "Expected process ID from status file to match supplied pid");
#endif
return b;
}
internal static bool TryParseStatusFile(string statusFilePath, out ParsedStatus result)
{
if (!TryReadFile(statusFilePath, out string? fileContents))
{
// Between the time that we get an ID and the time that we try to read the associated stat
// file(s), the process could be gone.
result = default(ParsedStatus);
return false;
}
ParsedStatus results = default(ParsedStatus);
ReadOnlySpan<char> statusFileContents = fileContents.AsSpan();
int unitSliceLength = -1;
#if DEBUG
int nonUnitSliceLength = -1;
#endif
while (!statusFileContents.IsEmpty)
{
int startIndex = statusFileContents.IndexOf(':');
if (startIndex == -1)
{
// Reached end of file
break;
}
ReadOnlySpan<char> title = statusFileContents.Slice(0, startIndex);
statusFileContents = statusFileContents.Slice(startIndex + 1);
int endIndex = statusFileContents.IndexOf('\n');
if (endIndex == -1)
{
endIndex = statusFileContents.Length - 1;
unitSliceLength = statusFileContents.Length - 3;
#if DEBUG
nonUnitSliceLength = statusFileContents.Length;
#endif
}
else
{
unitSliceLength = endIndex - 3;
#if DEBUG
nonUnitSliceLength = endIndex;
#endif
}
ReadOnlySpan<char> value = default;
bool valueParsed = true;
#if DEBUG
if (title.SequenceEqual("Pid".AsSpan()))
{
value = statusFileContents.Slice(0, nonUnitSliceLength);
valueParsed = int.TryParse(value, out results.Pid);
}
#endif
if (title.SequenceEqual("VmHWM".AsSpan()))
{
value = statusFileContents.Slice(0, unitSliceLength);
valueParsed = ulong.TryParse(value, out results.VmHWM);
}
else if (title.SequenceEqual("VmRSS".AsSpan()))
{
value = statusFileContents.Slice(0, unitSliceLength);
valueParsed = ulong.TryParse(value, out results.VmRSS);
}
else if (title.SequenceEqual("VmData".AsSpan()))
{
value = statusFileContents.Slice(0, unitSliceLength);
valueParsed = ulong.TryParse(value, out ulong vmData);
results.VmData += vmData;
}
else if (title.SequenceEqual("VmSwap".AsSpan()))
{
value = statusFileContents.Slice(0, unitSliceLength);
valueParsed = ulong.TryParse(value, out results.VmSwap);
}
else if (title.SequenceEqual("VmSize".AsSpan()))
{
value = statusFileContents.Slice(0, unitSliceLength);
valueParsed = ulong.TryParse(value, out results.VmSize);
}
else if (title.SequenceEqual("VmPeak".AsSpan()))
{
value = statusFileContents.Slice(0, unitSliceLength);
valueParsed = ulong.TryParse(value, out results.VmPeak);
}
else if (title.SequenceEqual("VmStk".AsSpan()))
{
value = statusFileContents.Slice(0, unitSliceLength);
valueParsed = ulong.TryParse(value, out ulong vmStack);
results.VmData += vmStack;
}
Debug.Assert(valueParsed);
statusFileContents = statusFileContents.Slice(endIndex + 1);
}
results.VmData *= 1024;
results.VmPeak *= 1024;
results.VmSize *= 1024;
results.VmSwap *= 1024;
results.VmRSS *= 1024;
results.VmHWM *= 1024;
result = results;
return true;
}
private static bool TryReadFile(string path, [NotNullWhen(true)] out string? contents)
{
Debug.Assert(!string.IsNullOrEmpty(path));
byte[] bytes = ArrayPool<byte>.Shared.Rent(4096);
int count = 0;
try
{
using var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 1, useAsync: false);
while (true)
{
int read = fileStream.Read(bytes, count, bytes.Length - count);
if (read == 0)
{
contents = Encoding.UTF8.GetString(bytes, 0, count);
return true;
}
count += read;
if (count >= bytes.Length)
{
byte[] temp = ArrayPool<byte>.Shared.Rent(bytes.Length * 2);
Array.Copy(bytes, temp, bytes.Length);
byte[] toReturn = bytes;
bytes = temp;
ArrayPool<byte>.Shared.Return(toReturn);
}
}
}
catch (Exception ex) when (ex is IOException || ex.InnerException is IOException)
{
contents = null;
return false;
}
finally
{
ArrayPool<byte>.Shared.Return(bytes);
}
}
}
}