slstatus

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

ram.c (4408B)


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