blob: 4c842762e3f2d483e2f7f29a3fac640ce89602ea [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -03002#include "cpumap.h"
Arnaldo Carvalho de Melof0ce8882015-09-08 13:30:00 -03003#include "env.h"
Arnaldo Carvalho de Melo4e8fbc12017-12-11 14:47:49 -03004#include "sane_ctype.h"
Arnaldo Carvalho de Melof0ce8882015-09-08 13:30:00 -03005#include "util.h"
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03006#include <errno.h>
Arnaldo Carvalho de Melo4e8fbc12017-12-11 14:47:49 -03007#include <sys/utsname.h>
Arnaldo Carvalho de Melof0ce8882015-09-08 13:30:00 -03008
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -03009struct perf_env perf_env;
10
Arnaldo Carvalho de Melof0ce8882015-09-08 13:30:00 -030011void perf_env__exit(struct perf_env *env)
12{
Jiri Olsa720e98b2016-02-16 16:01:43 +010013 int i;
14
Arnaldo Carvalho de Melof0ce8882015-09-08 13:30:00 -030015 zfree(&env->hostname);
16 zfree(&env->os_release);
17 zfree(&env->version);
18 zfree(&env->arch);
19 zfree(&env->cpu_desc);
20 zfree(&env->cpuid);
21 zfree(&env->cmdline);
22 zfree(&env->cmdline_argv);
23 zfree(&env->sibling_cores);
24 zfree(&env->sibling_threads);
Arnaldo Carvalho de Melof0ce8882015-09-08 13:30:00 -030025 zfree(&env->pmu_mappings);
26 zfree(&env->cpu);
Jiri Olsa720e98b2016-02-16 16:01:43 +010027
Jiri Olsac60da222016-07-04 14:16:20 +020028 for (i = 0; i < env->nr_numa_nodes; i++)
29 cpu_map__put(env->numa_nodes[i].map);
30 zfree(&env->numa_nodes);
31
Jiri Olsa720e98b2016-02-16 16:01:43 +010032 for (i = 0; i < env->caches_cnt; i++)
33 cpu_cache_level__free(&env->caches[i]);
34 zfree(&env->caches);
Jiri Olsae7259202018-03-09 11:14:34 +010035
36 for (i = 0; i < env->nr_memory_nodes; i++)
37 free(env->memory_nodes[i].set);
38 zfree(&env->memory_nodes);
Arnaldo Carvalho de Melof0ce8882015-09-08 13:30:00 -030039}
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -030040
41int perf_env__set_cmdline(struct perf_env *env, int argc, const char *argv[])
42{
43 int i;
44
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -030045 /* do not include NULL termination */
46 env->cmdline_argv = calloc(argc, sizeof(char *));
47 if (env->cmdline_argv == NULL)
48 goto out_enomem;
49
50 /*
51 * Must copy argv contents because it gets moved around during option
52 * parsing:
53 */
54 for (i = 0; i < argc ; i++) {
55 env->cmdline_argv[i] = argv[i];
56 if (env->cmdline_argv[i] == NULL)
57 goto out_free;
58 }
59
60 env->nr_cmdline = argc;
61
62 return 0;
63out_free:
64 zfree(&env->cmdline_argv);
65out_enomem:
66 return -ENOMEM;
67}
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -030068
69int perf_env__read_cpu_topology_map(struct perf_env *env)
70{
71 int cpu, nr_cpus;
72
73 if (env->cpu != NULL)
74 return 0;
75
76 if (env->nr_cpus_avail == 0)
Jan Stancekda8a58b2017-02-17 12:10:26 +010077 env->nr_cpus_avail = cpu__max_present_cpu();
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -030078
79 nr_cpus = env->nr_cpus_avail;
80 if (nr_cpus == -1)
81 return -EINVAL;
82
83 env->cpu = calloc(nr_cpus, sizeof(env->cpu[0]));
84 if (env->cpu == NULL)
85 return -ENOMEM;
86
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);
90 }
91
92 env->nr_cpus_avail = nr_cpus;
93 return 0;
94}
Jiri Olsa720e98b2016-02-16 16:01:43 +010095
96void cpu_cache_level__free(struct cpu_cache_level *cache)
97{
98 free(cache->type);
99 free(cache->map);
100 free(cache->size);
101}
Arnaldo Carvalho de Melo4e8fbc12017-12-11 14:47:49 -0300102
103/*
104 * Return architecture name in a normalized form.
105 * The conversion logic comes from the Makefile.
106 */
107static const char *normalize_arch(char *arch)
108{
109 if (!strcmp(arch, "x86_64"))
110 return "x86";
111 if (arch[0] == 'i' && arch[2] == '8' && arch[3] == '6')
112 return "x86";
113 if (!strcmp(arch, "sun4u") || !strncmp(arch, "sparc", 5))
114 return "sparc";
115 if (!strcmp(arch, "aarch64") || !strcmp(arch, "arm64"))
116 return "arm64";
117 if (!strncmp(arch, "arm", 3) || !strcmp(arch, "sa110"))
118 return "arm";
119 if (!strncmp(arch, "s390", 4))
120 return "s390";
121 if (!strncmp(arch, "parisc", 6))
122 return "parisc";
123 if (!strncmp(arch, "powerpc", 7) || !strncmp(arch, "ppc", 3))
124 return "powerpc";
125 if (!strncmp(arch, "mips", 4))
126 return "mips";
127 if (!strncmp(arch, "sh", 2) && isdigit(arch[2]))
128 return "sh";
129
130 return arch;
131}
132
133const char *perf_env__arch(struct perf_env *env)
134{
135 struct utsname uts;
136 char *arch_name;
137
138 if (!env) { /* Assume local operation */
139 if (uname(&uts) < 0)
140 return NULL;
141 arch_name = uts.machine;
142 } else
143 arch_name = env->arch;
144
145 return normalize_arch(arch_name);
146}