From: Kichan Kwon Date: Tue, 22 Aug 2017 08:19:22 +0000 (+0900) Subject: [Information] Separate usage functions from key-value functions X-Git-Tag: preview1-00199~7^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=9a248ebbf4740f865e4e6cae9c3ac5509fc0100e;p=platform%2Fcore%2Fcsapi%2Ftizenfx.git [Information] Separate usage functions from key-value functions - Integrate data structure with function - Each class read data during construction - You can also update with latest by calling update function Change-Id: I4c4237091315451e2eec0543cae50a931f43bbd0 Signed-off-by: Kichan Kwon --- diff --git a/src/Tizen.System.Information/RuntimeInfo/CpuUsage.cs b/src/Tizen.System.Information/RuntimeInfo/CpuUsage.cs deleted file mode 100755 index 35ceb90..0000000 --- a/src/Tizen.System.Information/RuntimeInfo/CpuUsage.cs +++ /dev/null @@ -1,77 +0,0 @@ -/* -* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved -* -* Licensed under the Apache License, Version 2.0 (the License); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an AS IS BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.ComponentModel; - -namespace Tizen.System -{ - /// - /// The structure for CPU usage. - /// - public class CpuUsage - { - [EditorBrowsable(EditorBrowsableState.Never)] - internal CpuUsage(Interop.RuntimeInfo.CpuUsage usage) - { - IoWait = usage.IoWait; - Nice = usage.IoWait; - System = usage.System; - User = usage.User; - } - /// - /// Running time of un-niced user processes (Percent). - /// - public double User { get; internal set; } - /// - /// Running time of kernel processes (Percent). - /// - public double System { get; internal set; } - /// - /// Running time of niced user processes (Percent). - /// - public double Nice { get; internal set; } - /// - /// Time waiting for I/O completion (Percent). - /// - public double IoWait { get; internal set; } - } - - /// - /// The structure for CPU usage per process. - /// - public class ProcessCpuUsage - { - [EditorBrowsable(EditorBrowsableState.Never)] - internal ProcessCpuUsage(Interop.RuntimeInfo.ProcessCpuUsage usage) - { - UTime = usage.UTime; - STime = usage.STime; - } - /// - /// The amount of time this process has been scheduled in user mode (clock ticks). - /// - public uint UTime { get; internal set; } - /// - /// The amount of time this process has been scheduled in kernel mode (clock ticks). - /// - public uint STime { get; internal set; } - } -} diff --git a/src/Tizen.System.Information/RuntimeInfo/MemoryInformation.cs b/src/Tizen.System.Information/RuntimeInfo/MemoryInformation.cs deleted file mode 100755 index fa38d06..0000000 --- a/src/Tizen.System.Information/RuntimeInfo/MemoryInformation.cs +++ /dev/null @@ -1,107 +0,0 @@ -/* -* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved -* -* Licensed under the Apache License, Version 2.0 (the License); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an AS IS BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.ComponentModel; - -namespace Tizen.System -{ - /// - /// Memory information. - /// - public class SystemMemoryInformation - { - [EditorBrowsable(EditorBrowsableState.Never)] - internal SystemMemoryInformation(Interop.RuntimeInfo.MemoryInfo info) - { - Total = info.Total; - Used = info.Used; - Cache = info.Cache; - Free = info.Free; - Swap = info.Swap; - } - /// - /// Total memory (KiB). - /// - public int Total { get; internal set; } - /// - /// Used memory (KiB). - /// - public int Used { get; internal set; } - /// - /// Free memory (KiB). - /// - public int Free { get; internal set; } - /// - /// Cache memory (KiB). - /// - public int Cache { get; internal set; } - /// - /// Swap memory (KiB). - /// - public int Swap { get; internal set; } - } - - /// - /// Memory information per process. - /// - public class ProcessMemoryInformation - { - [EditorBrowsable(EditorBrowsableState.Never)] - internal ProcessMemoryInformation(Interop.RuntimeInfo.ProcessMemoryInfo info) - { - PrivateClean = info.PrivateClean; - PrivateDirty = info.PrivateDirty; - Pss = info.Pss; - Rss = info.Rss; - SharedClean = info.SharedClean; - SharedDirty = info.SharedDirty; - Vsz = info.Vsz; - } - /// - /// Virtual memory size (KiB). - /// - public int Vsz { get; internal set; } - /// - /// Resident set size (KiB). - /// - public int Rss { get; internal set; } - /// - /// Proportional set size (KiB). - /// - public int Pss { get; internal set; } - /// - /// Not modified and mapped by other processes (KiB). - /// - public int SharedClean { get; internal set; } - /// - /// Modified and mapped by other processes (KiB). - /// - public int SharedDirty { get; internal set; } - /// - /// Not modified and available only to that process (KiB). - /// - public int PrivateClean { get; internal set; } - /// - /// Modified and available only to that process (KiB). - /// - public int PrivateDirty { get; internal set; } - } -} diff --git a/src/Tizen.System.Information/RuntimeInfo/RuntimeInformation.cs b/src/Tizen.System.Information/RuntimeInfo/RuntimeInformation.cs index f5513d2..0910768 100755 --- a/src/Tizen.System.Information/RuntimeInfo/RuntimeInformation.cs +++ b/src/Tizen.System.Information/RuntimeInfo/RuntimeInformation.cs @@ -167,7 +167,7 @@ namespace Tizen.System internal static int is_TV_product = -1; [EditorBrowsable(EditorBrowsableState.Never)] - /// This function is for a TV product. It will be removed. + /* This function is for a TV product. It will be removed. */ internal static RuntimeInformationKey ConvertKeyIfTvProduct(RuntimeInformationKey key) { bool is_key_existed = false; @@ -273,188 +273,6 @@ namespace Tizen.System } /// - /// Gets the system memory information. - /// - /// 3 - /// The system memory information structure. - /// Thrown when I/O error occurs while reading from the system. - public static SystemMemoryInformation GetSystemMemoryInformation() - { - Interop.RuntimeInfo.MemoryInfo info = new Interop.RuntimeInfo.MemoryInfo(); - int ret = Interop.RuntimeInfo.GetSystemMemoryInfo(out info); - if (ret != (int)RuntimeInfoError.None) - { - Log.Error(RuntimeInfoErrorFactory.LogTag, "Interop failed to get System memory information"); - RuntimeInfoErrorFactory.ThrowException(ret); - } - - return new SystemMemoryInformation(info); - } - - /// - /// Gets the memory information per process. - /// - /// 3 - /// List of unique process ids. - /// List of memory information per process. - /// http://tizen.org/privilege/systemmonitor - /// Thrown when the is empty. - /// Thrown when an I/O error occurs while reading from the system or requesting to the resource management daemon. - /// Thrown when the memory is not enough to allocate. - /// Thrown when the caller does not have privilege to use this method. - public static IDictionary GetProcessMemoryInformation(IEnumerable pid) - { - int[] processArray = pid.ToArray(); - Interop.RuntimeInfo.ProcessMemoryInfo[] processMemoryArray = new Interop.RuntimeInfo.ProcessMemoryInfo[pid.Count()]; - Dictionary map = new Dictionary(); - IntPtr ptr = new IntPtr(); - int ret = Interop.RuntimeInfo.GetProcessMemoryInfo(processArray, pid.Count(), ref ptr); - if (ret != (int)RuntimeInfoError.None) - { - Log.Error(RuntimeInfoErrorFactory.LogTag, "Interop failed to get Process memory information"); - RuntimeInfoErrorFactory.ThrowException(ret); - } - - for (int i = 0; i < pid.Count(); i++) - { - processMemoryArray[i] = Marshal.PtrToStructure(ptr); - ptr += Marshal.SizeOf(); - } - - int idx = 0; - foreach (Interop.RuntimeInfo.ProcessMemoryInfo cur in processMemoryArray) - { - ProcessMemoryInformation processMemory = new ProcessMemoryInformation(cur); - map.Add(processArray[idx], processMemory); - idx++; - } - - return map; - } - - /// - /// Gets the system CPU usage time. - /// - /// 3 - /// The system CPU usage time structure. - /// Thrown when an I/O error occurs while reading from the system. - public static CpuUsage GetCpuUsage() - { - Interop.RuntimeInfo.CpuUsage usage = new Interop.RuntimeInfo.CpuUsage(); - int ret = Interop.RuntimeInfo.GetCpuUsage(out usage); - if (ret != (int)RuntimeInfoError.None) - { - Log.Error(RuntimeInfoErrorFactory.LogTag, "Interop failed to get cpu usage"); - RuntimeInfoErrorFactory.ThrowException(ret); - } - return new CpuUsage(usage); - } - - /// - /// Gets the CPU usage time per process. - /// - /// 3 - /// List of unique process ids. - /// List of CPU usage information per process. - /// http://tizen.org/privilege/systemmonitor - /// Thrown when the is empty. - /// Thrown when an I/O error occurs while reading from the system or requesting to the resource management daemon. - /// Thrown when the memory is not enough to allocate. - /// Thrown when the caller does not have privilege to use this method. - public static IDictionary GetProcessCpuUsage(IEnumerable pid) - { - int[] processArray = pid.ToArray(); - Interop.RuntimeInfo.ProcessCpuUsage[] processCpuUsageArray = new Interop.RuntimeInfo.ProcessCpuUsage[pid.Count()]; - Dictionary map = new Dictionary(); - IntPtr ptr = new IntPtr(); - int ret = Interop.RuntimeInfo.GetProcessCpuUsage(processArray, pid.Count(), ref ptr); - if (ret != (int)RuntimeInfoError.None) - { - Log.Error(RuntimeInfoErrorFactory.LogTag, "Interop failed to get Process cpu usage"); - RuntimeInfoErrorFactory.ThrowException(ret); - } - - for (int i = 0; i < pid.Count(); i++) - { - processCpuUsageArray[i] = Marshal.PtrToStructure(ptr); - ptr += Marshal.SizeOf(); - } - - int idx = 0; - foreach (Interop.RuntimeInfo.ProcessCpuUsage cur in processCpuUsageArray) - { - ProcessCpuUsage processUsage = new ProcessCpuUsage(cur); - map.Add(processArray[idx], processUsage); - idx++; - } - - return map; - } - - /// - /// Gets the number of processors. - /// - /// The number of processors. - /// Thrown when an I/O error occurs while reading from system. - public static int ProcessorCount - { - get - { - int count; - int ret = Interop.RuntimeInfo.GetProcessorCount(out count); - if (ret != (int)RuntimeInfoError.None) - { - Log.Error(RuntimeInfoErrorFactory.LogTag, "Interop failed to get Processor count"); - RuntimeInfoErrorFactory.ThrowException(ret); - } - - return count; - } - } - - /// - /// Gets the current frequency of the processor. - /// - /// 3 - /// The index (from 0) of the CPU core that you want to know the frequency of. - /// The current frequency(MHz) of processor. - /// Thrown when the is invalid. - /// Thrown when an I/O error occurs while reading from system. - /// Thrown when this system does not store the current CPU frequency. - public static int GetProcessorCurrentFrequency(int coreId) - { - int frequency; - int ret = Interop.RuntimeInfo.GetProcessorCurrentFrequency(coreId, out frequency); - if (ret != (int)RuntimeInfoError.None) - { - Log.Error(RuntimeInfoErrorFactory.LogTag, "Interop failed to get Processor current frequency"); - RuntimeInfoErrorFactory.ThrowException(ret); - } - return frequency; - } - - /// - /// Gets the max frequency of the processor. - /// - /// 3 - /// The index (from 0) of CPU core that you want to know the frequency of. - /// The max frequency(MHz) of processor. - /// Thrown when the is invalid. - /// Thrown when an I/O error occurs while reading from system. - /// Thrown when this system does not store the max CPU frequency. - public static int GetProcessorMaxFrequency(int coreId) - { - int frequency; - int ret = Interop.RuntimeInfo.GetProcessorMaxFrequency(coreId, out frequency); - if (ret != (int)RuntimeInfoError.None) - { - Log.Error(RuntimeInfoErrorFactory.LogTag, "Interop failed to get Processor max frequency"); - RuntimeInfoErrorFactory.ThrowException(ret); - } - return frequency; - } - - /// /// (event) BluetoothEnabled is raised when the system preference for Bluetooth is changed. /// public static event EventHandler BluetoothEnabled diff --git a/src/Tizen.System.Information/Tizen.System.Information.csproj b/src/Tizen.System.Information/Tizen.System.Information.csproj old mode 100644 new mode 100755 diff --git a/src/Tizen.System.Information/Usage/ProcessCpuUsage.cs b/src/Tizen.System.Information/Usage/ProcessCpuUsage.cs new file mode 100755 index 0000000..b664c1b --- /dev/null +++ b/src/Tizen.System.Information/Usage/ProcessCpuUsage.cs @@ -0,0 +1,123 @@ +/* +* Copyright (c) 2016 - 2017 Samsung Electronics Co., Ltd All Rights Reserved +* +* Licensed under the Apache License, Version 2.0 (the License); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an AS IS BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; + +namespace Tizen.System +{ + /// + /// The class for CPU usage per process. + /// + public class ProcessCpuUsage + { + private int[] Pids; + private Interop.RuntimeInfo.ProcessCpuUsage[] Usages; + + /// + /// The constructor of ProcessCpuUsage class. + /// + /// 4 + /// List of unique process ids. + /// http://tizen.org/privilege/systemmonitor + /// Thrown when the is empty. + /// Thrown when an I/O error occurs while reading from the system or requesting to the resource management daemon. + /// Thrown when the memory is not enough to allocate. + /// Thrown when the caller does not have privilege to use this method. + public ProcessCpuUsage(IEnumerable pid) + { + Update(pid); + } + + /// + /// The number of usage entries. + /// + /// 4 + public int Count { get; internal set; } + + /// + /// Gets the amount of time this process has been scheduled in user mode. + /// + /// 4 + /// The process id. + /// The amount of time has been scheduled in user mode (clock ticks). + /// Thrown when the is invalid. + public uint GetUTime(int pid) + { + for (int i = 0; i < Count; i++) + if (pid == Pids[i]) + return Usages[i].UTime; + + Log.Error(RuntimeInfoErrorFactory.LogTag, "Invalid pid"); + RuntimeInfoErrorFactory.ThrowException((int)RuntimeInfoError.InvalidParameter); + return 0; + } + + /// + /// Gets the amount of time this process has been scheduled in kernel mode. + /// + /// 4 + /// The process id. + /// The amount of time has been scheduled in kernel mode (clock ticks). + /// Thrown when the is invalid. + public uint GetSTime(int pid) + { + for (int i = 0; i < Count; i++) + if (pid == Pids[i]) + return Usages[i].STime; + + Log.Error(RuntimeInfoErrorFactory.LogTag, "Invalid pid"); + RuntimeInfoErrorFactory.ThrowException((int)RuntimeInfoError.InvalidParameter); + return 0; + } + + /// + /// Update the process CPU usage to the latest. + /// + /// 4 + /// List of unique process ids. + /// http://tizen.org/privilege/systemmonitor + /// Thrown when the is empty. + /// Thrown when an I/O error occurs while reading from the system or requesting to the resource management daemon. + /// Thrown when the memory is not enough to allocate. + /// Thrown when the caller does not have privilege to use this method. + public void Update(IEnumerable pid) + { + int ret; + + Pids = pid.ToArray(); + IntPtr ptr = new IntPtr(); + Count = Pids.Count(); + + ret = Interop.RuntimeInfo.GetProcessCpuUsage(Pids, Count, ref ptr); + if (ret != (int)RuntimeInfoError.None) + { + Log.Error(RuntimeInfoErrorFactory.LogTag, "Interop failed to get Process cpu usage"); + RuntimeInfoErrorFactory.ThrowException(ret); + } + + Usages = new Interop.RuntimeInfo.ProcessCpuUsage[Count]; + for (int i = 0; i < Count; i++) + { + Usages[i] = Marshal.PtrToStructure(ptr); + ptr += Marshal.SizeOf(); + } + } + } +} diff --git a/src/Tizen.System.Information/Usage/ProcessMemoryUsage.cs b/src/Tizen.System.Information/Usage/ProcessMemoryUsage.cs new file mode 100755 index 0000000..b1bd7d6 --- /dev/null +++ b/src/Tizen.System.Information/Usage/ProcessMemoryUsage.cs @@ -0,0 +1,213 @@ +/* +* Copyright (c) 2016 - 2017 Samsung Electronics Co., Ltd All Rights Reserved +* +* Licensed under the Apache License, Version 2.0 (the License); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an AS IS BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; + +namespace Tizen.System +{ + /// + /// The class for memory information per process. + /// + public class ProcessMemoryUsage + { + private int[] Pids; + private Interop.RuntimeInfo.ProcessMemoryInfo[] Usages; + + /// + /// The constructor of ProcessMemoryInformation class. + /// + /// 4 + /// List of unique process ids. + /// http://tizen.org/privilege/systemmonitor + /// Thrown when the is empty. + /// Thrown when an I/O error occurs while reading from the system or requesting to the resource management daemon. + /// Thrown when the memory is not enough to allocate. + /// Thrown when the caller does not have privilege to use this method. + public ProcessMemoryUsage(IEnumerable pid) + { + Update(pid); + } + + /// + /// The number of usage entries. + /// + /// 4 + public int Count { get; internal set; } + + /// + /// Gets the virtual memory size of a process. + /// + /// 4 + /// The process id. + /// The virtual memory size is using (KiB). + /// Thrown when the is invalid. + public int GetVsz(int pid) + { + for (int i = 0; i < Count; i++) + if (pid == Pids[i]) + return Usages[i].Vsz; + + Log.Error(RuntimeInfoErrorFactory.LogTag, "Invalid pid"); + RuntimeInfoErrorFactory.ThrowException((int)RuntimeInfoError.InvalidParameter); + return 0; + } + + /// + /// Gets the resident set size of a process. + /// + /// 4 + /// The process id. + /// The resident set size is using (KiB). + /// Thrown when the is invalid. + public int GetRss(int pid) + { + for (int i = 0; i < Count; i++) + if (pid == Pids[i]) + return Usages[i].Rss; + + Log.Error(RuntimeInfoErrorFactory.LogTag, "Invalid pid"); + RuntimeInfoErrorFactory.ThrowException((int)RuntimeInfoError.InvalidParameter); + return 0; + } + + /// + /// Gets the proportional set size of a process. + /// + /// 4 + /// The process id. + /// The proportional set size is using (KiB). + /// Thrown when the is invalid. + public int GetPss(int pid) + { + for (int i = 0; i < Count; i++) + if (pid == Pids[i]) + return Usages[i].Pss; + + Log.Error(RuntimeInfoErrorFactory.LogTag, "Invalid pid"); + RuntimeInfoErrorFactory.ThrowException((int)RuntimeInfoError.InvalidParameter); + return 0; + } + + /// + /// Gets the size not modified and mapped by other processes of a process. + /// + /// 4 + /// The process id. + /// The shared clean memory size is using (KiB). + /// Thrown when the is invalid. + public int GetSharedClean(int pid) + { + for (int i = 0; i < Count; i++) + if (pid == Pids[i]) + return Usages[i].SharedClean; + + Log.Error(RuntimeInfoErrorFactory.LogTag, "Invalid pid"); + RuntimeInfoErrorFactory.ThrowException((int)RuntimeInfoError.InvalidParameter); + return 0; + } + + /// + /// Gets the size modified and mapped by other processes of a process. + /// + /// 4 + /// The process id. + /// The shared dirty memory size is using (KiB). + /// Thrown when the is invalid. + public int GetSharedDirty(int pid) + { + for (int i = 0; i < Count; i++) + if (pid == Pids[i]) + return Usages[i].SharedDirty; + + Log.Error(RuntimeInfoErrorFactory.LogTag, "Invalid pid"); + RuntimeInfoErrorFactory.ThrowException((int)RuntimeInfoError.InvalidParameter); + return 0; + } + + /// + /// Gets the size not modified and available only to that process of a process. + /// + /// 4 + /// The process id. + /// The private clean memory size is using (KiB). + /// Thrown when the is invalid. + public int GetPrivateClean(int pid) + { + for (int i = 0; i < Count; i++) + if (pid == Pids[i]) + return Usages[i].PrivateClean; + + Log.Error(RuntimeInfoErrorFactory.LogTag, "Invalid pid"); + RuntimeInfoErrorFactory.ThrowException((int)RuntimeInfoError.InvalidParameter); + return 0; + } + + /// + /// Gets the size modified and available only to that process of a process. + /// + /// 4 + /// The process id. + /// The private dirty memory size is using (KiB). + /// Thrown when the is invalid. + public int GetPrivateDirty(int pid) + { + for (int i = 0; i < Count; i++) + if (pid == Pids[i]) + return Usages[i].PrivateDirty; + + Log.Error(RuntimeInfoErrorFactory.LogTag, "Invalid pid"); + RuntimeInfoErrorFactory.ThrowException((int)RuntimeInfoError.InvalidParameter); + return 0; + } + + /// + /// Update the process memory information to the latest. + /// + /// 4 + /// List of unique process ids. + /// http://tizen.org/privilege/systemmonitor + /// Thrown when the is empty. + /// Thrown when an I/O error occurs while reading from the system or requesting to the resource management daemon. + /// Thrown when the memory is not enough to allocate. + /// Thrown when the caller does not have privilege to use this method. + public void Update(IEnumerable pid) + { + int ret; + + Pids = pid.ToArray(); + IntPtr ptr = new IntPtr(); + Count = Pids.Count(); + + ret = Interop.RuntimeInfo.GetProcessMemoryInfo(Pids, Count, ref ptr); + if (ret != (int)RuntimeInfoError.None) + { + Log.Error(RuntimeInfoErrorFactory.LogTag, "Interop failed to get Process cpu usage"); + RuntimeInfoErrorFactory.ThrowException(ret); + } + + Usages = new Interop.RuntimeInfo.ProcessMemoryInfo[Count]; + for (int i = 0; i < Count; i++) + { + Usages[i] = Marshal.PtrToStructure(ptr); + ptr += Marshal.SizeOf(); + } + } + } +} diff --git a/src/Tizen.System.Information/Usage/SystemCpuUsage.cs b/src/Tizen.System.Information/Usage/SystemCpuUsage.cs new file mode 100755 index 0000000..32d007a --- /dev/null +++ b/src/Tizen.System.Information/Usage/SystemCpuUsage.cs @@ -0,0 +1,180 @@ +/* +* Copyright (c) 2016 - 2017 Samsung Electronics Co., Ltd All Rights Reserved +* +* Licensed under the Apache License, Version 2.0 (the License); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an AS IS BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using System.IO; + +namespace Tizen.System +{ + /// + /// The class for system CPU usage. + /// + public class SystemCpuUsage + { + internal Interop.RuntimeInfo.CpuUsage Usage; + internal int[] CurrentFrequencies; + internal int[] MaxFrequencies; + + /// + /// The constructor of SystemCpuUsage class. + /// + /// 4 + /// Thrown when an I/O error occurs while reading from the system. + /// Thrown when this system does not store the current CPU frequency. + public SystemCpuUsage() + { + Update(); + } + + /// + /// Running time of un-niced user processes (Percent). + /// + /// 4 + public double User + { + get + { + return Usage.User; + } + } + + /// + /// Running time of kernel processes (Percent). + /// + /// 4 + public double System + { + get + { + return Usage.System; + } + } + + /// + /// Running time of niced user processes (Percent). + /// + /// 4 + public double Nice + { + get + { + return Usage.Nice; + } + } + + /// + /// Time waiting for I/O completion (Percent). + /// + /// 4 + public double IoWait + { + get + { + return Usage.IoWait; + } + } + + /// + /// The number of processors. + /// + /// 4 + public int ProcessorCount { get; internal set; } + + /// + /// Gets the current frequency of the processor. + /// + /// 4 + /// The index (from 0) of the CPU core that you want to know the frequency of. + /// The current frequency(MHz) of processor. + /// Thrown when the is invalid. + public int GetCurrentFrequency(int coreId) + { + if(coreId < 0 || coreId >= ProcessorCount) + { + Log.Error(RuntimeInfoErrorFactory.LogTag, "Invalid core ID " + coreId); + RuntimeInfoErrorFactory.ThrowException((int)RuntimeInfoError.InvalidParameter); + } + + return CurrentFrequencies[coreId]; + } + + /// + /// Gets the max frequency of the processor. + /// + /// 4 + /// The index (from 0) of CPU core that you want to know the frequency of. + /// The max frequency(MHz) of processor. + /// Thrown when the is invalid. + public int GetMaxFrequency(int coreId) + { + if (coreId < 0 || coreId >= ProcessorCount) + { + Log.Error(RuntimeInfoErrorFactory.LogTag, "Invalid core ID " + coreId); + RuntimeInfoErrorFactory.ThrowException((int)RuntimeInfoError.InvalidParameter); + } + + return MaxFrequencies[coreId]; + } + + /// + /// Update the system CPU usage to the latest. + /// + /// 4 + /// Thrown when an I/O error occurs while reading from the system. + /// Thrown when this system does not store the current CPU frequency. + public void Update() + { + int ret; + int count; + + ret = Interop.RuntimeInfo.GetCpuUsage(out Usage); + if (ret != (int)RuntimeInfoError.None) + { + Log.Error(RuntimeInfoErrorFactory.LogTag, "Interop failed to get cpu usage"); + RuntimeInfoErrorFactory.ThrowException(ret); + } + + ret = Interop.RuntimeInfo.GetProcessorCount(out count); + if (ret != (int)RuntimeInfoError.None) + { + Log.Error(RuntimeInfoErrorFactory.LogTag, "Interop failed to get Processor count"); + RuntimeInfoErrorFactory.ThrowException(ret); + return; + } + + ProcessorCount = count; + CurrentFrequencies = new int[ProcessorCount]; + MaxFrequencies = new int[ProcessorCount]; + + for (int coreId = 0; coreId < ProcessorCount; coreId++) + { + ret = Interop.RuntimeInfo.GetProcessorCurrentFrequency(coreId, out CurrentFrequencies[coreId]); + if (ret != (int)RuntimeInfoError.None) + { + Log.Error(RuntimeInfoErrorFactory.LogTag, "Interop failed to get the current frequency of processor " + coreId); + RuntimeInfoErrorFactory.ThrowException(ret); + } + + ret = Interop.RuntimeInfo.GetProcessorMaxFrequency(coreId, out MaxFrequencies[coreId]); + if (ret != (int)RuntimeInfoError.None) + { + Log.Error(RuntimeInfoErrorFactory.LogTag, "Interop failed to get the max frequency of processor " + coreId); + RuntimeInfoErrorFactory.ThrowException(ret); + } + } + } + } +} diff --git a/src/Tizen.System.Information/Usage/SystemMemoryUsage.cs b/src/Tizen.System.Information/Usage/SystemMemoryUsage.cs new file mode 100755 index 0000000..4c6f718 --- /dev/null +++ b/src/Tizen.System.Information/Usage/SystemMemoryUsage.cs @@ -0,0 +1,113 @@ +/* +* Copyright (c) 2016 - 2017 Samsung Electronics Co., Ltd All Rights Reserved +* +* Licensed under the Apache License, Version 2.0 (the License); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an AS IS BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System.IO; + +namespace Tizen.System +{ + /// + /// The class for system memory information. + /// + public class SystemMemoryUsage + { + internal Interop.RuntimeInfo.MemoryInfo Info; + + /// + /// The constructor of MemoryInformation class. + /// + /// 4 + /// Thrown when an I/O error occurs while reading from the system. + public SystemMemoryUsage() + { + Update(); + } + + /// + /// Total memory (KiB). + /// + /// 4 + public int Total + { + get + { + return Info.Total; + } + } + + /// + /// Used memory (KiB). + /// + /// 4 + public int Used + { + get + { + return Info.Used; + } + } + + /// + /// Free memory (KiB). + /// + /// 4 + public int Free + { + get + { + return Info.Free; + } + } + + /// + /// Cache memory (KiB). + /// + /// 4 + public int Cache + { + get + { + return Info.Cache; + } + } + + /// + /// Swap memory (KiB). + /// + /// 4 + public int Swap + { + get + { + return Info.Swap; + } + } + + /// + /// Update the system memory information to the latest. + /// + /// 4 + /// Thrown when I/O error occurs while reading from the system. + public void Update() + { + int ret = Interop.RuntimeInfo.GetSystemMemoryInfo(out Info); + if (ret != (int)RuntimeInfoError.None) + { + Log.Error(RuntimeInfoErrorFactory.LogTag, "Interop failed to get System memory information"); + RuntimeInfoErrorFactory.ThrowException(ret); + } + } + } +}