ram.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* See LICENSE file for copyright and license details. */
  2. #include <stdio.h>
  3. #include <math.h> // Include math.h for round function
  4. #include "../util.h"
  5. #if defined(__linux__)
  6. #include <stdint.h>
  7. const char *
  8. ram_free(void)
  9. {
  10. uintmax_t free;
  11. if (pscanf("/proc/meminfo",
  12. "MemTotal: %ju kB\n"
  13. "MemFree: %ju kB\n"
  14. "MemAvailable: %ju kB\n",
  15. &free, &free, &free) != 3) {
  16. return NULL;
  17. }
  18. return fmt_human(free * 1024, 1024);
  19. }
  20. const char *
  21. ram_perc(void)
  22. {
  23. uintmax_t total, free, buffers, cached;
  24. if (pscanf("/proc/meminfo",
  25. "MemTotal: %ju kB\n"
  26. "MemFree: %ju kB\n"
  27. "MemAvailable: %ju kB\n"
  28. "Buffers: %ju kB\n"
  29. "Cached: %ju kB\n",
  30. &total, &free, &buffers, &buffers, &cached) != 5) {
  31. return NULL;
  32. }
  33. if (total == 0) {
  34. return NULL;
  35. }
  36. return bprintf("%d", 100 * ((total - free) - (buffers + cached))
  37. / total);
  38. }
  39. const char *
  40. ram_total(void)
  41. {
  42. uintmax_t total;
  43. if (pscanf("/proc/meminfo", "MemTotal: %ju kB\n", &total)
  44. != 1) {
  45. return NULL;
  46. }
  47. return fmt_human(total * 1024, 1024);
  48. }
  49. const char *
  50. ram_used(void)
  51. {
  52. uintmax_t total, free, buffers, cached;
  53. if (pscanf("/proc/meminfo",
  54. "MemTotal: %ju kB\n"
  55. "MemFree: %ju kB\n"
  56. "MemAvailable: %ju kB\n"
  57. "Buffers: %ju kB\n"
  58. "Cached: %ju kB\n",
  59. &total, &free, &buffers, &buffers, &cached) != 5) {
  60. return NULL;
  61. }
  62. return fmt_human((total - free - buffers - cached) * 1024,
  63. 1024);
  64. }
  65. #elif defined(__OpenBSD__)
  66. #include <stdlib.h>
  67. #include <sys/sysctl.h>
  68. #include <sys/types.h>
  69. #include <unistd.h>
  70. #define LOG1024 10
  71. #define pagetok(size, pageshift) (size_t)(size << (pageshift - LOG1024))
  72. inline int
  73. load_uvmexp(struct uvmexp *uvmexp)
  74. {
  75. int uvmexp_mib[] = {CTL_VM, VM_UVMEXP};
  76. size_t size;
  77. size = sizeof(*uvmexp);
  78. if (sysctl(uvmexp_mib, 2, uvmexp, &size, NULL, 0) >= 0) {
  79. return 1;
  80. }
  81. return 0;
  82. }
  83. const char *
  84. ram_free(void)
  85. {
  86. struct uvmexp uvmexp;
  87. int free_pages;
  88. if (load_uvmexp(&uvmexp)) {
  89. free_pages = uvmexp.npages - uvmexp.active;
  90. return fmt_human(pagetok(free_pages, uvmexp.pageshift) *
  91. 1024, 1024);
  92. }
  93. return NULL;
  94. }
  95. const char *
  96. ram_perc(void)
  97. {
  98. struct uvmexp uvmexp;
  99. int percent;
  100. if (load_uvmexp(&uvmexp)) {
  101. percent = uvmexp.active * 100 / uvmexp.npages;
  102. return bprintf("%d", percent);
  103. }
  104. return NULL;
  105. }
  106. const char *
  107. ram_total(void)
  108. {
  109. struct uvmexp uvmexp;
  110. if (load_uvmexp(&uvmexp)) {
  111. return fmt_human(pagetok(uvmexp.npages,
  112. uvmexp.pageshift) * 1024,
  113. 1024);
  114. }
  115. return NULL;
  116. }
  117. const char *
  118. ram_used(void)
  119. {
  120. struct uvmexp uvmexp;
  121. if (load_uvmexp(&uvmexp)) {
  122. return fmt_human(pagetok(uvmexp.active,
  123. uvmexp.pageshift) * 1024,
  124. 1024);
  125. }
  126. return NULL;
  127. }
  128. #elif defined(__FreeBSD__)
  129. #include <sys/sysctl.h>
  130. #include <sys/vmmeter.h>
  131. #include <unistd.h>
  132. #include <vm/vm_param.h>
  133. const char *
  134. ram_free(void) {
  135. struct vmtotal vm_stats;
  136. int mib[] = {CTL_VM, VM_TOTAL};
  137. size_t len;
  138. len = sizeof(struct vmtotal);
  139. if (sysctl(mib, 2, &vm_stats, &len, NULL, 0) == -1
  140. || !len)
  141. return NULL;
  142. return fmt_human(vm_stats.t_free * getpagesize(), 1024);
  143. }
  144. const char *
  145. ram_total(void) {
  146. long npages;
  147. size_t len;
  148. len = sizeof(npages);
  149. if (sysctlbyname("vm.stats.vm.v_page_count", &npages, &len, NULL, 0) == -1
  150. || !len)
  151. return NULL;
  152. return fmt_human(npages * getpagesize(), 1024);
  153. }
  154. const char *
  155. ram_perc(void) {
  156. long npages;
  157. long active;
  158. size_t len;
  159. len = sizeof(npages);
  160. if (sysctlbyname("vm.stats.vm.v_page_count", &npages, &len, NULL, 0) == -1
  161. || !len)
  162. return NULL;
  163. if (sysctlbyname("vm.stats.vm.v_active_count", &active, &len, NULL, 0) == -1
  164. || !len)
  165. return NULL;
  166. return bprintf("%d", active * 100 / npages);
  167. }
  168. const char *
  169. ram_used(void) {
  170. long active;
  171. size_t len;
  172. len = sizeof(active);
  173. if (sysctlbyname("vm.stats.vm.v_active_count", &active, &len, NULL, 0) == -1
  174. || !len)
  175. return NULL;
  176. return fmt_human(active * getpagesize(), 1024);
  177. }
  178. #endif