Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Arnaldo Carvalho de Melo | aa36ddd | 2015-09-09 10:37:01 -0300 | [diff] [blame] | 2 | #include "cpumap.h" |
Arnaldo Carvalho de Melo | 5e51b0b | 2019-08-22 10:48:31 -0300 | [diff] [blame] | 3 | #include "debug.h" |
Arnaldo Carvalho de Melo | f0ce888 | 2015-09-08 13:30:00 -0300 | [diff] [blame] | 4 | #include "env.h" |
Arnaldo Carvalho de Melo | f1cedfb | 2019-09-30 11:50:15 -0300 | [diff] [blame] | 5 | #include "util/header.h" |
Arnaldo Carvalho de Melo | 3052ba5 | 2019-06-25 17:27:31 -0300 | [diff] [blame] | 6 | #include <linux/ctype.h> |
Arnaldo Carvalho de Melo | 7f7c536 | 2019-07-04 11:32:27 -0300 | [diff] [blame] | 7 | #include <linux/zalloc.h> |
Namhyung Kim | d1277aa | 2020-03-25 21:45:31 +0900 | [diff] [blame] | 8 | #include "cgroup.h" |
Arnaldo Carvalho de Melo | a43783a | 2017-04-18 10:46:11 -0300 | [diff] [blame] | 9 | #include <errno.h> |
Arnaldo Carvalho de Melo | 4e8fbc1 | 2017-12-11 14:47:49 -0300 | [diff] [blame] | 10 | #include <sys/utsname.h> |
Arnaldo Carvalho de Melo | 215a0d3 | 2019-07-04 11:21:24 -0300 | [diff] [blame] | 11 | #include <stdlib.h> |
Arnaldo Carvalho de Melo | 8520a98 | 2019-08-29 16:18:59 -0300 | [diff] [blame] | 12 | #include <string.h> |
Kim Phillips | 9fe8895 | 2021-08-17 17:15:07 -0500 | [diff] [blame] | 13 | #include "strbuf.h" |
Arnaldo Carvalho de Melo | f0ce888 | 2015-09-08 13:30:00 -0300 | [diff] [blame] | 14 | |
Arnaldo Carvalho de Melo | b699869 | 2015-09-08 16:58:20 -0300 | [diff] [blame] | 15 | struct perf_env perf_env; |
| 16 | |
Arnaldo Carvalho de Melo | ef0580e | 2020-10-20 15:57:21 -0300 | [diff] [blame] | 17 | #ifdef HAVE_LIBBPF_SUPPORT |
| 18 | #include "bpf-event.h" |
Dave Marchevsky | 6ac22d0 | 2021-10-11 01:20:30 -0700 | [diff] [blame] | 19 | #include "bpf-utils.h" |
Arnaldo Carvalho de Melo | ef0580e | 2020-10-20 15:57:21 -0300 | [diff] [blame] | 20 | #include <bpf/libbpf.h> |
| 21 | |
Song Liu | e4378f0 | 2019-03-11 22:30:42 -0700 | [diff] [blame] | 22 | void perf_env__insert_bpf_prog_info(struct perf_env *env, |
| 23 | struct bpf_prog_info_node *info_node) |
| 24 | { |
| 25 | __u32 prog_id = info_node->info_linear->info.id; |
| 26 | struct bpf_prog_info_node *node; |
| 27 | struct rb_node *parent = NULL; |
| 28 | struct rb_node **p; |
| 29 | |
| 30 | down_write(&env->bpf_progs.lock); |
| 31 | p = &env->bpf_progs.infos.rb_node; |
| 32 | |
| 33 | while (*p != NULL) { |
| 34 | parent = *p; |
| 35 | node = rb_entry(parent, struct bpf_prog_info_node, rb_node); |
| 36 | if (prog_id < node->info_linear->info.id) { |
| 37 | p = &(*p)->rb_left; |
| 38 | } else if (prog_id > node->info_linear->info.id) { |
| 39 | p = &(*p)->rb_right; |
| 40 | } else { |
| 41 | pr_debug("duplicated bpf prog info %u\n", prog_id); |
| 42 | goto out; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | rb_link_node(&info_node->rb_node, parent, p); |
| 47 | rb_insert_color(&info_node->rb_node, &env->bpf_progs.infos); |
| 48 | env->bpf_progs.infos_cnt++; |
| 49 | out: |
| 50 | up_write(&env->bpf_progs.lock); |
| 51 | } |
| 52 | |
| 53 | struct bpf_prog_info_node *perf_env__find_bpf_prog_info(struct perf_env *env, |
| 54 | __u32 prog_id) |
| 55 | { |
| 56 | struct bpf_prog_info_node *node = NULL; |
| 57 | struct rb_node *n; |
| 58 | |
| 59 | down_read(&env->bpf_progs.lock); |
| 60 | n = env->bpf_progs.infos.rb_node; |
| 61 | |
| 62 | while (n) { |
| 63 | node = rb_entry(n, struct bpf_prog_info_node, rb_node); |
| 64 | if (prog_id < node->info_linear->info.id) |
| 65 | n = n->rb_left; |
| 66 | else if (prog_id > node->info_linear->info.id) |
| 67 | n = n->rb_right; |
| 68 | else |
Jiri Olsa | aa52660 | 2019-04-16 15:41:51 +0200 | [diff] [blame] | 69 | goto out; |
Song Liu | e4378f0 | 2019-03-11 22:30:42 -0700 | [diff] [blame] | 70 | } |
Jiri Olsa | aa52660 | 2019-04-16 15:41:51 +0200 | [diff] [blame] | 71 | node = NULL; |
Song Liu | e4378f0 | 2019-03-11 22:30:42 -0700 | [diff] [blame] | 72 | |
Jiri Olsa | aa52660 | 2019-04-16 15:41:51 +0200 | [diff] [blame] | 73 | out: |
Song Liu | e4378f0 | 2019-03-11 22:30:42 -0700 | [diff] [blame] | 74 | up_read(&env->bpf_progs.lock); |
| 75 | return node; |
| 76 | } |
| 77 | |
Ian Rogers | 4924b1f | 2021-11-11 23:45:25 -0800 | [diff] [blame] | 78 | bool perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node) |
Song Liu | 3792cb2 | 2019-03-11 22:30:44 -0700 | [diff] [blame] | 79 | { |
| 80 | struct rb_node *parent = NULL; |
| 81 | __u32 btf_id = btf_node->id; |
| 82 | struct btf_node *node; |
| 83 | struct rb_node **p; |
Ian Rogers | 4924b1f | 2021-11-11 23:45:25 -0800 | [diff] [blame] | 84 | bool ret = true; |
Song Liu | 3792cb2 | 2019-03-11 22:30:44 -0700 | [diff] [blame] | 85 | |
| 86 | down_write(&env->bpf_progs.lock); |
| 87 | p = &env->bpf_progs.btfs.rb_node; |
| 88 | |
| 89 | while (*p != NULL) { |
| 90 | parent = *p; |
| 91 | node = rb_entry(parent, struct btf_node, rb_node); |
| 92 | if (btf_id < node->id) { |
| 93 | p = &(*p)->rb_left; |
| 94 | } else if (btf_id > node->id) { |
| 95 | p = &(*p)->rb_right; |
| 96 | } else { |
| 97 | pr_debug("duplicated btf %u\n", btf_id); |
Ian Rogers | 4924b1f | 2021-11-11 23:45:25 -0800 | [diff] [blame] | 98 | ret = false; |
Song Liu | 3792cb2 | 2019-03-11 22:30:44 -0700 | [diff] [blame] | 99 | goto out; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | rb_link_node(&btf_node->rb_node, parent, p); |
| 104 | rb_insert_color(&btf_node->rb_node, &env->bpf_progs.btfs); |
| 105 | env->bpf_progs.btfs_cnt++; |
| 106 | out: |
| 107 | up_write(&env->bpf_progs.lock); |
Ian Rogers | 4924b1f | 2021-11-11 23:45:25 -0800 | [diff] [blame] | 108 | return ret; |
Song Liu | 3792cb2 | 2019-03-11 22:30:44 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | struct btf_node *perf_env__find_btf(struct perf_env *env, __u32 btf_id) |
| 112 | { |
| 113 | struct btf_node *node = NULL; |
| 114 | struct rb_node *n; |
| 115 | |
| 116 | down_read(&env->bpf_progs.lock); |
| 117 | n = env->bpf_progs.btfs.rb_node; |
| 118 | |
| 119 | while (n) { |
| 120 | node = rb_entry(n, struct btf_node, rb_node); |
| 121 | if (btf_id < node->id) |
| 122 | n = n->rb_left; |
| 123 | else if (btf_id > node->id) |
| 124 | n = n->rb_right; |
| 125 | else |
Jiri Olsa | 2db7b1e | 2019-04-17 16:55:39 +0200 | [diff] [blame] | 126 | goto out; |
Song Liu | 3792cb2 | 2019-03-11 22:30:44 -0700 | [diff] [blame] | 127 | } |
Jiri Olsa | 2db7b1e | 2019-04-17 16:55:39 +0200 | [diff] [blame] | 128 | node = NULL; |
Song Liu | 3792cb2 | 2019-03-11 22:30:44 -0700 | [diff] [blame] | 129 | |
Jiri Olsa | 2db7b1e | 2019-04-17 16:55:39 +0200 | [diff] [blame] | 130 | out: |
Bo YU | 2e71267 | 2019-04-22 04:01:38 -0400 | [diff] [blame] | 131 | up_read(&env->bpf_progs.lock); |
Song Liu | 3792cb2 | 2019-03-11 22:30:44 -0700 | [diff] [blame] | 132 | return node; |
| 133 | } |
| 134 | |
Song Liu | e4378f0 | 2019-03-11 22:30:42 -0700 | [diff] [blame] | 135 | /* purge data in bpf_progs.infos tree */ |
| 136 | static void perf_env__purge_bpf(struct perf_env *env) |
| 137 | { |
| 138 | struct rb_root *root; |
| 139 | struct rb_node *next; |
| 140 | |
| 141 | down_write(&env->bpf_progs.lock); |
| 142 | |
| 143 | root = &env->bpf_progs.infos; |
| 144 | next = rb_first(root); |
| 145 | |
| 146 | while (next) { |
| 147 | struct bpf_prog_info_node *node; |
| 148 | |
| 149 | node = rb_entry(next, struct bpf_prog_info_node, rb_node); |
| 150 | next = rb_next(&node->rb_node); |
| 151 | rb_erase(&node->rb_node, root); |
Riccardo Mancini | 67069a1 | 2021-06-03 00:40:23 +0200 | [diff] [blame] | 152 | free(node->info_linear); |
Song Liu | e4378f0 | 2019-03-11 22:30:42 -0700 | [diff] [blame] | 153 | free(node); |
| 154 | } |
| 155 | |
| 156 | env->bpf_progs.infos_cnt = 0; |
| 157 | |
Song Liu | 3792cb2 | 2019-03-11 22:30:44 -0700 | [diff] [blame] | 158 | root = &env->bpf_progs.btfs; |
| 159 | next = rb_first(root); |
| 160 | |
| 161 | while (next) { |
| 162 | struct btf_node *node; |
| 163 | |
| 164 | node = rb_entry(next, struct btf_node, rb_node); |
| 165 | next = rb_next(&node->rb_node); |
| 166 | rb_erase(&node->rb_node, root); |
| 167 | free(node); |
| 168 | } |
| 169 | |
| 170 | env->bpf_progs.btfs_cnt = 0; |
| 171 | |
Song Liu | e4378f0 | 2019-03-11 22:30:42 -0700 | [diff] [blame] | 172 | up_write(&env->bpf_progs.lock); |
| 173 | } |
Arnaldo Carvalho de Melo | ef0580e | 2020-10-20 15:57:21 -0300 | [diff] [blame] | 174 | #else // HAVE_LIBBPF_SUPPORT |
| 175 | static void perf_env__purge_bpf(struct perf_env *env __maybe_unused) |
| 176 | { |
| 177 | } |
| 178 | #endif // HAVE_LIBBPF_SUPPORT |
Song Liu | e4378f0 | 2019-03-11 22:30:42 -0700 | [diff] [blame] | 179 | |
Arnaldo Carvalho de Melo | f0ce888 | 2015-09-08 13:30:00 -0300 | [diff] [blame] | 180 | void perf_env__exit(struct perf_env *env) |
| 181 | { |
Ravi Bangoria | ff34eaa | 2022-06-04 10:15:15 +0530 | [diff] [blame] | 182 | int i, j; |
Jiri Olsa | 720e98b | 2016-02-16 16:01:43 +0100 | [diff] [blame] | 183 | |
Song Liu | e4378f0 | 2019-03-11 22:30:42 -0700 | [diff] [blame] | 184 | perf_env__purge_bpf(env); |
Namhyung Kim | d1277aa | 2020-03-25 21:45:31 +0900 | [diff] [blame] | 185 | perf_env__purge_cgroups(env); |
Arnaldo Carvalho de Melo | f0ce888 | 2015-09-08 13:30:00 -0300 | [diff] [blame] | 186 | zfree(&env->hostname); |
| 187 | zfree(&env->os_release); |
| 188 | zfree(&env->version); |
| 189 | zfree(&env->arch); |
| 190 | zfree(&env->cpu_desc); |
| 191 | zfree(&env->cpuid); |
| 192 | zfree(&env->cmdline); |
| 193 | zfree(&env->cmdline_argv); |
Riccardo Mancini | 42db3d9 | 2021-07-15 18:07:07 +0200 | [diff] [blame] | 194 | zfree(&env->sibling_dies); |
Arnaldo Carvalho de Melo | f0ce888 | 2015-09-08 13:30:00 -0300 | [diff] [blame] | 195 | zfree(&env->sibling_cores); |
| 196 | zfree(&env->sibling_threads); |
Arnaldo Carvalho de Melo | f0ce888 | 2015-09-08 13:30:00 -0300 | [diff] [blame] | 197 | zfree(&env->pmu_mappings); |
| 198 | zfree(&env->cpu); |
Ravi Bangoria | ff34eaa | 2022-06-04 10:15:15 +0530 | [diff] [blame] | 199 | for (i = 0; i < env->nr_cpu_pmu_caps; i++) |
| 200 | zfree(&env->cpu_pmu_caps[i]); |
Riccardo Mancini | da6b7c6 | 2021-07-15 18:07:13 +0200 | [diff] [blame] | 201 | zfree(&env->cpu_pmu_caps); |
Jiri Olsa | 389799a | 2019-08-29 13:31:48 +0200 | [diff] [blame] | 202 | zfree(&env->numa_map); |
Jiri Olsa | 720e98b | 2016-02-16 16:01:43 +0100 | [diff] [blame] | 203 | |
Jiri Olsa | c60da22 | 2016-07-04 14:16:20 +0200 | [diff] [blame] | 204 | for (i = 0; i < env->nr_numa_nodes; i++) |
Jiri Olsa | 38f01d8 | 2019-07-21 13:24:17 +0200 | [diff] [blame] | 205 | perf_cpu_map__put(env->numa_nodes[i].map); |
Jiri Olsa | c60da22 | 2016-07-04 14:16:20 +0200 | [diff] [blame] | 206 | zfree(&env->numa_nodes); |
| 207 | |
Jiri Olsa | 720e98b | 2016-02-16 16:01:43 +0100 | [diff] [blame] | 208 | for (i = 0; i < env->caches_cnt; i++) |
| 209 | cpu_cache_level__free(&env->caches[i]); |
| 210 | zfree(&env->caches); |
Jiri Olsa | e725920 | 2018-03-09 11:14:34 +0100 | [diff] [blame] | 211 | |
| 212 | for (i = 0; i < env->nr_memory_nodes; i++) |
Arnaldo Carvalho de Melo | d8f9da2 | 2019-07-04 12:06:20 -0300 | [diff] [blame] | 213 | zfree(&env->memory_nodes[i].set); |
Jiri Olsa | e725920 | 2018-03-09 11:14:34 +0100 | [diff] [blame] | 214 | zfree(&env->memory_nodes); |
Jin Yao | f7d74ce | 2021-05-14 20:29:47 +0800 | [diff] [blame] | 215 | |
| 216 | for (i = 0; i < env->nr_hybrid_nodes; i++) { |
| 217 | zfree(&env->hybrid_nodes[i].pmu_name); |
| 218 | zfree(&env->hybrid_nodes[i].cpus); |
| 219 | } |
| 220 | zfree(&env->hybrid_nodes); |
Jin Yao | e119083 | 2021-05-14 20:29:48 +0800 | [diff] [blame] | 221 | |
Ravi Bangoria | 2139f74 | 2022-06-04 10:15:16 +0530 | [diff] [blame] | 222 | for (i = 0; i < env->nr_pmus_with_caps; i++) { |
| 223 | for (j = 0; j < env->pmu_caps[i].nr_caps; j++) |
| 224 | zfree(&env->pmu_caps[i].caps[j]); |
| 225 | zfree(&env->pmu_caps[i].caps); |
| 226 | zfree(&env->pmu_caps[i].pmu_name); |
Jin Yao | e119083 | 2021-05-14 20:29:48 +0800 | [diff] [blame] | 227 | } |
Ravi Bangoria | 2139f74 | 2022-06-04 10:15:16 +0530 | [diff] [blame] | 228 | zfree(&env->pmu_caps); |
Arnaldo Carvalho de Melo | f0ce888 | 2015-09-08 13:30:00 -0300 | [diff] [blame] | 229 | } |
Arnaldo Carvalho de Melo | b699869 | 2015-09-08 16:58:20 -0300 | [diff] [blame] | 230 | |
Leo Yan | 7c0223e | 2021-08-09 19:27:25 +0800 | [diff] [blame] | 231 | void perf_env__init(struct perf_env *env) |
Song Liu | e4378f0 | 2019-03-11 22:30:42 -0700 | [diff] [blame] | 232 | { |
Arnaldo Carvalho de Melo | ef0580e | 2020-10-20 15:57:21 -0300 | [diff] [blame] | 233 | #ifdef HAVE_LIBBPF_SUPPORT |
Song Liu | e4378f0 | 2019-03-11 22:30:42 -0700 | [diff] [blame] | 234 | env->bpf_progs.infos = RB_ROOT; |
Song Liu | 3792cb2 | 2019-03-11 22:30:44 -0700 | [diff] [blame] | 235 | env->bpf_progs.btfs = RB_ROOT; |
Song Liu | e4378f0 | 2019-03-11 22:30:42 -0700 | [diff] [blame] | 236 | init_rwsem(&env->bpf_progs.lock); |
Arnaldo Carvalho de Melo | ef0580e | 2020-10-20 15:57:21 -0300 | [diff] [blame] | 237 | #endif |
Leo Yan | 7c0223e | 2021-08-09 19:27:25 +0800 | [diff] [blame] | 238 | env->kernel_is_64_bit = -1; |
| 239 | } |
| 240 | |
| 241 | static void perf_env__init_kernel_mode(struct perf_env *env) |
| 242 | { |
| 243 | const char *arch = perf_env__raw_arch(env); |
| 244 | |
| 245 | if (!strncmp(arch, "x86_64", 6) || !strncmp(arch, "aarch64", 7) || |
| 246 | !strncmp(arch, "arm64", 5) || !strncmp(arch, "mips64", 6) || |
| 247 | !strncmp(arch, "parisc64", 8) || !strncmp(arch, "riscv64", 7) || |
| 248 | !strncmp(arch, "s390x", 5) || !strncmp(arch, "sparc64", 7)) |
| 249 | env->kernel_is_64_bit = 1; |
| 250 | else |
| 251 | env->kernel_is_64_bit = 0; |
| 252 | } |
| 253 | |
| 254 | int perf_env__kernel_is_64_bit(struct perf_env *env) |
| 255 | { |
| 256 | if (env->kernel_is_64_bit == -1) |
| 257 | perf_env__init_kernel_mode(env); |
| 258 | |
| 259 | return env->kernel_is_64_bit; |
Song Liu | e4378f0 | 2019-03-11 22:30:42 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Arnaldo Carvalho de Melo | b699869 | 2015-09-08 16:58:20 -0300 | [diff] [blame] | 262 | int perf_env__set_cmdline(struct perf_env *env, int argc, const char *argv[]) |
| 263 | { |
| 264 | int i; |
| 265 | |
Arnaldo Carvalho de Melo | b699869 | 2015-09-08 16:58:20 -0300 | [diff] [blame] | 266 | /* do not include NULL termination */ |
| 267 | env->cmdline_argv = calloc(argc, sizeof(char *)); |
| 268 | if (env->cmdline_argv == NULL) |
| 269 | goto out_enomem; |
| 270 | |
| 271 | /* |
| 272 | * Must copy argv contents because it gets moved around during option |
| 273 | * parsing: |
| 274 | */ |
| 275 | for (i = 0; i < argc ; i++) { |
| 276 | env->cmdline_argv[i] = argv[i]; |
| 277 | if (env->cmdline_argv[i] == NULL) |
| 278 | goto out_free; |
| 279 | } |
| 280 | |
| 281 | env->nr_cmdline = argc; |
| 282 | |
| 283 | return 0; |
| 284 | out_free: |
| 285 | zfree(&env->cmdline_argv); |
| 286 | out_enomem: |
| 287 | return -ENOMEM; |
| 288 | } |
Arnaldo Carvalho de Melo | aa36ddd | 2015-09-09 10:37:01 -0300 | [diff] [blame] | 289 | |
| 290 | int perf_env__read_cpu_topology_map(struct perf_env *env) |
| 291 | { |
Ian Rogers | 6d18804 | 2022-01-04 22:13:51 -0800 | [diff] [blame] | 292 | int idx, nr_cpus; |
Arnaldo Carvalho de Melo | aa36ddd | 2015-09-09 10:37:01 -0300 | [diff] [blame] | 293 | |
| 294 | if (env->cpu != NULL) |
| 295 | return 0; |
| 296 | |
| 297 | if (env->nr_cpus_avail == 0) |
Ian Rogers | 6d18804 | 2022-01-04 22:13:51 -0800 | [diff] [blame] | 298 | env->nr_cpus_avail = cpu__max_present_cpu().cpu; |
Arnaldo Carvalho de Melo | aa36ddd | 2015-09-09 10:37:01 -0300 | [diff] [blame] | 299 | |
| 300 | nr_cpus = env->nr_cpus_avail; |
| 301 | if (nr_cpus == -1) |
| 302 | return -EINVAL; |
| 303 | |
| 304 | env->cpu = calloc(nr_cpus, sizeof(env->cpu[0])); |
| 305 | if (env->cpu == NULL) |
| 306 | return -ENOMEM; |
| 307 | |
Ian Rogers | 6d18804 | 2022-01-04 22:13:51 -0800 | [diff] [blame] | 308 | for (idx = 0; idx < nr_cpus; ++idx) { |
| 309 | struct perf_cpu cpu = { .cpu = idx }; |
| 310 | |
| 311 | env->cpu[idx].core_id = cpu__get_core_id(cpu); |
| 312 | env->cpu[idx].socket_id = cpu__get_socket_id(cpu); |
| 313 | env->cpu[idx].die_id = cpu__get_die_id(cpu); |
Arnaldo Carvalho de Melo | aa36ddd | 2015-09-09 10:37:01 -0300 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | env->nr_cpus_avail = nr_cpus; |
| 317 | return 0; |
| 318 | } |
Jiri Olsa | 720e98b | 2016-02-16 16:01:43 +0100 | [diff] [blame] | 319 | |
Kim Phillips | 9fe8895 | 2021-08-17 17:15:07 -0500 | [diff] [blame] | 320 | int perf_env__read_pmu_mappings(struct perf_env *env) |
| 321 | { |
| 322 | struct perf_pmu *pmu = NULL; |
| 323 | u32 pmu_num = 0; |
| 324 | struct strbuf sb; |
| 325 | |
| 326 | while ((pmu = perf_pmu__scan(pmu))) { |
| 327 | if (!pmu->name) |
| 328 | continue; |
| 329 | pmu_num++; |
| 330 | } |
| 331 | if (!pmu_num) { |
| 332 | pr_debug("pmu mappings not available\n"); |
| 333 | return -ENOENT; |
| 334 | } |
| 335 | env->nr_pmu_mappings = pmu_num; |
| 336 | |
| 337 | if (strbuf_init(&sb, 128 * pmu_num) < 0) |
| 338 | return -ENOMEM; |
| 339 | |
| 340 | while ((pmu = perf_pmu__scan(pmu))) { |
| 341 | if (!pmu->name) |
| 342 | continue; |
| 343 | if (strbuf_addf(&sb, "%u:%s", pmu->type, pmu->name) < 0) |
| 344 | goto error; |
| 345 | /* include a NULL character at the end */ |
| 346 | if (strbuf_add(&sb, "", 1) < 0) |
| 347 | goto error; |
| 348 | } |
| 349 | |
| 350 | env->pmu_mappings = strbuf_detach(&sb, NULL); |
| 351 | |
| 352 | return 0; |
| 353 | |
| 354 | error: |
| 355 | strbuf_release(&sb); |
| 356 | return -1; |
| 357 | } |
| 358 | |
Arnaldo Carvalho de Melo | f1cedfb | 2019-09-30 11:50:15 -0300 | [diff] [blame] | 359 | int perf_env__read_cpuid(struct perf_env *env) |
| 360 | { |
| 361 | char cpuid[128]; |
| 362 | int err = get_cpuid(cpuid, sizeof(cpuid)); |
| 363 | |
| 364 | if (err) |
| 365 | return err; |
| 366 | |
| 367 | free(env->cpuid); |
| 368 | env->cpuid = strdup(cpuid); |
| 369 | if (env->cpuid == NULL) |
| 370 | return ENOMEM; |
| 371 | return 0; |
| 372 | } |
| 373 | |
Adrian Hunter | dbbd34a | 2018-05-17 12:21:53 +0300 | [diff] [blame] | 374 | static int perf_env__read_arch(struct perf_env *env) |
| 375 | { |
| 376 | struct utsname uts; |
| 377 | |
| 378 | if (env->arch) |
| 379 | return 0; |
| 380 | |
| 381 | if (!uname(&uts)) |
| 382 | env->arch = strdup(uts.machine); |
| 383 | |
| 384 | return env->arch ? 0 : -ENOMEM; |
| 385 | } |
| 386 | |
Adrian Hunter | 9cecca3 | 2018-05-22 13:54:32 +0300 | [diff] [blame] | 387 | static int perf_env__read_nr_cpus_avail(struct perf_env *env) |
| 388 | { |
| 389 | if (env->nr_cpus_avail == 0) |
Ian Rogers | 6d18804 | 2022-01-04 22:13:51 -0800 | [diff] [blame] | 390 | env->nr_cpus_avail = cpu__max_present_cpu().cpu; |
Adrian Hunter | 9cecca3 | 2018-05-22 13:54:32 +0300 | [diff] [blame] | 391 | |
| 392 | return env->nr_cpus_avail ? 0 : -ENOENT; |
| 393 | } |
| 394 | |
Adrian Hunter | dbbd34a | 2018-05-17 12:21:53 +0300 | [diff] [blame] | 395 | const char *perf_env__raw_arch(struct perf_env *env) |
| 396 | { |
| 397 | return env && !perf_env__read_arch(env) ? env->arch : "unknown"; |
| 398 | } |
| 399 | |
Adrian Hunter | 9cecca3 | 2018-05-22 13:54:32 +0300 | [diff] [blame] | 400 | int perf_env__nr_cpus_avail(struct perf_env *env) |
| 401 | { |
| 402 | return env && !perf_env__read_nr_cpus_avail(env) ? env->nr_cpus_avail : 0; |
| 403 | } |
| 404 | |
Jiri Olsa | 720e98b | 2016-02-16 16:01:43 +0100 | [diff] [blame] | 405 | void cpu_cache_level__free(struct cpu_cache_level *cache) |
| 406 | { |
Arnaldo Carvalho de Melo | d8f9da2 | 2019-07-04 12:06:20 -0300 | [diff] [blame] | 407 | zfree(&cache->type); |
| 408 | zfree(&cache->map); |
| 409 | zfree(&cache->size); |
Jiri Olsa | 720e98b | 2016-02-16 16:01:43 +0100 | [diff] [blame] | 410 | } |
Arnaldo Carvalho de Melo | 4e8fbc1 | 2017-12-11 14:47:49 -0300 | [diff] [blame] | 411 | |
| 412 | /* |
| 413 | * Return architecture name in a normalized form. |
| 414 | * The conversion logic comes from the Makefile. |
| 415 | */ |
| 416 | static const char *normalize_arch(char *arch) |
| 417 | { |
| 418 | if (!strcmp(arch, "x86_64")) |
| 419 | return "x86"; |
| 420 | if (arch[0] == 'i' && arch[2] == '8' && arch[3] == '6') |
| 421 | return "x86"; |
| 422 | if (!strcmp(arch, "sun4u") || !strncmp(arch, "sparc", 5)) |
| 423 | return "sparc"; |
Li Huafei | 4502da0 | 2021-07-26 20:38:53 +0800 | [diff] [blame] | 424 | if (!strncmp(arch, "aarch64", 7) || !strncmp(arch, "arm64", 5)) |
Arnaldo Carvalho de Melo | 4e8fbc1 | 2017-12-11 14:47:49 -0300 | [diff] [blame] | 425 | return "arm64"; |
| 426 | if (!strncmp(arch, "arm", 3) || !strcmp(arch, "sa110")) |
| 427 | return "arm"; |
| 428 | if (!strncmp(arch, "s390", 4)) |
| 429 | return "s390"; |
| 430 | if (!strncmp(arch, "parisc", 6)) |
| 431 | return "parisc"; |
| 432 | if (!strncmp(arch, "powerpc", 7) || !strncmp(arch, "ppc", 3)) |
| 433 | return "powerpc"; |
| 434 | if (!strncmp(arch, "mips", 4)) |
| 435 | return "mips"; |
| 436 | if (!strncmp(arch, "sh", 2) && isdigit(arch[2])) |
| 437 | return "sh"; |
| 438 | |
| 439 | return arch; |
| 440 | } |
| 441 | |
| 442 | const char *perf_env__arch(struct perf_env *env) |
| 443 | { |
Arnaldo Carvalho de Melo | 4e8fbc1 | 2017-12-11 14:47:49 -0300 | [diff] [blame] | 444 | char *arch_name; |
| 445 | |
Arnaldo Carvalho de Melo | 804234f | 2018-11-27 11:45:49 -0300 | [diff] [blame] | 446 | if (!env || !env->arch) { /* Assume local operation */ |
Arnaldo Carvalho de Melo | ebcb946 | 2020-03-02 11:23:03 -0300 | [diff] [blame] | 447 | static struct utsname uts = { .machine[0] = '\0', }; |
| 448 | if (uts.machine[0] == '\0' && uname(&uts) < 0) |
Arnaldo Carvalho de Melo | 4e8fbc1 | 2017-12-11 14:47:49 -0300 | [diff] [blame] | 449 | return NULL; |
| 450 | arch_name = uts.machine; |
| 451 | } else |
| 452 | arch_name = env->arch; |
| 453 | |
| 454 | return normalize_arch(arch_name); |
| 455 | } |
Jiri Olsa | 389799a | 2019-08-29 13:31:48 +0200 | [diff] [blame] | 456 | |
Kim Phillips | 9fe8895 | 2021-08-17 17:15:07 -0500 | [diff] [blame] | 457 | const char *perf_env__cpuid(struct perf_env *env) |
| 458 | { |
| 459 | int status; |
| 460 | |
| 461 | if (!env || !env->cpuid) { /* Assume local operation */ |
| 462 | status = perf_env__read_cpuid(env); |
| 463 | if (status) |
| 464 | return NULL; |
| 465 | } |
| 466 | |
| 467 | return env->cpuid; |
| 468 | } |
| 469 | |
| 470 | int perf_env__nr_pmu_mappings(struct perf_env *env) |
| 471 | { |
| 472 | int status; |
| 473 | |
| 474 | if (!env || !env->nr_pmu_mappings) { /* Assume local operation */ |
| 475 | status = perf_env__read_pmu_mappings(env); |
| 476 | if (status) |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | return env->nr_pmu_mappings; |
| 481 | } |
| 482 | |
| 483 | const char *perf_env__pmu_mappings(struct perf_env *env) |
| 484 | { |
| 485 | int status; |
| 486 | |
| 487 | if (!env || !env->pmu_mappings) { /* Assume local operation */ |
| 488 | status = perf_env__read_pmu_mappings(env); |
| 489 | if (status) |
| 490 | return NULL; |
| 491 | } |
| 492 | |
| 493 | return env->pmu_mappings; |
| 494 | } |
Jiri Olsa | 389799a | 2019-08-29 13:31:48 +0200 | [diff] [blame] | 495 | |
Ian Rogers | 6d18804 | 2022-01-04 22:13:51 -0800 | [diff] [blame] | 496 | int perf_env__numa_node(struct perf_env *env, struct perf_cpu cpu) |
Jiri Olsa | 389799a | 2019-08-29 13:31:48 +0200 | [diff] [blame] | 497 | { |
| 498 | if (!env->nr_numa_map) { |
| 499 | struct numa_node *nn; |
| 500 | int i, nr = 0; |
| 501 | |
| 502 | for (i = 0; i < env->nr_numa_nodes; i++) { |
| 503 | nn = &env->numa_nodes[i]; |
Ian Rogers | 6d18804 | 2022-01-04 22:13:51 -0800 | [diff] [blame] | 504 | nr = max(nr, perf_cpu_map__max(nn->map).cpu); |
Jiri Olsa | 389799a | 2019-08-29 13:31:48 +0200 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | nr++; |
| 508 | |
| 509 | /* |
| 510 | * We initialize the numa_map array to prepare |
| 511 | * it for missing cpus, which return node -1 |
| 512 | */ |
| 513 | env->numa_map = malloc(nr * sizeof(int)); |
| 514 | if (!env->numa_map) |
| 515 | return -1; |
| 516 | |
| 517 | for (i = 0; i < nr; i++) |
| 518 | env->numa_map[i] = -1; |
| 519 | |
| 520 | env->nr_numa_map = nr; |
| 521 | |
| 522 | for (i = 0; i < env->nr_numa_nodes; i++) { |
Ian Rogers | 6d18804 | 2022-01-04 22:13:51 -0800 | [diff] [blame] | 523 | struct perf_cpu tmp; |
| 524 | int j; |
Jiri Olsa | 389799a | 2019-08-29 13:31:48 +0200 | [diff] [blame] | 525 | |
| 526 | nn = &env->numa_nodes[i]; |
Ian Rogers | 6d18804 | 2022-01-04 22:13:51 -0800 | [diff] [blame] | 527 | perf_cpu_map__for_each_cpu(tmp, j, nn->map) |
| 528 | env->numa_map[tmp.cpu] = i; |
Jiri Olsa | 389799a | 2019-08-29 13:31:48 +0200 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | |
Ian Rogers | 6d18804 | 2022-01-04 22:13:51 -0800 | [diff] [blame] | 532 | return cpu.cpu >= 0 && cpu.cpu < env->nr_numa_map ? env->numa_map[cpu.cpu] : -1; |
Jiri Olsa | 389799a | 2019-08-29 13:31:48 +0200 | [diff] [blame] | 533 | } |
Ravi Bangoria | 2139f74 | 2022-06-04 10:15:16 +0530 | [diff] [blame] | 534 | |
| 535 | char *perf_env__find_pmu_cap(struct perf_env *env, const char *pmu_name, |
| 536 | const char *cap) |
| 537 | { |
| 538 | char *cap_eq; |
| 539 | int cap_size; |
| 540 | char **ptr; |
| 541 | int i, j; |
| 542 | |
| 543 | if (!pmu_name || !cap) |
| 544 | return NULL; |
| 545 | |
| 546 | cap_size = strlen(cap); |
| 547 | cap_eq = zalloc(cap_size + 2); |
| 548 | if (!cap_eq) |
| 549 | return NULL; |
| 550 | |
| 551 | memcpy(cap_eq, cap, cap_size); |
| 552 | cap_eq[cap_size] = '='; |
| 553 | |
| 554 | if (!strcmp(pmu_name, "cpu")) { |
| 555 | for (i = 0; i < env->nr_cpu_pmu_caps; i++) { |
| 556 | if (!strncmp(env->cpu_pmu_caps[i], cap_eq, cap_size + 1)) { |
| 557 | free(cap_eq); |
| 558 | return &env->cpu_pmu_caps[i][cap_size + 1]; |
| 559 | } |
| 560 | } |
| 561 | goto out; |
| 562 | } |
| 563 | |
| 564 | for (i = 0; i < env->nr_pmus_with_caps; i++) { |
| 565 | if (strcmp(env->pmu_caps[i].pmu_name, pmu_name)) |
| 566 | continue; |
| 567 | |
| 568 | ptr = env->pmu_caps[i].caps; |
| 569 | |
| 570 | for (j = 0; j < env->pmu_caps[i].nr_caps; j++) { |
| 571 | if (!strncmp(ptr[j], cap_eq, cap_size + 1)) { |
| 572 | free(cap_eq); |
| 573 | return &ptr[j][cap_size + 1]; |
| 574 | } |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | out: |
| 579 | free(cap_eq); |
| 580 | return NULL; |
| 581 | } |