slstatus

status monitor
git clone git://git.suckless.org/slstatus
Log | Files | Refs | README | LICENSE

ram.c (4281B)


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