(root)/
util-linux-2.39/
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) != 0)
     348  			continue;
     349  		if (!isdigit_string(de->d_name + 4))
     350  			continue;
     351  		errno = 0;
     352  		node = strtol(de->d_name + 4, NULL, 10);
     353  		if (errno)
     354  			continue;
     355  		break;
     356  	}
     357  	closedir(dir);
     358  	return node;
     359  }
     360  
     361  static int memory_block_read_attrs(struct lsmem *lsmem, char *name,
     362  				    struct memory_block *blk)
     363  {
     364  	char *line = NULL;
     365  	int i, x = 0, rc = 0;
     366  
     367  	memset(blk, 0, sizeof(*blk));
     368  
     369  	errno = 0;
     370  	blk->count = 1;
     371  	blk->state = MEMORY_STATE_UNKNOWN;
     372  	blk->index = strtoumax(name + 6, NULL, 10); /* get <num> of "memory<num>" */
     373  
     374  	if (errno)
     375  		rc = -errno;
     376  
     377  	if (ul_path_readf_s32(lsmem->sysmem, &x, "%s/removable", name) == 0)
     378  		blk->removable = x == 1;
     379  
     380  	if (ul_path_readf_string(lsmem->sysmem, &line, "%s/state", name) > 0 && line) {
     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  	}
     389  
     390  	if (lsmem->have_nodes)
     391  		blk->node = memory_block_get_node(lsmem, name);
     392  
     393  	blk->nr_zones = 0;
     394  	if (lsmem->have_zones
     395  	    && ul_path_readf_string(lsmem->sysmem, &line, "%s/valid_zones", name) > 0
     396  	    && line) {
     397  
     398  		char *token = strtok(line, " ");
     399  
     400  		for (i = 0; token && i < MAX_NR_ZONES; i++) {
     401  			blk->zones[i] = zone_name_to_id(token);
     402  			blk->nr_zones++;
     403  			token = strtok(NULL, " ");
     404  		}
     405  		free(line);
     406  	}
     407  
     408  	return rc;
     409  }
     410  
     411  static int is_mergeable(struct lsmem *lsmem, struct memory_block *blk)
     412  {
     413  	struct memory_block *curr;
     414  	int i;
     415  
     416  	if (!lsmem->nblocks)
     417  		return 0;
     418  	curr = &lsmem->blocks[lsmem->nblocks - 1];
     419  	if (lsmem->list_all)
     420  		return 0;
     421  	if (curr->index + curr->count != blk->index)
     422  		return 0;
     423  	if (lsmem->split_by_state && curr->state != blk->state)
     424  		return 0;
     425  	if (lsmem->split_by_removable && curr->removable != blk->removable)
     426  		return 0;
     427  	if (lsmem->split_by_node && lsmem->have_nodes) {
     428  		if (curr->node != blk->node)
     429  			return 0;
     430  	}
     431  	if (lsmem->split_by_zones && lsmem->have_zones) {
     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  	}
     440  	return 1;
     441  }
     442  
     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  
     455  static void read_info(struct lsmem *lsmem)
     456  {
     457  	struct memory_block blk;
     458  	char buf[128];
     459  	int i;
     460  
     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"));
     463  
     464  	errno = 0;
     465  	lsmem->block_size = strtoumax(buf, NULL, 16);
     466  	if (errno)
     467  		err(EXIT_FAILURE, _("failed to read memory block size"));
     468  
     469  	for (i = 0; i < lsmem->ndirs; i++) {
     470  		memory_block_read_attrs(lsmem, lsmem->dirs[i]->d_name, &blk);
     471  		if (blk.state == MEMORY_STATE_ONLINE)
     472  			lsmem->mem_online += lsmem->block_size;
     473  		else
     474  			lsmem->mem_offline += lsmem->block_size;
     475  		if (is_mergeable(lsmem, &blk)) {
     476  			lsmem->blocks[lsmem->nblocks - 1].count++;
     477  			continue;
     478  		}
     479  		lsmem->nblocks++;
     480  		lsmem->blocks = xrealloc(lsmem->blocks, lsmem->nblocks * sizeof(blk));
     481  		*&lsmem->blocks[lsmem->nblocks - 1] = blk;
     482  	}
     483  }
     484  
     485  static int memory_block_filter(const struct dirent *de)
     486  {
     487  	if (strncmp("memory", de->d_name, 6) != 0)
     488  		return 0;
     489  	return isdigit_string(de->d_name + 6);
     490  }
     491  
     492  static void read_basic_info(struct lsmem *lsmem)
     493  {
     494  	char dir[PATH_MAX];
     495  
     496  	if (ul_path_access(lsmem->sysmem, F_OK, "block_size_bytes") != 0)
     497  		errx(EXIT_FAILURE, _("This system does not support memory blocks"));
     498  
     499  	ul_path_get_abspath(lsmem->sysmem, dir, sizeof(dir), NULL);
     500  
     501  	lsmem->ndirs = scandir(dir, &lsmem->dirs, memory_block_filter, versionsort);
     502  	if (lsmem->ndirs <= 0)
     503  		err(EXIT_FAILURE, _("Failed to read %s"), dir);
     504  
     505  	if (memory_block_get_node(lsmem, lsmem->dirs[0]->d_name) != -1)
     506  		lsmem->have_nodes = 1;
     507  
     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)
     510  		lsmem->have_zones = 1;
     511  }
     512  
     513  static void __attribute__((__noreturn__)) usage(void)
     514  {
     515  	FILE *out = stdout;
     516  	size_t i;
     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);
     525  	fputs(_(" -J, --json           use JSON output format\n"), out);
     526  	fputs(_(" -P, --pairs          use key=\"value\" output format\n"), out);
     527  	fputs(_(" -a, --all            list each individual memory block\n"), out);
     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);
     531  	fputs(_("     --output-all     output all columns\n"), out);
     532  	fputs(_(" -r, --raw            use raw output format\n"), out);
     533  	fputs(_(" -S, --split <list>   split ranges by specified columns\n"), out);
     534  	fputs(_(" -s, --sysroot <dir>  use the specified directory as system root\n"), out);
     535  	fputs(_("     --summary[=when] print summary information (never,always or only)\n"), out);
     536  
     537  	fputs(USAGE_SEPARATOR, out);
     538  	printf(USAGE_HELP_OPTIONS(22));
     539  
     540  	fputs(USAGE_COLUMNS, out);
     541  	for (i = 0; i < ARRAY_SIZE(coldescs); i++)
     542  		fprintf(out, " %10s  %s\n", coldescs[i].name, _(coldescs[i].help));
     543  
     544  	printf(USAGE_MAN_TAIL("lsmem(1)"));
     545  
     546  	exit(EXIT_SUCCESS);
     547  }
     548  
     549  int main(int argc, char **argv)
     550  {
     551  	struct lsmem _lsmem = {
     552  			.want_table = 1,
     553  			.want_summary = 1
     554  		}, *lsmem = &_lsmem;
     555  
     556  	const char *outarg = NULL, *splitarg = NULL, *prefix = NULL;
     557  	int c;
     558  	size_t i;
     559  
     560  	enum {
     561  		LSMEM_OPT_SUMARRY = CHAR_MAX + 1,
     562  		OPT_OUTPUT_ALL
     563  	};
     564  
     565  	static const struct option longopts[] = {
     566  		{"all",		no_argument,		NULL, 'a'},
     567  		{"bytes",	no_argument,		NULL, 'b'},
     568  		{"help",	no_argument,		NULL, 'h'},
     569  		{"json",	no_argument,		NULL, 'J'},
     570  		{"noheadings",	no_argument,		NULL, 'n'},
     571  		{"output",	required_argument,	NULL, 'o'},
     572  		{"output-all",	no_argument,		NULL, OPT_OUTPUT_ALL},
     573  		{"pairs",	no_argument,		NULL, 'P'},
     574  		{"raw",		no_argument,		NULL, 'r'},
     575  		{"sysroot",	required_argument,	NULL, 's'},
     576  		{"split",       required_argument,      NULL, 'S'},
     577  		{"version",	no_argument,		NULL, 'V'},
     578  		{"summary",     optional_argument,	NULL, LSMEM_OPT_SUMARRY },
     579  		{NULL,		0,			NULL, 0}
     580  	};
     581  	static const ul_excl_t excl[] = {	/* rows and cols in ASCII order */
     582  		{ 'J', 'P', 'r' },
     583  		{ 'S', 'a' },
     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);
     591  	close_stdout_atexit();
     592  
     593  	while ((c = getopt_long(argc, argv, "abhJno:PrS:s:V", longopts, NULL)) != -1) {
     594  
     595  		err_exclusive_options(c, longopts, excl, excl_st);
     596  
     597  		switch (c) {
     598  		case 'a':
     599  			lsmem->list_all = 1;
     600  			break;
     601  		case 'b':
     602  			lsmem->bytes = 1;
     603  			break;
     604  		case 'J':
     605  			lsmem->json = 1;
     606  			lsmem->want_summary = 0;
     607  			break;
     608  		case 'n':
     609  			lsmem->noheadings = 1;
     610  			break;
     611  		case 'o':
     612  			outarg = optarg;
     613  			break;
     614  		case OPT_OUTPUT_ALL:
     615  			for (ncolumns = 0; (size_t)ncolumns < ARRAY_SIZE(coldescs); ncolumns++)
     616  				columns[ncolumns] = ncolumns;
     617  			break;
     618  		case 'P':
     619  			lsmem->export = 1;
     620  			lsmem->want_summary = 0;
     621  			break;
     622  		case 'r':
     623  			lsmem->raw = 1;
     624  			lsmem->want_summary = 0;
     625  			break;
     626  		case 's':
     627  			prefix = optarg;
     628  			break;
     629  		case 'S':
     630  			splitarg = optarg;
     631  			break;
     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;
     645  
     646  		case 'h':
     647  			usage();
     648  		case 'V':
     649  			print_version(EXIT_SUCCESS);
     650  		default:
     651  			errtryhelp(EXIT_FAILURE);
     652  		}
     653  	}
     654  
     655  	if (argc != optind) {
     656  		warnx(_("bad usage"));
     657  		errtryhelp(EXIT_FAILURE);
     658  	}
     659  
     660  	if (lsmem->want_table + lsmem->want_summary == 0)
     661  		errx(EXIT_FAILURE, _("options --{raw,json,pairs} and --summary=only are mutually exclusive"));
     662  
     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"));
     670  	if (!ul_path_is_accessible(lsmem->sysmem))
     671  		err(EXIT_FAILURE, _("cannot open %s"), _PATH_SYS_MEMORY);
     672  
     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  
     681  	/*
     682  	 * Default columns
     683  	 */
     684  	if (!ncolumns) {
     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);
     690  	}
     691  
     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  
     701  	if (!(lsmem->table = scols_new_table()))
     702  		errx(EXIT_FAILURE, _("failed to initialize output table"));
     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);
     707  
     708  	if (lsmem->json)
     709  		scols_table_set_name(lsmem->table, "memory");
     710  
     711  	for (i = 0; i < ncolumns; i++) {
     712  		struct coldesc *ci = get_column_desc(i);
     713  		struct libscols_column *cl;
     714  
     715  		cl = scols_table_new_column(lsmem->table, ci->name, ci->whint, ci->flags);
     716  		if (!cl)
     717  			err(EXIT_FAILURE, _("Failed to initialize output column"));
     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  		}
     735  	}
     736  
     737  	if (splitarg) {
     738  		int split[ARRAY_SIZE(coldescs)] = { 0 };
     739  		static size_t nsplits = 0;
     740  
     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  
     747  		set_split_policy(lsmem, split, nsplits);
     748  
     749  	} else
     750  		/* follow output columns */
     751  		set_split_policy(lsmem, columns, ncolumns);
     752  
     753  	/*
     754  	 * Read data and print output
     755  	 */
     756  	read_basic_info(lsmem);
     757  	read_info(lsmem);
     758  
     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  	}
     766  
     767  	if (lsmem->want_summary)
     768  		print_summary(lsmem);
     769  
     770  	scols_unref_table(lsmem->table);
     771  	ul_unref_path(lsmem->sysmem);
     772  	free_info(lsmem);
     773  	return 0;
     774  }