ram.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* See LICENSE file for copyright and license details. */
  2. #include <stdio.h>
  3. #include "../slstatus.h"
  4. #include "../util.h"
  5. #if defined(__linux__)
  6. #include <stdint.h>
  7. const char *
  8. ram_free(const char *unused)
  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. return fmt_human(free * 1024, 1024);
  18. }
  19. const char *
  20. ram_perc(const char *unused)
  21. {
  22. uintmax_t total, free, buffers, cached;
  23. int percent;
  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. if (total == 0)
  33. return NULL;
  34. percent = 100 * ((total - free) - (buffers + cached)) / total;
  35. return bprintf("%d", percent);
  36. }
  37. const char *
  38. ram_total(const char *unused)
  39. {
  40. uintmax_t total;
  41. if (pscanf("/proc/meminfo", "MemTotal: %ju kB\n", &total)
  42. != 1)
  43. return NULL;
  44. return fmt_human(total * 1024, 1024);
  45. }
  46. const char *
  47. ram_used(const char *unused)
  48. {
  49. uintmax_t total, free, buffers, cached, used;
  50. if (pscanf("/proc/meminfo",
  51. "MemTotal: %ju kB\n"
  52. "MemFree: %ju kB\n"
  53. "MemAvailable: %ju kB\n"
  54. "Buffers: %ju kB\n"
  55. "Cached: %ju kB\n",
  56. &total, &free, &buffers, &buffers, &cached) != 5)
  57. return NULL;
  58. used = (total - free - buffers - cached);
  59. return fmt_human(used * 1024, 1024);
  60. }
  61. #elif defined(__OpenBSD__)
  62. #include <stdlib.h>
  63. #include <sys/sysctl.h>
  64. #include <sys/types.h>
  65. #include <unistd.h>
  66. #define LOG1024 10
  67. #define pagetok(size, pageshift) (size_t)(size << (pageshift - LOG1024))
  68. inline int
  69. load_uvmexp(struct uvmexp *uvmexp)
  70. {
  71. int uvmexp_mib[] = {CTL_VM, VM_UVMEXP};
  72. size_t size;
  73. size = sizeof(*uvmexp);
  74. if (sysctl(uvmexp_mib, 2, uvmexp, &size, NULL, 0) >= 0)
  75. return 1;
  76. return 0;
  77. }
  78. const char *
  79. ram_free(const char *unused)
  80. {
  81. struct uvmexp uvmexp;
  82. int free_pages;
  83. if (!load_uvmexp(&uvmexp))
  84. return NULL;
  85. free_pages = uvmexp.npages - uvmexp.active;
  86. return fmt_human(pagetok(free_pages, uvmexp.pageshift) *
  87. 1024, 1024);
  88. }
  89. const char *
  90. ram_perc(const char *unused)
  91. {
  92. struct uvmexp uvmexp;
  93. int percent;
  94. if (!load_uvmexp(&uvmexp))
  95. return NULL;
  96. percent = uvmexp.active * 100 / uvmexp.npages;
  97. return bprintf("%d", percent);
  98. }
  99. const char *
  100. ram_total(const char *unused)
  101. {
  102. struct uvmexp uvmexp;
  103. if (!load_uvmexp(&uvmexp))
  104. return NULL;
  105. return fmt_human(pagetok(uvmexp.npages,
  106. uvmexp.pageshift) * 1024, 1024);
  107. }
  108. const char *
  109. ram_used(const char *unused)
  110. {
  111. struct uvmexp uvmexp;
  112. if (!load_uvmexp(&uvmexp))
  113. return NULL;
  114. return fmt_human(pagetok(uvmexp.active,
  115. uvmexp.pageshift) * 1024, 1024);
  116. }
  117. #elif defined(__FreeBSD__)
  118. #include <sys/sysctl.h>
  119. #include <sys/vmmeter.h>
  120. #include <unistd.h>
  121. #include <vm/vm_param.h>
  122. const char *
  123. ram_free(const char *unused) {
  124. struct vmtotal vm_stats;
  125. int mib[] = {CTL_VM, VM_TOTAL};
  126. size_t len;
  127. len = sizeof(struct vmtotal);
  128. if (sysctl(mib, 2, &vm_stats, &len, NULL, 0) < 0
  129. || !len)
  130. return NULL;
  131. return fmt_human(vm_stats.t_free * getpagesize(), 1024);
  132. }
  133. const char *
  134. ram_total(const char *unused) {
  135. unsigned int npages;
  136. size_t len;
  137. len = sizeof(npages);
  138. if (sysctlbyname("vm.stats.vm.v_page_count",
  139. &npages, &len, NULL, 0) < 0 || !len)
  140. return NULL;
  141. return fmt_human(npages * getpagesize(), 1024);
  142. }
  143. const char *
  144. ram_perc(const char *unused) {
  145. unsigned int npages;
  146. unsigned int active;
  147. size_t len;
  148. len = sizeof(npages);
  149. if (sysctlbyname("vm.stats.vm.v_page_count",
  150. &npages, &len, NULL, 0) < 0 || !len)
  151. return NULL;
  152. if (sysctlbyname("vm.stats.vm.v_active_count",
  153. &active, &len, NULL, 0) < 0 || !len)
  154. return NULL;
  155. return bprintf("%d", active * 100 / npages);
  156. }
  157. const char *
  158. ram_used(const char *unused) {
  159. unsigned int active;
  160. size_t len;
  161. len = sizeof(active);
  162. if (sysctlbyname("vm.stats.vm.v_active_count",
  163. &active, &len, NULL, 0) < 0 || !len)
  164. return NULL;
  165. return fmt_human(active * getpagesize(), 1024);
  166. }
  167. #elif defined(__NetBSD__)
  168. #include <stdlib.h>
  169. #include <sys/sysctl.h>
  170. #include <sys/types.h>
  171. #include <unistd.h>
  172. #include <uvm/uvm_extern.h>
  173. #define LOG1024 10
  174. #define pagetok(size, pageshift) (size_t)(size << (pageshift - LOG1024))
  175. int
  176. load_uvmexp(struct uvmexp_sysctl *uvmexp)
  177. {
  178. int uvmexp_mib[] = {CTL_VM, VM_UVMEXP2};
  179. size_t size;
  180. size = sizeof(*uvmexp);
  181. if (sysctl(uvmexp_mib, 2, uvmexp, &size, NULL, 0) >= 0)
  182. return 1;
  183. return 0;
  184. }
  185. const char *
  186. ram_free(const char *unused)
  187. {
  188. struct uvmexp_sysctl uvmexp;
  189. int free_pages;
  190. if (!load_uvmexp(&uvmexp))
  191. return NULL;
  192. free_pages = uvmexp.npages - uvmexp.active;
  193. return fmt_human(pagetok(free_pages, uvmexp.pageshift) *
  194. 1024, 1024);
  195. }
  196. const char *
  197. ram_perc(const char *unused)
  198. {
  199. struct uvmexp_sysctl uvmexp;
  200. int percent;
  201. if (!load_uvmexp(&uvmexp))
  202. return NULL;
  203. percent = uvmexp.active * 100 / uvmexp.npages;
  204. return bprintf("%d", percent);
  205. }
  206. const char *
  207. ram_total(const char *unused)
  208. {
  209. struct uvmexp_sysctl uvmexp;
  210. if (!load_uvmexp(&uvmexp))
  211. return NULL;
  212. return fmt_human(pagetok(uvmexp.npages,
  213. uvmexp.pageshift) * 1024, 1024);
  214. }
  215. const char *
  216. ram_used(const char *unused)
  217. {
  218. struct uvmexp_sysctl uvmexp;
  219. if (!load_uvmexp(&uvmexp))
  220. return NULL;
  221. return fmt_human(pagetok(uvmexp.active,
  222. uvmexp.pageshift) * 1024, 1024);
  223. }
  224. #endif