1 #include "cpumap.h" 2 #include "env.h" 3 #include "util.h" 4 5 struct perf_env perf_env; 6 7 void perf_env__exit(struct perf_env *env) 8 { 9 int i; 10 11 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); 24 25 for (i = 0; i < env->caches_cnt; i++) 26 cpu_cache_level__free(&env->caches[i]); 27 zfree(&env->caches); 28 } 29 30 int perf_env__set_cmdline(struct perf_env *env, int argc, const char *argv[]) 31 { 32 int i; 33 34 /* 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; 52 out_free: 53 zfree(&env->cmdline_argv); 54 out_enomem: 55 return -ENOMEM; 56 } 57 58 int 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 } 84 85 void cpu_cache_level__free(struct cpu_cache_level *cache) 86 { 87 free(cache->type); 88 free(cache->map); 89 free(cache->size); 90 } 91