runtime-info : Add cpu information APIs 46/58846/8
authorKichan Kwon <k_c.kwon@samsung.com>
Wed, 3 Feb 2016 10:45:52 +0000 (19:45 +0900)
committerKichan Kwon <k_c.kwon@samsung.com>
Thu, 25 Feb 2016 01:24:55 +0000 (10:24 +0900)
 - runtime_info_get_processor_count

 - runtime_info_get_processor_current_frequency

 - runtime_info_get_processor_max_frequency

Change-Id: Ic331e1a4fd54962a7bf86186761ca58c8a91569d
Signed-off-by: Kichan Kwon <k_c.kwon@samsung.com>
include/runtime_info.h
src/runtime_info_usage.c

index 8a3b473..25e2472 100644 (file)
@@ -366,8 +366,42 @@ typedef struct {
 int runtime_info_get_process_cpu_usage(int *pid, int size, process_cpu_usage_s **usage);
 
 /**
- * @}
+ * @brief  Gets the number of processors
+ * @since_tizen  3.0
+ *
+ * @param[out]  num_core The number of whole processors
+ *
+ * @retval  #RUNTIME_INFO_ERROR_NONE  Successful
+ * @retval  #RUNTIME_INFO_ERROR_INVALID_PARAMETER  Invalid parameter
+ * @retval  #RUNTIME_INFO_ERROR_IO_ERROR  An I/O error occurred (during file open operation)
+ */
+int runtime_info_get_processor_count(int *num_core);
+
+/**
+ * @brief  Gets the current frequency of processor
+ * @since_tizen  3.0
+ *
+ * @param[in]  core_idx The index (from 0) of CPU core that you want to know the frequency
+ * @param[out]  cpu_freq The current frequency(MHz) of processor
+ *
+ * @retval  #RUNTIME_INFO_ERROR_NONE  Successful
+ * @retval  #RUNTIME_INFO_ERROR_INVALID_PARAMETER  Invalid parameter
+ * @retval  #RUNTIME_INFO_ERROR_IO_ERROR  An I/O error occurred (during file open operation)
+ */
+int runtime_info_get_processor_current_frequency(int core_idx, int *cpu_freq);
+
+/**
+ * @brief  Gets the max frequency of processor
+ * @since_tizen  3.0
+ *
+ * @param[in]  core_idx The index (from 0) of CPU core that you want to know the frequency
+ * @param[out]  cpu_freq The max frequency(MHz) of processor
+ *
+ * @retval  #RUNTIME_INFO_ERROR_NONE  Successful
+ * @retval  #RUNTIME_INFO_ERROR_INVALID_PARAMETER  Invalid parameter
+ * @retval  #RUNTIME_INFO_ERROR_IO_ERROR  An I/O error occurred (during file open operation)
  */
+int runtime_info_get_processor_max_frequency(int core_idx, int *cpu_freq);
 
 #ifdef __cplusplus
 }
index 87f6c04..fec3cc2 100644 (file)
@@ -387,3 +387,133 @@ API int runtime_info_get_process_cpu_usage(int *pid, int size, process_cpu_usage
        dbus_message_unref(replymsg);
        return RUNTIME_INFO_ERROR_NONE;
 }
