parse-no-sample-id-all.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/kernel.h>
  3. #include <linux/types.h>
  4. #include <stddef.h>
  5. #include "tests.h"
  6. #include "event.h"
  7. #include "evlist.h"
  8. #include "header.h"
  9. #include "util.h"
  10. #include "debug.h"
  11. static int process_event(struct perf_evlist **pevlist, union perf_event *event)
  12. {
  13. struct perf_sample sample;
  14. if (event->header.type == PERF_RECORD_HEADER_ATTR) {
  15. if (perf_event__process_attr(NULL, event, pevlist)) {
  16. pr_debug("perf_event__process_attr failed\n");
  17. return -1;
  18. }
  19. return 0;
  20. }
  21. if (event->header.type >= PERF_RECORD_USER_TYPE_START)
  22. return -1;
  23. if (!*pevlist)
  24. return -1;
  25. if (perf_evlist__parse_sample(*pevlist, event, &sample)) {
  26. pr_debug("perf_evlist__parse_sample failed\n");
  27. return -1;
  28. }
  29. return 0;
  30. }
  31. static int process_events(union perf_event **events, size_t count)
  32. {
  33. struct perf_evlist *evlist = NULL;
  34. int err = 0;
  35. size_t i;
  36. for (i = 0; i < count && !err; i++)
  37. err = process_event(&evlist, events[i]);
  38. perf_evlist__delete(evlist);
  39. return err;
  40. }
  41. struct test_attr_event {
  42. struct perf_event_header header;
  43. struct perf_event_attr attr;
  44. u64 id;
  45. };
  46. /**
  47. * test__parse_no_sample_id_all - test parsing with no sample_id_all bit set.
  48. *
  49. * This function tests parsing data produced on kernel's that do not support the
  50. * sample_id_all bit. Without the sample_id_all bit, non-sample events (such as
  51. * mmap events) do not have an id sample appended, and consequently logic
  52. * designed to determine the id will not work. That case happens when there is
  53. * more than one selected event, so this test processes three events: 2
  54. * attributes representing the selected events and one mmap event.
  55. *
  56. * Return: %0 on success, %-1 if the test fails.
  57. */
  58. int test__parse_no_sample_id_all(struct test *test __maybe_unused, int subtest __maybe_unused)
  59. {
  60. int err;
  61. struct test_attr_event event1 = {
  62. .header = {
  63. .type = PERF_RECORD_HEADER_ATTR,
  64. .size = sizeof(struct test_attr_event),
  65. },
  66. .id = 1,
  67. };
  68. struct test_attr_event event2 = {
  69. .header = {
  70. .type = PERF_RECORD_HEADER_ATTR,
  71. .size = sizeof(struct test_attr_event),
  72. },
  73. .id = 2,
  74. };
  75. struct mmap_event event3 = {
  76. .header = {
  77. .type = PERF_RECORD_MMAP,
  78. .size = sizeof(struct mmap_event),
  79. },
  80. };
  81. union perf_event *events[] = {
  82. (union perf_event *)&event1,
  83. (union perf_event *)&event2,
  84. (union perf_event *)&event3,
  85. };
  86. err = process_events(events, ARRAY_SIZE(events));
  87. if (err)
  88. return -1;
  89. return 0;
  90. }