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. if (evlist)
  37. perf_evlist__delete(evlist);
  38. return err;
  39. }
  40. struct test_attr_event {
  41. struct attr_event attr;
  42. u64 id;
  43. };
  44. /**
  45. * test__parse_no_sample_id_all - test parsing with no sample_id_all bit set.
  46. *
  47. * This function tests parsing data produced on kernel's that do not support the
  48. * sample_id_all bit. Without the sample_id_all bit, non-sample events (such as
  49. * mmap events) do not have an id sample appended, and consequently logic
  50. * designed to determine the id will not work. That case happens when there is
  51. * more than one selected event, so this test processes three events: 2
  52. * attributes representing the selected events and one mmap event.
  53. *
  54. * Return: %0 on success, %-1 if the test fails.
  55. */
  56. int test__parse_no_sample_id_all(void)
  57. {
  58. int err;
  59. struct test_attr_event event1 = {
  60. .attr = {
  61. .header = {
  62. .type = PERF_RECORD_HEADER_ATTR,
  63. .size = sizeof(struct test_attr_event),
  64. },
  65. },
  66. .id = 1,
  67. };
  68. struct test_attr_event event2 = {
  69. .attr = {
  70. .header = {
  71. .type = PERF_RECORD_HEADER_ATTR,
  72. .size = sizeof(struct test_attr_event),
  73. },
  74. },
  75. .id = 2,
  76. };
  77. struct mmap_event event3 = {
  78. .header = {
  79. .type = PERF_RECORD_MMAP,
  80. .size = sizeof(struct mmap_event),
  81. },
  82. };
  83. union perf_event *events[] = {
  84. (union perf_event *)&event1,
  85. (union perf_event *)&event2,
  86. (union perf_event *)&event3,
  87. };
  88. err = process_events(events, ARRAY_SIZE(events));
  89. if (err)
  90. return -1;
  91. return 0;
  92. }