1 // SPDX-License-Identifier: GPL-2.0
4 #include "sane_ctype.h"
7 #include <sys/utsname.h>
9 struct perf_env perf_env
;
11 void perf_env__exit(struct perf_env
*env
)
15 zfree(&env
->hostname
);
16 zfree(&env
->os_release
);
19 zfree(&env
->cpu_desc
);
22 zfree(&env
->cmdline_argv
);
23 zfree(&env
->sibling_cores
);
24 zfree(&env
->sibling_threads
);
25 zfree(&env
->pmu_mappings
);
28 for (i
= 0; i
< env
->nr_numa_nodes
; i
++)
29 cpu_map__put(env
->numa_nodes
[i
].map
);
30 zfree(&env
->numa_nodes
);
32 for (i
= 0; i
< env
->caches_cnt
; i
++)
33 cpu_cache_level__free(&env
->caches
[i
]);
36 for (i
= 0; i
< env
->nr_memory_nodes
; i
++)
37 free(env
->memory_nodes
[i
].set
);
38 zfree(&env
->memory_nodes
);
41 int perf_env__set_cmdline(struct perf_env
*env
, int argc
, const char *argv
[])
45 /* do not include NULL termination */
46 env
->cmdline_argv
= calloc(argc
, sizeof(char *));
47 if (env
->cmdline_argv
== NULL
)
51 * Must copy argv contents because it gets moved around during option
54 for (i
= 0; i
< argc
; i
++) {
55 env
->cmdline_argv
[i
] = argv
[i
];
56 if (env
->cmdline_argv
[i
] == NULL
)
60 env
->nr_cmdline
= argc
;
64 zfree(&env
->cmdline_argv
);
69 int perf_env__read_cpu_topology_map(struct perf_env
*env
)
76 if (env
->nr_cpus_avail
== 0)
77 env
->nr_cpus_avail
= cpu__max_present_cpu();
79 nr_cpus
= env
->nr_cpus_avail
;
83 env
->cpu
= calloc(nr_cpus
, sizeof(env
->cpu
[0]));
87 for (cpu
= 0; cpu
< nr_cpus
; ++cpu
) {
88 env
->cpu
[cpu
].core_id
= cpu_map__get_core_id(cpu
);
89 env
->cpu
[cpu
].socket_id
= cpu_map__get_socket_id(cpu
);
92 env
->nr_cpus_avail
= nr_cpus
;
96 static int perf_env__read_arch(struct perf_env
*env
)
104 env
->arch
= strdup(uts
.machine
);
106 return env
->arch
? 0 : -ENOMEM
;
109 static int perf_env__read_nr_cpus_avail(struct perf_env
*env
)
111 if (env
->nr_cpus_avail
== 0)
112 env
->nr_cpus_avail
= cpu__max_present_cpu();
114 return env
->nr_cpus_avail
? 0 : -ENOENT
;
117 const char *perf_env__raw_arch(struct perf_env
*env
)
119 return env
&& !perf_env__read_arch(env
) ? env
->arch
: "unknown";
122 int perf_env__nr_cpus_avail(struct perf_env
*env
)
124 return env
&& !perf_env__read_nr_cpus_avail(env
) ? env
->nr_cpus_avail
: 0;
127 void cpu_cache_level__free(struct cpu_cache_level
*cache
)
135 * Return architecture name in a normalized form.
136 * The conversion logic comes from the Makefile.
138 static const char *normalize_arch(char *arch
)
140 if (!strcmp(arch
, "x86_64"))
142 if (arch
[0] == 'i' && arch
[2] == '8' && arch
[3] == '6')
144 if (!strcmp(arch
, "sun4u") || !strncmp(arch
, "sparc", 5))
146 if (!strcmp(arch
, "aarch64") || !strcmp(arch
, "arm64"))
148 if (!strncmp(arch
, "arm", 3) || !strcmp(arch
, "sa110"))
150 if (!strncmp(arch
, "s390", 4))
152 if (!strncmp(arch
, "parisc", 6))
154 if (!strncmp(arch
, "powerpc", 7) || !strncmp(arch
, "ppc", 3))
156 if (!strncmp(arch
, "mips", 4))
158 if (!strncmp(arch
, "sh", 2) && isdigit(arch
[2]))
164 const char *perf_env__arch(struct perf_env
*env
)
169 if (!env
|| !env
->arch
) { /* Assume local operation */
172 arch_name
= uts
.machine
;
174 arch_name
= env
->arch
;
176 return normalize_arch(arch_name
);