Read cpuinfo to get CPU frequency when cpufreq isn't supported
[platform/core/api/runtime-info.git] / src / runtime_info_usage.c
index 87f6c04..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"
@@ -387,3 +378,118 @@ 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);
+               fclose(cpuinfo_fp);
+               return RUNTIME_INFO_ERROR_IO_ERROR;
+       }
+
+       *num_core = result + 1;
+
+       fclose(cpuinfo_fp);
+       return RUNTIME_INFO_ERROR_NONE;
+}
+
+API int runtime_info_get_processor_current_frequency(int core_idx, int *cpu_freq)
+{
+       int 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;
+       }
+
+       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;
+       }
+
+       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.");
+
+               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;
+               };
+       }
+
+       return RUNTIME_INFO_ERROR_NONE;
+}
+
+API int runtime_info_get_processor_max_frequency(int core_idx, int *cpu_freq)
+{
+       int 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;
+       }
+
+       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;
+       }
+
+       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.");
+
+               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;
+               };
+       }
+
+       return RUNTIME_INFO_ERROR_NONE;
+}