2 * Copyright (c) 2011 - 2017 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>
24 #include "runtime_info_test.h"
26 #define KiBtoPage(kib) (int)((kib) / 4.0)
28 void runtime_info_test_get_system_memory_info(void)
31 runtime_memory_info_s info;
34 unsigned long mem_total, mem_used, mem_free, cached, swap;
35 unsigned long mem_available, swap_total, swap_free;
37 printf("Runtime_info_get_system_memory_info test\n");
39 ret = runtime_info_get_system_memory_info(&info);
41 printf("runtime_info_get_system_memory_info failed (%d)\n", ret);
42 printf(" Result : %s\n", PRINT_RESULT(ERROR));
46 fp = fopen("/proc/meminfo", "r");
48 printf("Failed to read /proc/meminfo\n");
49 printf(" Result : %s\n", PRINT_RESULT(ERROR));
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)
57 if (sscanf(buf, "MemFree: %lu", &mem_free) == 1)
59 if (sscanf(buf, "Cached: %lu", &cached) == 1)
61 if (sscanf(buf, "MemAvailable: %lu", &mem_available) == 1)
63 if (sscanf(buf, "SwapTotal: %lu", &swap_total) == 1)
65 if (sscanf(buf, "SwapFree: %lu", &swap_free) == 1)
71 if (mem_available > 0) {
72 if (mem_total > mem_available)
73 mem_used = mem_total - mem_available;
77 if (mem_total > mem_free && mem_total - mem_free > cached)
78 mem_used = mem_total - mem_free - cached;
83 swap = (swap_total > swap_free) ? swap_total - swap_free : 0;
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)));
97 void runtime_info_test_get_cpu_usage(void)
100 runtime_cpu_usage_s usage;
104 unsigned long long user, nice, systime, idle, iowait, irq, softirq;
106 printf("Runtime_info_get_cpu_usage test\n");
108 ret = runtime_info_get_cpu_usage(&usage);
110 printf("runtime_info_get_cpu_usage failed (%d)\n", ret);
111 printf(" Result : %s\n", PRINT_RESULT(ERROR));
115 fp = fopen("/proc/stat", "r");
117 printf("Failed to read /proc/stat\n");
118 printf(" Result : %s\n", PRINT_RESULT(ERROR));
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)
131 total = user + nice + systime + idle + iowait + irq + softirq;
133 user = (double)user * 100 / total;
134 nice = (double)nice * 100 / total;
135 systime = (double)systime * 100 / total;
136 iowait = (double)iowait * 100 / total;
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)));
148 void runtime_info_test_get_physical_memory_size(void)
154 unsigned long total, value;
156 printf("Runtime_info_get_physical_memory_size test\n");
158 ret = runtime_info_get_physical_memory_size(&size);
160 printf("runtime_info_get_physical_memory_size failed (%d)\n\n", ret);
161 printf(" Result : %s\n", PRINT_RESULT(ERROR));
165 fp = fopen("/proc/zoneinfo", "r");
167 printf("Failed to read /proc/zoneinfo\n");
168 printf(" Result : %s\n", PRINT_RESULT(ERROR));
173 while (fgets(buf, BUF_SIZE, fp))
174 if (sscanf(buf, " spanned %lu", &value) == 1)
179 printf(" Physical memory size : %s\n",
180 PRINT_RESULT(check_value(KiBtoPage(size), total, 0)));