get_size.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. print("Testing system size.\n");
  67. print("1..1\n");
  68. ccode = sysinfo(&info);
  69. if (ccode < 0) {
  70. print("not ok 1 get runtime memory use\n");
  71. print("# could not get sysinfo\n");
  72. _exit(ccode);
  73. }
  74. /* ignore cache complexities for now */
  75. used = info.totalram - info.freeram - info.bufferram;
  76. print_k_value("ok 1 get runtime memory use # size = ", used,
  77. info.mem_unit);
  78. print("# System runtime memory report (units in Kilobytes):\n");
  79. print_k_value("# Total: ", info.totalram, info.mem_unit);
  80. print_k_value("# Free: ", info.freeram, info.mem_unit);
  81. print_k_value("# Buffer: ", info.bufferram, info.mem_unit);
  82. print_k_value("# In use: ", used, info.mem_unit);
  83. _exit(0);
  84. }