2 * Copyright 2014 Sony Mobile Communications Inc.
4 * Licensed under the terms of the GNU GPL License version 2
6 * Selftest for runtime system size
8 * Prints the amount of RAM that the currently running system is using.
10 * This program tries to be as small as possible itself, to
11 * avoid perturbing the system memory utilization with its
12 * own execution. It also attempts to have as few dependencies
13 * on kernel features as possible.
15 * It should be statically linked, with startup libs avoided.
16 * It uses no library calls, and only the following 3 syscalls:
17 * sysinfo(), write(), and _exit()
19 * For output, it avoids printf (which in some C libraries
20 * has large external dependencies) by implementing it's own
21 * number output and print routines, and using __builtin_strlen()
24 #include <sys/sysinfo.h>
27 #define STDOUT_FILENO 1
29 static int print(const char *s)
31 return write(STDOUT_FILENO, s, __builtin_strlen(s));
34 static inline char *num_to_str(unsigned long num, char *buf, int len)
38 /* put digits in buffer from back to front */
43 *(--buf) = digit + '0';
50 static int print_num(unsigned long num)
54 return print(num_to_str(num, num_buf, sizeof(num_buf)));
57 static int print_k_value(const char *s, unsigned long num, unsigned long units)
59 unsigned long long temp;
65 temp = (temp * units)/1024;
67 ccode = print_num(num);
72 /* this program has no main(), as startup libraries are not used */
78 static const char *test_name = " get runtime memory use\n";
80 print("TAP version 13\n");
81 print("# Testing system size.\n");
83 ccode = sysinfo(&info);
87 print(" ---\n reason: \"could not get sysinfo\"\n ...\n");
93 /* ignore cache complexities for now */
94 used = info.totalram - info.freeram - info.bufferram;
95 print("# System runtime memory report (units in Kilobytes):\n");
97 print_k_value(" Total: ", info.totalram, info.mem_unit);
98 print_k_value(" Free: ", info.freeram, info.mem_unit);
99 print_k_value(" Buffer: ", info.bufferram, info.mem_unit);
100 print_k_value(" In use: ", used, info.mem_unit);