slstatus

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

ram.c (4859B)


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