2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <runtime_info.h>
23 #include <runtime_info_private.h>
29 #define LOG_TAG "CAPI_SYSTEM_RUNTIME_INFO"
31 #define kBtoKiB(val) (int)(((long long)val*1024)/1000)
33 API int runtime_info_get_system_memory_info(runtime_memory_info_s *info)
37 unsigned long swap_total, swap_free, value;
40 LOGE("INVALID_PARAMETER(0x%08x) : invalid output param", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
41 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
44 swap_total = swap_free = 0;
46 meminfo_fp = fopen("/proc/meminfo", "r");
47 if (meminfo_fp == NULL) {
48 LOGE("IO_ERROR(0x%08x) : failed to open file to read memory usage", RUNTIME_INFO_ERROR_IO_ERROR);
49 return RUNTIME_INFO_ERROR_IO_ERROR;
52 while (fgets(buf, sizeof(buf), meminfo_fp) != NULL) {
53 if (sscanf(buf, "MemTotal: %lu", &value) == 1)
54 info->total = kBtoKiB(value);
55 else if (sscanf(buf, "MemFree: %lu", &value) == 1)
56 info->free = kBtoKiB(value);
57 else if (sscanf(buf, "Cached: %lu", &value) == 1)
58 info->cache = kBtoKiB(value);
59 else if (sscanf(buf, "SwapTotal: %lu", &value) == 1)
61 else if (sscanf(buf, "SwapFree: %lu", &value) == 1)
66 info->used = (info->total > info->free) ? (info->total - info->free) : 0;
67 info->swap = kBtoKiB(((swap_total > swap_free) ? (int)(swap_total - swap_free) : 0));
69 return RUNTIME_INFO_ERROR_NONE;
72 API int runtime_info_get_process_memory_info(int *pid, int size, process_memory_info_s **info)
74 return RUNTIME_INFO_ERROR_NONE;
77 API int runtime_info_get_cpu_usage(runtime_cpu_usage_s *usage)
81 unsigned long long user, nice, system, idle, iowait, irq, softirq, total_uptime;
84 LOGE("INVALID_PARAMETER(0x%08x) : invalid output param", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
85 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
88 cpuinfo_fp = fopen("/proc/stat", "r");
89 if (cpuinfo_fp == NULL) {
90 LOGE("IO_ERROR(0x%08x) : failed to open file to read cpu usage", RUNTIME_INFO_ERROR_IO_ERROR);
91 return RUNTIME_INFO_ERROR_IO_ERROR;
94 while (fgets(buf, sizeof(buf), cpuinfo_fp) != NULL) {
95 if (!strncmp(buf, "cpu ", 4) &&
96 sscanf(buf+4, "%llu %llu %llu %llu %llu %llu %llu",
97 &user, &nice, &system, &idle,
98 &iowait, &irq, &softirq) == 7)
103 total_uptime = user+nice+system+idle+iowait+irq+softirq;
105 usage->user = (double)user*100/total_uptime;
106 usage->nice = (double)nice*100/total_uptime;
107 usage->system = (double)system*100/total_uptime;
108 usage->iowait = (double)iowait*100/total_uptime;
110 return RUNTIME_INFO_ERROR_NONE;
113 API int runtime_info_get_process_cpu_usage(int *pid, int size, process_cpu_usage_s **usage)
115 return RUNTIME_INFO_ERROR_NONE;