Release 4.0.0-preview1-00201
[platform/core/csapi/tizenfx.git] / src / Tizen.System.Information / Usage / ProcessMemoryUsage.cs
1 /*
2 * Copyright (c) 2016 - 2017 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 using System;
18 using System.Collections.Generic;
19 using System.IO;
20 using System.Linq;
21 using System.Runtime.InteropServices;
22
23 namespace Tizen.System
24 {
25     /// <summary>
26     /// The class for memory information per process.
27     /// </summary>
28     public class ProcessMemoryUsage
29     {
30         private int[] Pids;
31         private Interop.RuntimeInfo.ProcessMemoryInfo[] Usages;
32
33         /// <summary>
34         /// The constructor of ProcessMemoryInformation class.
35         /// </summary>
36         /// <since_tizen> 4 </since_tizen>
37         /// <param name="pid">List of unique process ids.</param>
38         /// <privilege>http://tizen.org/privilege/systemmonitor</privilege>
39         /// <exception cref="ArgumentException">Thrown when the <paramref name="pid"/> is empty.</exception>
40         /// <exception cref="IOException">Thrown when an I/O error occurs while reading from the system or requesting to the resource management daemon.</exception>
41         /// <exception cref="OutOfMemoryException">Thrown when the memory is not enough to allocate.</exception>
42         /// <exception cref="UnauthorizedAccessException">Thrown when the caller does not have privilege to use this method.</exception>
43         public ProcessMemoryUsage(IEnumerable<int> pid)
44         {
45             Update(pid);
46         }
47
48         /// <summary>
49         /// The number of usage entries.
50         /// </summary>
51         /// <since_tizen> 4 </since_tizen>
52         public int Count { get; internal set; }
53
54         /// <summary>
55         /// Gets the virtual memory size of a process.
56         /// </summary>
57         /// <since_tizen> 4 </since_tizen>
58         /// <param name="pid">The process id.</param>
59         /// <returns>The virtual memory size <paramref name="pid"/> is using (KiB).</returns>
60         /// <exception cref="ArgumentException">Thrown when the <paramref name="pid"/> is invalid.</exception>
61         public int GetVsz(int pid)
62         {
63             for (int i = 0; i < Count; i++)
64                 if (pid == Pids[i])
65                     return Usages[i].Vsz;
66
67             Log.Error(InformationErrorFactory.LogTag, "Invalid pid");
68             InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
69             return 0;
70         }
71
72         /// <summary>
73         /// Gets the resident set size of a process.
74         /// </summary>
75         /// <since_tizen> 4 </since_tizen>
76         /// <param name="pid">The process id.</param>
77         /// <returns>The resident set size <paramref name="pid"/> is using (KiB).</returns>
78         /// <exception cref="ArgumentException">Thrown when the <paramref name="pid"/> is invalid.</exception>
79         public int GetRss(int pid)
80         {
81             for (int i = 0; i < Count; i++)
82                 if (pid == Pids[i])
83                     return Usages[i].Rss;
84
85             Log.Error(InformationErrorFactory.LogTag, "Invalid pid");
86             InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
87             return 0;
88         }
89
90         /// <summary>
91         /// Gets the proportional set size of a process.
92         /// </summary>
93         /// <since_tizen> 4 </since_tizen>
94         /// <param name="pid">The process id.</param>
95         /// <returns>The proportional set size <paramref name="pid"/> is using (KiB).</returns>
96         /// <exception cref="ArgumentException">Thrown when the <paramref name="pid"/> is invalid.</exception>
97         public int GetPss(int pid)
98         {
99             for (int i = 0; i < Count; i++)
100                 if (pid == Pids[i])
101                     return Usages[i].Pss;
102
103             Log.Error(InformationErrorFactory.LogTag, "Invalid pid");
104             InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
105             return 0;
106         }
107
108         /// <summary>
109         /// Gets the size not modified and mapped by other processes of a process.
110         /// </summary>
111         /// <since_tizen> 4 </since_tizen>
112         /// <param name="pid">The process id.</param>
113         /// <returns>The shared clean memory size <paramref name="pid"/> is using (KiB).</returns>
114         /// <exception cref="ArgumentException">Thrown when the <paramref name="pid"/> is invalid.</exception>
115         public int GetSharedClean(int pid)
116         {
117             for (int i = 0; i < Count; i++)
118                 if (pid == Pids[i])
119                     return Usages[i].SharedClean;
120
121             Log.Error(InformationErrorFactory.LogTag, "Invalid pid");
122             InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
123             return 0;
124         }
125
126         /// <summary>
127         /// Gets the size modified and mapped by other processes of a process.
128         /// </summary>
129         /// <since_tizen> 4 </since_tizen>
130         /// <param name="pid">The process id.</param>
131         /// <returns>The shared dirty memory size <paramref name="pid"/> is using (KiB).</returns>
132         /// <exception cref="ArgumentException">Thrown when the <paramref name="pid"/> is invalid.</exception>
133         public int GetSharedDirty(int pid)
134         {
135             for (int i = 0; i < Count; i++)
136                 if (pid == Pids[i])
137                     return Usages[i].SharedDirty;
138
139             Log.Error(InformationErrorFactory.LogTag, "Invalid pid");
140             InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
141             return 0;
142         }
143
144         /// <summary>
145         /// Gets the size not modified and available only to that process of a process.
146         /// </summary>
147         /// <since_tizen> 4 </since_tizen>
148         /// <param name="pid">The process id.</param>
149         /// <returns>The private clean memory size <paramref name="pid"/> is using (KiB).</returns>
150         /// <exception cref="ArgumentException">Thrown when the <paramref name="pid"/> is invalid.</exception>
151         public int GetPrivateClean(int pid)
152         {
153             for (int i = 0; i < Count; i++)
154                 if (pid == Pids[i])
155                     return Usages[i].PrivateClean;
156
157             Log.Error(InformationErrorFactory.LogTag, "Invalid pid");
158             InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
159             return 0;
160         }
161
162         /// <summary>
163         /// Gets the size modified and available only to that process of a process.
164         /// </summary>
165         /// <since_tizen> 4 </since_tizen>
166         /// <param name="pid">The process id.</param>
167         /// <returns>The private dirty memory size <paramref name="pid"/> is using (KiB).</returns>
168         /// <exception cref="ArgumentException">Thrown when the <paramref name="pid"/> is invalid.</exception>
169         public int GetPrivateDirty(int pid)
170         {
171             for (int i = 0; i < Count; i++)
172                 if (pid == Pids[i])
173                     return Usages[i].PrivateDirty;
174
175             Log.Error(InformationErrorFactory.LogTag, "Invalid pid");
176             InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
177             return 0;
178         }
179
180         /// <summary>
181         /// Update the process memory information to the latest.
182         /// </summary>
183         /// <since_tizen> 4 </since_tizen>
184         /// <param name="pid">List of unique process ids.</param>
185         /// <privilege>http://tizen.org/privilege/systemmonitor</privilege>
186         /// <exception cref="ArgumentException">Thrown when the <paramref name="pid"/> is empty.</exception>
187         /// <exception cref="IOException">Thrown when an I/O error occurs while reading from the system or requesting to the resource management daemon.</exception>
188         /// <exception cref="OutOfMemoryException">Thrown when the memory is not enough to allocate.</exception>
189         /// <exception cref="UnauthorizedAccessException">Thrown when the caller does not have privilege to use this method.</exception>
190         public void Update(IEnumerable<int> pid)
191         {
192             InformationError ret;
193
194             Pids = pid.ToArray<int>();
195             IntPtr ptr = new IntPtr();
196             Count = Pids.Count<int>();
197
198             ret = Interop.RuntimeInfo.GetProcessMemoryInfo(Pids, Count, ref ptr);
199             if (ret != InformationError.None)
200             {
201                 Log.Error(InformationErrorFactory.LogTag, "Interop failed to get Process cpu usage");
202                 InformationErrorFactory.ThrowException(ret);
203             }
204
205             Usages = new Interop.RuntimeInfo.ProcessMemoryInfo[Count];
206             for (int i = 0; i < Count; i++)
207             {
208                 Usages[i] = Marshal.PtrToStructure<Interop.RuntimeInfo.ProcessMemoryInfo>(ptr);
209                 ptr += Marshal.SizeOf<Interop.RuntimeInfo.ProcessCpuUsage>();
210             }
211         }
212     }
213 }