1 // SPDX-License-Identifier: GPL-2.0 2 #include "cpumap.h" 3 #include "env.h" 4 #include "util.h" 5 #include <errno.h> 6 7 struct perf_env perf_env; 8 9 void perf_env__exit(struct perf_env *env) 10 { 11 int i; 12 13 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); 23 zfree(&env->pmu_mappings); 24 zfree(&env->cpu); 25 26 for (i = 0; i < env->nr_numa_nodes; i++) 27 cpu_map__put(env->numa_nodes[i].map); 28 zfree(&env->numa_nodes); 29 30 for (i = 0; i < env->caches_cnt; i++) 31 cpu_cache_level__free(&env->caches[i]); 32 zfree(&env->caches); 33 } 34 35 int perf_env__set_cmdline(struct perf_env *env, int argc, const char *argv[]) 36 { 37 int i; 38 39 /* 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; 57 out_free: 58 zfree(&env->cmdline_argv); 59 out_enomem: 60 return -ENOMEM; 61 } 62 63 int 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) 71 env->nr_cpus_avail = cpu__max_present_cpu(); 72 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 } 89 90 void cpu_cache_level__free(struct cpu_cache_level *cache) 91 { 92 free(cache->type); 93 free(cache->map); 94 free(cache->size); 95 } 96