Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
NtProcessInfoHelper.cs
Go to the documentation of this file.
6
7namespace System.Diagnostics;
8
9internal static class NtProcessInfoHelper
10{
11 private static long[] CachedBuffer;
12
13 internal unsafe static ProcessInfo[] GetProcessInfos(int? processIdFilter = null)
14 {
15 int num = 131072;
17 try
18 {
19 while (true)
20 {
21 if (array == null)
22 {
23 array = new long[(num + 7) / 8];
24 }
25 uint requiredSize = 0u;
27 {
28 uint num2 = global::Interop.NtDll.NtQuerySystemInformation(5, systemInformation, (uint)(array.Length * 8), &requiredSize);
29 if (num2 != 3221225476u)
30 {
31 if ((int)num2 < 0)
32 {
34 }
35 return GetProcessInfos(MemoryMarshal.AsBytes<long>(array), processIdFilter);
36 }
37 }
38 array = null;
39 num = GetNewBufferSize(num, (int)requiredSize);
40 }
41 }
42 finally
43 {
45 }
46 }
47
49 {
50 int num = ((requiredSize != 0) ? (requiredSize + 10240) : (existingBufferSize * 2));
51 if (num < 0)
52 {
53 throw new OutOfMemoryException();
54 }
55 return num;
56 }
57
59 {
61 int num = 0;
62 while (true)
63 {
64 ref readonly global::Interop.NtDll.SYSTEM_PROCESS_INFORMATION reference = ref MemoryMarshal.AsRef<global::Interop.NtDll.SYSTEM_PROCESS_INFORMATION>(data.Slice(num));
65 int num2 = reference.UniqueProcessId.ToInt32();
66 if (!processIdFilter.HasValue || processIdFilter.GetValueOrDefault() == num2)
67 {
68 ProcessInfo processInfo = new ProcessInfo((int)reference.NumberOfThreads)
69 {
71 SessionId = (int)reference.SessionId,
72 PoolPagedBytes = (long)(ulong)reference.QuotaPagedPoolUsage,
73 PoolNonPagedBytes = (long)(ulong)reference.QuotaNonPagedPoolUsage,
74 VirtualBytes = (long)(ulong)reference.VirtualSize,
75 VirtualBytesPeak = (long)(ulong)reference.PeakVirtualSize,
76 WorkingSetPeak = (long)(ulong)reference.PeakWorkingSetSize,
77 WorkingSet = (long)(ulong)reference.WorkingSetSize,
78 PageFileBytesPeak = (long)(ulong)reference.PeakPagefileUsage,
79 PageFileBytes = (long)(ulong)reference.PagefileUsage,
80 PrivateBytes = (long)(ulong)reference.PrivatePageCount,
81 BasePriority = reference.BasePriority,
82 HandleCount = (int)reference.HandleCount
83 };
84 if (reference.ImageName.Buffer == IntPtr.Zero)
85 {
87 {
88 processInfo.ProcessName = "System";
89 }
90 else if (processInfo.ProcessId == 0)
91 {
92 processInfo.ProcessName = "Idle";
93 }
94 else
95 {
96 processInfo.ProcessName = processInfo.ProcessId.ToString(CultureInfo.InvariantCulture);
97 }
98 }
99 else
100 {
101 string processShortName = GetProcessShortName(new ReadOnlySpan<char>(reference.ImageName.Buffer.ToPointer(), reference.ImageName.Length / 2));
102 processInfo.ProcessName = processShortName;
103 }
105 int num3 = num + sizeof(global::Interop.NtDll.SYSTEM_PROCESS_INFORMATION);
106 for (int i = 0; i < reference.NumberOfThreads; i++)
107 {
108 ref readonly global::Interop.NtDll.SYSTEM_THREAD_INFORMATION reference2 = ref MemoryMarshal.AsRef<global::Interop.NtDll.SYSTEM_THREAD_INFORMATION>(data.Slice(num3));
110 {
111 _processId = (int)reference2.ClientId.UniqueProcess,
112 _threadId = (ulong)(long)reference2.ClientId.UniqueThread,
113 _basePriority = reference2.BasePriority,
114 _currentPriority = reference2.Priority,
115 _startAddress = reference2.StartAddress,
116 _threadState = (ThreadState)reference2.ThreadState,
117 _threadWaitReason = NtProcessManager.GetThreadWaitReason((int)reference2.WaitReason)
118 };
119 processInfo._threadInfoList.Add(item);
120 num3 += sizeof(global::Interop.NtDll.SYSTEM_THREAD_INFORMATION);
121 }
122 }
123 if (reference.NextEntryOffset == 0)
124 {
125 break;
126 }
127 num += (int)reference.NextEntryOffset;
128 }
129 ProcessInfo[] array = new ProcessInfo[dictionary.Values.Count];
130 dictionary.Values.CopyTo(array, 0);
131 return array;
132 }
133
134 internal static string GetProcessShortName(ReadOnlySpan<char> name)
135 {
136 if (name.IsEmpty)
137 {
138 return string.Empty;
139 }
140 int num = -1;
141 int num2 = -1;
142 for (int i = 0; i < name.Length; i++)
143 {
144 if (name[i] == '\\')
145 {
146 num = i;
147 }
148 else if (name[i] == '.')
149 {
150 num2 = i;
151 }
152 }
153 if (num2 == -1)
154 {
155 num2 = name.Length - 1;
156 }
157 else
158 {
159 ReadOnlySpan<char> span = name.Slice(num2);
160 num2 = ((!MemoryExtensions.Equals(span, ".exe", StringComparison.OrdinalIgnoreCase)) ? (name.Length - 1) : (num2 - 1));
161 }
162 num = ((num != -1) ? (num + 1) : 0);
163 return name.Slice(num, num2 - num + 1).ToString();
164 }
165}
void Add(TKey key, TValue value)
static string GetProcessShortName(ReadOnlySpan< char > name)
static unsafe ProcessInfo[] GetProcessInfos(int? processIdFilter=null)
static unsafe ProcessInfo[] GetProcessInfos(ReadOnlySpan< byte > data, int? processIdFilter)
static int GetNewBufferSize(int existingBufferSize, int requiredSize)
static ThreadWaitReason GetThreadWaitReason(int value)
static CultureInfo InvariantCulture
static bool Equals(this ReadOnlySpan< char > span, ReadOnlySpan< char > other, StringComparison comparisonType)
static string CouldntGetProcessInfos
Definition SR.cs:46
Definition SR.cs:7
static int Exchange(ref int location1, int value)
static readonly IntPtr Zero
Definition IntPtr.cs:18