Release 4.0.0-preview1-00201
[platform/core/csapi/tizenfx.git] / src / Tizen.System.Information / Usage / SystemCpuUsage.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.IO;
19
20 namespace Tizen.System
21 {
22     /// <summary>
23     /// The class for system CPU usage.
24     /// </summary>
25     public class SystemCpuUsage
26     {
27         private Interop.RuntimeInfo.CpuUsage Usage;
28         private int[] CurrentFrequencies;
29         private int[] MaxFrequencies;
30
31         /// <summary>
32         /// The constructor of SystemCpuUsage class.
33         /// </summary>
34         /// <since_tizen> 4 </since_tizen>
35         /// <exception cref="IOException">Thrown when an I/O error occurs while reading from the system.</exception>
36         /// <exception cref="NotSupportedException">Thrown when this system does not store the current CPU frequency.</exception>
37         public SystemCpuUsage()
38         {
39             Update();
40         }
41
42         /// <summary>
43         /// Running time of un-niced user processes (Percent).
44         /// </summary>
45         /// <since_tizen> 4 </since_tizen>
46         public double User
47         {
48             get
49             {
50                 return Usage.User;
51             }
52         }
53
54         /// <summary>
55         /// Running time of kernel processes (Percent).
56         /// </summary>
57         /// <since_tizen> 4 </since_tizen>
58         public double System
59         {
60             get
61             {
62                 return Usage.System;
63             }
64         }
65
66         /// <summary>
67         /// Running time of niced user processes (Percent).
68         /// </summary>
69         /// <since_tizen> 4 </since_tizen>
70         public double Nice
71         {
72             get
73             {
74                 return Usage.Nice;
75             }
76         }
77
78         /// <summary>
79         /// Time waiting for I/O completion (Percent).
80         /// </summary>
81         /// <since_tizen> 4 </since_tizen>
82         public double IoWait
83         {
84             get
85             {
86                 return Usage.IoWait;
87             }
88         }
89
90         /// <summary>
91         /// The number of processors.
92         /// </summary>
93         /// <since_tizen> 4 </since_tizen>
94         public int ProcessorCount { get; internal set; }
95
96         /// <summary>
97         /// Gets the current frequency of the processor.
98         /// </summary>
99         /// <since_tizen> 4 </since_tizen>
100         /// <param name="coreId">The index (from 0) of the CPU core that you want to know the frequency of.</param>
101         /// <returns>The current frequency(MHz) of processor.</returns>
102         /// <exception cref="ArgumentException">Thrown when the <paramref name="coreId"/> is invalid.</exception>
103         public int GetCurrentFrequency(int coreId)
104         {
105             if(coreId < 0 || coreId >= ProcessorCount)
106             {
107                 Log.Error(InformationErrorFactory.LogTag, "Invalid core ID " + coreId);
108                 InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
109             }
110
111             return CurrentFrequencies[coreId];
112         }
113
114         /// <summary>
115         /// Gets the max frequency of the processor.
116         /// </summary>
117         /// <since_tizen> 4 </since_tizen>
118         /// <param name="coreId">The index (from 0) of CPU core that you want to know the frequency of.</param>
119         /// <returns>The max frequency(MHz) of processor.</returns>
120         /// <exception cref="ArgumentException">Thrown when the <paramref name="coreId"/> is invalid.</exception>
121         public int GetMaxFrequency(int coreId)
122         {
123             if (coreId < 0 || coreId >= ProcessorCount)
124             {
125                 Log.Error(InformationErrorFactory.LogTag, "Invalid core ID " + coreId);
126                 InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
127             }
128
129             return MaxFrequencies[coreId];
130         }
131
132         /// <summary>
133         /// Update the system CPU usage to the latest.
134         /// </summary>
135         /// <since_tizen> 4 </since_tizen>
136         /// <exception cref="IOException">Thrown when an I/O error occurs while reading from the system.</exception>
137         /// <exception cref="NotSupportedException">Thrown when this system does not store the current CPU frequency.</exception>
138         public void Update()
139         {
140             InformationError ret;
141             int count;
142
143             ret = Interop.RuntimeInfo.GetCpuUsage(out Usage);
144             if (ret != InformationError.None)
145             {
146                 Log.Error(InformationErrorFactory.LogTag, "Interop failed to get cpu usage");
147                 InformationErrorFactory.ThrowException(ret);
148             }
149
150             ret = Interop.RuntimeInfo.GetProcessorCount(out count);
151             if (ret != InformationError.None)
152             {
153                 Log.Error(InformationErrorFactory.LogTag, "Interop failed to get Processor count");
154                 InformationErrorFactory.ThrowException(ret);
155                 return;
156             }
157
158             ProcessorCount = count;
159             CurrentFrequencies = new int[ProcessorCount];
160             MaxFrequencies = new int[ProcessorCount];
161
162             for (int coreId = 0; coreId < ProcessorCount; coreId++)
163             {
164                 ret = Interop.RuntimeInfo.GetProcessorCurrentFrequency(coreId, out CurrentFrequencies[coreId]);
165                 if (ret != InformationError.None)
166                 {
167                     Log.Error(InformationErrorFactory.LogTag, "Interop failed to get the current frequency of processor " + coreId);
168                     InformationErrorFactory.ThrowException(ret);
169                 }
170
171                 ret = Interop.RuntimeInfo.GetProcessorMaxFrequency(coreId, out MaxFrequencies[coreId]);
172                 if (ret != InformationError.None)
173                 {
174                     Log.Error(InformationErrorFactory.LogTag, "Interop failed to get the max frequency of processor " + coreId);
175                     InformationErrorFactory.ThrowException(ret);
176                 }
177             }
178         }
179     }
180 }