2008-07-17 07:19:40 -05:00
|
|
|
/*
|
|
|
|
* lscpu - CPU architecture information helper
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 Cai Qian <qcai@redhat.com>
|
|
|
|
* Copyright (C) 2008 Karel Zak <kzak@redhat.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/utsname.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#include "nls.h"
|
|
|
|
|
|
|
|
#define CACHE_MAX 100
|
|
|
|
|
|
|
|
/* /sys paths */
|
2010-05-28 03:45:41 -05:00
|
|
|
#define _PATH_SYS_SYSTEM "/sys/devices/system"
|
2008-07-17 07:19:40 -05:00
|
|
|
#define _PATH_SYS_CPU0 _PATH_SYS_SYSTEM "/cpu/cpu0"
|
2010-05-28 03:45:41 -05:00
|
|
|
#define _PATH_PROC_XEN "/proc/xen"
|
2008-09-30 18:29:32 -05:00
|
|
|
#define _PATH_PROC_XENCAP _PATH_PROC_XEN "/capabilities"
|
2010-05-28 03:45:41 -05:00
|
|
|
#define _PATH_PROC_CPUINFO "/proc/cpuinfo"
|
|
|
|
#define _PATH_PROC_PCIDEVS "/proc/bus/pci/devices"
|
2008-07-17 07:19:40 -05:00
|
|
|
|
|
|
|
int have_topology;
|
|
|
|
int have_cache;
|
|
|
|
int have_node;
|
|
|
|
|
2008-08-10 02:33:51 -05:00
|
|
|
|
2008-09-30 18:29:32 -05:00
|
|
|
/* virtualization types */
|
|
|
|
enum {
|
|
|
|
VIRT_NONE = 0,
|
|
|
|
VIRT_PARA,
|
|
|
|
VIRT_FULL
|
|
|
|
};
|
|
|
|
const char *virt_types[] = {
|
|
|
|
[VIRT_NONE] = N_("none"),
|
|
|
|
[VIRT_PARA] = N_("para"),
|
|
|
|
[VIRT_FULL] = N_("full")
|
|
|
|
};
|
|
|
|
|
|
|
|
/* hypervisor vendors */
|
|
|
|
enum {
|
|
|
|
HYPER_NONE = 0,
|
|
|
|
HYPER_XEN,
|
|
|
|
HYPER_KVM,
|
|
|
|
HYPER_MSHV
|
|
|
|
};
|
|
|
|
const char *hv_vendors[] = {
|
|
|
|
[HYPER_NONE] = NULL,
|
|
|
|
[HYPER_XEN] = "Xen",
|
|
|
|
[HYPER_KVM] = "KVM",
|
|
|
|
[HYPER_MSHV] = "Microsoft"
|
|
|
|
};
|
|
|
|
|
2010-01-07 10:34:03 -06:00
|
|
|
/* CPU modes (bits) */
|
|
|
|
enum {
|
|
|
|
MODE_REAL = (1 << 1),
|
|
|
|
MODE_TRANSPARENT = (1 << 2),
|
|
|
|
MODE_LONG = (1 << 3)
|
|
|
|
};
|
2008-09-30 18:29:32 -05:00
|
|
|
|
2010-05-27 16:39:41 -05:00
|
|
|
/* cache(s) description */
|
|
|
|
struct cpu_cache {
|
|
|
|
char *name;
|
|
|
|
char *size;
|
|
|
|
int map;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* global description */
|
|
|
|
struct lscpu_desc {
|
2008-07-17 07:19:40 -05:00
|
|
|
/* counters */
|
|
|
|
int ct_cpu;
|
|
|
|
int ct_thread;
|
|
|
|
int ct_core;
|
|
|
|
int ct_socket;
|
|
|
|
int ct_node;
|
|
|
|
int ct_cache;
|
|
|
|
|
|
|
|
/* who is who */
|
|
|
|
char *arch;
|
|
|
|
char *vendor;
|
|
|
|
char *family;
|
|
|
|
char *model;
|
2008-09-30 18:29:32 -05:00
|
|
|
char *virtflag; /* virtualization flag (vmx, svm) */
|
|
|
|
int hyper; /* hypervisor vendor ID */
|
|
|
|
int virtype; /* VIRT_PARA|FULL|NONE ? */
|
2008-07-17 07:19:40 -05:00
|
|
|
|
|
|
|
/* caches */
|
2010-05-27 16:39:41 -05:00
|
|
|
struct cpu_cache cache[CACHE_MAX];
|
2008-07-17 07:19:40 -05:00
|
|
|
|
|
|
|
/* misc */
|
|
|
|
char *mhz;
|
|
|
|
char *stepping;
|
|
|
|
char *flags;
|
|
|
|
|
2010-01-07 10:34:03 -06:00
|
|
|
int mode; /* rm, lm or/and tm */
|
|
|
|
|
2008-07-17 07:19:40 -05:00
|
|
|
/* NUMA */
|
|
|
|
int *nodecpu;
|
|
|
|
};
|
|
|
|
|
2010-05-28 03:45:41 -05:00
|
|
|
static size_t sysrootlen;
|
|
|
|
static char pathbuf[PATH_MAX];
|
2008-07-17 07:19:40 -05:00
|
|
|
|
2010-05-28 03:45:41 -05:00
|
|
|
static const char *path_create(const char *path, ...)
|
|
|
|
__attribute__ ((__format__ (__printf__, 1, 2)));
|
|
|
|
static FILE *path_fopen(const char *mode, int exit_on_err, const char *path, ...)
|
|
|
|
__attribute__ ((__format__ (__printf__, 3, 4)));
|
2010-05-27 10:24:28 -05:00
|
|
|
static void path_getstr(char *result, size_t len, const char *path, ...)
|
|
|
|
__attribute__ ((__format__ (__printf__, 3, 4)));
|
|
|
|
static int path_getnum(const char *path, ...)
|
|
|
|
__attribute__ ((__format__ (__printf__, 1, 2)));
|
2008-07-17 07:19:40 -05:00
|
|
|
static int path_exist(const char *path, ...)
|
|
|
|
__attribute__ ((__format__ (__printf__, 1, 2)));
|
|
|
|
static int path_sibling(const char *path, ...)
|
|
|
|
__attribute__ ((__format__ (__printf__, 1, 2)));
|
|
|
|
|
2010-05-28 03:45:41 -05:00
|
|
|
static const char *
|
|
|
|
path_vcreate(const char *path, va_list ap)
|
|
|
|
{
|
|
|
|
if (sysrootlen)
|
|
|
|
vsnprintf(pathbuf + sysrootlen,
|
|
|
|
sizeof(pathbuf) - sysrootlen, path, ap);
|
|
|
|
else
|
|
|
|
vsnprintf(pathbuf, sizeof(pathbuf), path, ap);
|
|
|
|
return pathbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
path_create(const char *path, ...)
|
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, path);
|
|
|
|
p = path_vcreate(path, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2008-07-17 07:19:40 -05:00
|
|
|
static FILE *
|
2010-05-28 03:45:41 -05:00
|
|
|
path_vfopen(const char *mode, int exit_on_error, const char *path, va_list ap)
|
2008-07-17 07:19:40 -05:00
|
|
|
{
|
2010-05-28 03:45:41 -05:00
|
|
|
FILE *f;
|
|
|
|
const char *p = path_vcreate(path, ap);
|
|
|
|
|
|
|
|
f = fopen(p, mode);
|
|
|
|
if (!f && exit_on_error)
|
|
|
|
err(EXIT_FAILURE, _("error: cannot open %s"), p);
|
|
|
|
return f;
|
2008-07-17 07:19:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static FILE *
|
2010-05-28 03:45:41 -05:00
|
|
|
path_fopen(const char *mode, int exit_on_error, const char *path, ...)
|
2008-07-17 07:19:40 -05:00
|
|
|
{
|
2010-05-28 03:45:41 -05:00
|
|
|
FILE *fd;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, path);
|
|
|
|
fd = path_vfopen("r", exit_on_error, path, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return fd;
|
2008-07-17 07:19:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-05-27 10:24:28 -05:00
|
|
|
path_getstr(char *result, size_t len, const char *path, ...)
|
|
|
|
{
|
|
|
|
FILE *fd;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, path);
|
2010-05-28 03:45:41 -05:00
|
|
|
fd = path_vfopen("r", 1, path, ap);
|
2010-05-27 10:24:28 -05:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
if (!fgets(result, len, fd))
|
|
|
|
err(EXIT_FAILURE, _("failed to read: %s"), pathbuf);
|
|
|
|
fclose(fd);
|
|
|
|
|
|
|
|
len = strlen(result);
|
|
|
|
if (result[len - 1] == '\n')
|
|
|
|
result[len - 1] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
path_getnum(const char *path, ...)
|
2008-07-17 07:19:40 -05:00
|
|
|
{
|
|
|
|
FILE *fd;
|
|
|
|
va_list ap;
|
2010-05-27 10:24:28 -05:00
|
|
|
int result;
|
2008-07-17 07:19:40 -05:00
|
|
|
|
|
|
|
va_start(ap, path);
|
2010-05-28 03:45:41 -05:00
|
|
|
fd = path_vfopen("r", 1, path, ap);
|
2008-07-17 07:19:40 -05:00
|
|
|
va_end(ap);
|
|
|
|
|
2010-05-27 10:24:28 -05:00
|
|
|
if (fscanf(fd, "%d", &result) != 1) {
|
2008-07-17 07:19:40 -05:00
|
|
|
if (ferror(fd))
|
2010-05-28 03:45:41 -05:00
|
|
|
err(EXIT_FAILURE, _("failed to read: %s"), pathbuf);
|
2008-07-17 07:19:40 -05:00
|
|
|
else
|
2010-05-28 03:45:41 -05:00
|
|
|
errx(EXIT_FAILURE, _("parse error: %s"), pathbuf);
|
2008-07-17 07:19:40 -05:00
|
|
|
}
|
|
|
|
fclose(fd);
|
2010-05-27 10:24:28 -05:00
|
|
|
return result;
|
2008-07-17 07:19:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
path_exist(const char *path, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
2010-05-28 03:45:41 -05:00
|
|
|
const char *p;
|
2008-07-17 07:19:40 -05:00
|
|
|
|
|
|
|
va_start(ap, path);
|
2010-05-28 03:45:41 -05:00
|
|
|
p = path_vcreate(path, ap);
|
2008-07-17 07:19:40 -05:00
|
|
|
va_end(ap);
|
|
|
|
|
2010-05-28 03:45:41 -05:00
|
|
|
return access(p, F_OK) == 0;
|
2008-07-17 07:19:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
xstrdup(const char *str)
|
|
|
|
{
|
|
|
|
char *s = strdup(str);
|
|
|
|
if (!s)
|
|
|
|
err(EXIT_FAILURE, _("error: strdup failed"));
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* count the set bit in a mapping file */
|
|
|
|
static int
|
|
|
|
path_sibling(const char *path, ...)
|
|
|
|
{
|
|
|
|
int c, n;
|
|
|
|
int result = 0;
|
|
|
|
char s[2];
|
|
|
|
FILE *fp;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, path);
|
2010-05-28 03:45:41 -05:00
|
|
|
fp = path_vfopen("r", 1, path, ap);
|
2008-07-17 07:19:40 -05:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
while ((c = fgetc(fp)) != EOF) {
|
|
|
|
if (isxdigit(c)) {
|
|
|
|
s[0] = c;
|
|
|
|
s[1] = '\0';
|
|
|
|
for (n = strtol(s, NULL, 16); n > 0; n /= 2) {
|
|
|
|
if (n % 2)
|
|
|
|
result++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Lookup a pattern and get the value from cpuinfo.
|
|
|
|
* Format is:
|
|
|
|
*
|
|
|
|
* "<pattern> : <key>"
|
|
|
|
*/
|
|
|
|
int lookup(char *line, char *pattern, char **value)
|
|
|
|
{
|
|
|
|
char *p, *v;
|
|
|
|
int len = strlen(pattern);
|
|
|
|
|
|
|
|
if (!*line)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* pattern */
|
|
|
|
if (strncmp(line, pattern, len))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* white spaces */
|
|
|
|
for (p = line + len; isspace(*p); p++);
|
|
|
|
|
|
|
|
/* separator */
|
|
|
|
if (*p != ':')
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* white spaces */
|
|
|
|
for (++p; isspace(*p); p++);
|
|
|
|
|
|
|
|
/* value */
|
|
|
|
if (!*p)
|
|
|
|
return 0;
|
|
|
|
v = p;
|
|
|
|
|
|
|
|
/* end of value */
|
|
|
|
len = strlen(line) - 1;
|
|
|
|
for (p = line + len; isspace(*(p-1)); p--);
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
*value = xstrdup(v);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-05-27 16:39:41 -05:00
|
|
|
read_basicinfo(struct lscpu_desc *desc)
|
2008-07-17 07:19:40 -05:00
|
|
|
{
|
2010-05-28 03:45:41 -05:00
|
|
|
FILE *fp = path_fopen("r", 1, _PATH_PROC_CPUINFO);
|
2008-07-17 07:19:40 -05:00
|
|
|
char buf[BUFSIZ];
|
|
|
|
struct utsname utsbuf;
|
|
|
|
|
|
|
|
/* architecture */
|
|
|
|
if (uname(&utsbuf) == -1)
|
|
|
|
err(EXIT_FAILURE, _("error: uname failed"));
|
2010-05-27 16:39:41 -05:00
|
|
|
desc->arch = xstrdup(utsbuf.machine);
|
2008-07-17 07:19:40 -05:00
|
|
|
|
|
|
|
/* count CPU(s) */
|
2010-05-27 16:39:41 -05:00
|
|
|
while(path_exist(_PATH_SYS_SYSTEM "/cpu/cpu%d", desc->ct_cpu))
|
|
|
|
desc->ct_cpu++;
|
2008-07-17 07:19:40 -05:00
|
|
|
|
|
|
|
/* details */
|
|
|
|
while (fgets(buf, sizeof(buf), fp) != NULL) {
|
|
|
|
/* IA64 */
|
2010-05-27 16:39:41 -05:00
|
|
|
if (lookup(buf, "vendor", &desc->vendor)) ;
|
|
|
|
else if (lookup(buf, "vendor_id", &desc->vendor)) ;
|
2008-07-17 07:19:40 -05:00
|
|
|
/* IA64 */
|
2010-05-27 16:39:41 -05:00
|
|
|
else if (lookup(buf, "family", &desc->family)) ;
|
|
|
|
else if (lookup(buf, "cpu family", &desc->family)) ;
|
|
|
|
else if (lookup(buf, "model", &desc->model)) ;
|
|
|
|
else if (lookup(buf, "stepping", &desc->stepping)) ;
|
|
|
|
else if (lookup(buf, "cpu MHz", &desc->mhz)) ;
|
|
|
|
else if (lookup(buf, "flags", &desc->flags)) ;
|
2008-07-17 07:19:40 -05:00
|
|
|
else
|
|
|
|
continue;
|
|
|
|
}
|
2008-09-30 18:29:32 -05:00
|
|
|
|
2010-05-27 16:39:41 -05:00
|
|
|
if (desc->flags) {
|
|
|
|
snprintf(buf, sizeof(buf), " %s ", desc->flags);
|
2008-09-30 18:29:32 -05:00
|
|
|
if (strstr(buf, " svm "))
|
2010-05-27 16:39:41 -05:00
|
|
|
desc->virtflag = strdup("svm");
|
2008-09-30 18:29:32 -05:00
|
|
|
else if (strstr(buf, " vmx "))
|
2010-05-27 16:39:41 -05:00
|
|
|
desc->virtflag = strdup("vmx");
|
2010-01-07 10:34:03 -06:00
|
|
|
|
|
|
|
if (strstr(buf, " rm "))
|
2010-05-27 16:39:41 -05:00
|
|
|
desc->mode |= MODE_REAL;
|
2010-01-07 10:34:03 -06:00
|
|
|
if (strstr(buf, " tm "))
|
2010-05-27 16:39:41 -05:00
|
|
|
desc->mode |= MODE_TRANSPARENT;
|
2010-01-07 10:34:03 -06:00
|
|
|
if (strstr(buf, " lm "))
|
2010-05-27 16:39:41 -05:00
|
|
|
desc->mode |= MODE_LONG;
|
2008-09-30 18:29:32 -05:00
|
|
|
}
|
|
|
|
|
2008-07-17 07:19:40 -05:00
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
2008-09-30 18:29:32 -05:00
|
|
|
static int
|
|
|
|
has_pci_device(int vendor, int device)
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
int num, fn, ven, dev;
|
|
|
|
int res = 1;
|
|
|
|
|
2010-05-28 03:45:41 -05:00
|
|
|
f = path_fopen("r", 0, _PATH_PROC_PCIDEVS);
|
2008-09-30 18:29:32 -05:00
|
|
|
if (!f)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* for more details about bus/pci/devices format see
|
|
|
|
* drivers/pci/proc.c in linux kernel
|
|
|
|
*/
|
|
|
|
while(fscanf(f, "%02x%02x\t%04x%04x\t%*[^\n]",
|
|
|
|
&num, &fn, &ven, &dev) == 4) {
|
|
|
|
|
|
|
|
if (ven == vendor && dev == device)
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = 0;
|
|
|
|
found:
|
|
|
|
fclose(f);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(__x86_64__) || defined(__i386__)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This CPUID leaf returns the information about the hypervisor.
|
|
|
|
* EAX : maximum input value for CPUID supported by the hypervisor.
|
|
|
|
* EBX, ECX, EDX : Hypervisor vendor ID signature. E.g. VMwareVMware.
|
|
|
|
*/
|
|
|
|
#define HYPERVISOR_INFO_LEAF 0x40000000
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
cpuid(unsigned int op, unsigned int *eax, unsigned int *ebx,
|
|
|
|
unsigned int *ecx, unsigned int *edx)
|
|
|
|
{
|
2009-05-10 15:37:18 -05:00
|
|
|
__asm__(
|
|
|
|
#if defined(__PIC__) && defined(__i386__)
|
|
|
|
/* x86 PIC cannot clobber ebx -- gcc bitches */
|
|
|
|
"pushl %%ebx;"
|
|
|
|
"cpuid;"
|
|
|
|
"movl %%ebx, %%esi;"
|
|
|
|
"popl %%ebx;"
|
|
|
|
: "=S" (*ebx),
|
|
|
|
#else
|
|
|
|
"cpuid;"
|
|
|
|
: "=b" (*ebx),
|
|
|
|
#endif
|
|
|
|
"=a" (*eax),
|
2008-09-30 18:29:32 -05:00
|
|
|
"=c" (*ecx),
|
|
|
|
"=d" (*edx)
|
2010-03-01 04:35:54 -06:00
|
|
|
: "1" (op), "c"(0));
|
2008-09-30 18:29:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-05-27 16:39:41 -05:00
|
|
|
read_hypervisor_cpuid(struct lscpu_desc *desc)
|
2008-09-30 18:29:32 -05:00
|
|
|
{
|
|
|
|
unsigned int eax = 0, ebx = 0, ecx = 0, edx = 0;
|
|
|
|
char hyper_vendor_id[13];
|
|
|
|
|
|
|
|
memset(hyper_vendor_id, 0, sizeof(hyper_vendor_id));
|
|
|
|
|
|
|
|
cpuid(HYPERVISOR_INFO_LEAF, &eax, &ebx, &ecx, &edx);
|
|
|
|
memcpy(hyper_vendor_id + 0, &ebx, 4);
|
|
|
|
memcpy(hyper_vendor_id + 4, &ecx, 4);
|
|
|
|
memcpy(hyper_vendor_id + 8, &edx, 4);
|
|
|
|
hyper_vendor_id[12] = '\0';
|
|
|
|
|
|
|
|
if (!hyper_vendor_id[0])
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!strncmp("XenVMMXenVMM", hyper_vendor_id, 12))
|
2010-05-27 16:39:41 -05:00
|
|
|
desc->hyper = HYPER_XEN;
|
2008-09-30 18:29:32 -05:00
|
|
|
else if (!strncmp("KVMKVMKVM", hyper_vendor_id, 9))
|
2010-05-27 16:39:41 -05:00
|
|
|
desc->hyper = HYPER_KVM;
|
2008-09-30 18:29:32 -05:00
|
|
|
else if (!strncmp("Microsoft Hv", hyper_vendor_id, 12))
|
2010-05-27 16:39:41 -05:00
|
|
|
desc->hyper = HYPER_MSHV;
|
2008-09-30 18:29:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#else /* ! __x86_64__ */
|
|
|
|
static void
|
2010-05-27 16:39:41 -05:00
|
|
|
read_hypervisor_cpuid(struct lscpu_desc *desc)
|
2008-09-30 18:29:32 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void
|
2010-05-27 16:39:41 -05:00
|
|
|
read_hypervisor(struct lscpu_desc *desc)
|
2008-09-30 18:29:32 -05:00
|
|
|
{
|
2010-05-27 16:39:41 -05:00
|
|
|
read_hypervisor_cpuid(desc);
|
2008-09-30 18:29:32 -05:00
|
|
|
|
2010-05-27 16:39:41 -05:00
|
|
|
if (desc->hyper)
|
2008-09-30 18:29:32 -05:00
|
|
|
/* hvm */
|
2010-05-27 16:39:41 -05:00
|
|
|
desc->virtype = VIRT_FULL;
|
2008-09-30 18:29:32 -05:00
|
|
|
|
2010-05-28 03:45:41 -05:00
|
|
|
else if (path_exist(_PATH_PROC_XEN)) {
|
2008-09-30 18:29:32 -05:00
|
|
|
/* Xen para-virt or dom0 */
|
2010-05-28 03:45:41 -05:00
|
|
|
FILE *fd = path_fopen("r", 0, _PATH_PROC_XENCAP);
|
2008-09-30 18:29:32 -05:00
|
|
|
int dom0 = 0;
|
|
|
|
|
|
|
|
if (fd) {
|
|
|
|
char buf[256];
|
|
|
|
|
|
|
|
if (fscanf(fd, "%s", buf) == 1 &&
|
|
|
|
!strcmp(buf, "control_d"))
|
|
|
|
dom0 = 1;
|
|
|
|
fclose(fd);
|
|
|
|
}
|
2010-05-27 16:39:41 -05:00
|
|
|
desc->virtype = dom0 ? VIRT_NONE : VIRT_PARA;
|
|
|
|
desc->hyper = HYPER_XEN;
|
2008-09-30 18:29:32 -05:00
|
|
|
|
|
|
|
} else if (has_pci_device(0x5853, 0x0001)) {
|
|
|
|
/* Xen full-virt on non-x86_64 */
|
2010-05-27 16:39:41 -05:00
|
|
|
desc->hyper = HYPER_XEN;
|
|
|
|
desc->virtype = VIRT_FULL;
|
2008-09-30 18:29:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-17 07:19:40 -05:00
|
|
|
static void
|
2010-05-27 16:39:41 -05:00
|
|
|
read_topology(struct lscpu_desc *desc)
|
2008-07-17 07:19:40 -05:00
|
|
|
{
|
|
|
|
/* number of threads */
|
2010-05-27 16:39:41 -05:00
|
|
|
desc->ct_thread = path_sibling(
|
2008-07-17 07:19:40 -05:00
|
|
|
_PATH_SYS_CPU0 "/topology/thread_siblings");
|
|
|
|
|
|
|
|
/* number of cores */
|
2010-05-27 16:39:41 -05:00
|
|
|
desc->ct_core = path_sibling(
|
2008-07-17 07:19:40 -05:00
|
|
|
_PATH_SYS_CPU0 "/topology/core_siblings")
|
2010-05-27 16:39:41 -05:00
|
|
|
/ desc->ct_thread;
|
2008-07-17 07:19:40 -05:00
|
|
|
|
|
|
|
/* number of sockets */
|
2010-05-27 16:39:41 -05:00
|
|
|
desc->ct_socket = desc->ct_cpu / desc->ct_core / desc->ct_thread;
|
2008-07-17 07:19:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-05-27 16:39:41 -05:00
|
|
|
read_cache(struct lscpu_desc *desc)
|
2008-07-17 07:19:40 -05:00
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
DIR *dp;
|
|
|
|
struct dirent *dir;
|
|
|
|
int level, type;
|
2010-05-28 03:45:41 -05:00
|
|
|
const char *p = path_create(_PATH_SYS_CPU0 "/cache");
|
2008-07-17 07:19:40 -05:00
|
|
|
|
2010-05-28 03:45:41 -05:00
|
|
|
dp = opendir(p);
|
2008-07-17 07:19:40 -05:00
|
|
|
if (dp == NULL)
|
2010-05-28 03:45:41 -05:00
|
|
|
err(EXIT_FAILURE, _("error: %s"), p);
|
2008-07-17 07:19:40 -05:00
|
|
|
|
|
|
|
while ((dir = readdir(dp)) != NULL) {
|
|
|
|
if (!strcmp(dir->d_name, ".")
|
|
|
|
|| !strcmp(dir->d_name, ".."))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* cache type */
|
2010-05-27 10:24:28 -05:00
|
|
|
path_getstr(buf, sizeof(buf),
|
|
|
|
_PATH_SYS_CPU0 "/cache/%s/type", dir->d_name);
|
2008-07-17 07:19:40 -05:00
|
|
|
if (!strcmp(buf, "Data"))
|
|
|
|
type = 'd';
|
|
|
|
else if (!strcmp(buf, "Instruction"))
|
|
|
|
type = 'i';
|
|
|
|
else
|
|
|
|
type = 0;
|
|
|
|
|
|
|
|
/* cache level */
|
2010-05-27 10:24:28 -05:00
|
|
|
level = path_getnum(_PATH_SYS_CPU0 "/cache/%s/level",
|
|
|
|
dir->d_name);
|
2008-07-17 07:19:40 -05:00
|
|
|
|
|
|
|
if (type)
|
|
|
|
snprintf(buf, sizeof(buf), "L%d%c", level, type);
|
|
|
|
else
|
|
|
|
snprintf(buf, sizeof(buf), "L%d", level);
|
|
|
|
|
2010-05-27 16:39:41 -05:00
|
|
|
desc->cache[desc->ct_cache].name = xstrdup(buf);
|
2008-07-17 07:19:40 -05:00
|
|
|
|
|
|
|
/* cache size */
|
2010-05-27 10:24:28 -05:00
|
|
|
path_getstr(buf, sizeof(buf),
|
|
|
|
_PATH_SYS_CPU0 "/cache/%s/size", dir->d_name);
|
2010-05-27 16:39:41 -05:00
|
|
|
desc->cache[desc->ct_cache].size = xstrdup(buf);
|
2008-07-17 07:19:40 -05:00
|
|
|
|
|
|
|
/* information about how CPUs share different caches */
|
2010-05-27 16:39:41 -05:00
|
|
|
desc->cache[desc->ct_cache].map = path_sibling(
|
2008-07-17 07:19:40 -05:00
|
|
|
_PATH_SYS_CPU0 "/cache/%s/shared_cpu_map",
|
|
|
|
dir->d_name);
|
2010-05-27 16:39:41 -05:00
|
|
|
desc->ct_cache++;
|
2008-07-17 07:19:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-05-27 16:39:41 -05:00
|
|
|
read_nodes(struct lscpu_desc *desc)
|
2008-07-17 07:19:40 -05:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* number of NUMA node */
|
2010-05-27 16:39:41 -05:00
|
|
|
while (path_exist(_PATH_SYS_SYSTEM "/node/node%d", desc->ct_node))
|
|
|
|
desc->ct_node++;
|
2008-07-17 07:19:40 -05:00
|
|
|
|
2010-05-27 16:39:41 -05:00
|
|
|
desc->nodecpu = (int *) malloc(desc->ct_node * sizeof(int));
|
|
|
|
if (!desc->nodecpu)
|
2008-07-17 07:19:40 -05:00
|
|
|
err(EXIT_FAILURE, _("error: malloc failed"));
|
|
|
|
|
|
|
|
/* information about how nodes share different CPUs */
|
2010-05-27 16:39:41 -05:00
|
|
|
for (i = 0; i < desc->ct_node; i++)
|
|
|
|
desc->nodecpu[i] = path_sibling(
|
2008-07-17 07:19:40 -05:00
|
|
|
_PATH_SYS_SYSTEM "/node/node%d/cpumap",
|
|
|
|
i);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
check_system(void)
|
|
|
|
{
|
|
|
|
/* Read through sysfs. */
|
2010-05-28 03:45:41 -05:00
|
|
|
if (!path_exist(_PATH_SYS_SYSTEM))
|
2008-08-10 02:33:51 -05:00
|
|
|
errx(EXIT_FAILURE,
|
2010-05-28 03:45:41 -05:00
|
|
|
_("error: %s is not accessable."), pathbuf);
|
2008-07-17 07:19:40 -05:00
|
|
|
|
2010-05-28 03:45:41 -05:00
|
|
|
if (path_exist(_PATH_SYS_SYSTEM "/node"))
|
2008-07-17 07:19:40 -05:00
|
|
|
have_node = 1;
|
|
|
|
|
2010-05-28 03:45:41 -05:00
|
|
|
if (path_exist(_PATH_SYS_CPU0 "/topology/thread_siblings"))
|
2008-07-17 07:19:40 -05:00
|
|
|
have_topology = 1;
|
|
|
|
|
2010-05-28 03:45:41 -05:00
|
|
|
if (path_exist(_PATH_SYS_CPU0 "/cache"))
|
2008-07-17 07:19:40 -05:00
|
|
|
have_cache = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-05-27 16:39:41 -05:00
|
|
|
print_parsable(struct lscpu_desc *desc)
|
2008-07-17 07:19:40 -05:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
|
2008-08-10 02:33:51 -05:00
|
|
|
printf(_(
|
2008-07-17 07:19:40 -05:00
|
|
|
"# The following is the parsable format, which can be fed to other\n"
|
2008-08-10 02:33:51 -05:00
|
|
|
"# programs. Each different item in every column has an unique ID\n"
|
2008-07-17 07:19:40 -05:00
|
|
|
"# starting from zero.\n"
|
2008-08-10 02:33:51 -05:00
|
|
|
"# CPU,Core,Socket,Node"));
|
2008-07-17 07:19:40 -05:00
|
|
|
|
|
|
|
if (have_cache) {
|
|
|
|
/* separator between CPU topology and cache information */
|
|
|
|
putchar(',');
|
|
|
|
|
2010-05-27 16:39:41 -05:00
|
|
|
for (i = desc->ct_cache - 1; i >= 0; i--)
|
|
|
|
printf(",%s", desc->cache[i].name);
|
2008-07-17 07:19:40 -05:00
|
|
|
}
|
|
|
|
putchar('\n');
|
|
|
|
|
2010-05-27 16:39:41 -05:00
|
|
|
for (i = 0; i < desc->ct_cpu; i++) {
|
2008-07-17 07:19:40 -05:00
|
|
|
printf("%d", i);
|
|
|
|
|
|
|
|
if (have_topology)
|
|
|
|
printf(",%d,%d",
|
2010-05-27 16:39:41 -05:00
|
|
|
i / desc->ct_thread,
|
|
|
|
i / desc->ct_core / desc->ct_thread);
|
2008-07-17 07:19:40 -05:00
|
|
|
else
|
|
|
|
printf(",,");
|
|
|
|
|
|
|
|
if (have_node) {
|
|
|
|
int c = 0;
|
|
|
|
|
2010-05-27 16:39:41 -05:00
|
|
|
for (j = 0; j < desc->ct_node; j++) {
|
|
|
|
c += desc->nodecpu[j];
|
2008-07-17 07:19:40 -05:00
|
|
|
if (i < c) {
|
|
|
|
printf(",%d", j);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
putchar(',');
|
|
|
|
|
|
|
|
if (have_cache) {
|
|
|
|
putchar(',');
|
|
|
|
|
2010-05-27 16:39:41 -05:00
|
|
|
for (j = desc->ct_cache - 1; j >= 0; j--) {
|
2008-07-17 07:19:40 -05:00
|
|
|
/* If shared_cpu_map is 0, all CPUs share the same
|
|
|
|
cache. */
|
2010-05-27 16:39:41 -05:00
|
|
|
if (desc->cache[j].map == 0)
|
|
|
|
desc->cache[j].map = desc->ct_core *
|
|
|
|
desc->ct_thread;
|
2008-07-17 07:19:40 -05:00
|
|
|
|
2010-05-27 16:39:41 -05:00
|
|
|
printf(",%d", i / desc->cache[j].map);
|
2008-07-17 07:19:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* output formats "<key> <value>"*/
|
|
|
|
#define print_s(_key, _val) printf("%-23s%s\n", _key, _val)
|
|
|
|
#define print_n(_key, _val) printf("%-23s%d\n", _key, _val)
|
|
|
|
|
|
|
|
static void
|
2010-05-27 16:39:41 -05:00
|
|
|
print_readable(struct lscpu_desc *desc)
|
2008-07-17 07:19:40 -05:00
|
|
|
{
|
2010-05-27 16:39:41 -05:00
|
|
|
print_s("Architecture:", desc->arch);
|
2010-01-07 10:34:03 -06:00
|
|
|
|
2010-05-27 16:39:41 -05:00
|
|
|
if (desc->mode & (MODE_REAL | MODE_TRANSPARENT | MODE_LONG)) {
|
2010-01-07 10:34:03 -06:00
|
|
|
char buf[64], *p = buf;
|
|
|
|
|
2010-05-27 16:39:41 -05:00
|
|
|
if (desc->mode & MODE_REAL) {
|
2010-01-07 10:34:03 -06:00
|
|
|
strcpy(p, "16-bit, ");
|
|
|
|
p += 8;
|
|
|
|
}
|
2010-05-27 16:39:41 -05:00
|
|
|
if (desc->mode & MODE_TRANSPARENT) {
|
2010-01-07 10:34:03 -06:00
|
|
|
strcpy(p, "32-bit, ");
|
|
|
|
p += 8;
|
|
|
|
}
|
2010-05-27 16:39:41 -05:00
|
|
|
if (desc->mode & MODE_LONG) {
|
2010-01-07 10:34:03 -06:00
|
|
|
strcpy(p, "64-bit, ");
|
|
|
|
p += 8;
|
|
|
|
}
|
|
|
|
*(p - 2) = '\0';
|
|
|
|
print_s(_("CPU op-mode(s):"), buf);
|
|
|
|
}
|
|
|
|
|
2010-05-27 16:39:41 -05:00
|
|
|
print_n("CPU(s):", desc->ct_cpu);
|
2008-07-17 07:19:40 -05:00
|
|
|
|
|
|
|
if (have_topology) {
|
2010-05-27 16:39:41 -05:00
|
|
|
print_n(_("Thread(s) per core:"), desc->ct_thread);
|
|
|
|
print_n(_("Core(s) per socket:"), desc->ct_core);
|
|
|
|
print_n(_("CPU socket(s):"), desc->ct_socket);
|
2008-07-17 07:19:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (have_node)
|
2010-05-27 16:39:41 -05:00
|
|
|
print_n(_("NUMA node(s):"), desc->ct_node);
|
|
|
|
if (desc->vendor)
|
|
|
|
print_s(_("Vendor ID:"), desc->vendor);
|
|
|
|
if (desc->family)
|
|
|
|
print_s(_("CPU family:"), desc->family);
|
|
|
|
if (desc->model)
|
|
|
|
print_s(_("Model:"), desc->model);
|
|
|
|
if (desc->stepping)
|
|
|
|
print_s(_("Stepping:"), desc->stepping);
|
|
|
|
if (desc->mhz)
|
|
|
|
print_s(_("CPU MHz:"), desc->mhz);
|
|
|
|
if (desc->virtflag) {
|
|
|
|
if (!strcmp(desc->virtflag, "svm"))
|
2008-07-17 07:19:40 -05:00
|
|
|
print_s(_("Virtualization:"), "AMD-V");
|
2010-05-27 16:39:41 -05:00
|
|
|
else if (!strcmp(desc->virtflag, "vmx"))
|
2008-07-17 07:19:40 -05:00
|
|
|
print_s(_("Virtualization:"), "VT-x");
|
|
|
|
}
|
2010-05-27 16:39:41 -05:00
|
|
|
if (desc->hyper) {
|
|
|
|
print_s(_("Hypervisor vendor:"), hv_vendors[desc->hyper]);
|
|
|
|
print_s(_("Virtualization type:"), virt_types[desc->virtype]);
|
2008-09-30 18:29:32 -05:00
|
|
|
}
|
2008-07-17 07:19:40 -05:00
|
|
|
if (have_cache) {
|
2008-09-30 18:29:32 -05:00
|
|
|
char buf[512];
|
2008-07-17 07:19:40 -05:00
|
|
|
int i;
|
|
|
|
|
2010-05-27 16:39:41 -05:00
|
|
|
for (i = desc->ct_cache - 1; i >= 0; i--) {
|
2008-07-17 07:19:40 -05:00
|
|
|
snprintf(buf, sizeof(buf),
|
2010-05-27 16:39:41 -05:00
|
|
|
_("%s cache:"), desc->cache[i].name);
|
|
|
|
print_s(buf, desc->cache[i].size);
|
2008-07-17 07:19:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void usage(int rc)
|
|
|
|
{
|
|
|
|
printf(_("Usage: %s [option]\n"),
|
|
|
|
program_invocation_short_name);
|
|
|
|
|
|
|
|
puts(_( "CPU architecture information helper\n\n"
|
|
|
|
" -h, --help usage information\n"
|
2008-08-10 02:33:51 -05:00
|
|
|
" -p, --parse print out in parsable instead of printable format.\n"
|
|
|
|
" -s, --sysroot use the directory as a new system root.\n"));
|
2008-07-17 07:19:40 -05:00
|
|
|
exit(rc);
|
|
|
|
}
|
|
|
|
|
2008-08-10 02:33:51 -05:00
|
|
|
static int
|
|
|
|
ca_compare(const void *a, const void *b)
|
|
|
|
{
|
2010-05-27 16:39:41 -05:00
|
|
|
struct cpu_cache *c1 = (struct cpu_cache *) a;
|
|
|
|
struct cpu_cache *c2 = (struct cpu_cache *) b;
|
2008-08-10 02:33:51 -05:00
|
|
|
|
2010-05-27 16:39:41 -05:00
|
|
|
return strcmp(c2->name, c1->name);
|
2008-08-10 02:33:51 -05:00
|
|
|
}
|
|
|
|
|
2008-07-17 07:19:40 -05:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2010-05-27 16:39:41 -05:00
|
|
|
struct lscpu_desc _desc, *desc = &_desc;
|
2008-07-17 07:19:40 -05:00
|
|
|
int parsable = 0, c;
|
|
|
|
|
|
|
|
struct option longopts[] = {
|
2008-08-10 02:33:51 -05:00
|
|
|
{ "help", no_argument, 0, 'h' },
|
|
|
|
{ "parse", no_argument, 0, 'p' },
|
|
|
|
{ "sysroot", required_argument, 0, 's' },
|
2008-07-17 07:19:40 -05:00
|
|
|
{ NULL, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
setlocale(LC_MESSAGES, "");
|
|
|
|
bindtextdomain(PACKAGE, LOCALEDIR);
|
|
|
|
textdomain(PACKAGE);
|
|
|
|
|
2008-08-10 02:33:51 -05:00
|
|
|
while((c = getopt_long(argc, argv, "hps:", longopts, NULL)) != -1) {
|
2008-07-17 07:19:40 -05:00
|
|
|
switch (c) {
|
|
|
|
case 'h':
|
|
|
|
usage(EXIT_SUCCESS);
|
|
|
|
case 'p':
|
|
|
|
parsable = 1;
|
|
|
|
break;
|
2008-08-10 02:33:51 -05:00
|
|
|
case 's':
|
2010-05-28 03:45:41 -05:00
|
|
|
sysrootlen = strlen(optarg);
|
2008-08-10 02:33:51 -05:00
|
|
|
strncpy(pathbuf, optarg, sizeof(pathbuf));
|
2010-05-28 03:45:41 -05:00
|
|
|
pathbuf[sizeof(pathbuf) - 1] = '\0';
|
2008-08-10 02:33:51 -05:00
|
|
|
break;
|
2008-07-17 07:19:40 -05:00
|
|
|
default:
|
|
|
|
usage(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-27 16:39:41 -05:00
|
|
|
memset(desc, 0, sizeof(*desc));
|
2008-07-17 07:19:40 -05:00
|
|
|
|
|
|
|
check_system();
|
|
|
|
|
2010-05-27 16:39:41 -05:00
|
|
|
read_basicinfo(desc);
|
2008-07-17 07:19:40 -05:00
|
|
|
|
|
|
|
if (have_topology)
|
2010-05-27 16:39:41 -05:00
|
|
|
read_topology(desc);
|
2008-08-10 02:33:51 -05:00
|
|
|
if (have_cache) {
|
2010-05-27 16:39:41 -05:00
|
|
|
read_cache(desc);
|
|
|
|
qsort(desc->cache, desc->ct_cache, sizeof(struct cpu_cache), ca_compare);
|
2008-08-10 02:33:51 -05:00
|
|
|
}
|
2008-07-17 07:19:40 -05:00
|
|
|
if (have_node)
|
2010-05-27 16:39:41 -05:00
|
|
|
read_nodes(desc);
|
2008-07-17 07:19:40 -05:00
|
|
|
|
2010-05-27 16:39:41 -05:00
|
|
|
read_hypervisor(desc);
|
2008-09-30 18:29:32 -05:00
|
|
|
|
2008-07-17 07:19:40 -05:00
|
|
|
/* Show time! */
|
|
|
|
if (parsable)
|
2010-05-27 16:39:41 -05:00
|
|
|
print_parsable(desc);
|
2008-07-17 07:19:40 -05:00
|
|
|
else
|
2010-05-27 16:39:41 -05:00
|
|
|
print_readable(desc);
|
2008-07-17 07:19:40 -05:00
|
|
|
|
2008-10-21 11:45:40 -05:00
|
|
|
return EXIT_SUCCESS;
|
2008-07-17 07:19:40 -05:00
|
|
|
}
|