1 // vim:ts=4:sw=4:expandtab
5 #include <yajl/yajl_gen.h>
6 #include <yajl/yajl_version.h>
9 #define BINARY_BASE UINT64_C(1024)
11 #define MAX_EXPONENT 4
12 static const char *const iec_symbols[MAX_EXPONENT + 1] = {"", "Ki", "Mi", "Gi", "Ti"};
14 static const char memoryfile_linux[] = "/proc/meminfo";
17 * Prints the given amount of bytes in a human readable manner.
20 static int print_bytes_human(char *outwalk, uint64_t bytes) {
23 int bin_base = BINARY_BASE;
24 while (size >= bin_base && exponent < MAX_EXPONENT) {
28 return sprintf(outwalk, "%.1f %sB", size, iec_symbols[exponent]);
32 * Convert a string to its absolute representation based on the total
33 * memory of `mem_total`.
35 * The string can contain any percentage values, which then return a
36 * the value of `size` in relation to `mem_total`.
37 * Alternatively an absolute value can be given, suffixed with an iec
41 static long memory_absolute(const long mem_total, const char *size) {
42 long mem_absolute = -1;
45 mem_absolute = strtol(size, &endptr, 10);
48 while (endptr[0] != '\0' && isspace(endptr[0]))
54 mem_absolute *= BINARY_BASE;
57 mem_absolute *= BINARY_BASE;
60 mem_absolute *= BINARY_BASE;
63 mem_absolute *= BINARY_BASE;
66 mem_absolute = mem_total * mem_absolute / 100;
76 void print_memory(yajl_gen json_gen, char *buffer, const char *format, const char *format_degraded, const char *threshold_degraded, const char *threshold_critical, const char *memory_used_method) {
77 char *outwalk = buffer;
80 const char *selected_format = format;
82 const char *output_color = NULL;
86 long ram_available = -1;
90 long ram_buffers = -1;
92 FILE *file = fopen(memoryfile_linux, "r");
97 while (fgets(line, sizeof line, file)) {
98 if (BEGINS_WITH(line, "MemTotal:")) {
99 ram_total = strtol(line + strlen("MemTotal:"), NULL, 10);
101 if (BEGINS_WITH(line, "MemFree:")) {
102 ram_free = strtol(line + strlen("MemFree:"), NULL, 10);
104 if (BEGINS_WITH(line, "MemAvailable:")) {
105 ram_available = strtol(line + strlen("MemAvailable:"), NULL, 10);
107 if (BEGINS_WITH(line, "Buffers:")) {
108 ram_buffers = strtol(line + strlen("Buffers:"), NULL, 10);
110 if (BEGINS_WITH(line, "Cached:")) {
111 ram_cached = strtol(line + strlen("Cached:"), NULL, 10);
113 if (BEGINS_WITH(line, "Shmem:")) {
114 ram_shared = strtol(line + strlen("Shmem:"), NULL, 10);
116 if (ram_total != -1 && ram_free != -1 && ram_available != -1 && ram_buffers != -1 && ram_cached != -1 && ram_shared != -1) {
122 if (ram_total == -1 || ram_free == -1 || ram_available == -1 || ram_buffers == -1 || ram_cached == -1 || ram_shared == -1) {
126 ram_total = ram_total * BINARY_BASE;
127 ram_free = ram_free * BINARY_BASE;
128 ram_available = ram_available * BINARY_BASE;
129 ram_buffers = ram_buffers * BINARY_BASE;
130 ram_cached = ram_cached * BINARY_BASE;
131 ram_shared = ram_shared * BINARY_BASE;
133 if (BEGINS_WITH(memory_used_method, "memavailable")) {
134 ram_used = ram_total - ram_available;
135 } else if (BEGINS_WITH(memory_used_method, "classical")) {
136 ram_used = ram_total - ram_free - ram_buffers - ram_cached;
139 if (threshold_degraded) {
140 long abs = memory_absolute(ram_total, threshold_degraded);
141 if (ram_available < abs) {
142 output_color = "color_degraded";
146 if (threshold_critical) {
147 long abs = memory_absolute(ram_total, threshold_critical);
148 if (ram_available < abs) {
149 output_color = "color_bad";
154 START_COLOR(output_color);
157 selected_format = format_degraded;
160 for (walk = selected_format; *walk != '\0'; walk++) {
162 *(outwalk++) = *walk;
165 if (BEGINS_WITH(walk + 1, "total")) {
166 outwalk += print_bytes_human(outwalk, ram_total);
167 walk += strlen("total");
170 if (BEGINS_WITH(walk + 1, "used")) {
171 outwalk += print_bytes_human(outwalk, ram_used);
172 walk += strlen("used");
175 if (BEGINS_WITH(walk + 1, "free")) {
176 outwalk += print_bytes_human(outwalk, ram_free);
177 walk += strlen("free");
180 if (BEGINS_WITH(walk + 1, "available")) {
181 outwalk += print_bytes_human(outwalk, ram_available);
182 walk += strlen("available");
185 if (BEGINS_WITH(walk + 1, "shared")) {
186 outwalk += print_bytes_human(outwalk, ram_shared);
187 walk += strlen("shared");
190 if (BEGINS_WITH(walk + 1, "percentage_free")) {
191 outwalk += sprintf(outwalk, "%.01f%s", 100.0 * ram_free / ram_total, pct_mark);
192 walk += strlen("percentage_free");
195 if (BEGINS_WITH(walk + 1, "percentage_available")) {
196 outwalk += sprintf(outwalk, "%.01f%s", 100.0 * ram_available / ram_total, pct_mark);
197 walk += strlen("percentage_available");
200 if (BEGINS_WITH(walk + 1, "percentage_used")) {
201 outwalk += sprintf(outwalk, "%.01f%s", 100.0 * ram_used / ram_total, pct_mark);
202 walk += strlen("percentage_used");
205 if (BEGINS_WITH(walk + 1, "percentage_shared")) {
206 outwalk += sprintf(outwalk, "%.01f%s", 100.0 * ram_shared / ram_total, pct_mark);
207 walk += strlen("percentage_shared");
215 OUTPUT_FULL_TEXT(buffer);
219 OUTPUT_FULL_TEXT("can't read memory");
220 fputs("i3status: Cannot read system memory using /proc/meminfo\n", stderr);
222 OUTPUT_FULL_TEXT("");
223 fputs("i3status: Memory status information is not supported on this system\n", stderr);