Read cpuinfo to get CPU frequency when cpufreq isn't supported 08/68308/2 accepted/tizen/common/20160510.141636 accepted/tizen/ivi/20160512.050636 accepted/tizen/mobile/20160512.050620 accepted/tizen/tv/20160512.050616 accepted/tizen/wearable/20160512.050603 submit/tizen/20160509.003410 submit/tizen/20160510.124308
authorKichan Kwon <k_c.kwon@samsung.com>
Tue, 3 May 2016 10:36:53 +0000 (19:36 +0900)
committerKichan Kwon <k_c.kwon@samsung.com>
Wed, 4 May 2016 01:18:01 +0000 (10:18 +0900)
- If there is no "cpu MHz" information in the cpuinfo, then returns error

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

index 1f164a9..be5eab7 100644 (file)
@@ -29,6 +29,15 @@ extern "C"
 #define API __attribute__ ((visibility("default")))
 #endif
 
+#ifdef LOG_TAG
+#undef LOG_TAG
+#endif
+#define LOG_TAG "CAPI_SYSTEM_RUNTIME_INFO"
+
+#define _E(fmt, arg...) LOGE("[%s,%d] "fmt, __FUNCTION__, __LINE__, ##arg)
+#define _D(fmt, arg...) LOGD("[%s,%d] "fmt, __FUNCTION__, __LINE__, ##arg)
+#define _I(fmt, arg...) LOGI("[%s,%d] "fmt, __FUNCTION__, __LINE__, ##arg)
+
 typedef enum {
        RUNTIME_INFO_DATA_TYPE_STRING,
        RUNTIME_INFO_DATA_TYPE_INT,
@@ -167,6 +176,9 @@ int runtime_info_auto_rotation_enabled_get_value(runtime_info_value_h);
 int runtime_info_auto_rotation_enabled_set_event_cb(void);
 void runtime_info_auto_rotation_enabled_unset_event_cb(void);
 
+int runtime_info_get_frequency_cpufreq(int core_idx, char *type, int *cpu_freq);
+int runtime_info_get_frequency_cpuinfo(int core_idx, int *cpu_freq);
+
 #ifdef __cplusplus
 }
 #endif
index 4a98a29..fe8b10e 100644 (file)
 #include <runtime_info.h>
 #include <runtime_info_private.h>
 
-#ifdef LOG_TAG
-#undef LOG_TAG
-#endif
-
-#define LOG_TAG "CAPI_SYSTEM_RUNTIME_INFO"
-
 static const char *VCONF_AUDIO_JACK = VCONFKEY_SYSMAN_EARJACK;
 static const char *VCONF_VIBRATION_ENABLED = VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL;
 static const char *VCONF_ROTATION_LOCK_ENABLED = VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL;
@@ -282,3 +276,88 @@ void runtime_info_charger_connected_unset_event_cb(void)
 {
        runtime_info_vconf_unset_event_cb(VCONF_CHARGER_CONNECTED, 0);
 }
+
+int runtime_info_get_frequency_cpufreq(int core_idx, char *type, int *cpu_freq)
+{
+       char path[256];
+       FILE *cpufreq_fp;
+       int result;
+
+       if (core_idx < 0)
+               return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
+
+       if (!type || !cpu_freq)
+               return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
+
+       snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_%s_freq",
+                       core_idx, type);
+       cpufreq_fp = fopen(path, "r");
+       if (cpufreq_fp == NULL) {
+               if (core_idx > 0) {
+                       _I("Fail to get the information about core%d. Get the core0's instead",
+                                       core_idx);
+                       snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu0/cpufreq/scaling_%s_freq",
+                                       type);
+                       cpufreq_fp = fopen(path, "r");
+               }
+
+               if (cpufreq_fp == NULL) {
+                       _E("IO_ERROR(0x%08x) : failed to open cpufreq file",
+                                       RUNTIME_INFO_ERROR_IO_ERROR);
+                       return RUNTIME_INFO_ERROR_IO_ERROR;
+               }
+       }
+
+       if (!fscanf(cpufreq_fp, "%d", &result)) {
+               _E("IO_ERROR(0x%08x) : there is no information in the cpuinfo file",
+                               RUNTIME_INFO_ERROR_IO_ERROR);
+               fclose(cpufreq_fp);
+               return RUNTIME_INFO_ERROR_IO_ERROR;
+       }
+
+       *cpu_freq = result / 1000;
+       fclose(cpufreq_fp);
+       return RUNTIME_INFO_ERROR_NONE;
+}
+
+int runtime_info_get_frequency_cpuinfo(int core_idx, int *cpu_freq)
+{
+       FILE *cpuinfo_fp;
+       char line[128];
+       int cur_core = 0;
+       char *start;
+       int acc_freq;
+
+       if (core_idx < 0)
+               return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
+
+       if (!cpu_freq)
+               return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
+
+       cpuinfo_fp = fopen("/proc/cpuinfo", "r");
+       if (cpuinfo_fp == NULL) {
+               _E("Fail to open cpuinfo");
+               return RUNTIME_INFO_ERROR_IO_ERROR;
+       }
+
+       while (fgets(line, sizeof(line), cpuinfo_fp) != NULL) {
+               if (strncmp(line, "cpu MHz", 7))
+                       continue;
+
+               if (cur_core == core_idx) {
+                       /* String format in the cpuinfo : "cpu MHz : 1234" */
+                       start = strchr(line, ':') + 2;
+                       acc_freq = 0;
+                       while (*start >= '0' && *start <= '9') {
+                               acc_freq = (acc_freq * 10) + (*start - '0');
+                               ++start;
+                       }
+
+                       *cpu_freq = acc_freq;
+                       return RUNTIME_INFO_ERROR_NONE;
+               }
+               ++cur_core;
+       }
+
+       return RUNTIME_INFO_ERROR_NOT_SUPPORTED;
+}
index e9b9b73..d03e565 100644 (file)
 
 #include <E_DBus.h>
 
-#ifdef LOG_TAG
-#undef LOG_TAG
-#endif
-#define LOG_TAG "CAPI_RUNTIME_INFO_USAGE"
-
-#define _E(fmt, arg...) LOGE("[%s,%d] "fmt, __FUNCTION__, __LINE__, ##arg)
-#define _D(fmt, arg...) LOGD("[%s,%d] "fmt, __FUNCTION__, __LINE__, ##arg)
-#define _I(fmt, arg...) LOGI("[%s,%d] "fmt, __FUNCTION__, __LINE__, ##arg)
-
 #define RESOURCED_BUS_NAME "org.tizen.resourced"
 #define RESOURCED_USAGE_OBJECT_NAME "/Org/Tizen/ResourceD/Process"
 #define RESOURCED_USAGE_INTERFACE_NAME "org.tizen.resourced.process"
@@ -423,11 +414,9 @@ API int runtime_info_get_processor_count(int *num_core)
 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)) {
+       if (runtime_info_get_processor_count(&num_core)
+                       != RUNTIME_INFO_ERROR_NONE) {
                _E("runtime_info_get_processor_count is failed");
                return RUNTIME_INFO_ERROR_IO_ERROR;
        }
@@ -444,42 +433,32 @@ API int runtime_info_get_processor_current_frequency(int core_idx, int *cpu_freq
                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);
+       if (runtime_info_get_frequency_cpufreq(core_idx, "cur", cpu_freq)
+                       != RUNTIME_INFO_ERROR_NONE) {
+               _I("This system doesn't support cpufreq. Use cpuinfo instead.");
 
-               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);
+               switch (runtime_info_get_frequency_cpuinfo(core_idx, cpu_freq)) {
+               case RUNTIME_INFO_ERROR_NONE:
+                       _I("Notice : it is max CPU frequency");
+                       break;
+               case RUNTIME_INFO_ERROR_NOT_SUPPORTED:
+                       _E("This system doesn't support MHz information in the cpuinfo");
+                       return RUNTIME_INFO_ERROR_NOT_SUPPORTED;
+               default:
+                       _E("Fail to get current CPU frequency");
                        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);
-               fclose(cpuinfo_fp);
-               return RUNTIME_INFO_ERROR_IO_ERROR;
-       }
-
-       *cpu_freq = result / 1000;
-
-       fclose(cpuinfo_fp);
        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)) {
+       if (runtime_info_get_processor_count(&num_core)
+                       != RUNTIME_INFO_ERROR_NONE) {
                _E("runtime_info_get_processor_count is failed");
                return RUNTIME_INFO_ERROR_IO_ERROR;
        }
@@ -496,30 +475,21 @@ API int runtime_info_get_processor_max_frequency(int core_idx, int *cpu_freq)
                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);
+       if (runtime_info_get_frequency_cpufreq(core_idx, "max", cpu_freq)
+                       != RUNTIME_INFO_ERROR_NONE) {
+               _I("This system doesn't support cpufreq. Use cpuinfo instead.");
 
-               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);
+               switch (runtime_info_get_frequency_cpuinfo(core_idx, cpu_freq)) {
+               case RUNTIME_INFO_ERROR_NONE:
+                       break;
+               case RUNTIME_INFO_ERROR_NOT_SUPPORTED:
+                       _E("This system doesn't support MHz information in the cpuinfo");
+                       return RUNTIME_INFO_ERROR_NOT_SUPPORTED;
+               default:
+                       _E("Fail to get current CPU frequency");
                        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);
-               fclose(cpuinfo_fp);
-               return RUNTIME_INFO_ERROR_IO_ERROR;
-       }
-
-       *cpu_freq = result / 1000;
-
-       fclose(cpuinfo_fp);
        return RUNTIME_INFO_ERROR_NONE;
 }