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