blob: 6276b340f893e9afd7eed80c71cdd5f1987e8f96 [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"
4#include "util.h"
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03005#include <errno.h>
Arnaldo Carvalho de Melof0ce8882015-09-08 13:30:00 -03006
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -03007struct perf_env perf_env;
8
Arnaldo Carvalho de Melof0ce8882015-09-08 13:30:00 -03009void perf_env__exit(struct perf_env *env)
10{
Jiri Olsa720e98b2016-02-16 16:01:43 +010011 int i;
12
Arnaldo Carvalho de Melof0ce8882015-09-08 13:30:00 -030013 zfree(&env->hostname);
14 zfree(&env->os_release);
15 zfree(&env->version);
16 zfree(&env->arch);
17 zfree(&env->cpu_desc);
18 zfree(&env->cpuid);
19 zfree(&env->cmdline);
20 zfree(&env->cmdline_argv);
21 zfree(&env->sibling_cores);
22 zfree(&env->sibling_threads);
Arnaldo Carvalho de Melof0ce8882015-09-08 13:30:00 -030023 zfree(&env->pmu_mappings);
24 zfree(&env->cpu);
Jiri Olsa720e98b2016-02-16 16:01:43 +010025
Jiri Olsac60da222016-07-04 14:16:20 +020026 for (i = 0; i < env->nr_numa_nodes; i++)
27 cpu_map__put(env->numa_nodes[i].map);
28 zfree(&env->numa_nodes);
29
Jiri Olsa720e98b2016-02-16 16:01:43 +010030 for (i = 0; i < env->caches_cnt; i++)
31 cpu_cache_level__free(&env->caches[i]);
32 zfree(&env->caches);
Arnaldo Carvalho de Melof0ce8882015-09-08 13:30:00 -030033}
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -030034
35int perf_env__set_cmdline(struct perf_env *env, int argc, const char *argv[])
36{
37 int i;
38
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -030039 /* do not include NULL termination */
40 env->cmdline_argv = calloc(argc, sizeof(char *));
41 if (env->cmdline_argv == NULL)
42 goto out_enomem;
43
44 /*
45 * Must copy argv contents because it gets moved around during option
46 * parsing:
47 */
48 for (i = 0; i < argc ; i++) {
49 env->cmdline_argv[i] = argv[i];
50 if (env->cmdline_argv[i] == NULL)
51 goto out_free;
52 }
53
54 env->nr_cmdline = argc;
55
56 return 0;
57out_free:
58 zfree(&env->cmdline_argv);
59out_enomem:
60 return -ENOMEM;
61}
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -030062
63int perf_env__read_cpu_topology_map(struct perf_env *env)
64{
65 int cpu, nr_cpus;
66
67 if (env->cpu != NULL)
68 return 0;
69
70 if (env->nr_cpus_avail == 0)
Jan Stancekda8a58b2017-02-17 12:10:26 +010071 env->nr_cpus_avail = cpu__max_present_cpu();
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -030072
73 nr_cpus = env->nr_cpus_avail;
74 if (nr_cpus == -1)
75 return -EINVAL;
76
77 env->cpu = calloc(nr_cpus, sizeof(env->cpu[0]));
78 if (env->cpu == NULL)
79 return -ENOMEM;
80
81 for (cpu = 0; cpu < nr_cpus; ++cpu) {
82 env->cpu[cpu].core_id = cpu_map__get_core_id(cpu);
83 env->cpu[cpu].socket_id = cpu_map__get_socket_id(cpu);
84 }
85
86 env->nr_cpus_avail = nr_cpus;
87 return 0;
88}
Jiri Olsa720e98b2016-02-16 16:01:43 +010089
90void cpu_cache_level__free(struct cpu_cache_level *cache)
91{
92 free(cache->type);
93 free(cache->map);
94 free(cache->size);
95}