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.
20 namespace Tizen.System
23 /// The class for system CPU usage.
25 public class SystemCpuUsage
27 private Interop.RuntimeInfo.CpuUsage Usage;
28 private int[] CurrentFrequencies;
29 private int[] MaxFrequencies;
32 /// The constructor of SystemCpuUsage class.
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()
43 /// Running time of un-niced user processes (Percent).
45 /// <since_tizen> 4 </since_tizen>
55 /// Running time of kernel processes (Percent).
57 /// <since_tizen> 4 </since_tizen>
67 /// Running time of niced user processes (Percent).
69 /// <since_tizen> 4 </since_tizen>
79 /// Time waiting for I/O completion (Percent).
81 /// <since_tizen> 4 </since_tizen>
91 /// The number of processors.
93 /// <since_tizen> 4 </since_tizen>
94 public int ProcessorCount { get; internal set; }
97 /// Gets the current frequency of the processor.
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)
105 if(coreId < 0 || coreId >= ProcessorCount)
107 Log.Error(InformationErrorFactory.LogTag, "Invalid core ID " + coreId);
108 InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
111 return CurrentFrequencies[coreId];
115 /// Gets the max frequency of the processor.
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)
123 if (coreId < 0 || coreId >= ProcessorCount)
125 Log.Error(InformationErrorFactory.LogTag, "Invalid core ID " + coreId);
126 InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
129 return MaxFrequencies[coreId];
133 /// Update the system CPU usage to the latest.
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>
140 InformationError ret;
143 ret = Interop.RuntimeInfo.GetCpuUsage(out Usage);
144 if (ret != InformationError.None)
146 Log.Error(InformationErrorFactory.LogTag, "Interop failed to get cpu usage");
147 InformationErrorFactory.ThrowException(ret);
150 ret = Interop.RuntimeInfo.GetProcessorCount(out count);
151 if (ret != InformationError.None)
153 Log.Error(InformationErrorFactory.LogTag, "Interop failed to get Processor count");
154 InformationErrorFactory.ThrowException(ret);
158 ProcessorCount = count;
159 CurrentFrequencies = new int[ProcessorCount];
160 MaxFrequencies = new int[ProcessorCount];
162 for (int coreId = 0; coreId < ProcessorCount; coreId++)
164 ret = Interop.RuntimeInfo.GetProcessorCurrentFrequency(coreId, out CurrentFrequencies[coreId]);
165 if (ret != InformationError.None)
167 Log.Error(InformationErrorFactory.LogTag, "Interop failed to get the current frequency of processor " + coreId);
168 InformationErrorFactory.ThrowException(ret);
171 ret = Interop.RuntimeInfo.GetProcessorMaxFrequency(coreId, out MaxFrequencies[coreId]);
172 if (ret != InformationError.None)
174 Log.Error(InformationErrorFactory.LogTag, "Interop failed to get the max frequency of processor " + coreId);
175 InformationErrorFactory.ThrowException(ret);