get_size.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2014 Sony Mobile Communications Inc.
  4. *
  5. * Selftest for runtime system size
  6. *
  7. * Prints the amount of RAM that the currently running system is using.
  8. *
  9. * This program tries to be as small as possible itself, to
  10. * avoid perturbing the system memory utilization with its
  11. * own execution. It also attempts to have as few dependencies
  12. * on kernel features as possible.
  13. *
  14. * It should be statically linked, with startup libs avoided. It uses
  15. * no library calls except the syscall() function for the following 3
  16. * syscalls:
  17. * sysinfo(), write(), and _exit()
  18. *
  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()
  22. *
  23. * The test may crash if any of the above syscalls fails because in some
  24. * libc implementations (e.g. the GNU C Library) errno is saved in
  25. * thread-local storage, which does not get initialized due to avoiding
  26. * startup libs.
  27. */
  28. #include <sys/sysinfo.h>
  29. #include <unistd.h>
  30. #include <sys/syscall.h>
  31. #define STDOUT_FILENO 1
  32. static int print(const char *s)
  33. {
  34. size_t len = 0;
  35. while (s[len] != '\0')
  36. len++;
  37. return syscall(SYS_write, STDOUT_FILENO, s, len);
  38. }
  39. static inline char *num_to_str(unsigned long num, char *buf, int len)
  40. {
  41. unsigned int digit;
  42. /* put digits in buffer from back to front */
  43. buf += len - 1;
  44. *buf = 0;
  45. do {
  46. digit = num % 10;
  47. *(--buf) = digit + '0';
  48. num /= 10;
  49. } while (num > 0);
  50. return buf;
  51. }
  52. static int print_num(unsigned long num)
  53. {
  54. char num_buf[30];
  55. return print(num_to_str(num, num_buf, sizeof(num_buf)));
  56. }
  57. static int print_k_value(const char *s, unsigned long num, unsigned long units)
  58. {
  59. unsigned long long temp;
  60. int ccode;
  61. print(s);
  62. temp = num;
  63. temp = (temp * units)/1024;
  64. num = temp;
  65. ccode = print_num(num);
  66. print("\n");
  67. return ccode;
  68. }
  69. /* this program has no main(), as startup libraries are not used */
  70. void _start(void)
  71. {
  72. int ccode;
  73. struct sysinfo info;
  74. unsigned long used;
  75. static const char *test_name = " get runtime memory use\n";
  76. print("TAP version 13\n");
  77. print("# Testing system size.\n");
  78. ccode = syscall(SYS_sysinfo, &info);
  79. if (ccode < 0) {
  80. print("not ok 1");
  81. print(test_name);
  82. print(" ---\n reason: \"could not get sysinfo\"\n ...\n");
  83. syscall(SYS_exit, ccode);
  84. }
  85. print("ok 1");
  86. print(test_name);
  87. /* ignore cache complexities for now */
  88. used = info.totalram - info.freeram - info.bufferram;
  89. print("# System runtime memory report (units in Kilobytes):\n");
  90. print(" ---\n");
  91. print_k_value(" Total: ", info.totalram, info.mem_unit);
  92. print_k_value(" Free: ", info.freeram, info.mem_unit);
  93. print_k_value(" Buffer: ", info.bufferram, info.mem_unit);
  94. print_k_value(" In use: ", used, info.mem_unit);
  95. print(" ...\n");
  96. print("1..1\n");
  97. syscall(SYS_exit, 0);
  98. }