1 // SPDX-License-Identifier: GPL-2.0 2 #include "parse-events.h" 3 #include "evsel.h" 4 #include "evlist.h" 5 #include <api/fs/fs.h> 6 #include "tests.h" 7 #include "debug.h" 8 #include "parse-events.h" 9 #include "pmu.h" 10 #include "pmus.h" 11 #include <dirent.h> 12 #include <errno.h> 13 #include "fncache.h" 14 #include <sys/types.h> 15 #include <sys/stat.h> 16 #include <unistd.h> 17 #include <linux/kernel.h> 18 #include <linux/hw_breakpoint.h> 19 #include <api/fs/tracing_path.h> 20 21 #define PERF_TP_SAMPLE_TYPE (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | \ 22 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD) 23 24 static int num_core_entries(void) 25 { 26 /* 27 * If the kernel supports extended type, expect events to be 28 * opened once for each core PMU type. Otherwise fall back to the legacy 29 * behavior of opening only one event even though there are multiple 30 * PMUs 31 */ 32 if (perf_pmus__supports_extended_type()) 33 return perf_pmus__num_core_pmus(); 34 35 return 1; 36 } 37 38 static bool test_config(const struct evsel *evsel, __u64 expected_config) 39 { 40 __u32 type = evsel->core.attr.type; 41 __u64 config = evsel->core.attr.config; 42 43 if (type == PERF_TYPE_HARDWARE || type == PERF_TYPE_HW_CACHE) { 44 /* 45 * HARDWARE and HW_CACHE events encode the PMU's extended type 46 * in the top 32-bits. Mask in order to ignore. 47 */ 48 config &= PERF_HW_EVENT_MASK; 49 } 50 return config == expected_config; 51 } 52 53 static bool test_perf_config(const struct perf_evsel *evsel, __u64 expected_config) 54 { 55 return (evsel->attr.config & PERF_HW_EVENT_MASK) == expected_config; 56 } 57 58 #ifdef HAVE_LIBTRACEEVENT 59 60 #if defined(__s390x__) 61 /* Return true if kvm module is available and loaded. Test this 62 * and return success when trace point kvm_s390_create_vm 63 * exists. Otherwise this test always fails. 64 */ 65 static bool kvm_s390_create_vm_valid(void) 66 { 67 char *eventfile; 68 bool rc = false; 69 70 eventfile = get_events_file("kvm-s390"); 71 72 if (eventfile) { 73 DIR *mydir = opendir(eventfile); 74 75 if (mydir) { 76 rc = true; 77 closedir(mydir); 78 } 79 put_events_file(eventfile); 80 } 81 82 return rc; 83 } 84 #endif 85 86 static int test__checkevent_tracepoint(struct evlist *evlist) 87 { 88 struct evsel *evsel = evlist__first(evlist); 89 90 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries); 91 TEST_ASSERT_VAL("wrong number of groups", 0 == evlist__nr_groups(evlist)); 92 TEST_ASSERT_VAL("wrong type", PERF_TYPE_TRACEPOINT == evsel->core.attr.type); 93 TEST_ASSERT_VAL("wrong sample_type", 94 PERF_TP_SAMPLE_TYPE == evsel->core.attr.sample_type); 95 TEST_ASSERT_VAL("wrong sample_period", 1 == evsel->core.attr.sample_period); 96 return TEST_OK; 97 } 98 99 static int test__checkevent_tracepoint_multi(struct evlist *evlist) 100 { 101 struct evsel *evsel; 102 103 TEST_ASSERT_VAL("wrong number of entries", evlist->core.nr_entries > 1); 104 TEST_ASSERT_VAL("wrong number of groups", 0 == evlist__nr_groups(evlist)); 105 106 evlist__for_each_entry(evlist, evsel) { 107 TEST_ASSERT_VAL("wrong type", 108 PERF_TYPE_TRACEPOINT == evsel->core.attr.type); 109 TEST_ASSERT_VAL("wrong sample_type", 110 PERF_TP_SAMPLE_TYPE == evsel->core.attr.sample_type); 111 TEST_ASSERT_VAL("wrong sample_period", 112 1 == evsel->core.attr.sample_period); 113 } 114 return TEST_OK; 115 } 116 #endif /* HAVE_LIBTRACEEVENT */ 117 118 static int test__checkevent_raw(struct evlist *evlist) 119 { 120 struct perf_evsel *evsel; 121 bool raw_type_match = false; 122 123 TEST_ASSERT_VAL("wrong number of entries", 0 != evlist->core.nr_entries); 124 125 perf_evlist__for_each_evsel(&evlist->core, evsel) { 126 struct perf_pmu *pmu __maybe_unused = NULL; 127 bool type_matched = false; 128 129 TEST_ASSERT_VAL("wrong config", test_perf_config(evsel, 0x1a)); 130 TEST_ASSERT_VAL("event not parsed as raw type", 131 evsel->attr.type == PERF_TYPE_RAW); 132 #if defined(__aarch64__) 133 /* 134 * Arm doesn't have a real raw type PMU in sysfs, so raw events 135 * would never match any PMU. However, RAW events on Arm will 136 * always successfully open on the first available core PMU 137 * so no need to test for a matching type here. 138 */ 139 type_matched = raw_type_match = true; 140 #else 141 while ((pmu = perf_pmus__scan(pmu)) != NULL) { 142 if (pmu->type == evsel->attr.type) { 143 TEST_ASSERT_VAL("PMU type expected once", !type_matched); 144 type_matched = true; 145 if (pmu->type == PERF_TYPE_RAW) 146 raw_type_match = true; 147 } 148 } 149 #endif 150 TEST_ASSERT_VAL("No PMU found for type", type_matched); 151 } 152 TEST_ASSERT_VAL("Raw PMU not matched", raw_type_match); 153 return TEST_OK; 154 } 155 156 static int test__checkevent_numeric(struct evlist *evlist) 157 { 158 struct evsel *evsel = evlist__first(evlist); 159 160 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries); 161 TEST_ASSERT_VAL("wrong type", 1 == evsel->core.attr.type); 162 TEST_ASSERT_VAL("wrong config", test_config(evsel, 1)); 163 return TEST_OK; 164 } 165 166 167 static int assert_hw(struct perf_evsel *evsel, enum perf_hw_id id, const char *name) 168 { 169 struct perf_pmu *pmu; 170 171 if (evsel->attr.type == PERF_TYPE_HARDWARE) { 172 TEST_ASSERT_VAL("wrong config", test_perf_config(evsel, id)); 173 return 0; 174 } 175 pmu = perf_pmus__find_by_type(evsel->attr.type); 176 177 TEST_ASSERT_VAL("unexpected PMU type", pmu); 178 TEST_ASSERT_VAL("PMU missing event", perf_pmu__have_event(pmu, name)); 179 return 0; 180 } 181 182 static int test__checkevent_symbolic_name(struct evlist *evlist) 183 { 184 struct perf_evsel *evsel; 185 186 TEST_ASSERT_VAL("wrong number of entries", 0 != evlist->core.nr_entries); 187 188 perf_evlist__for_each_evsel(&evlist->core, evsel) { 189 int ret = assert_hw(evsel, PERF_COUNT_HW_INSTRUCTIONS, "instructions"); 190 191 if (ret) 192 return ret; 193 } 194 195 return TEST_OK; 196 } 197 198 static int test__checkevent_symbolic_name_config(struct evlist *evlist) 199 { 200 struct perf_evsel *evsel; 201 202 TEST_ASSERT_VAL("wrong number of entries", 0 != evlist->core.nr_entries); 203 204 perf_evlist__for_each_evsel(&evlist->core, evsel) { 205 int ret = assert_hw(evsel, PERF_COUNT_HW_CPU_CYCLES, "cycles"); 206 207 if (ret) 208 return ret; 209 /* 210 * The period value gets configured within evlist__config, 211 * while this test executes only parse events method. 212 */ 213 TEST_ASSERT_VAL("wrong period", 0 == evsel->attr.sample_period); 214 TEST_ASSERT_VAL("wrong config1", 0 == evsel->attr.config1); 215 TEST_ASSERT_VAL("wrong config2", 1 == evsel->attr.config2); 216 } 217 return TEST_OK; 218 } 219 220 static int test__checkevent_symbolic_alias(struct evlist *evlist) 221 { 222 struct evsel *evsel = evlist__first(evlist); 223 224 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries); 225 TEST_ASSERT_VAL("wrong type", PERF_TYPE_SOFTWARE == evsel->core.attr.type); 226 TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_SW_PAGE_FAULTS)); 227 return TEST_OK; 228 } 229 230 static int test__checkevent_genhw(struct evlist *evlist) 231 { 232 struct perf_evsel *evsel; 233 234 TEST_ASSERT_VAL("wrong number of entries", 0 != evlist->core.nr_entries); 235 236 perf_evlist__for_each_entry(&evlist->core, evsel) { 237 TEST_ASSERT_VAL("wrong type", PERF_TYPE_HW_CACHE == evsel->attr.type); 238 TEST_ASSERT_VAL("wrong config", test_perf_config(evsel, 1 << 16)); 239 } 240 return TEST_OK; 241 } 242 243 static int test__checkevent_breakpoint(struct evlist *evlist) 244 { 245 struct evsel *evsel = evlist__first(evlist); 246 247 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries); 248 TEST_ASSERT_VAL("wrong type", PERF_TYPE_BREAKPOINT == evsel->core.attr.type); 249 TEST_ASSERT_VAL("wrong config", test_config(evsel, 0)); 250 TEST_ASSERT_VAL("wrong bp_type", (HW_BREAKPOINT_R | HW_BREAKPOINT_W) == 251 evsel->core.attr.bp_type); 252 TEST_ASSERT_VAL("wrong bp_len", HW_BREAKPOINT_LEN_4 == 253 evsel->core.attr.bp_len); 254 return TEST_OK; 255 } 256 257 static int test__checkevent_breakpoint_x(struct evlist *evlist) 258 { 259 struct evsel *evsel = evlist__first(evlist); 260 261 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries); 262 TEST_ASSERT_VAL("wrong type", PERF_TYPE_BREAKPOINT == evsel->core.attr.type); 263 TEST_ASSERT_VAL("wrong config", test_config(evsel, 0)); 264 TEST_ASSERT_VAL("wrong bp_type", 265 HW_BREAKPOINT_X == evsel->core.attr.bp_type); 266 TEST_ASSERT_VAL("wrong bp_len", default_breakpoint_len() == evsel->core.attr.bp_len); 267 return TEST_OK; 268 } 269 270 static int test__checkevent_breakpoint_r(struct evlist *evlist) 271 { 272 struct evsel *evsel = evlist__first(evlist); 273 274 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries); 275 TEST_ASSERT_VAL("wrong type", 276 PERF_TYPE_BREAKPOINT == evsel->core.attr.type); 277 TEST_ASSERT_VAL("wrong config", test_config(evsel, 0)); 278 TEST_ASSERT_VAL("wrong bp_type", 279 HW_BREAKPOINT_R == evsel->core.attr.bp_type); 280 TEST_ASSERT_VAL("wrong bp_len", 281 HW_BREAKPOINT_LEN_4 == evsel->core.attr.bp_len); 282 return TEST_OK; 283 } 284 285 static int test__checkevent_breakpoint_w(struct evlist *evlist) 286 { 287 struct evsel *evsel = evlist__first(evlist); 288 289 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries); 290 TEST_ASSERT_VAL("wrong type", 291 PERF_TYPE_BREAKPOINT == evsel->core.attr.type); 292 TEST_ASSERT_VAL("wrong config", test_config(evsel, 0)); 293 TEST_ASSERT_VAL("wrong bp_type", 294 HW_BREAKPOINT_W == evsel->core.attr.bp_type); 295 TEST_ASSERT_VAL("wrong bp_len", 296 HW_BREAKPOINT_LEN_4 == evsel->core.attr.bp_len); 297 return TEST_OK; 298 } 299 300 static int test__checkevent_breakpoint_rw(struct evlist *evlist) 301 { 302 struct evsel *evsel = evlist__first(evlist); 303 304 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries); 305 TEST_ASSERT_VAL("wrong type", 306 PERF_TYPE_BREAKPOINT == evsel->core.attr.type); 307 TEST_ASSERT_VAL("wrong config", test_config(evsel, 0)); 308 TEST_ASSERT_VAL("wrong bp_type", 309 (HW_BREAKPOINT_R|HW_BREAKPOINT_W) == evsel->core.attr.bp_type); 310 TEST_ASSERT_VAL("wrong bp_len", 311 HW_BREAKPOINT_LEN_4 == evsel->core.attr.bp_len); 312 return TEST_OK; 313 } 314 315 #ifdef HAVE_LIBTRACEEVENT 316 static int test__checkevent_tracepoint_modifier(struct evlist *evlist) 317 { 318 struct evsel *evsel = evlist__first(evlist); 319 320 TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user); 321 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel); 322 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv); 323 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 324 325 return test__checkevent_tracepoint(evlist); 326 } 327 328 static int 329 test__checkevent_tracepoint_multi_modifier(struct evlist *evlist) 330 { 331 struct perf_evsel *evsel; 332 333 TEST_ASSERT_VAL("wrong number of entries", evlist->core.nr_entries > 1); 334 335 perf_evlist__for_each_entry(&evlist->core, evsel) { 336 TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user); 337 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel); 338 TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv); 339 TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip); 340 } 341 342 return test__checkevent_tracepoint_multi(evlist); 343 } 344 #endif /* HAVE_LIBTRACEEVENT */ 345 346 static int test__checkevent_raw_modifier(struct evlist *evlist) 347 { 348 struct perf_evsel *evsel; 349 350 perf_evlist__for_each_entry(&evlist->core, evsel) { 351 TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user); 352 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel); 353 TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv); 354 TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip); 355 } 356 return test__checkevent_raw(evlist); 357 } 358 359 static int test__checkevent_numeric_modifier(struct evlist *evlist) 360 { 361 struct perf_evsel *evsel; 362 363 perf_evlist__for_each_entry(&evlist->core, evsel) { 364 TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user); 365 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel); 366 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv); 367 TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip); 368 } 369 return test__checkevent_numeric(evlist); 370 } 371 372 static int test__checkevent_symbolic_name_modifier(struct evlist *evlist) 373 { 374 struct perf_evsel *evsel; 375 376 TEST_ASSERT_VAL("wrong number of entries", 377 evlist->core.nr_entries == num_core_entries()); 378 379 perf_evlist__for_each_entry(&evlist->core, evsel) { 380 TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user); 381 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel); 382 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv); 383 TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip); 384 } 385 return test__checkevent_symbolic_name(evlist); 386 } 387 388 static int test__checkevent_exclude_host_modifier(struct evlist *evlist) 389 { 390 struct perf_evsel *evsel; 391 392 perf_evlist__for_each_entry(&evlist->core, evsel) { 393 TEST_ASSERT_VAL("wrong exclude guest", !evsel->attr.exclude_guest); 394 TEST_ASSERT_VAL("wrong exclude host", evsel->attr.exclude_host); 395 } 396 return test__checkevent_symbolic_name(evlist); 397 } 398 399 static int test__checkevent_exclude_guest_modifier(struct evlist *evlist) 400 { 401 struct perf_evsel *evsel; 402 403 perf_evlist__for_each_entry(&evlist->core, evsel) { 404 TEST_ASSERT_VAL("wrong exclude guest", evsel->attr.exclude_guest); 405 TEST_ASSERT_VAL("wrong exclude host", !evsel->attr.exclude_host); 406 } 407 return test__checkevent_symbolic_name(evlist); 408 } 409 410 static int test__checkevent_symbolic_alias_modifier(struct evlist *evlist) 411 { 412 struct evsel *evsel = evlist__first(evlist); 413 414 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user); 415 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel); 416 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv); 417 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 418 419 return test__checkevent_symbolic_alias(evlist); 420 } 421 422 static int test__checkevent_genhw_modifier(struct evlist *evlist) 423 { 424 struct perf_evsel *evsel; 425 426 perf_evlist__for_each_entry(&evlist->core, evsel) { 427 TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user); 428 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel); 429 TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv); 430 TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip); 431 } 432 return test__checkevent_genhw(evlist); 433 } 434 435 static int test__checkevent_exclude_idle_modifier(struct evlist *evlist) 436 { 437 struct evsel *evsel = evlist__first(evlist); 438 439 TEST_ASSERT_VAL("wrong exclude idle", evsel->core.attr.exclude_idle); 440 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest); 441 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host); 442 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user); 443 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel); 444 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv); 445 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 446 447 return test__checkevent_symbolic_name(evlist); 448 } 449 450 static int test__checkevent_exclude_idle_modifier_1(struct evlist *evlist) 451 { 452 struct evsel *evsel = evlist__first(evlist); 453 454 TEST_ASSERT_VAL("wrong exclude idle", evsel->core.attr.exclude_idle); 455 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest); 456 TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host); 457 TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user); 458 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel); 459 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv); 460 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 461 462 return test__checkevent_symbolic_name(evlist); 463 } 464 465 static int test__checkevent_breakpoint_modifier(struct evlist *evlist) 466 { 467 struct evsel *evsel = evlist__first(evlist); 468 469 470 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user); 471 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel); 472 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv); 473 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 474 TEST_ASSERT_VAL("wrong name", evsel__name_is(evsel, "mem:0:u")); 475 476 return test__checkevent_breakpoint(evlist); 477 } 478 479 static int test__checkevent_breakpoint_x_modifier(struct evlist *evlist) 480 { 481 struct evsel *evsel = evlist__first(evlist); 482 483 TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user); 484 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel); 485 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv); 486 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 487 TEST_ASSERT_VAL("wrong name", evsel__name_is(evsel, "mem:0:x:k")); 488 489 return test__checkevent_breakpoint_x(evlist); 490 } 491 492 static int test__checkevent_breakpoint_r_modifier(struct evlist *evlist) 493 { 494 struct evsel *evsel = evlist__first(evlist); 495 496 TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user); 497 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel); 498 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv); 499 TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip); 500 TEST_ASSERT_VAL("wrong name", evsel__name_is(evsel, "mem:0:r:hp")); 501 502 return test__checkevent_breakpoint_r(evlist); 503 } 504 505 static int test__checkevent_breakpoint_w_modifier(struct evlist *evlist) 506 { 507 struct evsel *evsel = evlist__first(evlist); 508 509 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user); 510 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel); 511 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv); 512 TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip); 513 TEST_ASSERT_VAL("wrong name", evsel__name_is(evsel, "mem:0:w:up")); 514 515 return test__checkevent_breakpoint_w(evlist); 516 } 517 518 static int test__checkevent_breakpoint_rw_modifier(struct evlist *evlist) 519 { 520 struct evsel *evsel = evlist__first(evlist); 521 522 TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user); 523 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel); 524 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv); 525 TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip); 526 TEST_ASSERT_VAL("wrong name", evsel__name_is(evsel, "mem:0:rw:kp")); 527 528 return test__checkevent_breakpoint_rw(evlist); 529 } 530 531 static int test__checkevent_breakpoint_modifier_name(struct evlist *evlist) 532 { 533 struct evsel *evsel = evlist__first(evlist); 534 535 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user); 536 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel); 537 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv); 538 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 539 TEST_ASSERT_VAL("wrong name", evsel__name_is(evsel, "breakpoint")); 540 541 return test__checkevent_breakpoint(evlist); 542 } 543 544 static int test__checkevent_breakpoint_x_modifier_name(struct evlist *evlist) 545 { 546 struct evsel *evsel = evlist__first(evlist); 547 548 TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user); 549 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel); 550 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv); 551 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 552 TEST_ASSERT_VAL("wrong name", evsel__name_is(evsel, "breakpoint")); 553 554 return test__checkevent_breakpoint_x(evlist); 555 } 556 557 static int test__checkevent_breakpoint_r_modifier_name(struct evlist *evlist) 558 { 559 struct evsel *evsel = evlist__first(evlist); 560 561 TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user); 562 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel); 563 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv); 564 TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip); 565 TEST_ASSERT_VAL("wrong name", evsel__name_is(evsel, "breakpoint")); 566 567 return test__checkevent_breakpoint_r(evlist); 568 } 569 570 static int test__checkevent_breakpoint_w_modifier_name(struct evlist *evlist) 571 { 572 struct evsel *evsel = evlist__first(evlist); 573 574 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user); 575 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel); 576 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv); 577 TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip); 578 TEST_ASSERT_VAL("wrong name", evsel__name_is(evsel, "breakpoint")); 579 580 return test__checkevent_breakpoint_w(evlist); 581 } 582 583 static int test__checkevent_breakpoint_rw_modifier_name(struct evlist *evlist) 584 { 585 struct evsel *evsel = evlist__first(evlist); 586 587 TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user); 588 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel); 589 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv); 590 TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip); 591 TEST_ASSERT_VAL("wrong name", evsel__name_is(evsel, "breakpoint")); 592 593 return test__checkevent_breakpoint_rw(evlist); 594 } 595 596 static int test__checkevent_breakpoint_2_events(struct evlist *evlist) 597 { 598 struct evsel *evsel = evlist__first(evlist); 599 600 TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries); 601 602 TEST_ASSERT_VAL("wrong type", PERF_TYPE_BREAKPOINT == evsel->core.attr.type); 603 TEST_ASSERT_VAL("wrong name", evsel__name_is(evsel, "breakpoint1")); 604 605 evsel = evsel__next(evsel); 606 607 TEST_ASSERT_VAL("wrong type", PERF_TYPE_BREAKPOINT == evsel->core.attr.type); 608 TEST_ASSERT_VAL("wrong name", evsel__name_is(evsel, "breakpoint2")); 609 610 return TEST_OK; 611 } 612 613 static int test__checkevent_pmu(struct evlist *evlist) 614 { 615 616 struct evsel *evsel = evlist__first(evlist); 617 618 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries); 619 TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->core.attr.type); 620 TEST_ASSERT_VAL("wrong config", test_config(evsel, 10)); 621 TEST_ASSERT_VAL("wrong config1", 1 == evsel->core.attr.config1); 622 TEST_ASSERT_VAL("wrong config2", 3 == evsel->core.attr.config2); 623 TEST_ASSERT_VAL("wrong config3", 0 == evsel->core.attr.config3); 624 /* 625 * The period value gets configured within evlist__config, 626 * while this test executes only parse events method. 627 */ 628 TEST_ASSERT_VAL("wrong period", 0 == evsel->core.attr.sample_period); 629 630 return TEST_OK; 631 } 632 633 #ifdef HAVE_LIBTRACEEVENT 634 static int test__checkevent_list(struct evlist *evlist) 635 { 636 struct evsel *evsel = evlist__first(evlist); 637 638 TEST_ASSERT_VAL("wrong number of entries", 3 <= evlist->core.nr_entries); 639 640 /* r1 */ 641 TEST_ASSERT_VAL("wrong type", PERF_TYPE_TRACEPOINT != evsel->core.attr.type); 642 while (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) { 643 TEST_ASSERT_VAL("wrong config", test_config(evsel, 1)); 644 TEST_ASSERT_VAL("wrong config1", 0 == evsel->core.attr.config1); 645 TEST_ASSERT_VAL("wrong config2", 0 == evsel->core.attr.config2); 646 TEST_ASSERT_VAL("wrong config3", 0 == evsel->core.attr.config3); 647 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user); 648 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel); 649 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv); 650 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 651 evsel = evsel__next(evsel); 652 } 653 654 /* syscalls:sys_enter_openat:k */ 655 TEST_ASSERT_VAL("wrong type", PERF_TYPE_TRACEPOINT == evsel->core.attr.type); 656 TEST_ASSERT_VAL("wrong sample_type", 657 PERF_TP_SAMPLE_TYPE == evsel->core.attr.sample_type); 658 TEST_ASSERT_VAL("wrong sample_period", 1 == evsel->core.attr.sample_period); 659 TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user); 660 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel); 661 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv); 662 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 663 664 /* 1:1:hp */ 665 evsel = evsel__next(evsel); 666 TEST_ASSERT_VAL("wrong type", 1 == evsel->core.attr.type); 667 TEST_ASSERT_VAL("wrong config", test_config(evsel, 1)); 668 TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user); 669 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel); 670 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv); 671 TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip); 672 673 return TEST_OK; 674 } 675 #endif 676 677 static int test__checkevent_pmu_name(struct evlist *evlist) 678 { 679 struct evsel *evsel = evlist__first(evlist); 680 681 /* cpu/config=1,name=krava/u */ 682 TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries); 683 TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->core.attr.type); 684 TEST_ASSERT_VAL("wrong config", test_config(evsel, 1)); 685 TEST_ASSERT_VAL("wrong name", evsel__name_is(evsel, "krava")); 686 687 /* cpu/config=2/u" */ 688 evsel = evsel__next(evsel); 689 TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries); 690 TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->core.attr.type); 691 TEST_ASSERT_VAL("wrong config", test_config(evsel, 2)); 692 TEST_ASSERT_VAL("wrong name", evsel__name_is(evsel, "cpu/config=2/u")); 693 694 return TEST_OK; 695 } 696 697 static int test__checkevent_pmu_partial_time_callgraph(struct evlist *evlist) 698 { 699 struct evsel *evsel = evlist__first(evlist); 700 701 /* cpu/config=1,call-graph=fp,time,period=100000/ */ 702 TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries); 703 TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->core.attr.type); 704 TEST_ASSERT_VAL("wrong config", test_config(evsel, 1)); 705 /* 706 * The period, time and callgraph value gets configured within evlist__config, 707 * while this test executes only parse events method. 708 */ 709 TEST_ASSERT_VAL("wrong period", 0 == evsel->core.attr.sample_period); 710 TEST_ASSERT_VAL("wrong callgraph", !evsel__has_callchain(evsel)); 711 TEST_ASSERT_VAL("wrong time", !(PERF_SAMPLE_TIME & evsel->core.attr.sample_type)); 712 713 /* cpu/config=2,call-graph=no,time=0,period=2000/ */ 714 evsel = evsel__next(evsel); 715 TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->core.attr.type); 716 TEST_ASSERT_VAL("wrong config", test_config(evsel, 2)); 717 /* 718 * The period, time and callgraph value gets configured within evlist__config, 719 * while this test executes only parse events method. 720 */ 721 TEST_ASSERT_VAL("wrong period", 0 == evsel->core.attr.sample_period); 722 TEST_ASSERT_VAL("wrong callgraph", !evsel__has_callchain(evsel)); 723 TEST_ASSERT_VAL("wrong time", !(PERF_SAMPLE_TIME & evsel->core.attr.sample_type)); 724 725 return TEST_OK; 726 } 727 728 static int test__checkevent_pmu_events(struct evlist *evlist) 729 { 730 struct evsel *evsel = evlist__first(evlist); 731 732 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries); 733 TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->core.attr.type || 734 strcmp(evsel->pmu_name, "cpu")); 735 TEST_ASSERT_VAL("wrong exclude_user", 736 !evsel->core.attr.exclude_user); 737 TEST_ASSERT_VAL("wrong exclude_kernel", 738 evsel->core.attr.exclude_kernel); 739 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv); 740 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 741 TEST_ASSERT_VAL("wrong pinned", !evsel->core.attr.pinned); 742 TEST_ASSERT_VAL("wrong exclusive", !evsel->core.attr.exclusive); 743 744 return TEST_OK; 745 } 746 747 748 static int test__checkevent_pmu_events_mix(struct evlist *evlist) 749 { 750 struct evsel *evsel = NULL; 751 752 /* 753 * The wild card event will be opened at least once, but it may be 754 * opened on each core PMU. 755 */ 756 TEST_ASSERT_VAL("wrong number of entries", evlist->core.nr_entries >= 2); 757 for (int i = 0; i < evlist->core.nr_entries - 1; i++) { 758 evsel = (i == 0 ? evlist__first(evlist) : evsel__next(evsel)); 759 /* pmu-event:u */ 760 TEST_ASSERT_VAL("wrong exclude_user", 761 !evsel->core.attr.exclude_user); 762 TEST_ASSERT_VAL("wrong exclude_kernel", 763 evsel->core.attr.exclude_kernel); 764 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv); 765 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 766 TEST_ASSERT_VAL("wrong pinned", !evsel->core.attr.pinned); 767 TEST_ASSERT_VAL("wrong exclusive", !evsel->core.attr.exclusive); 768 } 769 /* cpu/pmu-event/u*/ 770 evsel = evsel__next(evsel); 771 TEST_ASSERT_VAL("wrong type", evsel__find_pmu(evsel)->is_core); 772 TEST_ASSERT_VAL("wrong exclude_user", 773 !evsel->core.attr.exclude_user); 774 TEST_ASSERT_VAL("wrong exclude_kernel", 775 evsel->core.attr.exclude_kernel); 776 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv); 777 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 778 TEST_ASSERT_VAL("wrong pinned", !evsel->core.attr.pinned); 779 TEST_ASSERT_VAL("wrong exclusive", !evsel->core.attr.pinned); 780 781 return TEST_OK; 782 } 783 784 static int test__checkterms_simple(struct parse_events_terms *terms) 785 { 786 struct parse_events_term *term; 787 788 /* config=10 */ 789 term = list_entry(terms->terms.next, struct parse_events_term, list); 790 TEST_ASSERT_VAL("wrong type term", 791 term->type_term == PARSE_EVENTS__TERM_TYPE_CONFIG); 792 TEST_ASSERT_VAL("wrong type val", 793 term->type_val == PARSE_EVENTS__TERM_TYPE_NUM); 794 TEST_ASSERT_VAL("wrong val", term->val.num == 10); 795 TEST_ASSERT_VAL("wrong config", !strcmp(term->config, "config")); 796 797 /* config1 */ 798 term = list_entry(term->list.next, struct parse_events_term, list); 799 TEST_ASSERT_VAL("wrong type term", 800 term->type_term == PARSE_EVENTS__TERM_TYPE_CONFIG1); 801 TEST_ASSERT_VAL("wrong type val", 802 term->type_val == PARSE_EVENTS__TERM_TYPE_NUM); 803 TEST_ASSERT_VAL("wrong val", term->val.num == 1); 804 TEST_ASSERT_VAL("wrong config", !strcmp(term->config, "config1")); 805 806 /* config2=3 */ 807 term = list_entry(term->list.next, struct parse_events_term, list); 808 TEST_ASSERT_VAL("wrong type term", 809 term->type_term == PARSE_EVENTS__TERM_TYPE_CONFIG2); 810 TEST_ASSERT_VAL("wrong type val", 811 term->type_val == PARSE_EVENTS__TERM_TYPE_NUM); 812 TEST_ASSERT_VAL("wrong val", term->val.num == 3); 813 TEST_ASSERT_VAL("wrong config", !strcmp(term->config, "config2")); 814 815 /* config3=4 */ 816 term = list_entry(term->list.next, struct parse_events_term, list); 817 TEST_ASSERT_VAL("wrong type term", 818 term->type_term == PARSE_EVENTS__TERM_TYPE_CONFIG3); 819 TEST_ASSERT_VAL("wrong type val", 820 term->type_val == PARSE_EVENTS__TERM_TYPE_NUM); 821 TEST_ASSERT_VAL("wrong val", term->val.num == 4); 822 TEST_ASSERT_VAL("wrong config", !strcmp(term->config, "config3")); 823 824 /* umask=1*/ 825 term = list_entry(term->list.next, struct parse_events_term, list); 826 TEST_ASSERT_VAL("wrong type term", 827 term->type_term == PARSE_EVENTS__TERM_TYPE_USER); 828 TEST_ASSERT_VAL("wrong type val", 829 term->type_val == PARSE_EVENTS__TERM_TYPE_NUM); 830 TEST_ASSERT_VAL("wrong val", term->val.num == 1); 831 TEST_ASSERT_VAL("wrong config", !strcmp(term->config, "umask")); 832 833 /* 834 * read 835 * 836 * The perf_pmu__test_parse_init injects 'read' term into 837 * perf_pmu_events_list, so 'read' is evaluated as read term 838 * and not as raw event with 'ead' hex value. 839 */ 840 term = list_entry(term->list.next, struct parse_events_term, list); 841 TEST_ASSERT_VAL("wrong type term", 842 term->type_term == PARSE_EVENTS__TERM_TYPE_RAW); 843 TEST_ASSERT_VAL("wrong type val", 844 term->type_val == PARSE_EVENTS__TERM_TYPE_STR); 845 TEST_ASSERT_VAL("wrong val", !strcmp(term->val.str, "read")); 846 TEST_ASSERT_VAL("wrong config", !strcmp(term->config, "raw")); 847 848 /* 849 * r0xead 850 * 851 * To be still able to pass 'ead' value with 'r' syntax, 852 * we added support to parse 'r0xHEX' event. 853 */ 854 term = list_entry(term->list.next, struct parse_events_term, list); 855 TEST_ASSERT_VAL("wrong type term", 856 term->type_term == PARSE_EVENTS__TERM_TYPE_RAW); 857 TEST_ASSERT_VAL("wrong type val", 858 term->type_val == PARSE_EVENTS__TERM_TYPE_STR); 859 TEST_ASSERT_VAL("wrong val", !strcmp(term->val.str, "r0xead")); 860 TEST_ASSERT_VAL("wrong config", !strcmp(term->config, "raw")); 861 return TEST_OK; 862 } 863 864 static int test__group1(struct evlist *evlist) 865 { 866 struct evsel *evsel, *leader; 867 868 TEST_ASSERT_VAL("wrong number of entries", 869 evlist->core.nr_entries == (num_core_entries() * 2)); 870 TEST_ASSERT_VAL("wrong number of groups", 871 evlist__nr_groups(evlist) == num_core_entries()); 872 873 for (int i = 0; i < num_core_entries(); i++) { 874 int ret; 875 876 /* instructions:k */ 877 evsel = leader = (i == 0 ? evlist__first(evlist) : evsel__next(evsel)); 878 ret = assert_hw(&evsel->core, PERF_COUNT_HW_INSTRUCTIONS, "instructions"); 879 if (ret) 880 return ret; 881 882 TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user); 883 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel); 884 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv); 885 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest); 886 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host); 887 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 888 TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel)); 889 TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2); 890 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 0); 891 TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read); 892 893 /* cycles:upp */ 894 evsel = evsel__next(evsel); 895 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CPU_CYCLES, "cycles"); 896 if (ret) 897 return ret; 898 899 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user); 900 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel); 901 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv); 902 /* use of precise requires exclude_guest */ 903 TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest); 904 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host); 905 TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip == 2); 906 TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader)); 907 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 1); 908 TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read); 909 } 910 return TEST_OK; 911 } 912 913 static int test__group2(struct evlist *evlist) 914 { 915 struct evsel *evsel, *leader = NULL; 916 917 TEST_ASSERT_VAL("wrong number of entries", 918 evlist->core.nr_entries == (2 * num_core_entries() + 1)); 919 /* 920 * TODO: Currently the software event won't be grouped with the hardware 921 * event except for 1 PMU. 922 */ 923 TEST_ASSERT_VAL("wrong number of groups", 1 == evlist__nr_groups(evlist)); 924 925 evlist__for_each_entry(evlist, evsel) { 926 int ret; 927 928 if (evsel->core.attr.type == PERF_TYPE_SOFTWARE) { 929 /* faults + :ku modifier */ 930 leader = evsel; 931 TEST_ASSERT_VAL("wrong config", 932 test_config(evsel, PERF_COUNT_SW_PAGE_FAULTS)); 933 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user); 934 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel); 935 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv); 936 TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest); 937 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host); 938 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 939 TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel)); 940 TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2); 941 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 0); 942 TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read); 943 continue; 944 } 945 if (evsel->core.attr.type == PERF_TYPE_HARDWARE && 946 test_config(evsel, PERF_COUNT_HW_BRANCH_INSTRUCTIONS)) { 947 /* branches + :u modifier */ 948 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user); 949 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel); 950 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv); 951 TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest); 952 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host); 953 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 954 if (evsel__has_leader(evsel, leader)) 955 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 1); 956 TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read); 957 continue; 958 } 959 /* cycles:k */ 960 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CPU_CYCLES, "cycles"); 961 if (ret) 962 return ret; 963 964 TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user); 965 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel); 966 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv); 967 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest); 968 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host); 969 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 970 TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel)); 971 TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read); 972 } 973 return TEST_OK; 974 } 975 976 #ifdef HAVE_LIBTRACEEVENT 977 static int test__group3(struct evlist *evlist __maybe_unused) 978 { 979 struct evsel *evsel, *group1_leader = NULL, *group2_leader = NULL; 980 int ret; 981 982 TEST_ASSERT_VAL("wrong number of entries", 983 evlist->core.nr_entries == (3 * perf_pmus__num_core_pmus() + 2)); 984 /* 985 * Currently the software event won't be grouped with the hardware event 986 * except for 1 PMU. This means there are always just 2 groups 987 * regardless of the number of core PMUs. 988 */ 989 TEST_ASSERT_VAL("wrong number of groups", 2 == evlist__nr_groups(evlist)); 990 991 evlist__for_each_entry(evlist, evsel) { 992 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) { 993 /* group1 syscalls:sys_enter_openat:H */ 994 group1_leader = evsel; 995 TEST_ASSERT_VAL("wrong sample_type", 996 evsel->core.attr.sample_type == PERF_TP_SAMPLE_TYPE); 997 TEST_ASSERT_VAL("wrong sample_period", 1 == evsel->core.attr.sample_period); 998 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user); 999 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel); 1000 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv); 1001 TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest); 1002 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host); 1003 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 1004 TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel)); 1005 TEST_ASSERT_VAL("wrong group name", !strcmp(evsel->group_name, "group1")); 1006 TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2); 1007 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 0); 1008 TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read); 1009 continue; 1010 } 1011 if (evsel->core.attr.type == PERF_TYPE_HARDWARE && 1012 test_config(evsel, PERF_COUNT_HW_CPU_CYCLES)) { 1013 if (evsel->core.attr.exclude_user) { 1014 /* group1 cycles:kppp */ 1015 TEST_ASSERT_VAL("wrong exclude_user", 1016 evsel->core.attr.exclude_user); 1017 TEST_ASSERT_VAL("wrong exclude_kernel", 1018 !evsel->core.attr.exclude_kernel); 1019 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv); 1020 /* use of precise requires exclude_guest */ 1021 TEST_ASSERT_VAL("wrong exclude guest", 1022 evsel->core.attr.exclude_guest); 1023 TEST_ASSERT_VAL("wrong exclude host", 1024 !evsel->core.attr.exclude_host); 1025 TEST_ASSERT_VAL("wrong precise_ip", 1026 evsel->core.attr.precise_ip == 3); 1027 if (evsel__has_leader(evsel, group1_leader)) { 1028 TEST_ASSERT_VAL("wrong group name", !evsel->group_name); 1029 TEST_ASSERT_VAL("wrong group_idx", 1030 evsel__group_idx(evsel) == 1); 1031 } 1032 TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read); 1033 } else { 1034 /* group2 cycles + G modifier */ 1035 group2_leader = evsel; 1036 TEST_ASSERT_VAL("wrong exclude_kernel", 1037 !evsel->core.attr.exclude_kernel); 1038 TEST_ASSERT_VAL("wrong exclude_hv", 1039 !evsel->core.attr.exclude_hv); 1040 TEST_ASSERT_VAL("wrong exclude guest", 1041 !evsel->core.attr.exclude_guest); 1042 TEST_ASSERT_VAL("wrong exclude host", 1043 evsel->core.attr.exclude_host); 1044 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 1045 TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel)); 1046 if (evsel->core.nr_members == 2) { 1047 TEST_ASSERT_VAL("wrong group_idx", 1048 evsel__group_idx(evsel) == 0); 1049 } 1050 TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read); 1051 } 1052 continue; 1053 } 1054 if (evsel->core.attr.type == 1) { 1055 /* group2 1:3 + G modifier */ 1056 TEST_ASSERT_VAL("wrong config", test_config(evsel, 3)); 1057 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user); 1058 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel); 1059 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv); 1060 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest); 1061 TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host); 1062 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 1063 if (evsel__has_leader(evsel, group2_leader)) 1064 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 1); 1065 TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read); 1066 continue; 1067 } 1068 /* instructions:u */ 1069 ret = assert_hw(&evsel->core, PERF_COUNT_HW_INSTRUCTIONS, "instructions"); 1070 if (ret) 1071 return ret; 1072 1073 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user); 1074 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel); 1075 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv); 1076 TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest); 1077 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host); 1078 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 1079 TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel)); 1080 TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read); 1081 } 1082 return TEST_OK; 1083 } 1084 #endif 1085 1086 static int test__group4(struct evlist *evlist __maybe_unused) 1087 { 1088 struct evsel *evsel, *leader; 1089 1090 TEST_ASSERT_VAL("wrong number of entries", 1091 evlist->core.nr_entries == (num_core_entries() * 2)); 1092 TEST_ASSERT_VAL("wrong number of groups", 1093 num_core_entries() == evlist__nr_groups(evlist)); 1094 1095 for (int i = 0; i < num_core_entries(); i++) { 1096 int ret; 1097 1098 /* cycles:u + p */ 1099 evsel = leader = (i == 0 ? evlist__first(evlist) : evsel__next(evsel)); 1100 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CPU_CYCLES, "cycles"); 1101 if (ret) 1102 return ret; 1103 1104 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user); 1105 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel); 1106 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv); 1107 /* use of precise requires exclude_guest */ 1108 TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest); 1109 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host); 1110 TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip == 1); 1111 TEST_ASSERT_VAL("wrong group name", !evsel->group_name); 1112 TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel)); 1113 TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2); 1114 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 0); 1115 TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read); 1116 1117 /* instructions:kp + p */ 1118 evsel = evsel__next(evsel); 1119 ret = assert_hw(&evsel->core, PERF_COUNT_HW_INSTRUCTIONS, "instructions"); 1120 if (ret) 1121 return ret; 1122 1123 TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user); 1124 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel); 1125 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv); 1126 /* use of precise requires exclude_guest */ 1127 TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest); 1128 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host); 1129 TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip == 2); 1130 TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader)); 1131 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 1); 1132 TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read); 1133 } 1134 return TEST_OK; 1135 } 1136 1137 static int test__group5(struct evlist *evlist __maybe_unused) 1138 { 1139 struct evsel *evsel = NULL, *leader; 1140 int ret; 1141 1142 TEST_ASSERT_VAL("wrong number of entries", 1143 evlist->core.nr_entries == (5 * num_core_entries())); 1144 TEST_ASSERT_VAL("wrong number of groups", 1145 evlist__nr_groups(evlist) == (2 * num_core_entries())); 1146 1147 for (int i = 0; i < num_core_entries(); i++) { 1148 /* cycles + G */ 1149 evsel = leader = (i == 0 ? evlist__first(evlist) : evsel__next(evsel)); 1150 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CPU_CYCLES, "cycles"); 1151 if (ret) 1152 return ret; 1153 1154 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user); 1155 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel); 1156 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv); 1157 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest); 1158 TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host); 1159 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 1160 TEST_ASSERT_VAL("wrong group name", !evsel->group_name); 1161 TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel)); 1162 TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2); 1163 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 0); 1164 TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read); 1165 1166 /* instructions + G */ 1167 evsel = evsel__next(evsel); 1168 ret = assert_hw(&evsel->core, PERF_COUNT_HW_INSTRUCTIONS, "instructions"); 1169 if (ret) 1170 return ret; 1171 1172 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user); 1173 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel); 1174 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv); 1175 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest); 1176 TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host); 1177 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 1178 TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader)); 1179 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 1); 1180 TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read); 1181 } 1182 for (int i = 0; i < num_core_entries(); i++) { 1183 /* cycles:G */ 1184 evsel = leader = evsel__next(evsel); 1185 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CPU_CYCLES, "cycles"); 1186 if (ret) 1187 return ret; 1188 1189 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user); 1190 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel); 1191 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv); 1192 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest); 1193 TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host); 1194 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 1195 TEST_ASSERT_VAL("wrong group name", !evsel->group_name); 1196 TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel)); 1197 TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2); 1198 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 0); 1199 TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read); 1200 1201 /* instructions:G */ 1202 evsel = evsel__next(evsel); 1203 ret = assert_hw(&evsel->core, PERF_COUNT_HW_INSTRUCTIONS, "instructions"); 1204 if (ret) 1205 return ret; 1206 1207 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user); 1208 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel); 1209 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv); 1210 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest); 1211 TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host); 1212 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 1213 TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader)); 1214 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 1); 1215 } 1216 for (int i = 0; i < num_core_entries(); i++) { 1217 /* cycles */ 1218 evsel = evsel__next(evsel); 1219 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CPU_CYCLES, "cycles"); 1220 if (ret) 1221 return ret; 1222 1223 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user); 1224 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel); 1225 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv); 1226 TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest); 1227 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host); 1228 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 1229 TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel)); 1230 } 1231 return TEST_OK; 1232 } 1233 1234 static int test__group_gh1(struct evlist *evlist) 1235 { 1236 struct evsel *evsel = NULL, *leader; 1237 1238 TEST_ASSERT_VAL("wrong number of entries", 1239 evlist->core.nr_entries == (2 * num_core_entries())); 1240 TEST_ASSERT_VAL("wrong number of groups", 1241 evlist__nr_groups(evlist) == num_core_entries()); 1242 1243 for (int i = 0; i < num_core_entries(); i++) { 1244 int ret; 1245 1246 /* cycles + :H group modifier */ 1247 evsel = leader = (i == 0 ? evlist__first(evlist) : evsel__next(evsel)); 1248 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CPU_CYCLES, "cycles"); 1249 if (ret) 1250 return ret; 1251 1252 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user); 1253 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel); 1254 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv); 1255 TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest); 1256 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host); 1257 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 1258 TEST_ASSERT_VAL("wrong group name", !evsel->group_name); 1259 TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel)); 1260 TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2); 1261 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 0); 1262 1263 /* cache-misses:G + :H group modifier */ 1264 evsel = evsel__next(evsel); 1265 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CACHE_MISSES, "cache-misses"); 1266 if (ret) 1267 return ret; 1268 1269 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user); 1270 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel); 1271 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv); 1272 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest); 1273 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host); 1274 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 1275 TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader)); 1276 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 1); 1277 } 1278 return TEST_OK; 1279 } 1280 1281 static int test__group_gh2(struct evlist *evlist) 1282 { 1283 struct evsel *evsel = NULL, *leader; 1284 1285 TEST_ASSERT_VAL("wrong number of entries", 1286 evlist->core.nr_entries == (2 * num_core_entries())); 1287 TEST_ASSERT_VAL("wrong number of groups", 1288 evlist__nr_groups(evlist) == num_core_entries()); 1289 1290 for (int i = 0; i < num_core_entries(); i++) { 1291 int ret; 1292 1293 /* cycles + :G group modifier */ 1294 evsel = leader = (i == 0 ? evlist__first(evlist) : evsel__next(evsel)); 1295 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CPU_CYCLES, "cycles"); 1296 if (ret) 1297 return ret; 1298 1299 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user); 1300 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel); 1301 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv); 1302 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest); 1303 TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host); 1304 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 1305 TEST_ASSERT_VAL("wrong group name", !evsel->group_name); 1306 TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel)); 1307 TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2); 1308 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 0); 1309 1310 /* cache-misses:H + :G group modifier */ 1311 evsel = evsel__next(evsel); 1312 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CACHE_MISSES, "cache-misses"); 1313 if (ret) 1314 return ret; 1315 1316 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user); 1317 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel); 1318 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv); 1319 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest); 1320 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host); 1321 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 1322 TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader)); 1323 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 1); 1324 } 1325 return TEST_OK; 1326 } 1327 1328 static int test__group_gh3(struct evlist *evlist) 1329 { 1330 struct evsel *evsel = NULL, *leader; 1331 1332 TEST_ASSERT_VAL("wrong number of entries", 1333 evlist->core.nr_entries == (2 * num_core_entries())); 1334 TEST_ASSERT_VAL("wrong number of groups", 1335 evlist__nr_groups(evlist) == num_core_entries()); 1336 1337 for (int i = 0; i < num_core_entries(); i++) { 1338 int ret; 1339 1340 /* cycles:G + :u group modifier */ 1341 evsel = leader = (i == 0 ? evlist__first(evlist) : evsel__next(evsel)); 1342 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CPU_CYCLES, "cycles"); 1343 if (ret) 1344 return ret; 1345 1346 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user); 1347 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel); 1348 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv); 1349 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest); 1350 TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host); 1351 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 1352 TEST_ASSERT_VAL("wrong group name", !evsel->group_name); 1353 TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel)); 1354 TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2); 1355 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 0); 1356 1357 /* cache-misses:H + :u group modifier */ 1358 evsel = evsel__next(evsel); 1359 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CACHE_MISSES, "cache-misses"); 1360 if (ret) 1361 return ret; 1362 1363 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user); 1364 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel); 1365 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv); 1366 TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest); 1367 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host); 1368 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 1369 TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader)); 1370 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 1); 1371 } 1372 return TEST_OK; 1373 } 1374 1375 static int test__group_gh4(struct evlist *evlist) 1376 { 1377 struct evsel *evsel = NULL, *leader; 1378 1379 TEST_ASSERT_VAL("wrong number of entries", 1380 evlist->core.nr_entries == (2 * num_core_entries())); 1381 TEST_ASSERT_VAL("wrong number of groups", 1382 evlist__nr_groups(evlist) == num_core_entries()); 1383 1384 for (int i = 0; i < num_core_entries(); i++) { 1385 int ret; 1386 1387 /* cycles:G + :uG group modifier */ 1388 evsel = leader = (i == 0 ? evlist__first(evlist) : evsel__next(evsel)); 1389 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CPU_CYCLES, "cycles"); 1390 if (ret) 1391 return ret; 1392 1393 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user); 1394 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel); 1395 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv); 1396 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest); 1397 TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host); 1398 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 1399 TEST_ASSERT_VAL("wrong group name", !evsel->group_name); 1400 TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel)); 1401 TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2); 1402 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 0); 1403 1404 /* cache-misses:H + :uG group modifier */ 1405 evsel = evsel__next(evsel); 1406 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CACHE_MISSES, "cache-misses"); 1407 if (ret) 1408 return ret; 1409 1410 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user); 1411 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel); 1412 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv); 1413 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest); 1414 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host); 1415 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 1416 TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader)); 1417 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 1); 1418 } 1419 return TEST_OK; 1420 } 1421 1422 static int test__leader_sample1(struct evlist *evlist) 1423 { 1424 struct evsel *evsel = NULL, *leader; 1425 1426 TEST_ASSERT_VAL("wrong number of entries", 1427 evlist->core.nr_entries == (3 * num_core_entries())); 1428 1429 for (int i = 0; i < num_core_entries(); i++) { 1430 int ret; 1431 1432 /* cycles - sampling group leader */ 1433 evsel = leader = (i == 0 ? evlist__first(evlist) : evsel__next(evsel)); 1434 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CPU_CYCLES, "cycles"); 1435 if (ret) 1436 return ret; 1437 1438 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user); 1439 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel); 1440 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv); 1441 TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest); 1442 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host); 1443 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 1444 TEST_ASSERT_VAL("wrong group name", !evsel->group_name); 1445 TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader)); 1446 TEST_ASSERT_VAL("wrong sample_read", evsel->sample_read); 1447 1448 /* cache-misses - not sampling */ 1449 evsel = evsel__next(evsel); 1450 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CACHE_MISSES, "cache-misses"); 1451 if (ret) 1452 return ret; 1453 1454 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user); 1455 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel); 1456 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv); 1457 TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest); 1458 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host); 1459 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 1460 TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader)); 1461 TEST_ASSERT_VAL("wrong sample_read", evsel->sample_read); 1462 1463 /* branch-misses - not sampling */ 1464 evsel = evsel__next(evsel); 1465 ret = assert_hw(&evsel->core, PERF_COUNT_HW_BRANCH_MISSES, "branch-misses"); 1466 if (ret) 1467 return ret; 1468 1469 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user); 1470 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel); 1471 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv); 1472 TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest); 1473 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host); 1474 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 1475 TEST_ASSERT_VAL("wrong group name", !evsel->group_name); 1476 TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader)); 1477 TEST_ASSERT_VAL("wrong sample_read", evsel->sample_read); 1478 } 1479 return TEST_OK; 1480 } 1481 1482 static int test__leader_sample2(struct evlist *evlist __maybe_unused) 1483 { 1484 struct evsel *evsel = NULL, *leader; 1485 1486 TEST_ASSERT_VAL("wrong number of entries", 1487 evlist->core.nr_entries == (2 * num_core_entries())); 1488 1489 for (int i = 0; i < num_core_entries(); i++) { 1490 int ret; 1491 1492 /* instructions - sampling group leader */ 1493 evsel = leader = (i == 0 ? evlist__first(evlist) : evsel__next(evsel)); 1494 ret = assert_hw(&evsel->core, PERF_COUNT_HW_INSTRUCTIONS, "instructions"); 1495 if (ret) 1496 return ret; 1497 1498 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user); 1499 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel); 1500 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv); 1501 TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest); 1502 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host); 1503 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 1504 TEST_ASSERT_VAL("wrong group name", !evsel->group_name); 1505 TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader)); 1506 TEST_ASSERT_VAL("wrong sample_read", evsel->sample_read); 1507 1508 /* branch-misses - not sampling */ 1509 evsel = evsel__next(evsel); 1510 ret = assert_hw(&evsel->core, PERF_COUNT_HW_BRANCH_MISSES, "branch-misses"); 1511 if (ret) 1512 return ret; 1513 1514 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user); 1515 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel); 1516 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv); 1517 TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest); 1518 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host); 1519 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 1520 TEST_ASSERT_VAL("wrong group name", !evsel->group_name); 1521 TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader)); 1522 TEST_ASSERT_VAL("wrong sample_read", evsel->sample_read); 1523 } 1524 return TEST_OK; 1525 } 1526 1527 static int test__checkevent_pinned_modifier(struct evlist *evlist) 1528 { 1529 struct evsel *evsel = NULL; 1530 1531 TEST_ASSERT_VAL("wrong number of entries", 1532 evlist->core.nr_entries == num_core_entries()); 1533 1534 for (int i = 0; i < num_core_entries(); i++) { 1535 evsel = (i == 0 ? evlist__first(evlist) : evsel__next(evsel)); 1536 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user); 1537 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel); 1538 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv); 1539 TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip); 1540 TEST_ASSERT_VAL("wrong pinned", evsel->core.attr.pinned); 1541 } 1542 return test__checkevent_symbolic_name(evlist); 1543 } 1544 1545 static int test__pinned_group(struct evlist *evlist) 1546 { 1547 struct evsel *evsel = NULL, *leader; 1548 1549 TEST_ASSERT_VAL("wrong number of entries", 1550 evlist->core.nr_entries == (3 * num_core_entries())); 1551 1552 for (int i = 0; i < num_core_entries(); i++) { 1553 int ret; 1554 1555 /* cycles - group leader */ 1556 evsel = leader = (i == 0 ? evlist__first(evlist) : evsel__next(evsel)); 1557 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CPU_CYCLES, "cycles"); 1558 if (ret) 1559 return ret; 1560 1561 TEST_ASSERT_VAL("wrong group name", !evsel->group_name); 1562 TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader)); 1563 /* TODO: The group modifier is not copied to the split group leader. */ 1564 if (perf_pmus__num_core_pmus() == 1) 1565 TEST_ASSERT_VAL("wrong pinned", evsel->core.attr.pinned); 1566 1567 /* cache-misses - can not be pinned, but will go on with the leader */ 1568 evsel = evsel__next(evsel); 1569 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CACHE_MISSES, "cache-misses"); 1570 if (ret) 1571 return ret; 1572 1573 TEST_ASSERT_VAL("wrong pinned", !evsel->core.attr.pinned); 1574 1575 /* branch-misses - ditto */ 1576 evsel = evsel__next(evsel); 1577 ret = assert_hw(&evsel->core, PERF_COUNT_HW_BRANCH_MISSES, "branch-misses"); 1578 if (ret) 1579 return ret; 1580 1581 TEST_ASSERT_VAL("wrong pinned", !evsel->core.attr.pinned); 1582 } 1583 return TEST_OK; 1584 } 1585 1586 static int test__checkevent_exclusive_modifier(struct evlist *evlist) 1587 { 1588 struct evsel *evsel = evlist__first(evlist); 1589 1590 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user); 1591 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel); 1592 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv); 1593 TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip); 1594 TEST_ASSERT_VAL("wrong exclusive", evsel->core.attr.exclusive); 1595 1596 return test__checkevent_symbolic_name(evlist); 1597 } 1598 1599 static int test__exclusive_group(struct evlist *evlist) 1600 { 1601 struct evsel *evsel = NULL, *leader; 1602 1603 TEST_ASSERT_VAL("wrong number of entries", 1604 evlist->core.nr_entries == 3 * num_core_entries()); 1605 1606 for (int i = 0; i < num_core_entries(); i++) { 1607 int ret; 1608 1609 /* cycles - group leader */ 1610 evsel = leader = (i == 0 ? evlist__first(evlist) : evsel__next(evsel)); 1611 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CPU_CYCLES, "cycles"); 1612 if (ret) 1613 return ret; 1614 1615 TEST_ASSERT_VAL("wrong group name", !evsel->group_name); 1616 TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader)); 1617 /* TODO: The group modifier is not copied to the split group leader. */ 1618 if (perf_pmus__num_core_pmus() == 1) 1619 TEST_ASSERT_VAL("wrong exclusive", evsel->core.attr.exclusive); 1620 1621 /* cache-misses - can not be pinned, but will go on with the leader */ 1622 evsel = evsel__next(evsel); 1623 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CACHE_MISSES, "cache-misses"); 1624 if (ret) 1625 return ret; 1626 1627 TEST_ASSERT_VAL("wrong exclusive", !evsel->core.attr.exclusive); 1628 1629 /* branch-misses - ditto */ 1630 evsel = evsel__next(evsel); 1631 ret = assert_hw(&evsel->core, PERF_COUNT_HW_BRANCH_MISSES, "branch-misses"); 1632 if (ret) 1633 return ret; 1634 1635 TEST_ASSERT_VAL("wrong exclusive", !evsel->core.attr.exclusive); 1636 } 1637 return TEST_OK; 1638 } 1639 static int test__checkevent_breakpoint_len(struct evlist *evlist) 1640 { 1641 struct evsel *evsel = evlist__first(evlist); 1642 1643 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries); 1644 TEST_ASSERT_VAL("wrong type", PERF_TYPE_BREAKPOINT == evsel->core.attr.type); 1645 TEST_ASSERT_VAL("wrong config", test_config(evsel, 0)); 1646 TEST_ASSERT_VAL("wrong bp_type", (HW_BREAKPOINT_R | HW_BREAKPOINT_W) == 1647 evsel->core.attr.bp_type); 1648 TEST_ASSERT_VAL("wrong bp_len", HW_BREAKPOINT_LEN_1 == 1649 evsel->core.attr.bp_len); 1650 1651 return TEST_OK; 1652 } 1653 1654 static int test__checkevent_breakpoint_len_w(struct evlist *evlist) 1655 { 1656 struct evsel *evsel = evlist__first(evlist); 1657 1658 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries); 1659 TEST_ASSERT_VAL("wrong type", PERF_TYPE_BREAKPOINT == evsel->core.attr.type); 1660 TEST_ASSERT_VAL("wrong config", test_config(evsel, 0)); 1661 TEST_ASSERT_VAL("wrong bp_type", HW_BREAKPOINT_W == 1662 evsel->core.attr.bp_type); 1663 TEST_ASSERT_VAL("wrong bp_len", HW_BREAKPOINT_LEN_2 == 1664 evsel->core.attr.bp_len); 1665 1666 return TEST_OK; 1667 } 1668 1669 static int 1670 test__checkevent_breakpoint_len_rw_modifier(struct evlist *evlist) 1671 { 1672 struct evsel *evsel = evlist__first(evlist); 1673 1674 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user); 1675 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel); 1676 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv); 1677 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip); 1678 1679 return test__checkevent_breakpoint_rw(evlist); 1680 } 1681 1682 static int test__checkevent_precise_max_modifier(struct evlist *evlist) 1683 { 1684 struct evsel *evsel = evlist__first(evlist); 1685 1686 TEST_ASSERT_VAL("wrong number of entries", 1687 evlist->core.nr_entries == 1 + num_core_entries()); 1688 TEST_ASSERT_VAL("wrong type", PERF_TYPE_SOFTWARE == evsel->core.attr.type); 1689 TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_SW_TASK_CLOCK)); 1690 return TEST_OK; 1691 } 1692 1693 static int test__checkevent_config_symbol(struct evlist *evlist) 1694 { 1695 struct evsel *evsel = evlist__first(evlist); 1696 1697 TEST_ASSERT_VAL("wrong name setting", evsel__name_is(evsel, "insn")); 1698 return TEST_OK; 1699 } 1700 1701 static int test__checkevent_config_raw(struct evlist *evlist) 1702 { 1703 struct evsel *evsel = evlist__first(evlist); 1704 1705 TEST_ASSERT_VAL("wrong name setting", evsel__name_is(evsel, "rawpmu")); 1706 return TEST_OK; 1707 } 1708 1709 static int test__checkevent_config_num(struct evlist *evlist) 1710 { 1711 struct evsel *evsel = evlist__first(evlist); 1712 1713 TEST_ASSERT_VAL("wrong name setting", evsel__name_is(evsel, "numpmu")); 1714 return TEST_OK; 1715 } 1716 1717 static int test__checkevent_config_cache(struct evlist *evlist) 1718 { 1719 struct evsel *evsel = evlist__first(evlist); 1720 1721 TEST_ASSERT_VAL("wrong name setting", evsel__name_is(evsel, "cachepmu")); 1722 return test__checkevent_genhw(evlist); 1723 } 1724 1725 static bool test__pmu_cpu_valid(void) 1726 { 1727 return !!perf_pmus__find("cpu"); 1728 } 1729 1730 static bool test__pmu_cpu_event_valid(void) 1731 { 1732 struct perf_pmu *pmu = perf_pmus__find("cpu"); 1733 1734 if (!pmu) 1735 return false; 1736 1737 return perf_pmu__has_format(pmu, "event"); 1738 } 1739 1740 static bool test__intel_pt_valid(void) 1741 { 1742 return !!perf_pmus__find("intel_pt"); 1743 } 1744 1745 static int test__intel_pt(struct evlist *evlist) 1746 { 1747 struct evsel *evsel = evlist__first(evlist); 1748 1749 TEST_ASSERT_VAL("wrong name setting", evsel__name_is(evsel, "intel_pt//u")); 1750 return TEST_OK; 1751 } 1752 1753 static int test__checkevent_complex_name(struct evlist *evlist) 1754 { 1755 struct evsel *evsel = evlist__first(evlist); 1756 1757 TEST_ASSERT_VAL("wrong complex name parsing", 1758 evsel__name_is(evsel, 1759 "COMPLEX_CYCLES_NAME:orig=cycles,desc=chip-clock-ticks")); 1760 return TEST_OK; 1761 } 1762 1763 static int test__checkevent_raw_pmu(struct evlist *evlist) 1764 { 1765 struct evsel *evsel = evlist__first(evlist); 1766 1767 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries); 1768 TEST_ASSERT_VAL("wrong type", PERF_TYPE_SOFTWARE == evsel->core.attr.type); 1769 TEST_ASSERT_VAL("wrong config", test_config(evsel, 0x1a)); 1770 return TEST_OK; 1771 } 1772 1773 static int test__sym_event_slash(struct evlist *evlist) 1774 { 1775 struct evsel *evsel = evlist__first(evlist); 1776 int ret = assert_hw(&evsel->core, PERF_COUNT_HW_CPU_CYCLES, "cycles"); 1777 1778 if (ret) 1779 return ret; 1780 1781 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel); 1782 return TEST_OK; 1783 } 1784 1785 static int test__sym_event_dc(struct evlist *evlist) 1786 { 1787 struct evsel *evsel = evlist__first(evlist); 1788 int ret = assert_hw(&evsel->core, PERF_COUNT_HW_CPU_CYCLES, "cycles"); 1789 1790 if (ret) 1791 return ret; 1792 1793 TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user); 1794 return TEST_OK; 1795 } 1796 1797 static int test__term_equal_term(struct evlist *evlist) 1798 { 1799 struct evsel *evsel = evlist__first(evlist); 1800 int ret = assert_hw(&evsel->core, PERF_COUNT_HW_CPU_CYCLES, "cycles"); 1801 1802 if (ret) 1803 return ret; 1804 1805 TEST_ASSERT_VAL("wrong name setting", strcmp(evsel->name, "name") == 0); 1806 return TEST_OK; 1807 } 1808 1809 static int test__term_equal_legacy(struct evlist *evlist) 1810 { 1811 struct evsel *evsel = evlist__first(evlist); 1812 int ret = assert_hw(&evsel->core, PERF_COUNT_HW_CPU_CYCLES, "cycles"); 1813 1814 if (ret) 1815 return ret; 1816 1817 TEST_ASSERT_VAL("wrong name setting", strcmp(evsel->name, "l1d") == 0); 1818 return TEST_OK; 1819 } 1820 1821 #ifdef HAVE_LIBTRACEEVENT 1822 static int count_tracepoints(void) 1823 { 1824 struct dirent *events_ent; 1825 DIR *events_dir; 1826 int cnt = 0; 1827 1828 events_dir = tracing_events__opendir(); 1829 1830 TEST_ASSERT_VAL("Can't open events dir", events_dir); 1831 1832 while ((events_ent = readdir(events_dir))) { 1833 char *sys_path; 1834 struct dirent *sys_ent; 1835 DIR *sys_dir; 1836 1837 if (!strcmp(events_ent->d_name, ".") 1838 || !strcmp(events_ent->d_name, "..") 1839 || !strcmp(events_ent->d_name, "enable") 1840 || !strcmp(events_ent->d_name, "header_event") 1841 || !strcmp(events_ent->d_name, "header_page")) 1842 continue; 1843 1844 sys_path = get_events_file(events_ent->d_name); 1845 TEST_ASSERT_VAL("Can't get sys path", sys_path); 1846 1847 sys_dir = opendir(sys_path); 1848 TEST_ASSERT_VAL("Can't open sys dir", sys_dir); 1849 1850 while ((sys_ent = readdir(sys_dir))) { 1851 if (!strcmp(sys_ent->d_name, ".") 1852 || !strcmp(sys_ent->d_name, "..") 1853 || !strcmp(sys_ent->d_name, "enable") 1854 || !strcmp(sys_ent->d_name, "filter")) 1855 continue; 1856 1857 cnt++; 1858 } 1859 1860 closedir(sys_dir); 1861 put_events_file(sys_path); 1862 } 1863 1864 closedir(events_dir); 1865 return cnt; 1866 } 1867 1868 static int test__all_tracepoints(struct evlist *evlist) 1869 { 1870 TEST_ASSERT_VAL("wrong events count", 1871 count_tracepoints() == evlist->core.nr_entries); 1872 1873 return test__checkevent_tracepoint_multi(evlist); 1874 } 1875 #endif /* HAVE_LIBTRACEVENT */ 1876 1877 struct evlist_test { 1878 const char *name; 1879 bool (*valid)(void); 1880 int (*check)(struct evlist *evlist); 1881 }; 1882 1883 static const struct evlist_test test__events[] = { 1884 #ifdef HAVE_LIBTRACEEVENT 1885 { 1886 .name = "syscalls:sys_enter_openat", 1887 .check = test__checkevent_tracepoint, 1888 /* 0 */ 1889 }, 1890 { 1891 .name = "syscalls:*", 1892 .check = test__checkevent_tracepoint_multi, 1893 /* 1 */ 1894 }, 1895 #endif 1896 { 1897 .name = "r1a", 1898 .check = test__checkevent_raw, 1899 /* 2 */ 1900 }, 1901 { 1902 .name = "1:1", 1903 .check = test__checkevent_numeric, 1904 /* 3 */ 1905 }, 1906 { 1907 .name = "instructions", 1908 .check = test__checkevent_symbolic_name, 1909 /* 4 */ 1910 }, 1911 { 1912 .name = "cycles/period=100000,config2/", 1913 .check = test__checkevent_symbolic_name_config, 1914 /* 5 */ 1915 }, 1916 { 1917 .name = "faults", 1918 .check = test__checkevent_symbolic_alias, 1919 /* 6 */ 1920 }, 1921 { 1922 .name = "L1-dcache-load-miss", 1923 .check = test__checkevent_genhw, 1924 /* 7 */ 1925 }, 1926 { 1927 .name = "mem:0", 1928 .check = test__checkevent_breakpoint, 1929 /* 8 */ 1930 }, 1931 { 1932 .name = "mem:0:x", 1933 .check = test__checkevent_breakpoint_x, 1934 /* 9 */ 1935 }, 1936 { 1937 .name = "mem:0:r", 1938 .check = test__checkevent_breakpoint_r, 1939 /* 0 */ 1940 }, 1941 { 1942 .name = "mem:0:w", 1943 .check = test__checkevent_breakpoint_w, 1944 /* 1 */ 1945 }, 1946 #ifdef HAVE_LIBTRACEEVENT 1947 { 1948 .name = "syscalls:sys_enter_openat:k", 1949 .check = test__checkevent_tracepoint_modifier, 1950 /* 2 */ 1951 }, 1952 { 1953 .name = "syscalls:*:u", 1954 .check = test__checkevent_tracepoint_multi_modifier, 1955 /* 3 */ 1956 }, 1957 #endif 1958 { 1959 .name = "r1a:kp", 1960 .check = test__checkevent_raw_modifier, 1961 /* 4 */ 1962 }, 1963 { 1964 .name = "1:1:hp", 1965 .check = test__checkevent_numeric_modifier, 1966 /* 5 */ 1967 }, 1968 { 1969 .name = "instructions:h", 1970 .check = test__checkevent_symbolic_name_modifier, 1971 /* 6 */ 1972 }, 1973 { 1974 .name = "faults:u", 1975 .check = test__checkevent_symbolic_alias_modifier, 1976 /* 7 */ 1977 }, 1978 { 1979 .name = "L1-dcache-load-miss:kp", 1980 .check = test__checkevent_genhw_modifier, 1981 /* 8 */ 1982 }, 1983 { 1984 .name = "mem:0:u", 1985 .check = test__checkevent_breakpoint_modifier, 1986 /* 9 */ 1987 }, 1988 { 1989 .name = "mem:0:x:k", 1990 .check = test__checkevent_breakpoint_x_modifier, 1991 /* 0 */ 1992 }, 1993 { 1994 .name = "mem:0:r:hp", 1995 .check = test__checkevent_breakpoint_r_modifier, 1996 /* 1 */ 1997 }, 1998 { 1999 .name = "mem:0:w:up", 2000 .check = test__checkevent_breakpoint_w_modifier, 2001 /* 2 */ 2002 }, 2003 #ifdef HAVE_LIBTRACEEVENT 2004 { 2005 .name = "r1,syscalls:sys_enter_openat:k,1:1:hp", 2006 .check = test__checkevent_list, 2007 /* 3 */ 2008 }, 2009 #endif 2010 { 2011 .name = "instructions:G", 2012 .check = test__checkevent_exclude_host_modifier, 2013 /* 4 */ 2014 }, 2015 { 2016 .name = "instructions:H", 2017 .check = test__checkevent_exclude_guest_modifier, 2018 /* 5 */ 2019 }, 2020 { 2021 .name = "mem:0:rw", 2022 .check = test__checkevent_breakpoint_rw, 2023 /* 6 */ 2024 }, 2025 { 2026 .name = "mem:0:rw:kp", 2027 .check = test__checkevent_breakpoint_rw_modifier, 2028 /* 7 */ 2029 }, 2030 { 2031 .name = "{instructions:k,cycles:upp}", 2032 .check = test__group1, 2033 /* 8 */ 2034 }, 2035 { 2036 .name = "{faults:k,branches}:u,cycles:k", 2037 .check = test__group2, 2038 /* 9 */ 2039 }, 2040 #ifdef HAVE_LIBTRACEEVENT 2041 { 2042 .name = "group1{syscalls:sys_enter_openat:H,cycles:kppp},group2{cycles,1:3}:G,instructions:u", 2043 .check = test__group3, 2044 /* 0 */ 2045 }, 2046 #endif 2047 { 2048 .name = "{cycles:u,instructions:kp}:p", 2049 .check = test__group4, 2050 /* 1 */ 2051 }, 2052 { 2053 .name = "{cycles,instructions}:G,{cycles:G,instructions:G},cycles", 2054 .check = test__group5, 2055 /* 2 */ 2056 }, 2057 #ifdef HAVE_LIBTRACEEVENT 2058 { 2059 .name = "*:*", 2060 .check = test__all_tracepoints, 2061 /* 3 */ 2062 }, 2063 #endif 2064 { 2065 .name = "{cycles,cache-misses:G}:H", 2066 .check = test__group_gh1, 2067 /* 4 */ 2068 }, 2069 { 2070 .name = "{cycles,cache-misses:H}:G", 2071 .check = test__group_gh2, 2072 /* 5 */ 2073 }, 2074 { 2075 .name = "{cycles:G,cache-misses:H}:u", 2076 .check = test__group_gh3, 2077 /* 6 */ 2078 }, 2079 { 2080 .name = "{cycles:G,cache-misses:H}:uG", 2081 .check = test__group_gh4, 2082 /* 7 */ 2083 }, 2084 { 2085 .name = "{cycles,cache-misses,branch-misses}:S", 2086 .check = test__leader_sample1, 2087 /* 8 */ 2088 }, 2089 { 2090 .name = "{instructions,branch-misses}:Su", 2091 .check = test__leader_sample2, 2092 /* 9 */ 2093 }, 2094 { 2095 .name = "instructions:uDp", 2096 .check = test__checkevent_pinned_modifier, 2097 /* 0 */ 2098 }, 2099 { 2100 .name = "{cycles,cache-misses,branch-misses}:D", 2101 .check = test__pinned_group, 2102 /* 1 */ 2103 }, 2104 { 2105 .name = "mem:0/1", 2106 .check = test__checkevent_breakpoint_len, 2107 /* 2 */ 2108 }, 2109 { 2110 .name = "mem:0/2:w", 2111 .check = test__checkevent_breakpoint_len_w, 2112 /* 3 */ 2113 }, 2114 { 2115 .name = "mem:0/4:rw:u", 2116 .check = test__checkevent_breakpoint_len_rw_modifier, 2117 /* 4 */ 2118 }, 2119 #if defined(__s390x__) && defined(HAVE_LIBTRACEEVENT) 2120 { 2121 .name = "kvm-s390:kvm_s390_create_vm", 2122 .check = test__checkevent_tracepoint, 2123 .valid = kvm_s390_create_vm_valid, 2124 /* 0 */ 2125 }, 2126 #endif 2127 { 2128 .name = "instructions:I", 2129 .check = test__checkevent_exclude_idle_modifier, 2130 /* 5 */ 2131 }, 2132 { 2133 .name = "instructions:kIG", 2134 .check = test__checkevent_exclude_idle_modifier_1, 2135 /* 6 */ 2136 }, 2137 { 2138 .name = "task-clock:P,cycles", 2139 .check = test__checkevent_precise_max_modifier, 2140 /* 7 */ 2141 }, 2142 { 2143 .name = "instructions/name=insn/", 2144 .check = test__checkevent_config_symbol, 2145 /* 8 */ 2146 }, 2147 { 2148 .name = "r1234/name=rawpmu/", 2149 .check = test__checkevent_config_raw, 2150 /* 9 */ 2151 }, 2152 { 2153 .name = "4:0x6530160/name=numpmu/", 2154 .check = test__checkevent_config_num, 2155 /* 0 */ 2156 }, 2157 { 2158 .name = "L1-dcache-misses/name=cachepmu/", 2159 .check = test__checkevent_config_cache, 2160 /* 1 */ 2161 }, 2162 { 2163 .name = "intel_pt//u", 2164 .valid = test__intel_pt_valid, 2165 .check = test__intel_pt, 2166 /* 2 */ 2167 }, 2168 { 2169 .name = "cycles/name='COMPLEX_CYCLES_NAME:orig=cycles,desc=chip-clock-ticks'/Duk", 2170 .check = test__checkevent_complex_name, 2171 /* 3 */ 2172 }, 2173 { 2174 .name = "cycles//u", 2175 .check = test__sym_event_slash, 2176 /* 4 */ 2177 }, 2178 { 2179 .name = "cycles:k", 2180 .check = test__sym_event_dc, 2181 /* 5 */ 2182 }, 2183 { 2184 .name = "instructions:uep", 2185 .check = test__checkevent_exclusive_modifier, 2186 /* 6 */ 2187 }, 2188 { 2189 .name = "{cycles,cache-misses,branch-misses}:e", 2190 .check = test__exclusive_group, 2191 /* 7 */ 2192 }, 2193 { 2194 .name = "cycles/name=name/", 2195 .check = test__term_equal_term, 2196 /* 8 */ 2197 }, 2198 { 2199 .name = "cycles/name=l1d/", 2200 .check = test__term_equal_legacy, 2201 /* 9 */ 2202 }, 2203 { 2204 .name = "mem:0/name=breakpoint/", 2205 .check = test__checkevent_breakpoint, 2206 /* 0 */ 2207 }, 2208 { 2209 .name = "mem:0:x/name=breakpoint/", 2210 .check = test__checkevent_breakpoint_x, 2211 /* 1 */ 2212 }, 2213 { 2214 .name = "mem:0:r/name=breakpoint/", 2215 .check = test__checkevent_breakpoint_r, 2216 /* 2 */ 2217 }, 2218 { 2219 .name = "mem:0:w/name=breakpoint/", 2220 .check = test__checkevent_breakpoint_w, 2221 /* 3 */ 2222 }, 2223 { 2224 .name = "mem:0/name=breakpoint/u", 2225 .check = test__checkevent_breakpoint_modifier_name, 2226 /* 4 */ 2227 }, 2228 { 2229 .name = "mem:0:x/name=breakpoint/k", 2230 .check = test__checkevent_breakpoint_x_modifier_name, 2231 /* 5 */ 2232 }, 2233 { 2234 .name = "mem:0:r/name=breakpoint/hp", 2235 .check = test__checkevent_breakpoint_r_modifier_name, 2236 /* 6 */ 2237 }, 2238 { 2239 .name = "mem:0:w/name=breakpoint/up", 2240 .check = test__checkevent_breakpoint_w_modifier_name, 2241 /* 7 */ 2242 }, 2243 { 2244 .name = "mem:0:rw/name=breakpoint/", 2245 .check = test__checkevent_breakpoint_rw, 2246 /* 8 */ 2247 }, 2248 { 2249 .name = "mem:0:rw/name=breakpoint/kp", 2250 .check = test__checkevent_breakpoint_rw_modifier_name, 2251 /* 9 */ 2252 }, 2253 { 2254 .name = "mem:0/1/name=breakpoint/", 2255 .check = test__checkevent_breakpoint_len, 2256 /* 0 */ 2257 }, 2258 { 2259 .name = "mem:0/2:w/name=breakpoint/", 2260 .check = test__checkevent_breakpoint_len_w, 2261 /* 1 */ 2262 }, 2263 { 2264 .name = "mem:0/4:rw/name=breakpoint/u", 2265 .check = test__checkevent_breakpoint_len_rw_modifier, 2266 /* 2 */ 2267 }, 2268 { 2269 .name = "mem:0/1/name=breakpoint1/,mem:0/4:rw/name=breakpoint2/", 2270 .check = test__checkevent_breakpoint_2_events, 2271 /* 3 */ 2272 }, 2273 #ifdef HAVE_LIBTRACEEVENT 2274 { 2275 .name = "9p:9p_client_req", 2276 .check = test__checkevent_tracepoint, 2277 /* 4 */ 2278 }, 2279 #endif 2280 }; 2281 2282 static const struct evlist_test test__events_pmu[] = { 2283 { 2284 .name = "cpu/config=10,config1=1,config2=3,period=1000/u", 2285 .valid = test__pmu_cpu_valid, 2286 .check = test__checkevent_pmu, 2287 /* 0 */ 2288 }, 2289 { 2290 .name = "cpu/config=1,name=krava/u,cpu/config=2/u", 2291 .valid = test__pmu_cpu_valid, 2292 .check = test__checkevent_pmu_name, 2293 /* 1 */ 2294 }, 2295 { 2296 .name = "cpu/config=1,call-graph=fp,time,period=100000/,cpu/config=2,call-graph=no,time=0,period=2000/", 2297 .valid = test__pmu_cpu_valid, 2298 .check = test__checkevent_pmu_partial_time_callgraph, 2299 /* 2 */ 2300 }, 2301 { 2302 .name = "cpu/name='COMPLEX_CYCLES_NAME:orig=cycles,desc=chip-clock-ticks',period=0x1,event=0x2/ukp", 2303 .valid = test__pmu_cpu_event_valid, 2304 .check = test__checkevent_complex_name, 2305 /* 3 */ 2306 }, 2307 { 2308 .name = "software/r1a/", 2309 .check = test__checkevent_raw_pmu, 2310 /* 4 */ 2311 }, 2312 { 2313 .name = "software/r0x1a/", 2314 .check = test__checkevent_raw_pmu, 2315 /* 5 */ 2316 }, 2317 { 2318 .name = "cpu/L1-dcache-load-miss/", 2319 .valid = test__pmu_cpu_valid, 2320 .check = test__checkevent_genhw, 2321 /* 6 */ 2322 }, 2323 { 2324 .name = "cpu/L1-dcache-load-miss/kp", 2325 .valid = test__pmu_cpu_valid, 2326 .check = test__checkevent_genhw_modifier, 2327 /* 7 */ 2328 }, 2329 { 2330 .name = "cpu/L1-dcache-misses,name=cachepmu/", 2331 .valid = test__pmu_cpu_valid, 2332 .check = test__checkevent_config_cache, 2333 /* 8 */ 2334 }, 2335 { 2336 .name = "cpu/instructions/", 2337 .valid = test__pmu_cpu_valid, 2338 .check = test__checkevent_symbolic_name, 2339 /* 9 */ 2340 }, 2341 { 2342 .name = "cpu/cycles,period=100000,config2/", 2343 .valid = test__pmu_cpu_valid, 2344 .check = test__checkevent_symbolic_name_config, 2345 /* 0 */ 2346 }, 2347 { 2348 .name = "cpu/instructions/h", 2349 .valid = test__pmu_cpu_valid, 2350 .check = test__checkevent_symbolic_name_modifier, 2351 /* 1 */ 2352 }, 2353 { 2354 .name = "cpu/instructions/G", 2355 .valid = test__pmu_cpu_valid, 2356 .check = test__checkevent_exclude_host_modifier, 2357 /* 2 */ 2358 }, 2359 { 2360 .name = "cpu/instructions/H", 2361 .valid = test__pmu_cpu_valid, 2362 .check = test__checkevent_exclude_guest_modifier, 2363 /* 3 */ 2364 }, 2365 { 2366 .name = "{cpu/instructions/k,cpu/cycles/upp}", 2367 .valid = test__pmu_cpu_valid, 2368 .check = test__group1, 2369 /* 4 */ 2370 }, 2371 { 2372 .name = "{cpu/cycles/u,cpu/instructions/kp}:p", 2373 .valid = test__pmu_cpu_valid, 2374 .check = test__group4, 2375 /* 5 */ 2376 }, 2377 { 2378 .name = "{cpu/cycles/,cpu/cache-misses/G}:H", 2379 .valid = test__pmu_cpu_valid, 2380 .check = test__group_gh1, 2381 /* 6 */ 2382 }, 2383 { 2384 .name = "{cpu/cycles/,cpu/cache-misses/H}:G", 2385 .valid = test__pmu_cpu_valid, 2386 .check = test__group_gh2, 2387 /* 7 */ 2388 }, 2389 { 2390 .name = "{cpu/cycles/G,cpu/cache-misses/H}:u", 2391 .valid = test__pmu_cpu_valid, 2392 .check = test__group_gh3, 2393 /* 8 */ 2394 }, 2395 { 2396 .name = "{cpu/cycles/G,cpu/cache-misses/H}:uG", 2397 .valid = test__pmu_cpu_valid, 2398 .check = test__group_gh4, 2399 /* 9 */ 2400 }, 2401 { 2402 .name = "{cpu/cycles/,cpu/cache-misses/,cpu/branch-misses/}:S", 2403 .valid = test__pmu_cpu_valid, 2404 .check = test__leader_sample1, 2405 /* 0 */ 2406 }, 2407 { 2408 .name = "{cpu/instructions/,cpu/branch-misses/}:Su", 2409 .valid = test__pmu_cpu_valid, 2410 .check = test__leader_sample2, 2411 /* 1 */ 2412 }, 2413 { 2414 .name = "cpu/instructions/uDp", 2415 .valid = test__pmu_cpu_valid, 2416 .check = test__checkevent_pinned_modifier, 2417 /* 2 */ 2418 }, 2419 { 2420 .name = "{cpu/cycles/,cpu/cache-misses/,cpu/branch-misses/}:D", 2421 .valid = test__pmu_cpu_valid, 2422 .check = test__pinned_group, 2423 /* 3 */ 2424 }, 2425 { 2426 .name = "cpu/instructions/I", 2427 .valid = test__pmu_cpu_valid, 2428 .check = test__checkevent_exclude_idle_modifier, 2429 /* 4 */ 2430 }, 2431 { 2432 .name = "cpu/instructions/kIG", 2433 .valid = test__pmu_cpu_valid, 2434 .check = test__checkevent_exclude_idle_modifier_1, 2435 /* 5 */ 2436 }, 2437 { 2438 .name = "cpu/cycles/u", 2439 .valid = test__pmu_cpu_valid, 2440 .check = test__sym_event_slash, 2441 /* 6 */ 2442 }, 2443 { 2444 .name = "cpu/cycles/k", 2445 .valid = test__pmu_cpu_valid, 2446 .check = test__sym_event_dc, 2447 /* 7 */ 2448 }, 2449 { 2450 .name = "cpu/instructions/uep", 2451 .valid = test__pmu_cpu_valid, 2452 .check = test__checkevent_exclusive_modifier, 2453 /* 8 */ 2454 }, 2455 { 2456 .name = "{cpu/cycles/,cpu/cache-misses/,cpu/branch-misses/}:e", 2457 .valid = test__pmu_cpu_valid, 2458 .check = test__exclusive_group, 2459 /* 9 */ 2460 }, 2461 { 2462 .name = "cpu/cycles,name=name/", 2463 .valid = test__pmu_cpu_valid, 2464 .check = test__term_equal_term, 2465 /* 0 */ 2466 }, 2467 { 2468 .name = "cpu/cycles,name=l1d/", 2469 .valid = test__pmu_cpu_valid, 2470 .check = test__term_equal_legacy, 2471 /* 1 */ 2472 }, 2473 }; 2474 2475 struct terms_test { 2476 const char *str; 2477 int (*check)(struct parse_events_terms *terms); 2478 }; 2479 2480 static const struct terms_test test__terms[] = { 2481 [0] = { 2482 .str = "config=10,config1,config2=3,config3=4,umask=1,read,r0xead", 2483 .check = test__checkterms_simple, 2484 }, 2485 }; 2486 2487 static int test_event(const struct evlist_test *e) 2488 { 2489 struct parse_events_error err; 2490 struct evlist *evlist; 2491 int ret; 2492 2493 if (e->valid && !e->valid()) { 2494 pr_debug("... SKIP\n"); 2495 return TEST_OK; 2496 } 2497 2498 evlist = evlist__new(); 2499 if (evlist == NULL) { 2500 pr_err("Failed allocation"); 2501 return TEST_FAIL; 2502 } 2503 parse_events_error__init(&err); 2504 ret = __parse_events(evlist, e->name, /*pmu_filter=*/NULL, &err, /*fake_pmu=*/NULL, 2505 /*warn_if_reordered=*/true, /*fake_tp=*/true); 2506 if (ret) { 2507 pr_debug("failed to parse event '%s', err %d\n", e->name, ret); 2508 parse_events_error__print(&err, e->name); 2509 ret = TEST_FAIL; 2510 if (parse_events_error__contains(&err, "can't access trace events")) 2511 ret = TEST_SKIP; 2512 } else { 2513 ret = e->check(evlist); 2514 } 2515 parse_events_error__exit(&err); 2516 evlist__delete(evlist); 2517 2518 return ret; 2519 } 2520 2521 static int test_event_fake_pmu(const char *str) 2522 { 2523 struct parse_events_error err; 2524 struct evlist *evlist; 2525 int ret; 2526 2527 evlist = evlist__new(); 2528 if (!evlist) 2529 return -ENOMEM; 2530 2531 parse_events_error__init(&err); 2532 ret = __parse_events(evlist, str, /*pmu_filter=*/NULL, &err, 2533 &perf_pmu__fake, /*warn_if_reordered=*/true, 2534 /*fake_tp=*/true); 2535 if (ret) { 2536 pr_debug("failed to parse event '%s', err %d\n", 2537 str, ret); 2538 parse_events_error__print(&err, str); 2539 } 2540 2541 parse_events_error__exit(&err); 2542 evlist__delete(evlist); 2543 2544 return ret; 2545 } 2546 2547 static int combine_test_results(int existing, int latest) 2548 { 2549 if (existing == TEST_FAIL) 2550 return TEST_FAIL; 2551 if (existing == TEST_SKIP) 2552 return latest == TEST_OK ? TEST_SKIP : latest; 2553 return latest; 2554 } 2555 2556 static int test_events(const struct evlist_test *events, int cnt) 2557 { 2558 int ret = TEST_OK; 2559 2560 for (int i = 0; i < cnt; i++) { 2561 const struct evlist_test *e = &events[i]; 2562 int test_ret; 2563 2564 pr_debug("running test %d '%s'\n", i, e->name); 2565 test_ret = test_event(e); 2566 if (test_ret != TEST_OK) { 2567 pr_debug("Event test failure: test %d '%s'", i, e->name); 2568 ret = combine_test_results(ret, test_ret); 2569 } 2570 } 2571 2572 return ret; 2573 } 2574 2575 static int test__events2(struct test_suite *test __maybe_unused, int subtest __maybe_unused) 2576 { 2577 return test_events(test__events, ARRAY_SIZE(test__events)); 2578 } 2579 2580 static int test_term(const struct terms_test *t) 2581 { 2582 struct parse_events_terms terms; 2583 int ret; 2584 2585 2586 parse_events_terms__init(&terms); 2587 ret = parse_events_terms(&terms, t->str, /*input=*/ NULL); 2588 if (ret) { 2589 pr_debug("failed to parse terms '%s', err %d\n", 2590 t->str , ret); 2591 return ret; 2592 } 2593 2594 ret = t->check(&terms); 2595 parse_events_terms__exit(&terms); 2596 2597 return ret; 2598 } 2599 2600 static int test_terms(const struct terms_test *terms, int cnt) 2601 { 2602 int ret = 0; 2603 2604 for (int i = 0; i < cnt; i++) { 2605 const struct terms_test *t = &terms[i]; 2606 2607 pr_debug("running test %d '%s'\n", i, t->str); 2608 ret = test_term(t); 2609 if (ret) 2610 break; 2611 } 2612 2613 return ret; 2614 } 2615 2616 static int test__terms2(struct test_suite *test __maybe_unused, int subtest __maybe_unused) 2617 { 2618 return test_terms(test__terms, ARRAY_SIZE(test__terms)); 2619 } 2620 2621 static int test__pmu_events(struct test_suite *test __maybe_unused, int subtest __maybe_unused) 2622 { 2623 struct perf_pmu *pmu = NULL; 2624 int ret = TEST_OK; 2625 2626 while ((pmu = perf_pmus__scan(pmu)) != NULL) { 2627 struct stat st; 2628 char path[PATH_MAX]; 2629 char pmu_event[PATH_MAX]; 2630 char *buf = NULL; 2631 FILE *file; 2632 struct dirent *ent; 2633 size_t len = 0; 2634 DIR *dir; 2635 int err; 2636 int n; 2637 2638 snprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/events/", 2639 sysfs__mountpoint(), pmu->name); 2640 2641 err = stat(path, &st); 2642 if (err) { 2643 pr_debug("skipping PMU %s events tests: %s\n", pmu->name, path); 2644 continue; 2645 } 2646 2647 dir = opendir(path); 2648 if (!dir) { 2649 pr_debug("can't open pmu event dir: %s\n", path); 2650 ret = combine_test_results(ret, TEST_SKIP); 2651 continue; 2652 } 2653 2654 while ((ent = readdir(dir))) { 2655 struct evlist_test e = { .name = NULL, }; 2656 char name[2 * NAME_MAX + 1 + 12 + 3]; 2657 int test_ret; 2658 bool is_event_parameterized = 0; 2659 2660 /* Names containing . are special and cannot be used directly */ 2661 if (strchr(ent->d_name, '.')) 2662 continue; 2663 2664 /* exclude parameterized ones (name contains '?') */ 2665 n = snprintf(pmu_event, sizeof(pmu_event), "%s%s", path, ent->d_name); 2666 if (n >= PATH_MAX) { 2667 pr_err("pmu event name crossed PATH_MAX(%d) size\n", PATH_MAX); 2668 continue; 2669 } 2670 2671 file = fopen(pmu_event, "r"); 2672 if (!file) { 2673 pr_debug("can't open pmu event file for '%s'\n", ent->d_name); 2674 ret = combine_test_results(ret, TEST_FAIL); 2675 continue; 2676 } 2677 2678 if (getline(&buf, &len, file) < 0) { 2679 pr_debug(" pmu event: %s is a null event\n", ent->d_name); 2680 ret = combine_test_results(ret, TEST_FAIL); 2681 fclose(file); 2682 continue; 2683 } 2684 2685 if (strchr(buf, '?')) 2686 is_event_parameterized = 1; 2687 2688 free(buf); 2689 buf = NULL; 2690 fclose(file); 2691 2692 if (is_event_parameterized == 1) { 2693 pr_debug("skipping parameterized PMU event: %s which contains ?\n", pmu_event); 2694 continue; 2695 } 2696 2697 snprintf(name, sizeof(name), "%s/event=%s/u", pmu->name, ent->d_name); 2698 2699 e.name = name; 2700 e.check = test__checkevent_pmu_events; 2701 2702 test_ret = test_event(&e); 2703 if (test_ret != TEST_OK) { 2704 pr_debug("Test PMU event failed for '%s'", name); 2705 ret = combine_test_results(ret, test_ret); 2706 } 2707 2708 if (!is_pmu_core(pmu->name)) 2709 continue; 2710 2711 /* 2712 * Names containing '-' are recognized as prefixes and suffixes 2713 * due to '-' being a legacy PMU separator. This fails when the 2714 * prefix or suffix collides with an existing legacy token. For 2715 * example, branch-brs has a prefix (branch) that collides with 2716 * a PE_NAME_CACHE_TYPE token causing a parse error as a suffix 2717 * isn't expected after this. As event names in the config 2718 * slashes are allowed a '-' in the name we check this works 2719 * above. 2720 */ 2721 if (strchr(ent->d_name, '-')) 2722 continue; 2723 2724 snprintf(name, sizeof(name), "%s:u,%s/event=%s/u", 2725 ent->d_name, pmu->name, ent->d_name); 2726 e.name = name; 2727 e.check = test__checkevent_pmu_events_mix; 2728 test_ret = test_event(&e); 2729 if (test_ret != TEST_OK) { 2730 pr_debug("Test PMU event failed for '%s'", name); 2731 ret = combine_test_results(ret, test_ret); 2732 } 2733 } 2734 2735 closedir(dir); 2736 } 2737 return ret; 2738 } 2739 2740 static int test__pmu_events2(struct test_suite *test __maybe_unused, int subtest __maybe_unused) 2741 { 2742 return test_events(test__events_pmu, ARRAY_SIZE(test__events_pmu)); 2743 } 2744 2745 static bool test_alias(char **event, char **alias) 2746 { 2747 char path[PATH_MAX]; 2748 DIR *dir; 2749 struct dirent *dent; 2750 const char *sysfs = sysfs__mountpoint(); 2751 char buf[128]; 2752 FILE *file; 2753 2754 if (!sysfs) 2755 return false; 2756 2757 snprintf(path, PATH_MAX, "%s/bus/event_source/devices/", sysfs); 2758 dir = opendir(path); 2759 if (!dir) 2760 return false; 2761 2762 while ((dent = readdir(dir))) { 2763 if (!strcmp(dent->d_name, ".") || 2764 !strcmp(dent->d_name, "..")) 2765 continue; 2766 2767 snprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/alias", 2768 sysfs, dent->d_name); 2769 2770 if (!file_available(path)) 2771 continue; 2772 2773 file = fopen(path, "r"); 2774 if (!file) 2775 continue; 2776 2777 if (!fgets(buf, sizeof(buf), file)) { 2778 fclose(file); 2779 continue; 2780 } 2781 2782 /* Remove the last '\n' */ 2783 buf[strlen(buf) - 1] = 0; 2784 2785 fclose(file); 2786 *event = strdup(dent->d_name); 2787 *alias = strdup(buf); 2788 closedir(dir); 2789 2790 if (*event == NULL || *alias == NULL) { 2791 free(*event); 2792 free(*alias); 2793 return false; 2794 } 2795 2796 return true; 2797 } 2798 2799 closedir(dir); 2800 return false; 2801 } 2802 2803 static int test__checkevent_pmu_events_alias(struct evlist *evlist) 2804 { 2805 struct evsel *evsel1 = evlist__first(evlist); 2806 struct evsel *evsel2 = evlist__last(evlist); 2807 2808 TEST_ASSERT_VAL("wrong type", evsel1->core.attr.type == evsel2->core.attr.type); 2809 TEST_ASSERT_VAL("wrong config", evsel1->core.attr.config == evsel2->core.attr.config); 2810 return TEST_OK; 2811 } 2812 2813 static int test__pmu_events_alias(char *event, char *alias) 2814 { 2815 struct evlist_test e = { .name = NULL, }; 2816 char name[2 * NAME_MAX + 20]; 2817 2818 snprintf(name, sizeof(name), "%s/event=1/,%s/event=1/", 2819 event, alias); 2820 2821 e.name = name; 2822 e.check = test__checkevent_pmu_events_alias; 2823 return test_event(&e); 2824 } 2825 2826 static int test__alias(struct test_suite *test __maybe_unused, int subtest __maybe_unused) 2827 { 2828 char *event, *alias; 2829 int ret; 2830 2831 if (!test_alias(&event, &alias)) 2832 return TEST_SKIP; 2833 2834 ret = test__pmu_events_alias(event, alias); 2835 2836 free(event); 2837 free(alias); 2838 return ret; 2839 } 2840 2841 static int test__pmu_events_alias2(struct test_suite *test __maybe_unused, 2842 int subtest __maybe_unused) 2843 { 2844 static const char events[][30] = { 2845 "event-hyphen", 2846 "event-two-hyph", 2847 }; 2848 int ret = TEST_OK; 2849 2850 for (unsigned int i = 0; i < ARRAY_SIZE(events); i++) { 2851 int test_ret = test_event_fake_pmu(&events[i][0]); 2852 2853 if (test_ret != TEST_OK) { 2854 pr_debug("check_parse_fake %s failed\n", &events[i][0]); 2855 ret = combine_test_results(ret, test_ret); 2856 } 2857 } 2858 2859 return ret; 2860 } 2861 2862 static struct test_case tests__parse_events[] = { 2863 TEST_CASE_REASON("Test event parsing", 2864 events2, 2865 "permissions"), 2866 TEST_CASE_REASON("Parsing of all PMU events from sysfs", 2867 pmu_events, 2868 "permissions"), 2869 TEST_CASE_REASON("Parsing of given PMU events from sysfs", 2870 pmu_events2, 2871 "permissions"), 2872 TEST_CASE_REASON("Parsing of aliased events from sysfs", alias, 2873 "no aliases in sysfs"), 2874 TEST_CASE("Parsing of aliased events", pmu_events_alias2), 2875 TEST_CASE("Parsing of terms (event modifiers)", terms2), 2876 { .name = NULL, } 2877 }; 2878 2879 struct test_suite suite__parse_events = { 2880 .desc = "Parse event definition strings", 2881 .test_cases = tests__parse_events, 2882 }; 2883