get_size.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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.
  16. * It uses no library calls, and only the following 3 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. #include <sys/sysinfo.h>
  24. #include <unistd.h>
  25. #define STDOUT_FILENO 1
  26. static int print(const char *s)
  27. {
  28. return write(STDOUT_FILENO, s, __builtin_strlen(s));
  29. }
  30. static inline char *num_to_str(unsigned long num, char *buf, int len)
  31. {
  32. unsigned int digit;
  33. /* put digits in buffer from back to front */
  34. buf += len - 1;
  35. *buf = 0;
  36. do {
  37. digit = num % 10;
  38. *(--buf) = digit + '0';
  39. num /= 10;
  40. } while (num > 0);
  41. return buf;
  42. }
  43. static int print_num(unsigned long num)
  44. {
  45. char num_buf[30];
  46. return print(num_to_str(num, num_buf, sizeof(num_buf)));
  47. }
  48. static int print_k_value(const char *s, unsigned long num, unsigned long units)
  49. {
  50. unsigned long long temp;
  51. int ccode;
  52. print(s);
  53. temp = num;
  54. temp = (temp * units)/1024;
  55. num = temp;
  56. ccode = print_num(num);
  57. print("\n");
  58. return ccode;
  59. }
  60. /* this program has no main(), as startup libraries are not used */
  61. void _start(void)
  62. {
  63. int ccode;
  64. struct sysinfo info;
  65. unsigned long used;
  66. static const char *test_name = " get runtime memory use\n";
  67. print("TAP version 13\n");
  68. print("# Testing system size.\n");
  69. ccode = sysinfo(&info);
  70. if (ccode < 0) {
  71. print("not ok 1");
  72. print(test_name);
  73. print(" ---\n reason: \"could not get sysinfo\"\n ...\n");
  74. _exit(ccode);
  75. }
  76. print("ok 1");
  77. print(test_name);
  78. /* ignore cache complexities for now */
  79. used = info.totalram - info.freeram - info.bufferram;
  80. print("# System runtime memory report (units in Kilobytes):\n");
  81. print(" ---\n");
  82. print_k_value(" Total: ", info.totalram, info.mem_unit);
  83. print_k_value(" Free: ", info.freeram, info.mem_unit);
  84. print_k_value(" Buffer: ", info.bufferram, info.mem_unit);
  85. print_k_value(" In use: ", used, info.mem_unit);
  86. print(" ...\n");
  87. print("1..1\n");
  88. _exit(0);
  89. }