+
+API int runtime_info_get_processor_count(int *num_core)
+{
+       FILE *cpuinfo_fp;
+       int buf;
+       int result;
+
+       if (!num_core) {
+               _E("INVALID_PARAMETER(0x%08x) : invalid output parameter",
+                               RUNTIME_INFO_ERROR_INVALID_PARAMETER);
+               return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
+       }
+
+       cpuinfo_fp = fopen("/sys/devices/system/cpu/possible", "r");
+       if(cpuinfo_fp == NULL) {
+               _E("IO_ERROR(0x%08x) : failed to open file to read cpu information",
+                               RUNTIME_INFO_ERROR_IO_ERROR);
+               return RUNTIME_INFO_ERROR_IO_ERROR;
+       }
+
+       if(!fscanf(cpuinfo_fp, "%d-%d", &buf, &result)) {
+               _E("IO_ERROR(0x%08x) : there is no information in the system file",
+                               RUNTIME_INFO_ERROR_IO_ERROR);
+               return RUNTIME_INFO_ERROR_IO_ERROR;
+       }
+
+       *num_core = result + 1;
+
+       return RUNTIME_INFO_ERROR_NONE;
+}
+
+API int runtime_info_get_processor_current_frequency(int core_idx, int *cpu_freq)
+{
+       int num_core;
+       char path[256];
+       FILE *cpuinfo_fp;
+       int result;
+
+       if(runtime_info_get_processor_count(&num_core)) {
+               _E("runtime_info_get_processor_count is failed");
+               return RUNTIME_INFO_ERROR_IO_ERROR;
+       }
+
+       if(core_idx < 0 || core_idx >= num_core) {
+               _E("INVALID_PARAMETER(0x%08x) : invalid input parameter",
+                               RUNTIME_INFO_ERROR_INVALID_PARAMETER);
+               return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
+       }
+
+       if (!cpu_freq) {
+               _E("INVALID_PARAMETER(0x%08x) : invalid output parameter",
+                               RUNTIME_INFO_ERROR_INVALID_PARAMETER);
+               return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
+       }
+
+       snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq",
+                       core_idx);
+       cpuinfo_fp = fopen(path, "r");
+       if(cpuinfo_fp == NULL) {
+               _I("Fail to get the information about core%d. Get the core0's instead.",
+                               core_idx);
+
+               cpuinfo_fp = fopen("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq", "r");
+               if(cpuinfo_fp == NULL) {
+                       _E("IO_ERROR(0x%08x) : failed to open file to read cpu information",
+                                       RUNTIME_INFO_ERROR_IO_ERROR);
+                       return RUNTIME_INFO_ERROR_IO_ERROR;
+               }
+       }
+
+       if(!fscanf(cpuinfo_fp, "%d", &result)) {
+               _E("IO_ERROR(0x%08x) : there is no information in the system file",
+                               RUNTIME_INFO_ERROR_IO_ERROR);
+               return RUNTIME_INFO_ERROR_IO_ERROR;
+       }
+
+       *cpu_freq = result / 1000;
+
+       return RUNTIME_INFO_ERROR_NONE;
+}
+
+API int runtime_info_get_processor_max_frequency(int core_idx, int *cpu_freq)
+{
+       int num_core;
+       char path[256];
+       FILE *cpuinfo_fp;
+       int result;
+
+       if(runtime_info_get_processor_count(&num_core)) {
+               _E("runtime_info_get_processor_count is failed");
+               return RUNTIME_INFO_ERROR_IO_ERROR;
+       }
+
+       if(core_idx < 0 || core_idx >= num_core) {
+               _E("INVALID_PARAMETER(0x%08x) : invalid input parameter",
+                               RUNTIME_INFO_ERROR_INVALID_PARAMETER);
+               return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
+       }
+
+       if (!cpu_freq) {
+               _E("INVALID_PARAMETER(0x%08x) : invalid output parameter",
+                               RUNTIME_INFO_ERROR_INVALID_PARAMETER);
+               return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
+       }
+
+       snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq",
+                       core_idx);
+       cpuinfo_fp = fopen(path, "r");
+       if(cpuinfo_fp == NULL) {
+               _I("Fail to get the information about core%d. Get the core0's instead.",
+                               core_idx);
+
+               cpuinfo_fp = fopen("/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq", "r");
+               if(cpuinfo_fp == NULL) {
+                       _E("IO_ERROR(0x%08x) : failed to open file to read cpu information",
+                                       RUNTIME_INFO_ERROR_IO_ERROR);
+                       return RUNTIME_INFO_ERROR_IO_ERROR;
+               }
+       }
+
+       if(!fscanf(cpuinfo_fp, "%d", &result)) {
+               _E("IO_ERROR(0x%08x) : there is no information in the system file",
+                               RUNTIME_INFO_ERROR_IO_ERROR);
+               return RUNTIME_INFO_ERROR_IO_ERROR;
+       }
+
+       *cpu_freq = result / 1000;
+
+       return RUNTIME_INFO_ERROR_NONE;
+}