2 * Copyright (c) 2016 - 2017 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Collections.Generic;
21 using System.Runtime.InteropServices;
23 namespace Tizen.System
26 /// The class for CPU usage per process.
28 public class ProcessCpuUsage
31 private Interop.RuntimeInfo.ProcessCpuUsage[] Usages;
34 /// The constructor of ProcessCpuUsage class.
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 ProcessCpuUsage(IEnumerable<int> pid)
49 /// The number of usage entries.
51 /// <since_tizen> 4 </since_tizen>
52 public int Count { get; internal set; }
55 /// Gets the amount of time this process has been scheduled in user mode.
57 /// <since_tizen> 4 </since_tizen>
58 /// <param name="pid">The process id.</param>
59 /// <returns>The amount of time <paramref name="pid"/> has been scheduled in user mode (clock ticks).</returns>
60 /// <exception cref="ArgumentException">Thrown when the <paramref name="pid"/> is invalid.</exception>
61 public uint GetUTime(int pid)
63 for (int i = 0; i < Count; i++)
65 return Usages[i].UTime;
67 Log.Error(InformationErrorFactory.LogTag, "Invalid pid");
68 InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
73 /// Gets the amount of time this process has been scheduled in kernel mode.
75 /// <since_tizen> 4 </since_tizen>
76 /// <param name="pid">The process id.</param>
77 /// <returns>The amount of time <paramref name="pid"/> has been scheduled in kernel mode (clock ticks).</returns>
78 /// <exception cref="ArgumentException">Thrown when the <paramref name="pid"/> is invalid.</exception>
79 public uint GetSTime(int pid)
81 for (int i = 0; i < Count; i++)
83 return Usages[i].STime;
85 Log.Error(InformationErrorFactory.LogTag, "Invalid pid");
86 InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
91 /// Update the process CPU usage to the latest.
93 /// <since_tizen> 4 </since_tizen>
94 /// <param name="pid">List of unique process ids.</param>
95 /// <privilege>http://tizen.org/privilege/systemmonitor</privilege>
96 /// <exception cref="ArgumentException">Thrown when the <paramref name="pid"/> is empty.</exception>
97 /// <exception cref="IOException">Thrown when an I/O error occurs while reading from the system or requesting to the resource management daemon.</exception>
98 /// <exception cref="OutOfMemoryException">Thrown when the memory is not enough to allocate.</exception>
99 /// <exception cref="UnauthorizedAccessException">Thrown when the caller does not have privilege to use this method.</exception>
100 public void Update(IEnumerable<int> pid)
102 InformationError ret;
104 Pids = pid.ToArray<int>();
105 IntPtr ptr = new IntPtr();
106 Count = Pids.Count<int>();
108 ret = Interop.RuntimeInfo.GetProcessCpuUsage(Pids, Count, ref ptr);
109 if (ret != InformationError.None)
111 Log.Error(InformationErrorFactory.LogTag, "Interop failed to get Process cpu usage");
112 InformationErrorFactory.ThrowException(ret);
115 Usages = new Interop.RuntimeInfo.ProcessCpuUsage[Count];
116 for (int i = 0; i < Count; i++)
118 Usages[i] = Marshal.PtrToStructure<Interop.RuntimeInfo.ProcessCpuUsage>(ptr);
119 ptr += Marshal.SizeOf<Interop.RuntimeInfo.ProcessCpuUsage>();