| 1 | #include "memory.h" |
| 2 | #include "common.h" |
| 3 | |
| 4 | #include <stdio.h> |
| 5 | |
| 6 | #if defined(__OpenBSD__) |
| 7 | #include <sys/types.h> |
| 8 | #include <sys/sysctl.h> |
| 9 | |
| 10 | void display_memory() { |
| 11 | int mib[2]; |
| 12 | long long memtotal; |
| 13 | size_t len = sizeof(memtotal); |
| 14 | |
| 15 | mib[0] = CTL_HW; |
| 16 | mib[1] = HW_PHYSMEM64; |
| 17 | if (sysctl(mib, 2, &memtotal, &len, NULL, 0) != 0) { |
| 18 | perror("sysctl"); |
| 19 | return; |
| 20 | } |
| 21 | |
| 22 | struct uvmexp uvm; |
| 23 | mib[0] = CTL_VM; |
| 24 | mib[1] = VM_UVMEXP; |
| 25 | len = sizeof(uvm); |
| 26 | if (sysctl(mib, 2, &uvm, &len, NULL, 0) != 0) { |
| 27 | perror("sysctl"); |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | #define pgtok(a) ((a) * ((unsigned int)uvm.pagesize >> 10)) |
| 32 | |
| 33 | unsigned int memused = pgtok(uvm.active + uvm.swpginuse) / 1024; |
| 34 | |
| 35 | printf("%u MiB / %lld MiB", memused, memtotal / 1024LL / 1024LL); |
| 36 | } |
| 37 | #else |
| 38 | void display_memory() { |
| 39 | long long int memused = 0; |
| 40 | long long int memtotal = 0; |
| 41 | #if defined(__FreeBSD__) || defined(__DragonFly__) |
| 42 | |
| 43 | long long int used1 = run_command_lld("top | grep \"Mem:\" | awk '{print $2}' | " |
| 44 | "sed 's/M//'"); |
| 45 | long long int used2 = run_command_lld("top | grep \"Mem:\" | awk '{print $4}' | " |
| 46 | "sed 's/M//'"); |
| 47 | long long int used3 = run_command_lld("top | grep \"Mem:\" | awk '{print $6}' | " |
| 48 | "sed 's/M//'"); |
| 49 | long long int used4 = run_command_lld("top | grep \"ARC:\" | awk '{print $6}' | " |
| 50 | "sed 's/M//'"); |
| 51 | memused = used1 + used2 + used3 + used4; |
| 52 | memtotal = run_command_lld("sysctl -n hw.physmem") / 1024LL / 1024LL; |
| 53 | #elif defined(__NetBSD__) |
| 54 | memused = run_command_lld("top | grep \"Memory:\" | awk '{print $2}' | " |
| 55 | "sed 's/M//'"); |
| 56 | memtotal = run_command_lld("sysctl -n hw.physmem64") / 1024LL / 1024LL; |
| 57 | #elif defined(__sun) |
| 58 | |
| 59 | memused = run_command_lld("top | grep \"Memory:\" | awk '{print $5}' | " |
| 60 | "sed 's/M//'"); |
| 61 | memtotal = run_command_lld("top | grep \"Memory:\" | awk '{print $2}' | " |
| 62 | "sed 's/M//'"); |
| 63 | |
| 64 | #elif defined(__minix) |
| 65 | memtotal = run_command_lld("sysctl -n hw.physmem") / 1024LL / 1024LL; |
| 66 | #elif defined(__linux__) |
| 67 | long long int memaval = run_command_lld("cat /proc/meminfo | grep MemAvailable | " |
| 68 | "awk '{print $2}'") / 1024LL; |
| 69 | memtotal = run_command_lld("cat /proc/meminfo | grep MemTotal | " |
| 70 | "awk '{print $2}'") / 1024LL; |
| 71 | memused = memtotal - memaval; |
| 72 | |
| 73 | |
| 74 | #elif defined(__APPLE__) |
| 75 | memtotal = run_command_lld("sysctl -n hw.memsize") / 1024LL / 1024LL; |
| 76 | long long int memwired = run_command_lld("vm_stat | awk '/ wired/ { print $4 }'"); |
| 77 | long long int memactive = run_command_lld("vm_stat | awk '/ active/ { printf $3 }'"); |
| 78 | long long int memcompressed = run_command_lld("vm_stat | " |
| 79 | "awk '/ occupied/ { printf $5 }'"); |
| 80 | memused = (memwired + memactive + memcompressed) * 4LL / 1024LL; |
| 81 | #elif defined(__HAIKU__) |
| 82 | memtotal = run_command_lld("vmstat | awk '/max memory:/ {max=$3} END {print max/1024/1024}'"); |
| 83 | long long int memfree = run_command_lld("vmstat | awk '/free memory:/ {free=$3} END {print free/1024/1024}'"); |
| 84 | memused = memtotal - memfree; |
| 85 | #endif |
| 86 | |
| 87 | printf("%lld MiB / %lld MiB", memused, memtotal); |
| 88 | } |
| 89 | #endif |