blob: 9e21538c42ae96747b671e4f27a9f73ff83dc6ec [file] [log] [blame]
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -03001#include "cpumap.h"
Arnaldo Carvalho de Melof0ce8882015-09-08 13:30:00 -03002#include "env.h"
3#include "util.h"
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03004#include <errno.h>
Arnaldo Carvalho de Melof0ce8882015-09-08 13:30:00 -03005
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -03006struct perf_env perf_env;
7
Arnaldo Carvalho de Melof0ce8882015-09-08 13:30:00 -03008void perf_env__exit(struct perf_env *env)
9{
Jiri Olsa720e98b2016-02-16 16:01:43 +010010 int i;
11
Arnaldo Carvalho de Melof0ce8882015-09-08 13:30:00 -030012 zfree(&env->hostname);
13 zfree(&env->os_release);
14 zfree(&env->version);
15 zfree(&env->arch);
16 zfree(&env->cpu_desc);
17 zfree(&env->cpuid);
18 zfree(&env->cmdline);
19 zfree(&env->cmdline_argv);
20 zfree(&env->sibling_cores);
21 zfree(&env->sibling_threads);
Arnaldo Carvalho de Melof0ce8882015-09-08 13:30:00 -030022 zfree(&env->pmu_mappings);
23 zfree(&env->cpu);
Jiri Olsa720e98b2016-02-16 16:01:43 +010024
Jiri Olsac60da222016-07-04 14:16:20 +020025 for (i = 0; i < env->nr_numa_nodes; i++)
26 cpu_map__put(env->numa_nodes[i].map);
27 zfree(&env->numa_nodes);
28
Jiri Olsa720e98b2016-02-16 16:01:43 +010029 for (i = 0; i < env->caches_cnt; i++)
30 cpu_cache_level__free(&env->caches[i]);
31 zfree(&env->caches);
Arnaldo Carvalho de Melof0ce8882015-09-08 13:30:00 -030032}
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -030033
34int perf_env__set_cmdline(struct perf_env *env, int argc, const char *argv[])
35{
36 int i;
37
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -030038 /* do not include NULL termination */
39 env->cmdline_argv = calloc(argc, sizeof(char *));
40 if (env->cmdline_argv == NULL)
41 goto out_enomem;
42
43 /*
44 * Must copy argv contents because it gets moved around during option
45 * parsing:
46 */
47 for (i = 0; i < argc ; i++) {
48 env->cmdline_argv[i] = argv[i];
49 if (env->cmdline_argv[i] == NULL)
50 goto out_free;
51 }
52
53 env->nr_cmdline = argc;
54
55 return 0;
56out_free:
57 zfree(&env->cmdline_argv);
58out_enomem:
59 return -ENOMEM;
60}
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -030061
62int perf_env__read_cpu_topology_map(struct perf_env *env)
63{
64 int cpu, nr_cpus;
65
66 if (env->cpu != NULL)
67 return 0;
68
69 if (env->nr_cpus_avail == 0)
Jan Stancekda8a58b2017-02-17 12:10:26 +010070 env->nr_cpus_avail = cpu__max_present_cpu();
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -030071
72 nr_cpus = env->nr_cpus_avail;
73 if (nr_cpus == -1)
74 return -EINVAL;
75
76 env->cpu = calloc(nr_cpus, sizeof(env->cpu[0]));
77 if (env->cpu == NULL)
78 return -ENOMEM;
79
80 for (cpu = 0; cpu < nr_cpus; ++cpu) {
81 env->cpu[cpu].core_id = cpu_map__get_core_id(cpu);
82 env->cpu[cpu].socket_id = cpu_map__get_socket_id(cpu);
83 }
84
85 env->nr_cpus_avail = nr_cpus;
86 return 0;
87}
Jiri Olsa720e98b2016-02-16 16:01:43 +010088
89void cpu_cache_level__free(struct cpu_cache_level *cache)
90{
91 free(cache->type);
92 free(cache->map);
93 free(cache->size);
94}