(root)/
util-linux-2.39/
sys-utils/
lscpu.h
       1  #ifndef LSCPU_H
       2  #define LSCPU_H
       3  
       4  #include "c.h"
       5  #include "nls.h"
       6  #include "cpuset.h"
       7  #include "xalloc.h"
       8  #include "strutils.h"
       9  #include "bitops.h"
      10  #include "path.h"
      11  #include "pathnames.h"
      12  #include "all-io.h"
      13  #include "debug.h"
      14  
      15  #define LSCPU_DEBUG_INIT	(1 << 1)
      16  #define LSCPU_DEBUG_MISC	(1 << 2)
      17  #define LSCPU_DEBUG_GATHER	(1 << 3)
      18  #define LSCPU_DEBUG_TYPE	(1 << 4)
      19  #define LSCPU_DEBUG_CPU		(1 << 5)
      20  #define LSCPU_DEBUG_VIRT	(1 << 6)
      21  #define LSBLK_DEBUG_ALL		0xFFFF
      22  
      23  UL_DEBUG_DECLARE_MASK(lscpu);
      24  #define DBG(m, x)       __UL_DBG(lscpu, LSCPU_DEBUG_, m, x)
      25  #define ON_DBG(m, x)    __UL_DBG_CALL(lscpu, LSCPU_DEBUG_, m, x)
      26  
      27  #define UL_DEBUG_CURRENT_MASK	UL_DEBUG_MASK(lscpu)
      28  #include "debugobj.h"
      29  
      30  #define _PATH_SYS_SYSTEM	"/sys/devices/system"
      31  #define _PATH_SYS_HYP_FEATURES	"/sys/hypervisor/properties/features"
      32  #define _PATH_SYS_CPU		_PATH_SYS_SYSTEM "/cpu"
      33  #define _PATH_SYS_NODE		_PATH_SYS_SYSTEM "/node"
      34  #define _PATH_SYS_DMI		"/sys/firmware/dmi/tables/DMI"
      35  #define _PATH_ACPI_PPTT		"/sys/firmware/acpi/tables/PPTT"
      36  
      37  struct lscpu_cache {
      38  	int		id;		/* unique identifier */
      39  	int		nth;		/* cache<number> from cpuinfo */
      40  	char		*name;
      41  	char		*type;
      42  	char		*allocation_policy;
      43  	char		*write_policy;
      44  
      45  	int		level;
      46  	uint64_t	size;
      47  
      48  	unsigned int	ways_of_associativity;
      49  	unsigned int	physical_line_partition;
      50  	unsigned int	number_of_sets;
      51  	unsigned int	coherency_line_size;
      52  
      53  	cpu_set_t	*sharedmap;
      54  };
      55  
      56  struct lscpu_cputype {
      57  	int	refcount;
      58  
      59  	char	*vendor;
      60  	int	vendor_id;	/* created by lscpu_decode_arm() */
      61  	char	*bios_vendor;	/* aarch64 */
      62  	char	*machinetype;	/* s390 */
      63  	char	*family;
      64  	char	*model;
      65  	char	*modelname;
      66  	char	*bios_modelname; /* aarch64 */
      67  	char	*bios_family; /* aarch64 */
      68  	char	*revision;	/* alternative for model (ppc) */
      69  	char	*stepping;
      70  	char    *bogomips;
      71  	char	*flags;
      72  	char	*mtid;		/* maximum thread id (s390) */
      73  	char	*addrsz;	/* address sizes */
      74  	int	dispatching;	/* -1 if not evailable, DIST_* */
      75  	int	freqboost;	/* -1 if not evailable */
      76  
      77  	size_t	physsockets;	/* Physical sockets (modules) */
      78  	size_t	physchips;	/* Physical chips */
      79  	size_t	physcoresperchip;	/* Physical cores per chip */
      80  
      81  	size_t	nthreads_per_core;
      82  	size_t	ncores_per_socket;
      83  	size_t	nsockets_per_book;
      84  	size_t	nbooks_per_drawer;
      85  	size_t	ndrawers_per_system;
      86  
      87  	char	*dynamic_mhz;	/* s390; copy from the first CPU */
      88  	char	*static_mhz;	/* s390; copy from the first CPU */
      89  
      90  	/* siblings maps */
      91  	size_t		ncores;
      92  	cpu_set_t	**coremaps;
      93  	size_t		nsockets;
      94  	cpu_set_t       **socketmaps;
      95  	size_t		nbooks;
      96  	cpu_set_t	**bookmaps;
      97  	size_t		ndrawers;
      98  	cpu_set_t	**drawermaps;
      99  
     100  	unsigned int	has_freq : 1,
     101  			has_configured : 1,
     102  			has_polarization : 1,
     103  			has_addresses : 1;
     104  
     105  	size_t nr_socket_on_cluster; /* the number of sockets if the is_cluster is 1 */
     106  
     107  	char	*isa;	/* loongarch */
     108  };
     109  
     110  /* dispatching modes */
     111  enum {
     112  	DISP_HORIZONTAL = 0,
     113  	DISP_VERTICAL	= 1
     114  };
     115  
     116  /* cpu polarization */
     117  enum {
     118  	POLAR_UNKNOWN	= 0,
     119  	POLAR_VLOW,
     120  	POLAR_VMEDIUM,
     121  	POLAR_VHIGH,
     122  	POLAR_HORIZONTAL
     123  };
     124  
     125  struct lscpu_cpu {
     126  	int refcount;
     127  	struct lscpu_cputype *type;
     128  
     129  	int logical_id;
     130  
     131  	char	*bogomips;	/* per-CPU bogomips */
     132  	char	*mhz;		/* freq from cpuinfo */
     133  	char	*dynamic_mhz;   /* from cpuinf for s390 */
     134  	char	*static_mhz;	/* from cpuinf for s390 */
     135  	float	mhz_max_freq;	/* realtime freq from /sys/.../cpuinfo_max_freq */
     136  	float	mhz_min_freq;	/* realtime freq from /sys/.../cpuinfo_min_freq */
     137  	float   mhz_cur_freq;
     138  
     139  	int	coreid;
     140  	int	socketid;
     141  	int	bookid;
     142  	int	drawerid;
     143  
     144  	int	polarization;	/* POLAR_* */
     145  	int	address;	/* physical cpu address */
     146  	int	configured;	/* cpu configured */
     147  };
     148  
     149  struct lscpu_arch {
     150  	char	*name;		/* uname() .machine */
     151  
     152  	unsigned int	bit32:1,
     153  			bit64:1;
     154  };
     155  
     156  struct lscpu_vulnerability {
     157  	char	*name;
     158  	char	*text;
     159  };
     160  
     161  /* virtualization types */
     162  enum {
     163  	VIRT_TYPE_NONE	= 0,
     164  	VIRT_TYPE_PARA,
     165  	VIRT_TYPE_FULL,
     166  	VIRT_TYPE_CONTAINER
     167  };
     168  
     169  /* hypervisor vendors */
     170  enum {
     171  	VIRT_VENDOR_NONE	= 0,
     172  	VIRT_VENDOR_XEN,
     173  	VIRT_VENDOR_KVM,
     174  	VIRT_VENDOR_MSHV,
     175  	VIRT_VENDOR_VMWARE,
     176  	VIRT_VENDOR_IBM,		/* sys-z powervm */
     177  	VIRT_VENDOR_VSERVER,
     178  	VIRT_VENDOR_UML,
     179  	VIRT_VENDOR_INNOTEK,		/* VBOX */
     180  	VIRT_VENDOR_HITACHI,
     181  	VIRT_VENDOR_PARALLELS,	/* OpenVZ/VIrtuozzo */
     182  	VIRT_VENDOR_VBOX,
     183  	VIRT_VENDOR_OS400,
     184  	VIRT_VENDOR_PHYP,
     185  	VIRT_VENDOR_SPAR,
     186  	VIRT_VENDOR_WSL,
     187  };
     188  
     189  struct lscpu_virt {
     190  	char	*cpuflag;	/* virtualization flag (vmx, svm) */
     191  	char	*hypervisor;	/* hypervisor software */
     192  	int	vendor;		/* VIRT_VENDOR_* */
     193  	int	type;		/* VIRT_TYPE_* ? */
     194  
     195  };
     196  
     197  enum {
     198  	LSCPU_OUTPUT_SUMMARY = 0,	/* default */
     199  	LSCPU_OUTPUT_CACHES,
     200  	LSCPU_OUTPUT_PARSABLE,
     201  	LSCPU_OUTPUT_READABLE
     202  };
     203  
     204  struct lscpu_cxt {
     205  	int maxcpus;		/* size in bits of kernel cpu mask */
     206  	size_t setsize;
     207  	const char *prefix;	/* path to /sys and /proc snapshot or NULL */
     208  
     209  	struct path_cxt	*syscpu; /* _PATH_SYS_CPU path handler */
     210  	struct path_cxt *procfs; /* /proc path handler */
     211  	struct path_cxt *rootfs; /* / path handler */
     212  
     213  	size_t ncputypes;
     214  	struct lscpu_cputype **cputypes;
     215  
     216  	size_t npossibles;	/* number of possible CPUs */
     217  	struct lscpu_cpu **cpus; /* possible CPUs, contains gaps (cups[n]=NULL) */
     218  
     219  	size_t npresents;
     220  	cpu_set_t *present;	/* mask with present CPUs */
     221  
     222  	size_t nonlines;	/* aka number of trhreads */
     223  	cpu_set_t *online;	/* mask with online CPUs */
     224  
     225  	struct lscpu_arch *arch;
     226  	struct lscpu_virt *virt;
     227  
     228  	struct lscpu_vulnerability *vuls;	/* array of CPU vulnerabilities */
     229  	size_t  nvuls;				/* number of CPU vulnerabilities */
     230  
     231  	struct lscpu_cache *caches;		/* all instances of the all caches from /sys */
     232  	size_t ncaches;
     233  
     234  	struct lscpu_cache *ecaches;
     235  	size_t necaches;		/* extra caches (s390) from /proc/cpuinfo */
     236  
     237  	size_t nnodes;		/* number of NUMA modes */
     238  	int *idx2nodenum;	/* Support for discontinuous nodes */
     239  	cpu_set_t **nodemaps;	/* array with NUMA nodes */
     240  
     241  	int mode;	/* LSCPU_OUTPUT_* */
     242  
     243  	unsigned int noalive : 1,
     244  		     show_online : 1,
     245  		     show_offline : 1,
     246  		     show_physical : 1,
     247  		     show_compatible : 1,
     248  		     hex : 1,
     249  		     json : 1,
     250  		     bytes : 1;
     251  
     252  	int is_cluster; /* For aarch64 if the machine doesn't have ACPI PPTT */
     253  };
     254  
     255  #define is_cpu_online(_cxt, _cpu) \
     256  		((_cxt) && (_cpu) && (_cxt)->online && \
     257  		 CPU_ISSET_S((_cpu)->logical_id, (_cxt)->setsize, (_cxt)->online))
     258  
     259  #define is_cpu_present(_cxt, _cpu) \
     260  		((_cxt) && (_cpu) && (_cxt)->present && \
     261  		 CPU_ISSET_S((_cpu)->logical_id, (_cxt)->setsize, (_cxt)->present))
     262  
     263  struct lscpu_cputype *lscpu_new_cputype(void);
     264  void lscpu_ref_cputype(struct lscpu_cputype *ct);
     265  void lscpu_unref_cputype(struct lscpu_cputype *ct);
     266  struct lscpu_cputype *lscpu_add_cputype(struct lscpu_cxt *cxt, struct lscpu_cputype *ct);
     267  struct lscpu_cputype *lscpu_cputype_get_default(struct lscpu_cxt *cxt);
     268  
     269  int lscpu_read_cpuinfo(struct lscpu_cxt *cxt);
     270  int lscpu_read_cpulists(struct lscpu_cxt *cxt);
     271  int lscpu_read_archext(struct lscpu_cxt *cxt);
     272  int lscpu_read_vulnerabilities(struct lscpu_cxt *cxt);
     273  int lscpu_read_numas(struct lscpu_cxt *cxt);
     274  
     275  void lscpu_free_caches(struct lscpu_cache *caches, size_t n);
     276  void lscpu_sort_caches(struct lscpu_cache *caches, size_t n);
     277  
     278  size_t lscpu_get_cache_full_size(struct lscpu_cxt *cxt, const char *name, int *instances);
     279  struct lscpu_cache *lscpu_cpu_get_cache(struct lscpu_cxt *cxt,
     280                                  struct lscpu_cpu *cpu, const char *name);
     281  
     282  int lscpu_read_topology(struct lscpu_cxt *cxt);
     283  void lscpu_cputype_free_topology(struct lscpu_cputype *ct);
     284  
     285  float lsblk_cputype_get_maxmhz(struct lscpu_cxt *cxt, struct lscpu_cputype *ct);
     286  float lsblk_cputype_get_minmhz(struct lscpu_cxt *cxt, struct lscpu_cputype *ct);
     287  float lsblk_cputype_get_scalmhz(struct lscpu_cxt *cxt, struct lscpu_cputype *ct);
     288  
     289  struct lscpu_arch *lscpu_read_architecture(struct lscpu_cxt *cxt);
     290  void lscpu_free_architecture(struct lscpu_arch *ar);
     291  
     292  struct lscpu_virt *lscpu_read_virtualization(struct lscpu_cxt *cxt);
     293  void lscpu_free_virtualization(struct lscpu_virt *virt);
     294  
     295  struct lscpu_cpu *lscpu_new_cpu(int id);
     296  void lscpu_ref_cpu(struct lscpu_cpu *cpu);
     297  void lscpu_unref_cpu(struct lscpu_cpu *cpu);
     298  struct lscpu_cpu *lscpu_get_cpu(struct lscpu_cxt *cxt, int logical_id);
     299  int lscpu_cpu_set_type(struct lscpu_cpu *cpu, struct lscpu_cputype *type);
     300  int lscpu_create_cpus(struct lscpu_cxt *cxt, cpu_set_t *cpuset, size_t setsize);
     301  struct lscpu_cpu *lscpu_cpus_loopup_by_type(struct lscpu_cxt *cxt, struct lscpu_cputype *ct);
     302  
     303  void lscpu_decode_arm(struct lscpu_cxt *cxt);
     304  
     305  int lookup(char *line, char *pattern, char **value);
     306  
     307  void *get_mem_chunk(size_t base, size_t len, const char *devmem);
     308  
     309  struct lscpu_dmi_header
     310  {
     311  	uint8_t type;
     312  	uint8_t length;
     313  	uint16_t handle;
     314  	uint8_t *data;
     315  };
     316  
     317  struct dmi_info {
     318  	char *vendor;
     319  	char *product;
     320  	char *manufacturer;
     321  	int sockets;
     322  
     323  	/* Processor Information */
     324  	uint16_t processor_family;
     325  	char *processor_manufacturer;
     326  	char *processor_version;
     327  	uint16_t current_speed;
     328  	char *part_num;
     329  };
     330  
     331  
     332  void to_dmi_header(struct lscpu_dmi_header *h, uint8_t *data);
     333  char *dmi_string(const struct lscpu_dmi_header *dm, uint8_t s);
     334  int parse_dmi_table(uint16_t len, uint16_t num, uint8_t *data, struct dmi_info *di);
     335  size_t get_number_of_physical_sockets_from_dmi(void);
     336  int dmi_decode_cputype(struct lscpu_cputype *);
     337  #endif /* LSCPU_H */