[MediaController] Add capability method for subtitle, 360 mode (#1048)
[platform/core/csapi/tizenfx.git] / src / Tizen.System.Information / Usage / ProcessCpuUsage.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 CPU usage per process.
27     /// </summary>
28     /// <since_tizen> 3 </since_tizen>
29     public class ProcessCpuUsage
30     {
31         private int[] Pids;
32         private Interop.RuntimeInfo.ProcessCpuUsage[] Usages;
33
34         /// <summary>
35         /// The constructor of ProcessCpuUsage class.
36         /// </summary>
37         /// <since_tizen> 4 </since_tizen>
38         /// <param name="pid">List of unique process ids.</param>
39         /// <privilege>http://tizen.org/privilege/systemmonitor</privilege>
40         /// <exception cref="ArgumentException">Thrown when the <paramref name="pid"/> is empty.</exception>
41         /// <exception cref="IOException">Thrown when an I/O error occurs while reading from the system or requesting to the resource management daemon.</exception>
42         /// <exception cref="OutOfMemoryException">Thrown when the memory is not enough to allocate.</exception>
43         /// <exception cref="UnauthorizedAccessException">Thrown when the caller does not have privilege to use this method.</exception>
44         public ProcessCpuUsage(IEnumerable<int> pid)
45         {
46             Update(pid);
47         }
48
49         /// <summary>
50         /// The number of usage entries.
51         /// </summary>
52         /// <since_tizen> 4 </since_tizen>
53         public int Count { get; internal set; }
54
55         /// <summary>
56         /// Gets the amount of time this process has been scheduled in user mode.
57         /// </summary>
58         /// <since_tizen> 4 </since_tizen>
59         /// <param name="pid">The process id.</param>
60         /// <returns>The amount of time <paramref name="pid"/> has been scheduled in user mode (clock ticks).</returns>
61         /// <exception cref="ArgumentException">Thrown when the <paramref name="pid"/> is invalid.</exception>
62         public uint GetUTime(int pid)
63         {
64             for (int i = 0; i < Count; i++)
65                 if (pid == Pids[i])
66                     return Usages[i].UTime;
67
68             Log.Error(InformationErrorFactory.LogTag, "Invalid pid");
69             InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
70             return 0;
71         }
72
73         /// <summary>
74         /// Gets the amount of time this process has been scheduled in kernel mode.
75         /// </summary>
76         /// <since_tizen> 4 </since_tizen>
77         /// <param name="pid">The process id.</param>
78         /// <returns>The amount of time <paramref name="pid"/> has been scheduled in kernel mode (clock ticks).</returns>
79         /// <exception cref="ArgumentException">Thrown when the <paramref name="pid"/> is invalid.</exception>
80         public uint GetSTime(int pid)
81         {
82             for (int i = 0; i < Count; i++)
83                 if (pid == Pids[i])
84                     return Usages[i].STime;
85
86             Log.Error(InformationErrorFactory.LogTag, "Invalid pid");
87             InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
88             return 0;
89         }
90
91         /// <summary>
92         /// Update the process CPU usage to the latest.
93         /// </summary>
94         /// <since_tizen> 4 </since_tizen>
95         /// <param name="pid">List of unique process ids.</param>
96         /// <privilege>http://tizen.org/privilege/systemmonitor</privilege>
97         /// <exception cref="ArgumentException">Thrown when the <paramref name="pid"/> is empty.</exception>
98         /// <exception cref="IOException">Thrown when an I/O error occurs while reading from the system or requesting to the resource management daemon.</exception>
99         /// <exception cref="OutOfMemoryException">Thrown when the memory is not enough to allocate.</exception>
100         /// <exception cref="UnauthorizedAccessException">Thrown when the caller does not have privilege to use this method.</exception>
101         public void Update(IEnumerable<int> pid)
102         {
103             InformationError ret;
104
105             Pids = pid.ToArray<int>();
106             IntPtr ptr = new IntPtr();
107             Count = Pids.Count<int>();
108
109             ret = Interop.RuntimeInfo.GetProcessCpuUsage(Pids, Count, ref ptr);
110             if (ret != InformationError.None)
111             {
112                 Log.Error(InformationErrorFactory.LogTag, "Interop failed to get Process cpu usage");
113                 InformationErrorFactory.ThrowException(ret);
114             }
115
116             Usages = new Interop.RuntimeInfo.ProcessCpuUsage[Count];
117             for (int i = 0; i < Count; i++)
118             {
119                 Usages[i] = Marshal.PtrToStructure<Interop.RuntimeInfo.ProcessCpuUsage>(ptr);
120                 ptr += Marshal.SizeOf<Interop.RuntimeInfo.ProcessCpuUsage>();
121             }
122         }
123     }
124 }