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

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