Remove deprecated enum values
[platform/core/api/runtime-info.git] / test / runtime_info_test_proc.c
1 /*
2  * Copyright (c) 2011 - 2017 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 <limits.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21
22 #include <runtime_info.h>
23
24 #include "runtime_info_test.h"
25
26 #define KiBtoPage(kib)          (int)((kib) / 4.0)
27
28 void runtime_info_test_get_system_memory_info(void)
29 {
30         int ret;
31         runtime_memory_info_s info;
32         char buf[BUF_SIZE];
33         FILE *fp;
34         unsigned long mem_total, mem_used, mem_free, cached, swap;
35         unsigned long mem_available, swap_total, swap_free;
36
37         printf("Runtime_info_get_system_memory_info test\n");
38
39         ret = runtime_info_get_system_memory_info(&info);
40         if (ret) {
41                 printf("runtime_info_get_system_memory_info failed (%d)\n", ret);
42                 printf("  Result : %s\n", PRINT_RESULT(ERROR));
43                 return;
44         }
45
46         fp = fopen("/proc/meminfo", "r");
47         if (!fp) {
48                 printf("Failed to read /proc/meminfo\n");
49                 printf("  Result : %s\n", PRINT_RESULT(ERROR));
50                 return;
51         }
52
53         mem_total = mem_free = cached = mem_available = swap_total = swap_free = 0;
54         while (fgets(buf, BUF_SIZE, fp)) {
55                 if (sscanf(buf, "MemTotal: %lu", &mem_total) == 1)
56                         continue;
57                 if (sscanf(buf, "MemFree: %lu", &mem_free) == 1)
58                         continue;
59                 if (sscanf(buf, "Cached: %lu", &cached) == 1)
60                         continue;
61                 if (sscanf(buf, "MemAvailable: %lu", &mem_available) == 1)
62                         continue;
63                 if (sscanf(buf, "SwapTotal: %lu", &swap_total) == 1)
64                         continue;
65                 if (sscanf(buf, "SwapFree: %lu", &swap_free) == 1)
66                         continue;
67         }
68
69         fclose(fp);
70
71         if (mem_available > 0) {
72                 if (mem_total > mem_available)
73                         mem_used = mem_total - mem_available;
74                 else
75                         mem_used = 0;
76         } else {
77                 if (mem_total > mem_free && mem_total - mem_free > cached)
78                         mem_used = mem_total - mem_free - cached;
79                 else
80                         mem_used = 0;
81         }
82
83         swap = (swap_total > swap_free) ? swap_total - swap_free : 0;
84
85         printf("  Total memory size : %s\n",
86                 PRINT_RESULT(check_value(info.total, mem_total, 0)));
87         printf("  Used memory size  : %s\n",
88                 PRINT_RESULT(check_value(info.used, mem_used, 0)));
89         printf("  Free memory size  : %s\n",
90                 PRINT_RESULT(check_value(info.free, mem_free, 0)));
91         printf("  Cache memory size : %s\n",
92                 PRINT_RESULT(check_value(info.cache, cached, 0)));
93         printf("  Swap memory size  : %s\n",
94                 PRINT_RESULT(check_value(info.swap, swap, 0)));
95 }
96
97 void runtime_info_test_get_cpu_usage(void)
98 {
99         int ret;
100         runtime_cpu_usage_s usage;
101         char buf[BUF_SIZE];
102         FILE *fp;
103         int total = 0;
104         unsigned long long user, nice, systime, idle, iowait, irq, softirq;
105
106         printf("Runtime_info_get_cpu_usage test\n");
107
108         ret = runtime_info_get_cpu_usage(&usage);
109         if (ret) {
110                 printf("runtime_info_get_cpu_usage failed (%d)\n", ret);
111                 printf("  Result : %s\n", PRINT_RESULT(ERROR));
112                 return;
113         }
114
115         fp = fopen("/proc/stat", "r");
116         if (!fp) {
117                 printf("Failed to read /proc/stat\n");
118                 printf("  Result : %s\n", PRINT_RESULT(ERROR));
119                 return;
120         }
121
122         while (fgets(buf, BUF_SIZE, fp)) {
123                 if (sscanf(buf, "cpu %llu %llu %llu %llu %llu %llu %llu",
124                                 &user, &nice, &systime, &idle,
125                                 &iowait, &irq, &softirq) == 7)
126                         break;
127         }
128
129         fclose(fp);
130
131         total = user + nice + systime + idle + iowait + irq + softirq;
132
133         user = (double)user * 100 / total;
134         nice = (double)nice * 100 / total;
135         systime = (double)systime * 100 / total;
136         iowait = (double)iowait * 100 / total;
137
138         printf("  User time       : %s\n",
139                         PRINT_RESULT(check_value(usage.user, user, 1)));
140         printf("  Niced user time : %s\n",
141                         PRINT_RESULT(check_value(usage.nice, nice, 1)));
142         printf("  System time     : %s\n",
143                         PRINT_RESULT(check_value(usage.system, systime, 1)));
144         printf("  IO wait time    : %s\n",
145                         PRINT_RESULT(check_value(usage.iowait, iowait, 1)));
146 }
147
148 void runtime_info_test_get_physical_memory_size(void)
149 {
150         int ret;
151         int size;
152         char buf[BUF_SIZE];
153         FILE *fp;
154         unsigned long total, value;
155
156         printf("Runtime_info_get_physical_memory_size test\n");
157
158         ret = runtime_info_get_physical_memory_size(&size);
159         if (ret) {
160                 printf("runtime_info_get_physical_memory_size failed (%d)\n\n", ret);
161                 printf("  Result : %s\n", PRINT_RESULT(ERROR));
162                 return;
163         }
164
165         fp = fopen("/proc/zoneinfo", "r");
166         if (!fp) {
167                 printf("Failed to read /proc/zoneinfo\n");
168                 printf("  Result : %s\n", PRINT_RESULT(ERROR));
169                 return;
170         }
171
172         total = 0;
173         while (fgets(buf, BUF_SIZE, fp))
174                 if (sscanf(buf, " spanned %lu", &value) == 1)
175                         total += value;
176
177         fclose(fp);
178
179         printf("  Physical memory size : %s\n",
180                         PRINT_RESULT(check_value(KiBtoPage(size), total, 0)));
181 }