Linux Perf
env.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0
2 #include "cpumap.h"
3 #include "env.h"
4 #include "sane_ctype.h"
5 #include "util.h"
6 #include <errno.h>
7 #include <sys/utsname.h>
8 
10 
11 void perf_env__exit(struct perf_env *env)
12 {
13  int i;
14 
15  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);
25  zfree(&env->pmu_mappings);
26  zfree(&env->cpu);
27 
28  for (i = 0; i < env->nr_numa_nodes; i++)
29  cpu_map__put(env->numa_nodes[i].map);
30  zfree(&env->numa_nodes);
31 
32  for (i = 0; i < env->caches_cnt; i++)
33  cpu_cache_level__free(&env->caches[i]);
34  zfree(&env->caches);
35 
36  for (i = 0; i < env->nr_memory_nodes; i++)
37  free(env->memory_nodes[i].set);
38  zfree(&env->memory_nodes);
39 }
40 
41 int perf_env__set_cmdline(struct perf_env *env, int argc, const char *argv[])
42 {
43  int i;
44 
45  /* 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;
63 out_free:
64  zfree(&env->cmdline_argv);
65 out_enomem:
66  return -ENOMEM;
67 }
68 
70 {
71  int cpu, nr_cpus;
72 
73  if (env->cpu != NULL)
74  return 0;
75 
76  if (env->nr_cpus_avail == 0)
78 
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);
90  }
91 
92  env->nr_cpus_avail = nr_cpus;
93  return 0;
94 }
95 
96 static int perf_env__read_arch(struct perf_env *env)
97 {
98  struct utsname uts;
99 
100  if (env->arch)
101  return 0;
102 
103  if (!uname(&uts))
104  env->arch = strdup(uts.machine);
105 
106  return env->arch ? 0 : -ENOMEM;
107 }
108 
109 static int perf_env__read_nr_cpus_avail(struct perf_env *env)
110 {
111  if (env->nr_cpus_avail == 0)
113 
114  return env->nr_cpus_avail ? 0 : -ENOENT;
115 }
116 
117 const char *perf_env__raw_arch(struct perf_env *env)
118 {
119  return env && !perf_env__read_arch(env) ? env->arch : "unknown";
120 }
121 
123 {
124  return env && !perf_env__read_nr_cpus_avail(env) ? env->nr_cpus_avail : 0;
125 }
126 
128 {
129  free(cache->type);
130  free(cache->map);
131  free(cache->size);
132 }
133 
134 /*
135  * Return architecture name in a normalized form.
136  * The conversion logic comes from the Makefile.
137  */
138 static const char *normalize_arch(char *arch)
139 {
140  if (!strcmp(arch, "x86_64"))
141  return "x86";
142  if (arch[0] == 'i' && arch[2] == '8' && arch[3] == '6')
143  return "x86";
144  if (!strcmp(arch, "sun4u") || !strncmp(arch, "sparc", 5))
145  return "sparc";
146  if (!strcmp(arch, "aarch64") || !strcmp(arch, "arm64"))
147  return "arm64";
148  if (!strncmp(arch, "arm", 3) || !strcmp(arch, "sa110"))
149  return "arm";
150  if (!strncmp(arch, "s390", 4))
151  return "s390";
152  if (!strncmp(arch, "parisc", 6))
153  return "parisc";
154  if (!strncmp(arch, "powerpc", 7) || !strncmp(arch, "ppc", 3))
155  return "powerpc";
156  if (!strncmp(arch, "mips", 4))
157  return "mips";
158  if (!strncmp(arch, "sh", 2) && isdigit(arch[2]))
159  return "sh";
160 
161  return arch;
162 }
163 
164 const char *perf_env__arch(struct perf_env *env)
165 {
166  struct utsname uts;
167  char *arch_name;
168 
169  if (!env) { /* Assume local operation */
170  if (uname(&uts) < 0)
171  return NULL;
172  arch_name = uts.machine;
173  } else
174  arch_name = env->arch;
175 
176  return normalize_arch(arch_name);
177 }
Definition: env.h:36
int perf_env__set_cmdline(struct perf_env *env, int argc, const char *argv[])
Definition: env.c:41
const char ** cmdline_argv
Definition: env.h:56
char * sibling_cores
Definition: env.h:57
struct numa_node * numa_nodes
Definition: env.h:63
char * cmdline
Definition: env.h:55
unsigned long * set
Definition: env.h:33
char * hostname
Definition: env.h:37
int nr_numa_nodes
Definition: env.h:51
char * arch
Definition: env.h:40
void cpu_map__put(struct cpu_map *map)
Definition: cpumap.c:298
void perf_env__exit(struct perf_env *env)
Definition: env.c:11
int nr_cpus_avail
Definition: env.h:42
char * os_release
Definition: env.h:38
char * map
Definition: env.h:20
struct cpu_topology_map * cpu
Definition: env.h:60
char * pmu_mappings
Definition: env.h:59
struct memory_node * memory_nodes
Definition: env.h:64
char * cpuid
Definition: env.h:44
int cpu__max_present_cpu(void)
Definition: cpumap.c:525
static int perf_env__read_arch(struct perf_env *env)
Definition: env.c:96
const char * perf_env__arch(struct perf_env *env)
Definition: env.c:164
int socket_id
Definition: env.h:9
struct cpu_cache_level * caches
Definition: env.h:61
static const char * normalize_arch(char *arch)
Definition: env.c:138
#define zfree(ptr)
Definition: util.h:25
char * cpu_desc
Definition: env.h:43
int core_id
Definition: env.h:10
int cpu_map__get_socket_id(int cpu)
Definition: cpumap.c:314
Definition: annotate.c:60
int perf_env__nr_cpus_avail(struct perf_env *env)
Definition: env.c:122
void free(void *)
int nr_memory_nodes
Definition: env.h:52
int perf_env__read_cpu_topology_map(struct perf_env *env)
Definition: env.c:69
int cpu_map__get_core_id(int cpu)
Definition: cpumap.c:369
int caches_cnt
Definition: env.h:62
const char * perf_env__raw_arch(struct perf_env *env)
Definition: env.c:117
char * size
Definition: env.h:19
static int perf_env__read_nr_cpus_avail(struct perf_env *env)
Definition: env.c:109
struct cpu_map * map
Definition: env.h:27
#define isdigit(x)
Definition: sane_ctype.h:34
char * version
Definition: env.h:39
void cpu_cache_level__free(struct cpu_cache_level *cache)
Definition: env.c:127
int nr_cmdline
Definition: env.h:48
char * type
Definition: env.h:18
char * sibling_threads
Definition: env.h:58