runtime-info: Add memory and cpu information apis
[platform/core/api/runtime-info.git] / src / runtime_info_usage.c
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <stdio.h>
18 #include <string.h>
19
20 #include <dlog.h>
21
22 #include <runtime_info.h>
23 #include <runtime_info_private.h>
24
25 #ifdef LOG_TAG
26 #undef LOG_TAG
27 #endif
28
29 #define LOG_TAG "CAPI_SYSTEM_RUNTIME_INFO"
30
31 #define kBtoKiB(val) (int)(((long long)val*1024)/1000)
32
33 API int runtime_info_get_system_memory_info(runtime_memory_info_s *info)
34 {
35         FILE *meminfo_fp;
36         char buf[256];
37         unsigned long swap_total, swap_free, value;
38
39         if (info == NULL) {
40                 LOGE("INVALID_PARAMETER(0x%08x) : invalid output param", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
41                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
42         }
43
44         swap_total = swap_free = 0;
45
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;
50         }
51
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)
60                         swap_total = value;
61                 else if (sscanf(buf, "SwapFree: %lu", &value) == 1)
62                         swap_free = value;
63         }
64         fclose(meminfo_fp);
65
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));
68
69         return RUNTIME_INFO_ERROR_NONE;
70 }
71
72 API int runtime_info_get_process_memory_info(int *pid, int size, process_memory_info_s **info)
73 {
74         return RUNTIME_INFO_ERROR_NONE;
75 }
76
77 API int runtime_info_get_cpu_usage(runtime_cpu_usage_s *usage)
78 {
79         FILE *cpuinfo_fp;
80         char buf[256];
81         unsigned long long user, nice, system, idle, iowait, irq, softirq, total_uptime;
82
83         if (usage == NULL) {
84                 LOGE("INVALID_PARAMETER(0x%08x) : invalid output param", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
85                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
86         }
87
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;
92         }
93
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)
99                         break;
100         }
101         fclose(cpuinfo_fp);
102
103         total_uptime = user+nice+system+idle+iowait+irq+softirq;
104
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;
109
110         return RUNTIME_INFO_ERROR_NONE;
111 }
112
113 API int runtime_info_get_process_cpu_usage(int *pid, int size, process_cpu_usage_s **usage)
114 {
115         return RUNTIME_INFO_ERROR_NONE;
116 }