lscpu: cleanup output modes

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2011-09-09 07:25:21 +02:00
parent 44de912cdf
commit 8005924a89
1 changed files with 68 additions and 50 deletions

View File

@ -40,7 +40,6 @@
#include "strutils.h" #include "strutils.h"
#include "bitops.h" #include "bitops.h"
#define CACHE_MAX 100 #define CACHE_MAX 100
/* /sys paths */ /* /sys paths */
@ -117,12 +116,17 @@ enum {
POLAR_HORIZONTAL POLAR_HORIZONTAL
}; };
const char *polar_modes[] = { struct polarization_modes {
[POLAR_UNKNOWN] = "U", char *parsable;
[POLAR_VLOW] = "VL", char *readable;
[POLAR_VMEDIUM] = "VM", };
[POLAR_VHIGH] = "VH",
[POLAR_HORIZONTAL] = "H" struct polarization_modes polar_modes[] = {
[POLAR_UNKNOWN] = {"U", "-"},
[POLAR_VLOW] = {"VL", "vert-low"},
[POLAR_VMEDIUM] = {"VM", "vert-medium"},
[POLAR_VHIGH] = {"VH", "vert-high"},
[POLAR_HORIZONTAL] = {"H", "horizontal"},
}; };
/* global description */ /* global description */
@ -171,6 +175,17 @@ struct lscpu_desc {
int *addresses; /* physical cpu addresses */ int *addresses; /* physical cpu addresses */
}; };
enum {
OUTPUT_SUMMARY = 0, /* default */
OUTPUT_PARSABLE, /* -p */
};
struct lscpu_modifier {
int mode; /* OUTPUT_* */
int hex:1; /* print CPU masks rather than CPU lists */
int compat:1; /* use backwardly compatible format */
};
static size_t sysrootlen; static size_t sysrootlen;
static char pathbuf[PATH_MAX]; static char pathbuf[PATH_MAX];
static int maxcpus; /* size in bits of kernel cpu mask */ static int maxcpus; /* size in bits of kernel cpu mask */
@ -870,7 +885,8 @@ read_nodes(struct lscpu_desc *desc)
} }
static void static void
print_parsable_cell(struct lscpu_desc *desc, int i, int col, int compatible) print_parsable_cell(struct lscpu_desc *desc, int i, int col,
struct lscpu_modifier *mod)
{ {
int j; int j;
size_t setsize = CPU_ALLOC_SIZE(maxcpus); size_t setsize = CPU_ALLOC_SIZE(maxcpus);
@ -923,12 +939,12 @@ print_parsable_cell(struct lscpu_desc *desc, int i, int col, int compatible)
} }
} }
if (j != 0) if (j != 0)
putchar(compatible ? ',' : ':'); putchar(mod->compat ? ',' : ':');
} }
break; break;
case COL_POLARIZATION: case COL_POLARIZATION:
if (desc->polarization) if (desc->polarization)
printf("%s", polar_modes[desc->polarization[i]]); printf("%s", polar_modes[desc->polarization[i]].parsable);
break; break;
case COL_ADDRESS: case COL_ADDRESS:
if (desc->addresses) if (desc->addresses)
@ -938,7 +954,7 @@ print_parsable_cell(struct lscpu_desc *desc, int i, int col, int compatible)
} }
/* /*
* We support two formats: * We support two parsable formats:
* *
* 1) "compatible" -- this format is compatible with the original lscpu(1) * 1) "compatible" -- this format is compatible with the original lscpu(1)
* output and it contains fixed set of the columns. The CACHE columns are at * output and it contains fixed set of the columns. The CACHE columns are at
@ -961,7 +977,8 @@ print_parsable_cell(struct lscpu_desc *desc, int i, int col, int compatible)
* 1,1,0,0,1:1:0 * 1,1,0,0,1:1:0
*/ */
static void static void
print_parsable(struct lscpu_desc *desc, int cols[], int ncols, int compatible) print_parsable(struct lscpu_desc *desc, int cols[], int ncols,
struct lscpu_modifier *mod)
{ {
int i, c; int i, c;
@ -973,16 +990,16 @@ print_parsable(struct lscpu_desc *desc, int cols[], int ncols, int compatible)
fputs("# ", stdout); fputs("# ", stdout);
for (i = 0; i < ncols; i++) { for (i = 0; i < ncols; i++) {
if (cols[i] == COL_CACHE) { if (cols[i] == COL_CACHE) {
if (compatible && !desc->ncaches) if (mod->compat && !desc->ncaches)
continue; continue;
if (i > 0) if (i > 0)
putchar(','); putchar(',');
if (compatible && i != 0) if (mod->compat && i != 0)
putchar(','); putchar(',');
for (c = desc->ncaches - 1; c >= 0; c--) { for (c = desc->ncaches - 1; c >= 0; c--) {
printf("%s", desc->caches[c].name); printf("%s", desc->caches[c].name);
if (c > 0) if (c > 0)
putchar(compatible ? ',' : ':'); putchar(mod->compat ? ',' : ':');
} }
if (!desc->ncaches) if (!desc->ncaches)
fputs(colnames[cols[i]], stdout); fputs(colnames[cols[i]], stdout);
@ -998,7 +1015,7 @@ print_parsable(struct lscpu_desc *desc, int cols[], int ncols, int compatible)
if (desc->online && !is_cpu_online(desc, i)) if (desc->online && !is_cpu_online(desc, i))
continue; continue;
for (c = 0; c < ncols; c++) { for (c = 0; c < ncols; c++) {
if (compatible && cols[c] == COL_CACHE) { if (mod->compat && cols[c] == COL_CACHE) {
if (!desc->ncaches) if (!desc->ncaches)
continue; continue;
if (c > 0) if (c > 0)
@ -1006,7 +1023,7 @@ print_parsable(struct lscpu_desc *desc, int cols[], int ncols, int compatible)
} }
if (c > 0) if (c > 0)
putchar(','); putchar(',');
print_parsable_cell(desc, i, cols[c], compatible); print_parsable_cell(desc, i, cols[c], mod);
} }
putchar('\n'); putchar('\n');
} }
@ -1035,7 +1052,7 @@ print_cpuset(const char *key, cpu_set_t *set, int hex)
} }
static void static void
print_readable(struct lscpu_desc *desc, int hex) print_summary(struct lscpu_desc *desc, struct lscpu_modifier *mod)
{ {
char buf[512]; char buf[512];
int i; int i;
@ -1065,9 +1082,9 @@ print_readable(struct lscpu_desc *desc, int hex)
print_n(_("CPU(s):"), desc->ncpus); print_n(_("CPU(s):"), desc->ncpus);
if (desc->online) if (desc->online)
print_cpuset(hex ? _("On-line CPU(s) mask:") : print_cpuset(mod->hex ? _("On-line CPU(s) mask:") :
_("On-line CPU(s) list:"), _("On-line CPU(s) list:"),
desc->online, hex); desc->online, mod->hex);
if (desc->online && CPU_COUNT_S(setsize, desc->online) != desc->ncpus) { if (desc->online && CPU_COUNT_S(setsize, desc->online) != desc->ncpus) {
cpu_set_t *set; cpu_set_t *set;
@ -1084,9 +1101,9 @@ print_readable(struct lscpu_desc *desc, int hex)
if (!is_cpu_online(desc, i)) if (!is_cpu_online(desc, i))
CPU_SET_S(i, setsize, set); CPU_SET_S(i, setsize, set);
} }
print_cpuset(hex ? _("Off-line CPU(s) mask:") : print_cpuset(mod->hex ? _("Off-line CPU(s) mask:") :
_("Off-line CPU(s) list:"), _("Off-line CPU(s) list:"),
set, hex); set, mod->hex);
cpuset_free(set); cpuset_free(set);
} }
@ -1164,7 +1181,7 @@ print_readable(struct lscpu_desc *desc, int hex)
for (i = 0; i < desc->nnodes; i++) { for (i = 0; i < desc->nnodes; i++) {
snprintf(buf, sizeof(buf), _("NUMA node%d CPU(s):"), i); snprintf(buf, sizeof(buf), _("NUMA node%d CPU(s):"), i);
print_cpuset(buf, desc->nodemaps[i], hex); print_cpuset(buf, desc->nodemaps[i], mod->hex);
} }
} }
@ -1175,21 +1192,21 @@ static void __attribute__((__noreturn__)) usage(FILE *out)
_(" %s [options]\n"), program_invocation_short_name); _(" %s [options]\n"), program_invocation_short_name);
fputs(_("\nOptions:\n"), out); fputs(_("\nOptions:\n"), out);
fputs(_(" -h, --help print this help\n" fputs(_(" -h, --help print this help\n"
" -p, --parse <list> print out a parsable instead of a readable format\n" " -p, --parse[=<list>] print out a parsable format\n"
" -s, --sysroot <dir> use directory DIR as system root\n" " -s, --sysroot <dir> use directory DIR as system root\n"
" -x, --hex print hexadecimal masks rather than lists of CPUs\n" " -x, --hex print hexadecimal masks rather than lists of CPUs\n"
" -V, --version print version information and exit\n\n"), out); " -V, --version print version information and exit\n\n"), out);
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
struct lscpu_desc _desc, *desc = &_desc; struct lscpu_modifier _mod = { .mode = OUTPUT_SUMMARY }, *mod = &_mod;
int parsable = 0, c, i, hex = 0; struct lscpu_desc _desc = { .flags = 0 }, *desc = &_desc;
int c, i;
int columns[ARRAY_SIZE(colnames)], ncolumns = 0; int columns[ARRAY_SIZE(colnames)], ncolumns = 0;
int compatible = 0;
static const struct option longopts[] = { static const struct option longopts[] = {
{ "help", no_argument, 0, 'h' }, { "help", no_argument, 0, 'h' },
@ -1209,7 +1226,6 @@ int main(int argc, char *argv[])
case 'h': case 'h':
usage(stdout); usage(stdout);
case 'p': case 'p':
parsable = 1;
if (optarg) { if (optarg) {
if (*optarg == '=') if (*optarg == '=')
optarg++; optarg++;
@ -1218,14 +1234,8 @@ int main(int argc, char *argv[])
column_name_to_id); column_name_to_id);
if (ncolumns < 0) if (ncolumns < 0)
return EXIT_FAILURE; return EXIT_FAILURE;
} else {
columns[ncolumns++] = COL_CPU;
columns[ncolumns++] = COL_CORE;
columns[ncolumns++] = COL_SOCKET;
columns[ncolumns++] = COL_NODE;
columns[ncolumns++] = COL_CACHE;
compatible = 1;
} }
mod->mode = OUTPUT_PARSABLE;
break; break;
case 's': case 's':
sysrootlen = strlen(optarg); sysrootlen = strlen(optarg);
@ -1233,7 +1243,7 @@ int main(int argc, char *argv[])
pathbuf[sizeof(pathbuf) - 1] = '\0'; pathbuf[sizeof(pathbuf) - 1] = '\0';
break; break;
case 'x': case 'x':
hex = 1; mod->hex = 1;
break; break;
case 'V': case 'V':
printf(_("%s from %s\n"), program_invocation_short_name, printf(_("%s from %s\n"), program_invocation_short_name,
@ -1244,8 +1254,6 @@ int main(int argc, char *argv[])
} }
} }
memset(desc, 0, sizeof(*desc));
read_basicinfo(desc); read_basicinfo(desc);
for (i = 0; i < desc->ncpus; i++) { for (i = 0; i < desc->ncpus; i++) {
@ -1260,14 +1268,24 @@ int main(int argc, char *argv[])
qsort(desc->caches, desc->ncaches, sizeof(struct cpu_cache), cachecmp); qsort(desc->caches, desc->ncaches, sizeof(struct cpu_cache), cachecmp);
read_nodes(desc); read_nodes(desc);
read_hypervisor(desc); read_hypervisor(desc);
/* Show time! */ switch(mod->mode) {
if (parsable) case OUTPUT_SUMMARY:
print_parsable(desc, columns, ncolumns, compatible); print_summary(desc, mod);
else break;
print_readable(desc, hex); case OUTPUT_PARSABLE:
if (!ncolumns) {
columns[ncolumns++] = COL_CPU;
columns[ncolumns++] = COL_CORE;
columns[ncolumns++] = COL_SOCKET;
columns[ncolumns++] = COL_NODE;
columns[ncolumns++] = COL_CACHE;
mod->compat = 1;
}
print_parsable(desc, columns, ncolumns, mod);
break;
}
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }