/
ProcessMemoryUsage.cs
executable file
·330 lines (297 loc) · 13.8 KB
/
ProcessMemoryUsage.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
* Copyright (c) 2016 - 2017 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
namespace Tizen.System
{
/// <summary>
/// The class for memory information per process.
/// </summary>
/// <since_tizen> 4 </since_tizen>
public class ProcessMemoryUsage
{
private int[] Pids;
private Interop.RuntimeInfo.ProcessMemoryInfo[] Usages;
private int[] Swaps;
private int[] Gpus;
private int[] Gems;
/// <summary>
/// The constructor of ProcessMemoryInformation class.
/// </summary>
/// <since_tizen> 4 </since_tizen>
/// <param name="pid">List of unique process ids.</param>
/// <privilege>http://tizen.org/privilege/systemmonitor</privilege>
/// <exception cref="ArgumentException">Thrown when the <paramref name="pid"/> is empty.</exception>
/// <exception cref="IOException">Thrown when an I/O error occurs while reading from the system or requesting to the resource management daemon.</exception>
/// <exception cref="OutOfMemoryException">Thrown when the memory is not enough to allocate.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when the caller does not have privilege to use this method.</exception>
public ProcessMemoryUsage(IEnumerable<int> pid)
{
Update(pid);
}
/// <summary>
/// The number of usage entries.
/// </summary>
/// <since_tizen> 4 </since_tizen>
public int Count => Pids.Length;
/// <summary>
/// Gets the virtual memory size of a process.
/// </summary>
/// <since_tizen> 4 </since_tizen>
/// <param name="pid">The process id.</param>
/// <returns>The virtual memory size <paramref name="pid"/> is using (KiB).</returns>
/// <exception cref="ArgumentException">Thrown when the <paramref name="pid"/> is invalid.</exception>
public int GetVsz(int pid)
{
for (int i = 0; i < Count; i++)
if (pid == Pids[i])
return Usages[i].Vsz;
Log.Error(InformationErrorFactory.LogTag, "Invalid pid");
InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
return 0;
}
/// <summary>
/// Gets the resident set size of a process.
/// </summary>
/// <since_tizen> 4 </since_tizen>
/// <param name="pid">The process id.</param>
/// <returns>The resident set size <paramref name="pid"/> is using (KiB).</returns>
/// <exception cref="ArgumentException">Thrown when the <paramref name="pid"/> is invalid.</exception>
public int GetRss(int pid)
{
for (int i = 0; i < Count; i++)
if (pid == Pids[i])
return Usages[i].Rss;
Log.Error(InformationErrorFactory.LogTag, "Invalid pid");
InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
return 0;
}
/// <summary>
/// Gets the proportional set size of a process.
/// </summary>
/// <since_tizen> 4 </since_tizen>
/// <param name="pid">The process id.</param>
/// <returns>The proportional set size <paramref name="pid"/> is using (KiB).</returns>
/// <exception cref="ArgumentException">Thrown when the <paramref name="pid"/> is invalid.</exception>
public int GetPss(int pid)
{
for (int i = 0; i < Count; i++)
if (pid == Pids[i])
return Usages[i].Pss;
Log.Error(InformationErrorFactory.LogTag, "Invalid pid");
InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
return 0;
}
/// <summary>
/// Gets the size not modified and mapped by other processes of a process.
/// </summary>
/// <since_tizen> 4 </since_tizen>
/// <param name="pid">The process id.</param>
/// <returns>The shared clean memory size <paramref name="pid"/> is using (KiB).</returns>
/// <exception cref="ArgumentException">Thrown when the <paramref name="pid"/> is invalid.</exception>
public int GetSharedClean(int pid)
{
for (int i = 0; i < Count; i++)
if (pid == Pids[i])
return Usages[i].SharedClean;
Log.Error(InformationErrorFactory.LogTag, "Invalid pid");
InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
return 0;
}
/// <summary>
/// Gets the size modified and mapped by other processes of a process.
/// </summary>
/// <since_tizen> 4 </since_tizen>
/// <param name="pid">The process id.</param>
/// <returns>The shared dirty memory size <paramref name="pid"/> is using (KiB).</returns>
/// <exception cref="ArgumentException">Thrown when the <paramref name="pid"/> is invalid.</exception>
public int GetSharedDirty(int pid)
{
for (int i = 0; i < Count; i++)
if (pid == Pids[i])
return Usages[i].SharedDirty;
Log.Error(InformationErrorFactory.LogTag, "Invalid pid");
InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
return 0;
}
/// <summary>
/// Gets the size not modified and available only to that process of a process.
/// </summary>
/// <since_tizen> 4 </since_tizen>
/// <param name="pid">The process id.</param>
/// <returns>The private clean memory size <paramref name="pid"/> is using (KiB).</returns>
/// <exception cref="ArgumentException">Thrown when the <paramref name="pid"/> is invalid.</exception>
public int GetPrivateClean(int pid)
{
for (int i = 0; i < Count; i++)
if (pid == Pids[i])
return Usages[i].PrivateClean;
Log.Error(InformationErrorFactory.LogTag, "Invalid pid");
InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
return 0;
}
/// <summary>
/// Gets the size modified and available only to that process of a process.
/// </summary>
/// <since_tizen> 4 </since_tizen>
/// <param name="pid">The process id.</param>
/// <returns>The private dirty memory size <paramref name="pid"/> is using (KiB).</returns>
/// <exception cref="ArgumentException">Thrown when the <paramref name="pid"/> is invalid.</exception>
public int GetPrivateDirty(int pid)
{
for (int i = 0; i < Count; i++)
if (pid == Pids[i])
return Usages[i].PrivateDirty;
Log.Error(InformationErrorFactory.LogTag, "Invalid pid");
InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
return 0;
}
/// <summary>
/// Gets the GPU memory size of a process.
/// </summary>
/// <param name="pid">The process id.</param>
/// <returns>The GPU memory size <paramref name="pid"/> is using (KiB).</returns>
/// <exception cref="ArgumentException">Thrown when the <paramref name="pid"/> is invalid.</exception>
/// <exception cref="NotSupportedException">Thrown when the data is empty.</exception>
[EditorBrowsable(EditorBrowsableState.Never)]
public int GetGPU(int pid)
{
if (Gpus == null)
{
Log.Error(InformationErrorFactory.LogTag, "No data");
InformationErrorFactory.ThrowException(InformationError.NoData);
}
for (int i = 0; i < Count; i++)
if (pid == Pids[i])
return Gpus[i];
Log.Error(InformationErrorFactory.LogTag, "Invalid pid");
InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
return 0;
}
/// <summary>
/// Gets the resident set size in graphic execution manager of a process.
/// </summary>
/// <param name="pid">The process id.</param>
/// <returns>The resident set size <paramref name="pid"/> is using (KiB).</returns>
/// <exception cref="ArgumentException">Thrown when the <paramref name="pid"/> is invalid.</exception>
/// <exception cref="NotSupportedException">Thrown when the data is empty.</exception>
[EditorBrowsable(EditorBrowsableState.Never)]
public int GetGemRss(int pid)
{
if (Gems == null)
{
Log.Error(InformationErrorFactory.LogTag, "No data");
InformationErrorFactory.ThrowException(InformationError.NoData);
}
for (int i = 0; i < Count; i++)
if (pid == Pids[i])
return Gems[i];
Log.Error(InformationErrorFactory.LogTag, "Invalid pid");
InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
return 0;
}
/// <summary>
/// Gets the SWAP memory size of a process.
/// </summary>
/// <param name="pid">The process id.</param>
/// <returns>The SWAP memory size <paramref name="pid"/> is using (KiB).</returns>
/// <exception cref="ArgumentException">Thrown when the <paramref name="pid"/> is invalid.</exception>
/// <exception cref="NotSupportedException">Thrown when the data is empty.</exception>
[EditorBrowsable(EditorBrowsableState.Never)]
public int GetSwap(int pid)
{
if (Swaps == null)
{
Log.Error(InformationErrorFactory.LogTag, "No data");
InformationErrorFactory.ThrowException(InformationError.NoData);
}
for (int i = 0; i < Count; i++)
if (pid == Pids[i])
return Swaps[i];
Log.Error(InformationErrorFactory.LogTag, "Invalid pid");
InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
return 0;
}
private int[] GetProcessMemoryValueInt(Interop.RuntimeInfo.ProcessMemoryInfoKey key, IEnumerable<int> pids)
{
var ret = Interop.RuntimeInfo.GetProcessMemoryValueInt(pids.ToArray<int>(), pids.ToArray<int>().Length, key, out IntPtr ptr);
if (ret != InformationError.None)
{
Log.Error(InformationErrorFactory.LogTag, "Interop failed to get process memory info: " + key.ToString());
if (ret == InformationError.NoData)
return null;
InformationErrorFactory.ThrowException(ret);
}
try
{
var array = new int[Count];
unsafe
{
for (int i = 0; i < Count; i++)
array[i] = *((int*)ptr.ToPointer() + (i * sizeof(int)));
}
return array;
}
finally
{
Interop.Libc.Free(ptr);
}
}
/// <summary>
/// Update the process memory information to the latest.
/// </summary>
/// <since_tizen> 4 </since_tizen>
/// <param name="pid">List of unique process ids.</param>
/// <privilege>http://tizen.org/privilege/systemmonitor</privilege>
/// <exception cref="ArgumentException">Thrown when the <paramref name="pid"/> is empty.</exception>
/// <exception cref="IOException">Thrown when an I/O error occurs while reading from the system or requesting to the resource management daemon.</exception>
/// <exception cref="OutOfMemoryException">Thrown when the memory is not enough to allocate.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when the caller does not have privilege to use this method.</exception>
public void Update(IEnumerable<int> pid)
{
InformationError ret;
Pids = pid.ToArray<int>();
IntPtr ptr = new IntPtr();
ret = Interop.RuntimeInfo.GetProcessMemoryInfo(Pids, Count, ref ptr);
if (ret != InformationError.None)
{
Log.Error(InformationErrorFactory.LogTag, "Interop failed to get Process cpu usage.");
InformationErrorFactory.ThrowException(ret);
}
try
{
var data = ptr;
Usages = new Interop.RuntimeInfo.ProcessMemoryInfo[Count];
for (int i = 0; i < Count; i++)
{
Usages[i] = Marshal.PtrToStructure<Interop.RuntimeInfo.ProcessMemoryInfo>(data);
data += Marshal.SizeOf<Interop.RuntimeInfo.ProcessCpuUsage>();
}
}
finally
{
Interop.Libc.Free(ptr);
}
Gpus = GetProcessMemoryValueInt(Interop.RuntimeInfo.ProcessMemoryInfoKey.Gpu, pid);
Gems = GetProcessMemoryValueInt(Interop.RuntimeInfo.ProcessMemoryInfoKey.GemRss, pid);
Swaps = GetProcessMemoryValueInt(Interop.RuntimeInfo.ProcessMemoryInfoKey.Swap, pid);
}
}
}