1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include "../block.h"
#include "../format.h"
#include "../any_log.h"
typedef struct {
block_text_t block;
format_t format;
} block_ram_t;
typedef enum {
RAM_STRING = 0,
RAM_TOTAL,
RAM_FREE,
RAM_USED,
RAM_FREE_PERC,
RAM_USED_PERC,
} block_ram_mark_t;
#define RAM_SHIFT 12
static const format_pair_t block_ram_units[] = {
{ "-si", UNIT_SI << RAM_SHIFT },
{ "-b", UNIT_B << RAM_SHIFT },
{ "-kib", UNIT_KB << RAM_SHIFT },
{ "-mib", UNIT_MB << RAM_SHIFT },
{ "-gib", UNIT_GB << RAM_SHIFT },
{ "-tib", UNIT_TB << RAM_SHIFT },
{ "-kb", (UNIT_KB | UNIT_SI) << RAM_SHIFT },
{ "-mb", (UNIT_MB | UNIT_SI) << RAM_SHIFT },
{ "-gb", (UNIT_GB | UNIT_SI) << RAM_SHIFT },
{ "-tb", (UNIT_TB | UNIT_SI) << RAM_SHIFT },
{ NULL },
};
static const format_option_t block_ram_options[] = {
{ { "total", RAM_TOTAL }, .suffixes = block_ram_units },
{ { "free", RAM_FREE }, .suffixes = block_ram_units },
{ { "used", RAM_USED }, .suffixes = block_ram_units },
{ { "free-percentage", RAM_FREE_PERC } },
{ { "used-percentage", RAM_USED_PERC } },
{ { NULL } },
};
static const struct timespec block_ram_interval = {
.tv_sec = 1,
.tv_nsec = 0,
};
static void block_ram_update(block_t *block)
{
const char *path = "/proc/meminfo";
FILE *meminfo = fopen(path, "rb");
if (meminfo == NULL) {
log_value_debug("Failed to open meminfo",
"s:label", block->label,
"s:path", path,
"i:errno", errno);
return;
}
uintmax_t total, unused, available, buffers, cached;
int n = fscanf(meminfo,
"MemTotal: %ju kB\n"
"MemFree: %ju kB\n"
"MemAvailable: %ju kB\n"
"Buffers: %ju kB\n"
"Cached: %ju kB\n",
&total, &unused, &available, &buffers, &cached);
fclose(meminfo);
if (n != 5) {
log_value_debug("Failed to read meminfo",
"s:label", block->label,
"i:errno", errno);
return;
}
block_ram_t *ram = (block_ram_t *)block;
char buffer[256] = { 0 };
size_t start = 0, size = sizeof(buffer);
for (size_t i = 0; i < ram->format.length; i++) {
size_t rest = start >= size ? 0 : size - start;
if (rest == 0) break;
unit_t mask = UNIT_MASK << RAM_SHIFT;
unit_t unit = (ram->format.parts[i].mark & mask) >> RAM_SHIFT;
switch (ram->format.parts[i].mark & ~mask) {
case RAM_STRING:
start += snprintf(buffer + start, rest, "%s", ram->format.parts[i].string);
break;
case RAM_TOTAL:
start += snprintf_units(buffer + start, rest, total * 1024, unit);
break;
case RAM_FREE:
start += snprintf_units(buffer + start, rest, available * 1024, unit);
break;
case RAM_USED:
start += snprintf_units(buffer + start, rest, (total - available) * 1024, unit);
break;
case RAM_FREE_PERC:
start += snprintf(buffer + start, rest, "%ld", 100 * available / total);
break;
case RAM_USED_PERC:
start += snprintf(buffer + start, rest, "%ld", 100 * (total - available) / total);
break;
default:
unreachable();
}
}
free(ram->block.text);
ram->block.text = strcopy(buffer);
assert(ram->block.text != NULL);
}
static void block_ram_init(block_t *block, const block_scheme_t *scheme)
{
extern const block_scheme_t block_text_scheme;
block_text_scheme.init_fn(block, scheme);
block->update_interval = block_ram_interval;
block->update_fn = block_ram_update;
}
static void block_ram_clean(block_t *block)
{
block_ram_t *ram = (block_ram_t *)block;
format_free(&ram->format);
free(ram->block.text);
}
static int block_ram_validate(block_t *block, const block_scheme_t *scheme)
{
block_ram_t *ram = (block_ram_t *)block;
int errors = 0;
if (ram->block.text == NULL) {
log_error("Block '%s' requires key '%s'", block->label, "text");
errors++;
}
if (!format_init(&ram->format, ram->block.text, '%')) {
log_error("Block '%s' has an invalid format", block->label);
errors++;
} else {
int marked = format_remark(&ram->format, block->label, block_ram_options);
if (marked < 0)
errors += -marked;
if (marked == 0) {
log_warn("Block '%s' does not use any ram option", block->label);
block->update_fn = NULL;
log_debug("Disabled updates for block '%s'", block->label);
}
}
return errors;
}
const block_scheme_t block_ram_scheme = {
.name = "ram",
.entries = NULL,
.size = sizeof(block_ram_t),
.init_fn = block_ram_init,
.clean_fn = block_ram_clean,
.validate_fn = block_ram_validate,
};
|