get_size.c 2.8 KB

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