13#define VENDOR_UNKNOWN -1
14#define VENDOR_UNINITED 0
15#define VENDOR_INTEL_X86 1
20#define VENDOR_INTEL_IA64 9
21#define VENDOR_ARM_ARM 65
22#define VENDOR_ARM_BROADCOM 66
23#define VENDOR_ARM_CAVIUM 67
24#define VENDOR_ARM_FUJITSU 70
25#define VENDOR_ARM_HISILICON 72
26#define VENDOR_ARM_APM 80
27#define VENDOR_ARM_QUALCOMM 81
29#define _PATH_SYS_SYSTEM "/sys/devices/system/"
30#define _PATH_SYS_CPU0 _PATH_SYS_SYSTEM "/cpu/cpu0"
54 const char *namekey_x86 =
"vendor_id";
55 const char *namekey_ia64 =
"vendor";
56 const char *namekey_ibm =
"platform";
57 const char *namekey_mips =
"system type";
58 const char *namekey_arm =
"CPU implementer";
59 const char *namekey_dum =
"none";
61 const char *namekey_ptr = NULL;
66 namekey_ptr = namekey_x86;
68 namekey_ptr = namekey_ia64;
70 namekey_ptr = namekey_ibm;
72 namekey_ptr = namekey_mips;
80 namekey_ptr = namekey_arm;
82 namekey_ptr = namekey_dum;
91 const char *namekey_x86 =
"model name";
92 const char *namekey_ibm =
"model";
93 const char *namekey_arm =
"model name";
94 const char *namekey_dum =
"none";
96 const char *namekey_ptr = NULL;
101 namekey_ptr = namekey_x86;
103 namekey_ptr = namekey_ibm;
111 namekey_ptr = namekey_arm;
113 namekey_ptr = namekey_dum;
124#define TOPOKEY_NUM_KEY 4
125#define VERKEY_NUM_KEY 4
165 const char **verkey_ptr = NULL;
170 verkey_ptr = verkey_x86;
172 verkey_ptr = verkey_ibm;
180 verkey_ptr = verkey_arm;
182 verkey_ptr = verkey_dum;
264 CPU_SET(cpu, &cpuset);
266 return sched_setaffinity(0,
sizeof(cpuset), &cpuset);
272 return sysconf(_SC_NPROCESSORS_CONF);
296 static int sockets, nodes, threads, cores;
298 if (!strcmp(
"sockets",
key) && sockets) {
301 }
else if (!strcmp(
"nodes",
key) && nodes) {
304 }
else if (!strcmp(
"threads",
key) && threads) {
307 }
else if (!strcmp(
"cores",
key) && cores) {
327 sockets = totalcpus / cores / threads;
332 if (!strcmp(
"sockets",
key)) {
334 }
else if (!strcmp(
"nodes",
key)) {
335 *val = (nodes == 0) ? nodes = 1 : nodes;
336 }
else if (!strcmp(
"cores",
key)) {
338 }
else if (!strcmp(
"threads",
key)) {
350 if (!strcmp(
key,
"none")) {
351 strcpy(val,
"UNKNOWN");
355 FILE *
fp = fopen(
"/proc/cpuinfo",
"r");
374 if (!strcmp(
key,
"none")) {
379 if (!strcmp(
key,
"vendor_id") || !strcmp(
key,
"CPU implementer")) {
384 FILE *
fp = fopen(
"/proc/cpuinfo",
"r");
392 if (strcmp(
key,
"CPU architecture") == 0) {
395 if (strstr(str,
"AArch64"))
398 *val = strtol(str, NULL, 10);
404 char *t = strchr(str,
'(');
405 int tmp = *(t + 2) -
'0';
411 char *t = strchr(str,
'(');
412 int tmp = *(t + 2) -
'0';
418 sscanf(str,
"%x", val);
433 int size, line_size, associativity, sets;
437 int *level_count, level_index;
450 dir = opendir(
"/sys/devices/system/cpu/cpu0/cache");
460 if (strncmp(d->d_name,
"index", 5))
477 L[level_index].
cache[*level_count].
size = size * 1024;
498 (sets * associativity * partitions);
500 if (((size * 1024) / line_size / associativity) != sets) {
501 MEMDBG(
"Warning! sets %d != expected %d\n",
502 sets, ((size * 1024) / line_size / associativity));
505 if (level > max_level) {
510 MEMDBG(
"Exceeded maximum cache level %d\n",
529 char filename[BUFSIZ];
532 sprintf(filename,
"/sys/devices/system/cpu/cpu0/cache/%s/level",
535 FILE *
fff = fopen(filename,
"r");
537 MEMDBG(
"Cannot open level.\n");
541 int result = fscanf(
fff,
"%d", &level_index);
544 MEMDBG(
"Could not read cache level\n");
550 *value = level_index;
558 char filename[BUFSIZ];
559 char type_string[BUFSIZ];
562 sprintf(filename,
"/sys/devices/system/cpu/cpu0/cache/%s/type",
565 FILE *
fff = fopen(filename,
"r");
567 MEMDBG(
"Cannot open type\n");
571 char *
result = fgets(type_string, BUFSIZ,
fff);
574 MEMDBG(
"Could not read cache type\n");
578 if (!strcmp(type_string,
"Data")) {
582 if (!strcmp(type_string,
"Instruction")) {
586 if (!strcmp(type_string,
"Unified")) {
598 char filename[BUFSIZ];
601 sprintf(filename,
"/sys/devices/system/cpu/cpu0/cache/%s/size",
604 FILE *
fff = fopen(filename,
"r");
606 MEMDBG(
"Cannot open size\n");
613 MEMDBG(
"Could not read cache size\n");
625 char filename[BUFSIZ];
628 sprintf(filename,
"/sys/devices/system/cpu/cpu0/cache/%s/coherency_line_size",
631 FILE *
fff = fopen(filename,
"r");
633 MEMDBG(
"Cannot open linesize\n");
637 int result = fscanf(
fff,
"%d", &line_size);
640 MEMDBG(
"Could not read cache line-size\n");
652 char filename[BUFSIZ];
655 sprintf(filename,
"/sys/devices/system/cpu/cpu0/cache/%s/ways_of_associativity",
658 FILE *
fff = fopen(filename,
"r");
660 MEMDBG(
"Cannot open associativity\n");
664 int result = fscanf(
fff,
"%d", &associativity);
667 MEMDBG(
"Could not read cache associativity\n");
671 *value = associativity;
679 char filename[BUFSIZ];
682 sprintf(filename,
"/sys/devices/system/cpu/cpu0/cache/%s/physical_line_partition",
685 FILE *
fff = fopen(filename,
"r");
687 MEMDBG(
"Cannot open partitions\n");
691 int result = fscanf(
fff,
"%d", &partitions);
694 MEMDBG(
"Could not read partitions count\n");
706 char filename[BUFSIZ];
709 sprintf(filename,
"/sys/devices/system/cpu/cpu0/cache/%s/number_of_sets",
712 FILE *
fff = fopen(filename,
"r");
714 MEMDBG(
"Cannot open sets\n");
721 MEMDBG(
"Could not read cache sets\n");
736 FILE *
fp = fopen(filename,
"r");
742 sprintf(search_str,
"Node %d MemTotal", node);
745 sprintf(search_str,
"%s", str);
746 int len = strlen(search_str);
747 search_str[len-3] =
'\0';
748 *val = atoi(search_str);
777xfopen(
const char *path,
const char *mode )
779 FILE *fd = fopen(path, mode);
803 while ((
c = fgetc(
fp)) != EOF) {
807 for (n = strtol(
s, NULL, 16); n > 0; n /= 2) {
827 s=strstr(line, search_str);
832 if (
s == NULL)
break;
834 while (isspace(*
s)) {
840 if (
s != NULL) *
s = 0;
855 return access(
pathbuf, F_OK) == 0;
861 if (strcasecmp(
s,
"GenuineIntel") == 0)
863 else if ((strcasecmp(
s,
"AMD") == 0) ||
864 (strcasecmp(
s,
"AuthenticAMD") == 0 ))
866 else if (strcasecmp(
s,
"IBM") == 0)
868 else if (strcasecmp(
s,
"Cray") == 0)
870 else if (strcasecmp(
s,
"ARM_ARM") == 0)
872 else if (strcasecmp(
s,
"ARM_BROADCOM") == 0)
874 else if (strcasecmp(
s,
"ARM_CAVIUM") == 0)
876 else if (strcasecmp(
s,
"ARM_FUJITSU") == 0)
878 else if (strcasecmp(
s,
"ARM_HISILICON") == 0)
880 else if (strcasecmp(
s,
"ARM_APM") == 0)
882 else if (strcasecmp(
s,
"ARM_QUALCOMM") == 0)
884 else if (strcasecmp(
s,
"MIPS") == 0)
886 else if (strcasecmp(
s,
"SiCortex") == 0)
895 static int vendor_id;
900 FILE *
fp = fopen(
"/proc/cpuinfo",
"r");
923 if (strcasecmp(
s,
"pSeries") == 0 ||
924 strcasecmp(
s,
"PowerNV") == 0 ||
925 strcasecmp(
s,
"PowerMac") == 0) {
926 strcpy(vendor_string,
"IBM");
932 sscanf(
s,
"%x", &
tmp);
935 strcpy(vendor_string,
"ARM_ARM");
938 strcpy(vendor_string,
"ARM_BROADCOM");
941 strcpy(vendor_string,
"ARM_CAVIUM");
944 strcpy(vendor_string,
"ARM_FUJITSU");
947 strcpy(vendor_string,
"ARM_HISILICON");
950 strcpy(vendor_string,
"ARM_APM");
953 strcpy(vendor_string,
"ARM_QUALCOMM");
956 strcpy(vendor_string,
"UNKNOWN");
964 if (strlen(vendor_string)) {
int cpu_get_cache_info(CPU_attr_e attr, int level, _sysdetect_cache_level_info_t *clevel_ptr, int *value)
@ CPU_ATTR__CACHE_MAX_NUM_LEVELS
@ CPU_ATTR__CACHE_INST_TOT_SIZE
@ CPU_ATTR__HWTHREAD_NUMA_AFFINITY
@ CPU_ATTR__CACHE_UNIF_TOT_SIZE
@ CPU_ATTR__CACHE_DATA_ASSOCIATIVITY
@ CPU_ATTR__CACHE_INST_LINE_SIZE
@ CPU_ATTR__NUMA_MEM_SIZE
@ CPU_ATTR__CACHE_UNIF_LINE_SIZE
@ CPU_ATTR__CACHE_DATA_NUM_LINES
@ CPU_ATTR__CACHE_UNIF_ASSOCIATIVITY
@ CPU_ATTR__CPUID_STEPPING
@ CPU_ATTR__CACHE_INST_PRESENT
@ CPU_ATTR__CACHE_DATA_PRESENT
@ CPU_ATTR__CACHE_DATA_LINE_SIZE
@ CPU_ATTR__CACHE_INST_ASSOCIATIVITY
@ CPU_ATTR__CACHE_INST_NUM_LINES
@ CPU_ATTR__CACHE_UNIF_NUM_LINES
@ CPU_ATTR__CACHE_DATA_TOT_SIZE
@ CPU_ATTR__CACHE_UNIF_PRESENT
#define PAPI_MAX_MEM_HIERARCHY_LEVELS
#define PAPI_HUGE_STR_LEN
static struct timeval start
static double c[MATRIX_SIZE][MATRIX_SIZE]
int linux_cpu_get_attribute_at(CPU_attr_e attr, int loc, int *value)
static int get_vendor_id(void)
static void decode_vendor_string(char *s, int *vendor)
static int get_topology_info(const char *key, int *value)
int linux_cpu_load_affinity(void)
int linux_cpu_get_name(char *name)
#define VENDOR_ARM_CAVIUM
static char pathbuf[PATH_MAX]
static int get_cache_type(const char *dirname, int *value)
FILE * xfopen(const char *path, const char *mode)
#define VENDOR_ARM_HISILICON
static int get_thread_affinity(int thread, int *value)
int linux_cpu_store_affinity(void)
static int get_cache_partition_count(const char *dirname, int *value)
static _sysdetect_cache_level_info_t clevel[PAPI_MAX_MEM_HIERARCHY_LEVELS]
static int get_cache_line_size(const char *dirname, int *value)
static int get_cache_size(const char *dirname, int *value)
static int get_cache_set_count(const char *dirname, int *value)
static int get_mem_info(int node, int *value)
#define VENDOR_ARM_BROADCOM
static cpu_set_t saved_affinity
int linux_cpu_get_num_supported(void)
int linux_cpu_set_affinity(int cpu)
static int get_naming_info(const char *key, char *value)
static int path_exist(const char *path,...)
static int path_sibling(const char *path,...)
int linux_cpu_get_vendor(char *vendor)
#define VENDOR_ARM_QUALCOMM
FILE * path_vfopen(const char *mode, const char *path, va_list ap)
static int get_cache_info(CPU_attr_e attr, int level, int *value)
#define VENDOR_INTEL_IA64
static int get_versioning_info(const char *key, int *value)
#define VENDOR_ARM_FUJITSU
static int get_cache_associativity(const char *dirname, int *value)
static char * search_cpu_info(FILE *fp, const char *key)
static int get_cache_level(const char *dirname, int *value)
int linux_cpu_get_attribute(CPU_attr_e attr, int *value)
#define PAPI_MH_TYPE_DATA
#define PAPI_MH_TYPE_INST
#define PAPI_MH_TYPE_UNIFIED
#define MEMDBG(format, args...)
int fclose(FILE *__stream)
PAPI_mh_cache_info_t cache[PAPI_MH_MAX_LEVELS]