]>
Commit | Line | Data |
---|---|---|
cad2d1ac HC |
1 | /* |
2 | * lsmem - Show memory configuration | |
3 | * | |
4 | * Copyright IBM Corp. 2016 | |
a5c4535b | 5 | * Copyright (C) 2016 Karel Zak <kzak@redhat.com> |
cad2d1ac HC |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it would be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License along | |
18 | * with this program; if not, write to the Free Software Foundation, Inc., | |
19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
20 | */ | |
cad2d1ac HC |
21 | #include <c.h> |
22 | #include <nls.h> | |
23 | #include <path.h> | |
24 | #include <strutils.h> | |
25 | #include <closestream.h> | |
26 | #include <xalloc.h> | |
27 | #include <getopt.h> | |
28 | #include <stdio.h> | |
29 | #include <stdlib.h> | |
30 | #include <dirent.h> | |
cad2d1ac HC |
31 | #include <fcntl.h> |
32 | #include <inttypes.h> | |
33 | #include <assert.h> | |
34 | #include <optutils.h> | |
35 | #include <libsmartcols.h> | |
36 | ||
37 | #define _PATH_SYS_MEMORY "/sys/devices/system/memory" | |
cad2d1ac HC |
38 | |
39 | #define MEMORY_STATE_ONLINE 0 | |
40 | #define MEMORY_STATE_OFFLINE 1 | |
41 | #define MEMORY_STATE_GOING_OFFLINE 2 | |
42 | #define MEMORY_STATE_UNKNOWN 3 | |
43 | ||
60a7e9e9 GS |
44 | enum zone_id { |
45 | ZONE_DMA = 0, | |
46 | ZONE_DMA32, | |
47 | ZONE_NORMAL, | |
48 | ZONE_HIGHMEM, | |
49 | ZONE_MOVABLE, | |
50 | ZONE_DEVICE, | |
51 | ZONE_NONE, | |
52 | ZONE_UNKNOWN, | |
53 | MAX_NR_ZONES, | |
54 | }; | |
55 | ||
cad2d1ac HC |
56 | struct memory_block { |
57 | uint64_t index; | |
58 | uint64_t count; | |
59 | int state; | |
60 | int node; | |
60a7e9e9 GS |
61 | int nr_zones; |
62 | int zones[MAX_NR_ZONES]; | |
cad2d1ac HC |
63 | unsigned int removable:1; |
64 | }; | |
65 | ||
0cf0710a | 66 | struct lsmem { |
e4319a10 | 67 | struct path_cxt *sysmem; /* _PATH_SYS_MEMORY directory handler */ |
cad2d1ac HC |
68 | struct dirent **dirs; |
69 | int ndirs; | |
70 | struct memory_block *blocks; | |
71 | int nblocks; | |
cad2d1ac HC |
72 | uint64_t block_size; |
73 | uint64_t mem_online; | |
74 | uint64_t mem_offline; | |
cad2d1ac | 75 | |
0a1ed6df KZ |
76 | struct libscols_table *table; |
77 | unsigned int have_nodes : 1, | |
78 | raw : 1, | |
79 | export : 1, | |
80 | json : 1, | |
81 | noheadings : 1, | |
82 | summary : 1, | |
83 | list_all : 1, | |
84 | bytes : 1, | |
c5332fbb | 85 | want_summary : 1, |
60a7e9e9 | 86 | want_table : 1, |
d12e945d KZ |
87 | split_by_node : 1, |
88 | split_by_state : 1, | |
89 | split_by_removable : 1, | |
90 | split_by_zones : 1, | |
60a7e9e9 | 91 | have_zones : 1; |
cad2d1ac HC |
92 | }; |
93 | ||
d12e945d | 94 | |
cad2d1ac HC |
95 | enum { |
96 | COL_RANGE, | |
97 | COL_SIZE, | |
98 | COL_STATE, | |
99 | COL_REMOVABLE, | |
100 | COL_BLOCK, | |
101 | COL_NODE, | |
60a7e9e9 GS |
102 | COL_ZONES, |
103 | }; | |
104 | ||
105 | static char *zone_names[] = { | |
106 | [ZONE_DMA] = "DMA", | |
107 | [ZONE_DMA32] = "DMA32", | |
108 | [ZONE_NORMAL] = "Normal", | |
109 | [ZONE_HIGHMEM] = "Highmem", | |
110 | [ZONE_MOVABLE] = "Movable", | |
111 | [ZONE_DEVICE] = "Device", | |
112 | [ZONE_NONE] = "None", /* block contains more than one zone, can't be offlined */ | |
113 | [ZONE_UNKNOWN] = "Unknown", | |
cad2d1ac HC |
114 | }; |
115 | ||
0a1ed6df KZ |
116 | /* column names */ |
117 | struct coldesc { | |
118 | const char *name; /* header */ | |
119 | double whint; /* width hint (N < 1 is in percent of termwidth) */ | |
120 | int flags; /* SCOLS_FL_* */ | |
121 | const char *help; | |
cad2d1ac HC |
122 | }; |
123 | ||
0a1ed6df KZ |
124 | /* columns descriptions */ |
125 | static struct coldesc coldescs[] = { | |
4775cc69 KZ |
126 | [COL_RANGE] = { "RANGE", 0, 0, N_("start and end address of the memory range")}, |
127 | [COL_SIZE] = { "SIZE", 5, SCOLS_FL_RIGHT, N_("size of the memory range")}, | |
4c0e1eaf | 128 | [COL_STATE] = { "STATE", 0, SCOLS_FL_RIGHT, N_("online status of the memory range")}, |
0a1ed6df | 129 | [COL_REMOVABLE] = { "REMOVABLE", 0, SCOLS_FL_RIGHT, N_("memory is removable")}, |
4775cc69 KZ |
130 | [COL_BLOCK] = { "BLOCK", 0, SCOLS_FL_RIGHT, N_("memory block number or blocks range")}, |
131 | [COL_NODE] = { "NODE", 0, SCOLS_FL_RIGHT, N_("numa node of memory")}, | |
60a7e9e9 | 132 | [COL_ZONES] = { "ZONES", 0, SCOLS_FL_RIGHT, N_("valid zones for the memory range")}, |
cad2d1ac HC |
133 | }; |
134 | ||
0a1ed6df KZ |
135 | /* columns[] array specifies all currently wanted output column. The columns |
136 | * are defined by coldescs[] array and you can specify (on command line) each | |
137 | * column twice. That's enough, dynamically allocated array of the columns is | |
138 | * unnecessary overkill and over-engineering in this case */ | |
139 | static int columns[ARRAY_SIZE(coldescs) * 2]; | |
140 | static size_t ncolumns; | |
141 | ||
142 | static inline size_t err_columns_index(size_t arysz, size_t idx) | |
143 | { | |
144 | if (idx >= arysz) | |
145 | errx(EXIT_FAILURE, _("too many columns specified, " | |
146 | "the limit is %zu columns"), | |
147 | arysz - 1); | |
148 | return idx; | |
149 | } | |
150 | ||
60a7e9e9 GS |
151 | /* |
152 | * name must be null-terminated | |
153 | */ | |
154 | static int zone_name_to_id(const char *name) | |
155 | { | |
156 | size_t i; | |
157 | ||
158 | for (i = 0; i < ARRAY_SIZE(zone_names); i++) { | |
159 | if (!strcasecmp(name, zone_names[i])) | |
160 | return i; | |
161 | } | |
162 | return ZONE_UNKNOWN; | |
163 | } | |
164 | ||
0a1ed6df KZ |
165 | #define add_column(ary, n, id) \ |
166 | ((ary)[ err_columns_index(ARRAY_SIZE(ary), (n)) ] = (id)) | |
167 | ||
cad2d1ac HC |
168 | static int column_name_to_id(const char *name, size_t namesz) |
169 | { | |
170 | size_t i; | |
171 | ||
172 | for (i = 0; i < ARRAY_SIZE(coldescs); i++) { | |
173 | const char *cn = coldescs[i].name; | |
174 | ||
175 | if (!strncasecmp(name, cn, namesz) && !*(cn + namesz)) | |
176 | return i; | |
177 | } | |
178 | warnx(_("unknown column: %s"), name); | |
179 | return -1; | |
180 | } | |
181 | ||
0a1ed6df | 182 | static inline int get_column_id(int num) |
cad2d1ac | 183 | { |
0a1ed6df KZ |
184 | assert(num >= 0); |
185 | assert((size_t) num < ncolumns); | |
186 | assert(columns[num] < (int) ARRAY_SIZE(coldescs)); | |
187 | ||
188 | return columns[num]; | |
cad2d1ac HC |
189 | } |
190 | ||
0a1ed6df | 191 | static inline struct coldesc *get_column_desc(int num) |
cad2d1ac | 192 | { |
0a1ed6df | 193 | return &coldescs[ get_column_id(num) ]; |
cad2d1ac HC |
194 | } |
195 | ||
d12e945d | 196 | static inline void reset_split_policy(struct lsmem *l, int enable) |
cad2d1ac | 197 | { |
d12e945d KZ |
198 | l->split_by_state = enable; |
199 | l->split_by_node = enable; | |
200 | l->split_by_removable = enable; | |
201 | l->split_by_zones = enable; | |
cad2d1ac HC |
202 | } |
203 | ||
96cbe362 KZ |
204 | static void set_split_policy(struct lsmem *l, int cols[], size_t ncols) |
205 | { | |
206 | size_t i; | |
207 | ||
208 | reset_split_policy(l, 0); | |
209 | ||
210 | for (i = 0; i < ncols; i++) { | |
211 | switch (cols[i]) { | |
212 | case COL_STATE: | |
213 | l->split_by_state = 1; | |
214 | break; | |
215 | case COL_NODE: | |
216 | l->split_by_node = 1; | |
217 | break; | |
218 | case COL_REMOVABLE: | |
219 | l->split_by_removable = 1; | |
220 | break; | |
221 | case COL_ZONES: | |
222 | l->split_by_zones = 1; | |
223 | break; | |
224 | default: | |
225 | break; | |
226 | } | |
227 | } | |
228 | } | |
229 | ||
0cf0710a | 230 | static void add_scols_line(struct lsmem *lsmem, struct memory_block *blk) |
cad2d1ac | 231 | { |
0a1ed6df KZ |
232 | size_t i; |
233 | struct libscols_line *line; | |
cad2d1ac | 234 | |
0cf0710a | 235 | line = scols_table_new_line(lsmem->table, NULL); |
0a1ed6df KZ |
236 | if (!line) |
237 | err_oom(); | |
cad2d1ac | 238 | |
0a1ed6df KZ |
239 | for (i = 0; i < ncolumns; i++) { |
240 | char *str = NULL; | |
cad2d1ac | 241 | |
0a1ed6df KZ |
242 | switch (get_column_id(i)) { |
243 | case COL_RANGE: | |
244 | { | |
0cf0710a KZ |
245 | uint64_t start = blk->index * lsmem->block_size; |
246 | uint64_t size = blk->count * lsmem->block_size; | |
0a1ed6df KZ |
247 | xasprintf(&str, "0x%016"PRIx64"-0x%016"PRIx64, start, start + size - 1); |
248 | break; | |
249 | } | |
250 | case COL_SIZE: | |
0cf0710a KZ |
251 | if (lsmem->bytes) |
252 | xasprintf(&str, "%"PRId64, (uint64_t) blk->count * lsmem->block_size); | |
0a1ed6df KZ |
253 | else |
254 | str = size_to_human_string(SIZE_SUFFIX_1LETTER, | |
0cf0710a | 255 | (uint64_t) blk->count * lsmem->block_size); |
0a1ed6df KZ |
256 | break; |
257 | case COL_STATE: | |
258 | str = xstrdup( | |
259 | blk->state == MEMORY_STATE_ONLINE ? _("online") : | |
260 | blk->state == MEMORY_STATE_OFFLINE ? _("offline") : | |
261 | blk->state == MEMORY_STATE_GOING_OFFLINE ? _("on->off") : | |
262 | "?"); | |
263 | break; | |
264 | case COL_REMOVABLE: | |
265 | if (blk->state == MEMORY_STATE_ONLINE) | |
266 | str = xstrdup(blk->removable ? _("yes") : _("no")); | |
267 | break; | |
268 | case COL_BLOCK: | |
269 | if (blk->count == 1) | |
270 | xasprintf(&str, "%"PRId64, blk->index); | |
271 | else | |
272 | xasprintf(&str, "%"PRId64"-%"PRId64, | |
273 | blk->index, blk->index + blk->count - 1); | |
274 | break; | |
275 | case COL_NODE: | |
0cf0710a | 276 | if (lsmem->have_nodes) |
0a1ed6df KZ |
277 | xasprintf(&str, "%d", blk->node); |
278 | break; | |
60a7e9e9 GS |
279 | case COL_ZONES: |
280 | if (lsmem->have_zones) { | |
281 | char valid_zones[BUFSIZ]; | |
282 | int j, zone_id; | |
283 | ||
284 | valid_zones[0] = '\0'; | |
285 | for (j = 0; j < blk->nr_zones; j++) { | |
286 | zone_id = blk->zones[j]; | |
287 | if (strlen(valid_zones) + | |
288 | strlen(zone_names[zone_id]) > BUFSIZ - 2) | |
289 | break; | |
290 | strcat(valid_zones, zone_names[zone_id]); | |
291 | if (j + 1 < blk->nr_zones) | |
292 | strcat(valid_zones, "/"); | |
293 | } | |
294 | str = xstrdup(valid_zones); | |
92368ce1 | 295 | } |
60a7e9e9 | 296 | break; |
cad2d1ac | 297 | } |
0a1ed6df KZ |
298 | |
299 | if (str && scols_line_refer_data(line, i, str) != 0) | |
300 | err_oom(); | |
cad2d1ac | 301 | } |
cad2d1ac HC |
302 | } |
303 | ||
0cf0710a | 304 | static void fill_scols_table(struct lsmem *lsmem) |
0a1ed6df KZ |
305 | { |
306 | int i; | |
307 | ||
0cf0710a KZ |
308 | for (i = 0; i < lsmem->nblocks; i++) |
309 | add_scols_line(lsmem, &lsmem->blocks[i]); | |
0a1ed6df KZ |
310 | } |
311 | ||
0cf0710a | 312 | static void print_summary(struct lsmem *lsmem) |
cad2d1ac | 313 | { |
c5332fbb KZ |
314 | if (lsmem->bytes) { |
315 | printf("%-23s %15"PRId64"\n",_("Memory block size:"), lsmem->block_size); | |
316 | printf("%-23s %15"PRId64"\n",_("Total online memory:"), lsmem->mem_online); | |
317 | printf("%-23s %15"PRId64"\n",_("Total offline memory:"), lsmem->mem_offline); | |
318 | } else { | |
8e7a0c16 KZ |
319 | char *p; |
320 | ||
321 | if ((p = size_to_human_string(SIZE_SUFFIX_1LETTER, lsmem->block_size))) | |
322 | printf("%-23s %5s\n",_("Memory block size:"), p); | |
323 | free(p); | |
324 | ||
325 | if ((p = size_to_human_string(SIZE_SUFFIX_1LETTER, lsmem->mem_online))) | |
326 | printf("%-23s %5s\n",_("Total online memory:"), p); | |
327 | free(p); | |
328 | ||
329 | if ((p = size_to_human_string(SIZE_SUFFIX_1LETTER, lsmem->mem_offline))) | |
330 | printf("%-23s %5s\n",_("Total offline memory:"), p); | |
331 | free(p); | |
c5332fbb | 332 | } |
cad2d1ac HC |
333 | } |
334 | ||
e4319a10 | 335 | static int memory_block_get_node(struct lsmem *lsmem, char *name) |
cad2d1ac HC |
336 | { |
337 | struct dirent *de; | |
cad2d1ac HC |
338 | DIR *dir; |
339 | int node; | |
340 | ||
e4319a10 KZ |
341 | dir = ul_path_opendir(lsmem->sysmem, name); |
342 | if (!dir) | |
343 | err(EXIT_FAILURE, _("Failed to open %s"), name); | |
81435af3 | 344 | |
cad2d1ac HC |
345 | node = -1; |
346 | while ((de = readdir(dir)) != NULL) { | |
ad296391 | 347 | if (strncmp("node", de->d_name, 4) != 0) |
cad2d1ac HC |
348 | continue; |
349 | if (!isdigit_string(de->d_name + 4)) | |
350 | continue; | |
fe4e122a | 351 | errno = 0; |
cad2d1ac | 352 | node = strtol(de->d_name + 4, NULL, 10); |
fe4e122a KZ |
353 | if (errno) |
354 | continue; | |
51a90159 | 355 | break; |
cad2d1ac HC |
356 | } |
357 | closedir(dir); | |
358 | return node; | |
359 | } | |
360 | ||
fe4e122a | 361 | static int memory_block_read_attrs(struct lsmem *lsmem, char *name, |
cad2d1ac HC |
362 | struct memory_block *blk) |
363 | { | |
e4319a10 | 364 | char *line = NULL; |
fe4e122a | 365 | int i, x = 0, rc = 0; |
e4319a10 KZ |
366 | |
367 | memset(blk, 0, sizeof(*blk)); | |
cad2d1ac | 368 | |
fe4e122a | 369 | errno = 0; |
cad2d1ac | 370 | blk->count = 1; |
cad2d1ac | 371 | blk->state = MEMORY_STATE_UNKNOWN; |
e4319a10 | 372 | blk->index = strtoumax(name + 6, NULL, 10); /* get <num> of "memory<num>" */ |
9c41d270 | 373 | |
fe4e122a KZ |
374 | if (errno) |
375 | rc = -errno; | |
376 | ||
e4319a10 KZ |
377 | if (ul_path_readf_s32(lsmem->sysmem, &x, "%s/removable", name) == 0) |
378 | blk->removable = x == 1; | |
379 | ||
4397707e | 380 | if (ul_path_readf_string(lsmem->sysmem, &line, "%s/state", name) > 0 && line) { |
e4319a10 KZ |
381 | if (strcmp(line, "offline") == 0) |
382 | blk->state = MEMORY_STATE_OFFLINE; | |
383 | else if (strcmp(line, "online") == 0) | |
384 | blk->state = MEMORY_STATE_ONLINE; | |
385 | else if (strcmp(line, "going-offline") == 0) | |
386 | blk->state = MEMORY_STATE_GOING_OFFLINE; | |
387 | free(line); | |
388 | } | |
9c41d270 | 389 | |
0cf0710a | 390 | if (lsmem->have_nodes) |
e4319a10 | 391 | blk->node = memory_block_get_node(lsmem, name); |
60a7e9e9 GS |
392 | |
393 | blk->nr_zones = 0; | |
4397707e KZ |
394 | if (lsmem->have_zones |
395 | && ul_path_readf_string(lsmem->sysmem, &line, "%s/valid_zones", name) > 0 | |
396 | && line) { | |
e4319a10 KZ |
397 | |
398 | char *token = strtok(line, " "); | |
399 | ||
400 | for (i = 0; token && i < MAX_NR_ZONES; i++) { | |
60a7e9e9 GS |
401 | blk->zones[i] = zone_name_to_id(token); |
402 | blk->nr_zones++; | |
403 | token = strtok(NULL, " "); | |
404 | } | |
e4319a10 | 405 | free(line); |
60a7e9e9 | 406 | } |
fe4e122a KZ |
407 | |
408 | return rc; | |
cad2d1ac HC |
409 | } |
410 | ||
0cf0710a | 411 | static int is_mergeable(struct lsmem *lsmem, struct memory_block *blk) |
cad2d1ac HC |
412 | { |
413 | struct memory_block *curr; | |
60a7e9e9 | 414 | int i; |
cad2d1ac | 415 | |
0cf0710a | 416 | if (!lsmem->nblocks) |
cad2d1ac | 417 | return 0; |
0cf0710a KZ |
418 | curr = &lsmem->blocks[lsmem->nblocks - 1]; |
419 | if (lsmem->list_all) | |
cad2d1ac HC |
420 | return 0; |
421 | if (curr->index + curr->count != blk->index) | |
422 | return 0; | |
d12e945d | 423 | if (lsmem->split_by_state && curr->state != blk->state) |
cad2d1ac | 424 | return 0; |
d12e945d | 425 | if (lsmem->split_by_removable && curr->removable != blk->removable) |
cad2d1ac | 426 | return 0; |
d12e945d | 427 | if (lsmem->split_by_node && lsmem->have_nodes) { |
cad2d1ac HC |
428 | if (curr->node != blk->node) |
429 | return 0; | |
430 | } | |
d12e945d | 431 | if (lsmem->split_by_zones && lsmem->have_zones) { |
60a7e9e9 GS |
432 | if (curr->nr_zones != blk->nr_zones) |
433 | return 0; | |
434 | for (i = 0; i < curr->nr_zones; i++) { | |
435 | if (curr->zones[i] == ZONE_UNKNOWN || | |
436 | curr->zones[i] != blk->zones[i]) | |
437 | return 0; | |
438 | } | |
439 | } | |
cad2d1ac HC |
440 | return 1; |
441 | } | |
442 | ||
bf1d0a4e KZ |
443 | static void free_info(struct lsmem *lsmem) |
444 | { | |
445 | int i; | |
446 | ||
447 | if (!lsmem) | |
448 | return; | |
449 | free(lsmem->blocks); | |
450 | for (i = 0; i < lsmem->ndirs; i++) | |
451 | free(lsmem->dirs[i]); | |
452 | free(lsmem->dirs); | |
453 | } | |
454 | ||
0cf0710a | 455 | static void read_info(struct lsmem *lsmem) |
cad2d1ac | 456 | { |
cad2d1ac | 457 | struct memory_block blk; |
e4319a10 | 458 | char buf[128]; |
cad2d1ac HC |
459 | int i; |
460 | ||
e4319a10 KZ |
461 | if (ul_path_read_buffer(lsmem->sysmem, buf, sizeof(buf), "block_size_bytes") <= 0) |
462 | err(EXIT_FAILURE, _("failed to read memory block size")); | |
fe4e122a KZ |
463 | |
464 | errno = 0; | |
e4319a10 | 465 | lsmem->block_size = strtoumax(buf, NULL, 16); |
fe4e122a KZ |
466 | if (errno) |
467 | err(EXIT_FAILURE, _("failed to read memory block size")); | |
cad2d1ac | 468 | |
0cf0710a KZ |
469 | for (i = 0; i < lsmem->ndirs; i++) { |
470 | memory_block_read_attrs(lsmem, lsmem->dirs[i]->d_name, &blk); | |
49834246 GS |
471 | if (blk.state == MEMORY_STATE_ONLINE) |
472 | lsmem->mem_online += lsmem->block_size; | |
473 | else | |
474 | lsmem->mem_offline += lsmem->block_size; | |
0cf0710a KZ |
475 | if (is_mergeable(lsmem, &blk)) { |
476 | lsmem->blocks[lsmem->nblocks - 1].count++; | |
cad2d1ac HC |
477 | continue; |
478 | } | |
0cf0710a | 479 | lsmem->nblocks++; |
64d6d400 | 480 | lsmem->blocks = xreallocarray(lsmem->blocks, lsmem->nblocks, sizeof(blk)); |
0cf0710a | 481 | *&lsmem->blocks[lsmem->nblocks - 1] = blk; |
cad2d1ac | 482 | } |
cad2d1ac HC |
483 | } |
484 | ||
485 | static int memory_block_filter(const struct dirent *de) | |
486 | { | |
ad296391 | 487 | if (strncmp("memory", de->d_name, 6) != 0) |
cad2d1ac HC |
488 | return 0; |
489 | return isdigit_string(de->d_name + 6); | |
490 | } | |
491 | ||
0cf0710a | 492 | static void read_basic_info(struct lsmem *lsmem) |
cad2d1ac | 493 | { |
e4319a10 | 494 | char dir[PATH_MAX]; |
cad2d1ac | 495 | |
e4319a10 | 496 | if (ul_path_access(lsmem->sysmem, F_OK, "block_size_bytes") != 0) |
cad2d1ac HC |
497 | errx(EXIT_FAILURE, _("This system does not support memory blocks")); |
498 | ||
e4319a10 | 499 | ul_path_get_abspath(lsmem->sysmem, dir, sizeof(dir), NULL); |
81435af3 | 500 | |
0cf0710a | 501 | lsmem->ndirs = scandir(dir, &lsmem->dirs, memory_block_filter, versionsort); |
0cf0710a | 502 | if (lsmem->ndirs <= 0) |
e4319a10 | 503 | err(EXIT_FAILURE, _("Failed to read %s"), dir); |
cad2d1ac | 504 | |
e4319a10 | 505 | if (memory_block_get_node(lsmem, lsmem->dirs[0]->d_name) != -1) |
0cf0710a | 506 | lsmem->have_nodes = 1; |
60a7e9e9 | 507 | |
e4319a10 KZ |
508 | /* The valid_zones sysmem attribute was introduced with kernel 3.18 */ |
509 | if (ul_path_access(lsmem->sysmem, F_OK, "memory0/valid_zones") == 0) | |
60a7e9e9 | 510 | lsmem->have_zones = 1; |
cad2d1ac HC |
511 | } |
512 | ||
6e1eda6f | 513 | static void __attribute__((__noreturn__)) usage(void) |
cad2d1ac | 514 | { |
6e1eda6f | 515 | FILE *out = stdout; |
9c41d270 | 516 | size_t i; |
cad2d1ac HC |
517 | |
518 | fputs(USAGE_HEADER, out); | |
519 | fprintf(out, _(" %s [options]\n"), program_invocation_short_name); | |
520 | ||
521 | fputs(USAGE_SEPARATOR, out); | |
522 | fputs(_("List the ranges of available memory with their online status.\n"), out); | |
523 | ||
524 | fputs(USAGE_OPTIONS, out); | |
0a1ed6df KZ |
525 | fputs(_(" -J, --json use JSON output format\n"), out); |
526 | fputs(_(" -P, --pairs use key=\"value\" output format\n"), out); | |
0508f347 | 527 | fputs(_(" -a, --all list each individual memory block\n"), out); |
0a1ed6df KZ |
528 | fputs(_(" -b, --bytes print SIZE in bytes rather than in human readable format\n"), out); |
529 | fputs(_(" -n, --noheadings don't print headings\n"), out); | |
530 | fputs(_(" -o, --output <list> output columns\n"), out); | |
fcd4bbff | 531 | fputs(_(" --output-all output all columns\n"), out); |
0a1ed6df | 532 | fputs(_(" -r, --raw use raw output format\n"), out); |
d12e945d | 533 | fputs(_(" -S, --split <list> split ranges by specified columns\n"), out); |
0a1ed6df | 534 | fputs(_(" -s, --sysroot <dir> use the specified directory as system root\n"), out); |
c5332fbb | 535 | fputs(_(" --summary[=when] print summary information (never,always or only)\n"), out); |
0a1ed6df | 536 | |
cad2d1ac | 537 | fputs(USAGE_SEPARATOR, out); |
f45f3ec3 | 538 | printf(USAGE_HELP_OPTIONS(22)); |
cad2d1ac | 539 | |
6e2d5a44 | 540 | fputs(USAGE_COLUMNS, out); |
cad2d1ac | 541 | for (i = 0; i < ARRAY_SIZE(coldescs); i++) |
6e2d5a44 | 542 | fprintf(out, " %10s %s\n", coldescs[i].name, _(coldescs[i].help)); |
cad2d1ac | 543 | |
f45f3ec3 | 544 | printf(USAGE_MAN_TAIL("lsmem(1)")); |
cad2d1ac | 545 | |
2c308875 | 546 | exit(EXIT_SUCCESS); |
cad2d1ac HC |
547 | } |
548 | ||
549 | int main(int argc, char **argv) | |
550 | { | |
c5332fbb KZ |
551 | struct lsmem _lsmem = { |
552 | .want_table = 1, | |
553 | .want_summary = 1 | |
554 | }, *lsmem = &_lsmem; | |
555 | ||
e4319a10 | 556 | const char *outarg = NULL, *splitarg = NULL, *prefix = NULL; |
cad2d1ac | 557 | int c; |
0a1ed6df | 558 | size_t i; |
cad2d1ac | 559 | |
c5332fbb | 560 | enum { |
fcd4bbff SK |
561 | LSMEM_OPT_SUMARRY = CHAR_MAX + 1, |
562 | OPT_OUTPUT_ALL | |
c5332fbb KZ |
563 | }; |
564 | ||
cad2d1ac HC |
565 | static const struct option longopts[] = { |
566 | {"all", no_argument, NULL, 'a'}, | |
0a1ed6df | 567 | {"bytes", no_argument, NULL, 'b'}, |
cad2d1ac | 568 | {"help", no_argument, NULL, 'h'}, |
0a1ed6df KZ |
569 | {"json", no_argument, NULL, 'J'}, |
570 | {"noheadings", no_argument, NULL, 'n'}, | |
571 | {"output", required_argument, NULL, 'o'}, | |
fcd4bbff | 572 | {"output-all", no_argument, NULL, OPT_OUTPUT_ALL}, |
0a1ed6df KZ |
573 | {"pairs", no_argument, NULL, 'P'}, |
574 | {"raw", no_argument, NULL, 'r'}, | |
cad2d1ac | 575 | {"sysroot", required_argument, NULL, 's'}, |
d12e945d | 576 | {"split", required_argument, NULL, 'S'}, |
cad2d1ac | 577 | {"version", no_argument, NULL, 'V'}, |
c5332fbb | 578 | {"summary", optional_argument, NULL, LSMEM_OPT_SUMARRY }, |
cad2d1ac HC |
579 | {NULL, 0, NULL, 0} |
580 | }; | |
581 | static const ul_excl_t excl[] = { /* rows and cols in ASCII order */ | |
0a1ed6df | 582 | { 'J', 'P', 'r' }, |
d12e945d | 583 | { 'S', 'a' }, |
cad2d1ac HC |
584 | { 0 } |
585 | }; | |
586 | int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT; | |
587 | ||
588 | setlocale(LC_ALL, ""); | |
589 | bindtextdomain(PACKAGE, LOCALEDIR); | |
590 | textdomain(PACKAGE); | |
2c308875 | 591 | close_stdout_atexit(); |
cad2d1ac | 592 | |
d12e945d | 593 | while ((c = getopt_long(argc, argv, "abhJno:PrS:s:V", longopts, NULL)) != -1) { |
cad2d1ac HC |
594 | |
595 | err_exclusive_options(c, longopts, excl, excl_st); | |
596 | ||
597 | switch (c) { | |
598 | case 'a': | |
0cf0710a | 599 | lsmem->list_all = 1; |
0a1ed6df KZ |
600 | break; |
601 | case 'b': | |
0cf0710a | 602 | lsmem->bytes = 1; |
cad2d1ac | 603 | break; |
0a1ed6df | 604 | case 'J': |
0cf0710a | 605 | lsmem->json = 1; |
019ad6c1 | 606 | lsmem->want_summary = 0; |
0a1ed6df KZ |
607 | break; |
608 | case 'n': | |
0cf0710a | 609 | lsmem->noheadings = 1; |
0a1ed6df KZ |
610 | break; |
611 | case 'o': | |
612 | outarg = optarg; | |
613 | break; | |
fcd4bbff SK |
614 | case OPT_OUTPUT_ALL: |
615 | for (ncolumns = 0; (size_t)ncolumns < ARRAY_SIZE(coldescs); ncolumns++) | |
616 | columns[ncolumns] = ncolumns; | |
617 | break; | |
0a1ed6df | 618 | case 'P': |
0cf0710a | 619 | lsmem->export = 1; |
019ad6c1 | 620 | lsmem->want_summary = 0; |
0a1ed6df KZ |
621 | break; |
622 | case 'r': | |
0cf0710a | 623 | lsmem->raw = 1; |
019ad6c1 | 624 | lsmem->want_summary = 0; |
cad2d1ac HC |
625 | break; |
626 | case 's': | |
e4319a10 | 627 | prefix = optarg; |
cad2d1ac | 628 | break; |
d12e945d KZ |
629 | case 'S': |
630 | splitarg = optarg; | |
631 | break; | |
c5332fbb KZ |
632 | case LSMEM_OPT_SUMARRY: |
633 | if (optarg) { | |
634 | if (strcmp(optarg, "never") == 0) | |
635 | lsmem->want_summary = 0; | |
636 | else if (strcmp(optarg, "only") == 0) | |
637 | lsmem->want_table = 0; | |
638 | else if (strcmp(optarg, "always") == 0) | |
639 | lsmem->want_summary = 1; | |
640 | else | |
641 | errx(EXIT_FAILURE, _("unsupported --summary argument")); | |
642 | } else | |
643 | lsmem->want_table = 0; | |
644 | break; | |
2c308875 KZ |
645 | |
646 | case 'h': | |
647 | usage(); | |
648 | case 'V': | |
649 | print_version(EXIT_SUCCESS); | |
cad2d1ac | 650 | default: |
677ec86c | 651 | errtryhelp(EXIT_FAILURE); |
cad2d1ac HC |
652 | } |
653 | } | |
654 | ||
6e1eda6f RM |
655 | if (argc != optind) { |
656 | warnx(_("bad usage")); | |
657 | errtryhelp(EXIT_FAILURE); | |
658 | } | |
cad2d1ac | 659 | |
d4625442 KZ |
660 | if (lsmem->want_table + lsmem->want_summary == 0) |
661 | errx(EXIT_FAILURE, _("options --{raw,json,pairs} and --summary=only are mutually exclusive")); | |
662 | ||
e4319a10 KZ |
663 | ul_path_init_debug(); |
664 | ||
665 | lsmem->sysmem = ul_new_path(_PATH_SYS_MEMORY); | |
666 | if (!lsmem->sysmem) | |
667 | err(EXIT_FAILURE, _("failed to initialize %s handler"), _PATH_SYS_MEMORY); | |
668 | if (prefix && ul_path_set_prefix(lsmem->sysmem, prefix) != 0) | |
669 | err(EXIT_FAILURE, _("invalid argument to --sysroot")); | |
c02980d6 KZ |
670 | if (!ul_path_is_accessible(lsmem->sysmem)) |
671 | err(EXIT_FAILURE, _("cannot open %s"), _PATH_SYS_MEMORY); | |
e4319a10 | 672 | |
d4625442 KZ |
673 | /* Shortcut to avoid scols machinery on --summary=only */ |
674 | if (lsmem->want_table == 0 && lsmem->want_summary) { | |
675 | read_basic_info(lsmem); | |
676 | read_info(lsmem); | |
677 | print_summary(lsmem); | |
678 | return EXIT_SUCCESS; | |
679 | } | |
680 | ||
0a1ed6df KZ |
681 | /* |
682 | * Default columns | |
683 | */ | |
cad2d1ac | 684 | if (!ncolumns) { |
0a1ed6df KZ |
685 | add_column(columns, ncolumns++, COL_RANGE); |
686 | add_column(columns, ncolumns++, COL_SIZE); | |
687 | add_column(columns, ncolumns++, COL_STATE); | |
688 | add_column(columns, ncolumns++, COL_REMOVABLE); | |
689 | add_column(columns, ncolumns++, COL_BLOCK); | |
cad2d1ac HC |
690 | } |
691 | ||
0a1ed6df KZ |
692 | if (outarg && string_add_to_idarray(outarg, columns, ARRAY_SIZE(columns), |
693 | &ncolumns, column_name_to_id) < 0) | |
694 | return EXIT_FAILURE; | |
695 | ||
696 | /* | |
697 | * Initialize output | |
698 | */ | |
699 | scols_init_debug(0); | |
700 | ||
0cf0710a | 701 | if (!(lsmem->table = scols_new_table())) |
0a1ed6df | 702 | errx(EXIT_FAILURE, _("failed to initialize output table")); |
0cf0710a KZ |
703 | scols_table_enable_raw(lsmem->table, lsmem->raw); |
704 | scols_table_enable_export(lsmem->table, lsmem->export); | |
705 | scols_table_enable_json(lsmem->table, lsmem->json); | |
706 | scols_table_enable_noheadings(lsmem->table, lsmem->noheadings); | |
0a1ed6df | 707 | |
0cf0710a KZ |
708 | if (lsmem->json) |
709 | scols_table_set_name(lsmem->table, "memory"); | |
cad2d1ac | 710 | |
0a1ed6df KZ |
711 | for (i = 0; i < ncolumns; i++) { |
712 | struct coldesc *ci = get_column_desc(i); | |
704f9ba6 KZ |
713 | struct libscols_column *cl; |
714 | ||
715 | cl = scols_table_new_column(lsmem->table, ci->name, ci->whint, ci->flags); | |
716 | if (!cl) | |
0a1ed6df | 717 | err(EXIT_FAILURE, _("Failed to initialize output column")); |
704f9ba6 KZ |
718 | |
719 | if (lsmem->json) { | |
720 | int id = get_column_id(i); | |
721 | ||
722 | switch (id) { | |
723 | case COL_SIZE: | |
724 | if (!lsmem->bytes) | |
725 | break; | |
726 | /* fallthrough */ | |
727 | case COL_NODE: | |
728 | scols_column_set_json_type(cl, SCOLS_JSON_NUMBER); | |
729 | break; | |
730 | case COL_REMOVABLE: | |
731 | scols_column_set_json_type(cl, SCOLS_JSON_BOOLEAN); | |
732 | break; | |
733 | } | |
734 | } | |
cad2d1ac | 735 | } |
0a1ed6df | 736 | |
d12e945d KZ |
737 | if (splitarg) { |
738 | int split[ARRAY_SIZE(coldescs)] = { 0 }; | |
739 | static size_t nsplits = 0; | |
740 | ||
d12e945d KZ |
741 | if (strcasecmp(splitarg, "none") == 0) |
742 | ; | |
743 | else if (string_add_to_idarray(splitarg, split, ARRAY_SIZE(split), | |
744 | &nsplits, column_name_to_id) < 0) | |
745 | return EXIT_FAILURE; | |
746 | ||
96cbe362 KZ |
747 | set_split_policy(lsmem, split, nsplits); |
748 | ||
d12e945d | 749 | } else |
96cbe362 KZ |
750 | /* follow output columns */ |
751 | set_split_policy(lsmem, columns, ncolumns); | |
0a1ed6df KZ |
752 | |
753 | /* | |
754 | * Read data and print output | |
755 | */ | |
0cf0710a KZ |
756 | read_basic_info(lsmem); |
757 | read_info(lsmem); | |
0a1ed6df | 758 | |
c5332fbb KZ |
759 | if (lsmem->want_table) { |
760 | fill_scols_table(lsmem); | |
761 | scols_print_table(lsmem->table); | |
762 | ||
763 | if (lsmem->want_summary) | |
764 | fputc('\n', stdout); | |
765 | } | |
0a1ed6df | 766 | |
c5332fbb KZ |
767 | if (lsmem->want_summary) |
768 | print_summary(lsmem); | |
0a1ed6df | 769 | |
0cf0710a | 770 | scols_unref_table(lsmem->table); |
e4319a10 | 771 | ul_unref_path(lsmem->sysmem); |
bf1d0a4e | 772 | free_info(lsmem); |
cad2d1ac HC |
773 | return 0; |
774 | } |