My slstatus configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

223 line
4.2 KiB

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