blob: 49a11d9d8b8f050efa48715cfba9e54731f63eef [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"
4
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -03005struct perf_env perf_env;
6
Arnaldo Carvalho de Melof0ce8882015-09-08 13:30:00 -03007void perf_env__exit(struct perf_env *env)
8{
Jiri Olsa720e98b2016-02-16 16:01:43 +01009 int i;
10
Arnaldo Carvalho de Melof0ce8882015-09-08 13:30:00 -030011 zfree(&env->hostname);
12 zfree(&env->os_release);
13 zfree(&env->version);
14 zfree(&env->arch);
15 zfree(&env->cpu_desc);
16 zfree(&env->cpuid);
17 zfree(&env->cmdline);
18 zfree(&env->cmdline_argv);
19 zfree(&env->sibling_cores);
20 zfree(&env->sibling_threads);
21 zfree(&env->numa_nodes);
22 zfree(&env->pmu_mappings);
23 zfree(&env->cpu);
Jiri Olsa720e98b2016-02-16 16:01:43 +010024
25 for (i = 0; i < env->caches_cnt; i++)
26 cpu_cache_level__free(&env->caches[i]);
27 zfree(&env->caches);
Arnaldo Carvalho de Melof0ce8882015-09-08 13:30:00 -030028}
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -030029
30int perf_env__set_cmdline(struct perf_env *env, int argc, const char *argv[])
31{
32 int i;
33
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -030034 /* do not include NULL termination */
35 env->cmdline_argv = calloc(argc, sizeof(char *));
36 if (env->cmdline_argv == NULL)
37 goto out_enomem;
38
39 /*
40 * Must copy argv contents because it gets moved around during option
41 * parsing:
42 */
43 for (i = 0; i < argc ; i++) {
44 env->cmdline_argv[i] = argv[i];
45 if (env->cmdline_argv[i] == NULL)
46 goto out_free;
47 }
48
49 env->nr_cmdline = argc;
50
51 return 0;
52out_free:
53 zfree(&env->cmdline_argv);
54out_enomem:
55 return -ENOMEM;
56}
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -030057
58int perf_env__read_cpu_topology_map(struct perf_env *env)
59{
60 int cpu, nr_cpus;
61
62 if (env->cpu != NULL)
63 return 0;
64
65 if (env->nr_cpus_avail == 0)
66 env->nr_cpus_avail = sysconf(_SC_NPROCESSORS_CONF);
67
68 nr_cpus = env->nr_cpus_avail;
69 if (nr_cpus == -1)
70 return -EINVAL;
71
72 env->cpu = calloc(nr_cpus, sizeof(env->cpu[0]));
73 if (env->cpu == NULL)
74 return -ENOMEM;
75
76 for (cpu = 0; cpu < nr_cpus; ++cpu) {
77 env->cpu[cpu].core_id = cpu_map__get_core_id(cpu);
78 env->cpu[cpu].socket_id = cpu_map__get_socket_id(cpu);
79 }
80
81 env->nr_cpus_avail = nr_cpus;
82 return 0;
83}
Jiri Olsa720e98b2016-02-16 16:01:43 +010084
85void cpu_cache_level__free(struct cpu_cache_level *cache)
86{
87 free(cache->type);
88 free(cache->map);
89 free(cache->size);
90}