]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/lsmem.c
86383360ac5c18aeccaf4b3ea7e033c650d25ed9
[thirdparty/util-linux.git] / sys-utils / lsmem.c
1 /*
2 * lsmem - Show memory configuration
3 *
4 * Copyright IBM Corp. 2016
5 * Copyright (C) 2016 Karel Zak <kzak@redhat.com>
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 */
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>
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"
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
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
56 struct memory_block {
57 uint64_t index;
58 uint64_t count;
59 int state;
60 int node;
61 int nr_zones;
62 int zones[MAX_NR_ZONES];
63 unsigned int removable:1;
64 };
65
66 struct lsmem {
67 struct path_cxt *sysmem; /* _PATH_SYS_MEMORY directory handler */
68 struct dirent **dirs;
69 int ndirs;
70 struct memory_block *blocks;
71 int nblocks;
72 uint64_t block_size;
73 uint64_t mem_online;
74 uint64_t mem_offline;
75
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,
85 want_summary : 1,
86 want_table : 1,
87 split_by_node : 1,
88 split_by_state : 1,
89 split_by_removable : 1,
90 split_by_zones : 1,
91 have_zones : 1;
92 };
93
94
95 enum {
96 COL_RANGE,
97 COL_SIZE,
98 COL_STATE,
99 COL_REMOVABLE,
100 COL_BLOCK,
101 COL_NODE,
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",
114 };
115
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;
122 };
123
124 /* columns descriptions */
125 static struct coldesc coldescs[] = {
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")},
128 [COL_STATE] = { "STATE", 0, SCOLS_FL_RIGHT, N_("online status of the memory range")},
129 [COL_REMOVABLE] = { "REMOVABLE", 0, SCOLS_FL_RIGHT, N_("memory is removable")},
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")},
132 [COL_ZONES] = { "ZONES", 0, SCOLS_FL_RIGHT, N_("valid zones for the memory range")},
133 };
134
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
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
165 #define add_column(ary, n, id) \
166 ((ary)[ err_columns_index(ARRAY_SIZE(ary), (n)) ] = (id))
167
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
182 static inline int get_column_id(int num)
183 {
184 assert(num >= 0);
185 assert((size_t) num < ncolumns);
186 assert(columns[num] < (int) ARRAY_SIZE(coldescs));
187
188 return columns[num];
189 }
190
191 static inline struct coldesc *get_column_desc(int num)
192 {
193 return &coldescs[ get_column_id(num) ];
194 }
195
196 static inline void reset_split_policy(struct lsmem *l, int enable)
197 {
198 l->split_by_state = enable;
199 l->split_by_node = enable;
200 l->split_by_removable = enable;
201 l->split_by_zones = enable;
202 }
203
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
230 static void add_scols_line(struct lsmem *lsmem, struct memory_block *blk)
231 {
232 size_t i;
233 struct libscols_line *line;
234
235 line = scols_table_new_line(lsmem->table, NULL);
236 if (!line)
237 err_oom();
238
239 for (i = 0; i < ncolumns; i++) {
240 char *str = NULL;
241
242 switch (get_column_id(i)) {
243 case COL_RANGE:
244 {
245 uint64_t start = blk->index * lsmem->block_size;
246 uint64_t size = blk->count * lsmem->block_size;
247 xasprintf(&str, "0x%016"PRIx64"-0x%016"PRIx64, start, start + size - 1);
248 break;
249 }
250 case COL_SIZE:
251 if (lsmem->bytes)
252 xasprintf(&str, "%"PRId64, (uint64_t) blk->count * lsmem->block_size);
253 else
254 str = size_to_human_string(SIZE_SUFFIX_1LETTER,
255 (uint64_t) blk->count * lsmem->block_size);
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:
276 if (lsmem->have_nodes)
277 xasprintf(&str, "%d", blk->node);
278 break;
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);
295 }
296 break;
297 }
298
299 if (str && scols_line_refer_data(line, i, str) != 0)
300 err_oom();
301 }
302 }
303
304 static void fill_scols_table(struct lsmem *lsmem)
305 {
306 int i;
307
308 for (i = 0; i < lsmem->nblocks; i++)
309 add_scols_line(lsmem, &lsmem->blocks[i]);
310 }
311
312 static void print_summary(struct lsmem *lsmem)
313 {
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 {
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);
332 }
333 }
334
335 static int memory_block_get_node(struct lsmem *lsmem, char *name)
336 {
337 struct dirent *de;
338 DIR *dir;
339 int node;
340
341 dir = ul_path_opendir(lsmem->sysmem, name);
342 if (!dir)
343 err(EXIT_FAILURE, _("Failed to open %s"), name);
344
345 node = -1;
346 while ((de = readdir(dir)) != NULL) {
347 if (strncmp("node", de->d_name, 4))
348 continue;
349 if (!isdigit_string(de->d_name + 4))
350 continue;
351 node = strtol(de->d_name + 4, NULL, 10);
352 break;
353 }
354 closedir(dir);
355 return node;
356 }
357
358 static void memory_block_read_attrs(struct lsmem *lsmem, char *name,
359 struct memory_block *blk)
360 {
361 char *line = NULL;
362 int i, x = 0;
363
364 memset(blk, 0, sizeof(*blk));
365
366 blk->count = 1;
367 blk->state = MEMORY_STATE_UNKNOWN;
368 blk->index = strtoumax(name + 6, NULL, 10); /* get <num> of "memory<num>" */
369
370 if (ul_path_readf_s32(lsmem->sysmem, &x, "%s/removable", name) == 0)
371 blk->removable = x == 1;
372
373 if (ul_path_readf_string(lsmem->sysmem, &line, "%s/state", name) > 0) {
374 if (strcmp(line, "offline") == 0)
375 blk->state = MEMORY_STATE_OFFLINE;
376 else if (strcmp(line, "online") == 0)
377 blk->state = MEMORY_STATE_ONLINE;
378 else if (strcmp(line, "going-offline") == 0)
379 blk->state = MEMORY_STATE_GOING_OFFLINE;
380 free(line);
381 }
382
383 if (lsmem->have_nodes)
384 blk->node = memory_block_get_node(lsmem, name);
385
386 blk->nr_zones = 0;
387 if (lsmem->have_zones &&
388 ul_path_readf_string(lsmem->sysmem, &line, "%s/valid_zones", name) > 0) {
389
390 char *token = strtok(line, " ");
391
392 for (i = 0; token && i < MAX_NR_ZONES; i++) {
393 blk->zones[i] = zone_name_to_id(token);
394 blk->nr_zones++;
395 token = strtok(NULL, " ");
396 }
397
398 free(line);
399 }
400 }
401
402 static int is_mergeable(struct lsmem *lsmem, struct memory_block *blk)
403 {
404 struct memory_block *curr;
405 int i;
406
407 if (!lsmem->nblocks)
408 return 0;
409 curr = &lsmem->blocks[lsmem->nblocks - 1];
410 if (lsmem->list_all)
411 return 0;
412 if (curr->index + curr->count != blk->index)
413 return 0;
414 if (lsmem->split_by_state && curr->state != blk->state)
415 return 0;
416 if (lsmem->split_by_removable && curr->removable != blk->removable)
417 return 0;
418 if (lsmem->split_by_node && lsmem->have_nodes) {
419 if (curr->node != blk->node)
420 return 0;
421 }
422 if (lsmem->split_by_zones && lsmem->have_zones) {
423 if (curr->nr_zones != blk->nr_zones)
424 return 0;
425 for (i = 0; i < curr->nr_zones; i++) {
426 if (curr->zones[i] == ZONE_UNKNOWN ||
427 curr->zones[i] != blk->zones[i])
428 return 0;
429 }
430 }
431 return 1;
432 }
433
434 static void read_info(struct lsmem *lsmem)
435 {
436 struct memory_block blk;
437 char buf[128];
438 int i;
439
440 if (ul_path_read_buffer(lsmem->sysmem, buf, sizeof(buf), "block_size_bytes") <= 0)
441 err(EXIT_FAILURE, _("failed to read memory block size"));
442 lsmem->block_size = strtoumax(buf, NULL, 16);
443
444 for (i = 0; i < lsmem->ndirs; i++) {
445 memory_block_read_attrs(lsmem, lsmem->dirs[i]->d_name, &blk);
446 if (blk.state == MEMORY_STATE_ONLINE)
447 lsmem->mem_online += lsmem->block_size;
448 else
449 lsmem->mem_offline += lsmem->block_size;
450 if (is_mergeable(lsmem, &blk)) {
451 lsmem->blocks[lsmem->nblocks - 1].count++;
452 continue;
453 }
454 lsmem->nblocks++;
455 lsmem->blocks = xrealloc(lsmem->blocks, lsmem->nblocks * sizeof(blk));
456 *&lsmem->blocks[lsmem->nblocks - 1] = blk;
457 }
458 }
459
460 static int memory_block_filter(const struct dirent *de)
461 {
462 if (strncmp("memory", de->d_name, 6))
463 return 0;
464 return isdigit_string(de->d_name + 6);
465 }
466
467 static void read_basic_info(struct lsmem *lsmem)
468 {
469 char dir[PATH_MAX];
470
471 if (ul_path_access(lsmem->sysmem, F_OK, "block_size_bytes") != 0)
472 errx(EXIT_FAILURE, _("This system does not support memory blocks"));
473
474 ul_path_get_abspath(lsmem->sysmem, dir, sizeof(dir), NULL);
475
476 lsmem->ndirs = scandir(dir, &lsmem->dirs, memory_block_filter, versionsort);
477 if (lsmem->ndirs <= 0)
478 err(EXIT_FAILURE, _("Failed to read %s"), dir);
479
480 if (memory_block_get_node(lsmem, lsmem->dirs[0]->d_name) != -1)
481 lsmem->have_nodes = 1;
482
483 /* The valid_zones sysmem attribute was introduced with kernel 3.18 */
484 if (ul_path_access(lsmem->sysmem, F_OK, "memory0/valid_zones") == 0)
485 lsmem->have_zones = 1;
486 }
487
488 static void __attribute__((__noreturn__)) usage(void)
489 {
490 FILE *out = stdout;
491 size_t i;
492
493 fputs(USAGE_HEADER, out);
494 fprintf(out, _(" %s [options]\n"), program_invocation_short_name);
495
496 fputs(USAGE_SEPARATOR, out);
497 fputs(_("List the ranges of available memory with their online status.\n"), out);
498
499 fputs(USAGE_OPTIONS, out);
500 fputs(_(" -J, --json use JSON output format\n"), out);
501 fputs(_(" -P, --pairs use key=\"value\" output format\n"), out);
502 fputs(_(" -a, --all list each individual memory block\n"), out);
503 fputs(_(" -b, --bytes print SIZE in bytes rather than in human readable format\n"), out);
504 fputs(_(" -n, --noheadings don't print headings\n"), out);
505 fputs(_(" -o, --output <list> output columns\n"), out);
506 fputs(_(" --output-all output all columns\n"), out);
507 fputs(_(" -r, --raw use raw output format\n"), out);
508 fputs(_(" -S, --split <list> split ranges by specified columns\n"), out);
509 fputs(_(" -s, --sysroot <dir> use the specified directory as system root\n"), out);
510 fputs(_(" --summary[=when] print summary information (never,always or only)\n"), out);
511
512 fputs(USAGE_SEPARATOR, out);
513 printf(USAGE_HELP_OPTIONS(22));
514
515 fputs(USAGE_COLUMNS, out);
516 for (i = 0; i < ARRAY_SIZE(coldescs); i++)
517 fprintf(out, " %10s %s\n", coldescs[i].name, _(coldescs[i].help));
518
519 printf(USAGE_MAN_TAIL("lsmem(1)"));
520
521 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
522 }
523
524 int main(int argc, char **argv)
525 {
526 struct lsmem _lsmem = {
527 .want_table = 1,
528 .want_summary = 1
529 }, *lsmem = &_lsmem;
530
531 const char *outarg = NULL, *splitarg = NULL, *prefix = NULL;
532 int c;
533 size_t i;
534
535 enum {
536 LSMEM_OPT_SUMARRY = CHAR_MAX + 1,
537 OPT_OUTPUT_ALL
538 };
539
540 static const struct option longopts[] = {
541 {"all", no_argument, NULL, 'a'},
542 {"bytes", no_argument, NULL, 'b'},
543 {"help", no_argument, NULL, 'h'},
544 {"json", no_argument, NULL, 'J'},
545 {"noheadings", no_argument, NULL, 'n'},
546 {"output", required_argument, NULL, 'o'},
547 {"output-all", no_argument, NULL, OPT_OUTPUT_ALL},
548 {"pairs", no_argument, NULL, 'P'},
549 {"raw", no_argument, NULL, 'r'},
550 {"sysroot", required_argument, NULL, 's'},
551 {"split", required_argument, NULL, 'S'},
552 {"version", no_argument, NULL, 'V'},
553 {"summary", optional_argument, NULL, LSMEM_OPT_SUMARRY },
554 {NULL, 0, NULL, 0}
555 };
556 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
557 { 'J', 'P', 'r' },
558 { 'S', 'a' },
559 { 0 }
560 };
561 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
562
563 setlocale(LC_ALL, "");
564 bindtextdomain(PACKAGE, LOCALEDIR);
565 textdomain(PACKAGE);
566 atexit(close_stdout);
567
568 while ((c = getopt_long(argc, argv, "abhJno:PrS:s:V", longopts, NULL)) != -1) {
569
570 err_exclusive_options(c, longopts, excl, excl_st);
571
572 switch (c) {
573 case 'a':
574 lsmem->list_all = 1;
575 break;
576 case 'b':
577 lsmem->bytes = 1;
578 break;
579 case 'h':
580 usage();
581 break;
582 case 'J':
583 lsmem->json = 1;
584 lsmem->want_summary = 0;
585 break;
586 case 'n':
587 lsmem->noheadings = 1;
588 break;
589 case 'o':
590 outarg = optarg;
591 break;
592 case OPT_OUTPUT_ALL:
593 for (ncolumns = 0; (size_t)ncolumns < ARRAY_SIZE(coldescs); ncolumns++)
594 columns[ncolumns] = ncolumns;
595 break;
596 case 'P':
597 lsmem->export = 1;
598 lsmem->want_summary = 0;
599 break;
600 case 'r':
601 lsmem->raw = 1;
602 lsmem->want_summary = 0;
603 break;
604 case 's':
605 prefix = optarg;
606 break;
607 case 'S':
608 splitarg = optarg;
609 break;
610 case 'V':
611 printf(UTIL_LINUX_VERSION);
612 return 0;
613 case LSMEM_OPT_SUMARRY:
614 if (optarg) {
615 if (strcmp(optarg, "never") == 0)
616 lsmem->want_summary = 0;
617 else if (strcmp(optarg, "only") == 0)
618 lsmem->want_table = 0;
619 else if (strcmp(optarg, "always") == 0)
620 lsmem->want_summary = 1;
621 else
622 errx(EXIT_FAILURE, _("unsupported --summary argument"));
623 } else
624 lsmem->want_table = 0;
625 break;
626 default:
627 errtryhelp(EXIT_FAILURE);
628 }
629 }
630
631 if (argc != optind) {
632 warnx(_("bad usage"));
633 errtryhelp(EXIT_FAILURE);
634 }
635
636 if (lsmem->want_table + lsmem->want_summary == 0)
637 errx(EXIT_FAILURE, _("options --{raw,json,pairs} and --summary=only are mutually exclusive"));
638
639 ul_path_init_debug();
640
641 lsmem->sysmem = ul_new_path(_PATH_SYS_MEMORY);
642 if (!lsmem->sysmem)
643 err(EXIT_FAILURE, _("failed to initialize %s handler"), _PATH_SYS_MEMORY);
644 if (prefix && ul_path_set_prefix(lsmem->sysmem, prefix) != 0)
645 err(EXIT_FAILURE, _("invalid argument to --sysroot"));
646
647 /* Shortcut to avoid scols machinery on --summary=only */
648 if (lsmem->want_table == 0 && lsmem->want_summary) {
649 read_basic_info(lsmem);
650 read_info(lsmem);
651 print_summary(lsmem);
652 return EXIT_SUCCESS;
653 }
654
655 /*
656 * Default columns
657 */
658 if (!ncolumns) {
659 add_column(columns, ncolumns++, COL_RANGE);
660 add_column(columns, ncolumns++, COL_SIZE);
661 add_column(columns, ncolumns++, COL_STATE);
662 add_column(columns, ncolumns++, COL_REMOVABLE);
663 add_column(columns, ncolumns++, COL_BLOCK);
664 }
665
666 if (outarg && string_add_to_idarray(outarg, columns, ARRAY_SIZE(columns),
667 &ncolumns, column_name_to_id) < 0)
668 return EXIT_FAILURE;
669
670 /*
671 * Initialize output
672 */
673 scols_init_debug(0);
674
675 if (!(lsmem->table = scols_new_table()))
676 errx(EXIT_FAILURE, _("failed to initialize output table"));
677 scols_table_enable_raw(lsmem->table, lsmem->raw);
678 scols_table_enable_export(lsmem->table, lsmem->export);
679 scols_table_enable_json(lsmem->table, lsmem->json);
680 scols_table_enable_noheadings(lsmem->table, lsmem->noheadings);
681
682 if (lsmem->json)
683 scols_table_set_name(lsmem->table, "memory");
684
685 for (i = 0; i < ncolumns; i++) {
686 struct coldesc *ci = get_column_desc(i);
687 struct libscols_column *cl;
688
689 cl = scols_table_new_column(lsmem->table, ci->name, ci->whint, ci->flags);
690 if (!cl)
691 err(EXIT_FAILURE, _("Failed to initialize output column"));
692
693 if (lsmem->json) {
694 int id = get_column_id(i);
695
696 switch (id) {
697 case COL_SIZE:
698 if (!lsmem->bytes)
699 break;
700 /* fallthrough */
701 case COL_NODE:
702 scols_column_set_json_type(cl, SCOLS_JSON_NUMBER);
703 break;
704 case COL_REMOVABLE:
705 scols_column_set_json_type(cl, SCOLS_JSON_BOOLEAN);
706 break;
707 }
708 }
709 }
710
711 if (splitarg) {
712 int split[ARRAY_SIZE(coldescs)] = { 0 };
713 static size_t nsplits = 0;
714
715 if (strcasecmp(splitarg, "none") == 0)
716 ;
717 else if (string_add_to_idarray(splitarg, split, ARRAY_SIZE(split),
718 &nsplits, column_name_to_id) < 0)
719 return EXIT_FAILURE;
720
721 set_split_policy(lsmem, split, nsplits);
722
723 } else
724 /* follow output columns */
725 set_split_policy(lsmem, columns, ncolumns);
726
727 /*
728 * Read data and print output
729 */
730 read_basic_info(lsmem);
731 read_info(lsmem);
732
733 if (lsmem->want_table) {
734 fill_scols_table(lsmem);
735 scols_print_table(lsmem->table);
736
737 if (lsmem->want_summary)
738 fputc('\n', stdout);
739 }
740
741 if (lsmem->want_summary)
742 print_summary(lsmem);
743
744 scols_unref_table(lsmem->table);
745 ul_unref_path(lsmem->sysmem);
746 return 0;
747 }