From a5fde3ce890efb7005d8b7dde93912c3aa378b61 Mon Sep 17 00:00:00 2001 From: Kichan Kwon Date: Wed, 3 Feb 2016 19:45:52 +0900 Subject: [PATCH] runtime-info : Add cpu information APIs - 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 --- include/runtime_info.h | 36 ++++++++++++- src/runtime_info_usage.c | 130 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 165 insertions(+), 1 deletion(-) diff --git a/include/runtime_info.h b/include/runtime_info.h index 8a3b473..25e2472 100644 --- a/include/runtime_info.h +++ b/include/runtime_info.h @@ -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 } diff --git a/src/runtime_info_usage.c b/src/runtime_info_usage.c index 87f6c04..fec3cc2 100644 --- a/src/runtime_info_usage.c +++ b/src/runtime_info_usage.c @@ -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; +} -- 2.7.4