event_update.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/compiler.h>
  3. #include <perf/cpumap.h>
  4. #include <string.h>
  5. #include "cpumap.h"
  6. #include "evlist.h"
  7. #include "evsel.h"
  8. #include "header.h"
  9. #include "machine.h"
  10. #include "util/synthetic-events.h"
  11. #include "tool.h"
  12. #include "tests.h"
  13. #include "debug.h"
  14. static int process_event_unit(const struct perf_tool *tool __maybe_unused,
  15. union perf_event *event,
  16. struct perf_sample *sample __maybe_unused,
  17. struct machine *machine __maybe_unused)
  18. {
  19. struct perf_record_event_update *ev = (struct perf_record_event_update *)event;
  20. TEST_ASSERT_VAL("wrong id", ev->id == 123);
  21. TEST_ASSERT_VAL("wrong id", ev->type == PERF_EVENT_UPDATE__UNIT);
  22. TEST_ASSERT_VAL("wrong unit", !strcmp(ev->unit, "KRAVA"));
  23. return 0;
  24. }
  25. static int process_event_scale(const struct perf_tool *tool __maybe_unused,
  26. union perf_event *event,
  27. struct perf_sample *sample __maybe_unused,
  28. struct machine *machine __maybe_unused)
  29. {
  30. struct perf_record_event_update *ev = (struct perf_record_event_update *)event;
  31. TEST_ASSERT_VAL("wrong id", ev->id == 123);
  32. TEST_ASSERT_VAL("wrong id", ev->type == PERF_EVENT_UPDATE__SCALE);
  33. TEST_ASSERT_VAL("wrong scale", ev->scale.scale == 0.123);
  34. return 0;
  35. }
  36. struct event_name {
  37. struct perf_tool tool;
  38. const char *name;
  39. };
  40. static int process_event_name(const struct perf_tool *tool,
  41. union perf_event *event,
  42. struct perf_sample *sample __maybe_unused,
  43. struct machine *machine __maybe_unused)
  44. {
  45. struct event_name *tmp = container_of(tool, struct event_name, tool);
  46. struct perf_record_event_update *ev = (struct perf_record_event_update *)event;
  47. TEST_ASSERT_VAL("wrong id", ev->id == 123);
  48. TEST_ASSERT_VAL("wrong id", ev->type == PERF_EVENT_UPDATE__NAME);
  49. TEST_ASSERT_VAL("wrong name", !strcmp(ev->name, tmp->name));
  50. return 0;
  51. }
  52. static int process_event_cpus(const struct perf_tool *tool __maybe_unused,
  53. union perf_event *event,
  54. struct perf_sample *sample __maybe_unused,
  55. struct machine *machine __maybe_unused)
  56. {
  57. struct perf_record_event_update *ev = (struct perf_record_event_update *)event;
  58. struct perf_cpu_map *map;
  59. map = cpu_map__new_data(&ev->cpus.cpus);
  60. TEST_ASSERT_VAL("wrong id", ev->id == 123);
  61. TEST_ASSERT_VAL("wrong type", ev->type == PERF_EVENT_UPDATE__CPUS);
  62. TEST_ASSERT_VAL("wrong cpus", perf_cpu_map__nr(map) == 3);
  63. TEST_ASSERT_VAL("wrong cpus", perf_cpu_map__cpu(map, 0).cpu == 1);
  64. TEST_ASSERT_VAL("wrong cpus", perf_cpu_map__cpu(map, 1).cpu == 2);
  65. TEST_ASSERT_VAL("wrong cpus", perf_cpu_map__cpu(map, 2).cpu == 3);
  66. perf_cpu_map__put(map);
  67. return 0;
  68. }
  69. static int test__event_update(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
  70. {
  71. struct evsel *evsel;
  72. struct event_name tmp;
  73. struct evlist *evlist = evlist__new_default();
  74. TEST_ASSERT_VAL("failed to get evlist", evlist);
  75. evsel = evlist__first(evlist);
  76. TEST_ASSERT_VAL("failed to allocate ids",
  77. !perf_evsel__alloc_id(&evsel->core, 1, 1));
  78. perf_evlist__id_add(&evlist->core, &evsel->core, 0, 0, 123);
  79. free((char *)evsel->unit);
  80. evsel->unit = strdup("KRAVA");
  81. TEST_ASSERT_VAL("failed to synthesize attr update unit",
  82. !perf_event__synthesize_event_update_unit(NULL, evsel, process_event_unit));
  83. evsel->scale = 0.123;
  84. TEST_ASSERT_VAL("failed to synthesize attr update scale",
  85. !perf_event__synthesize_event_update_scale(NULL, evsel, process_event_scale));
  86. perf_tool__init(&tmp.tool, /*ordered_events=*/false);
  87. tmp.name = evsel__name(evsel);
  88. TEST_ASSERT_VAL("failed to synthesize attr update name",
  89. !perf_event__synthesize_event_update_name(&tmp.tool, evsel, process_event_name));
  90. perf_cpu_map__put(evsel->core.pmu_cpus);
  91. evsel->core.pmu_cpus = perf_cpu_map__new("1,2,3");
  92. TEST_ASSERT_VAL("failed to synthesize attr update cpus",
  93. !perf_event__synthesize_event_update_cpus(&tmp.tool, evsel, process_event_cpus));
  94. evlist__delete(evlist);
  95. return 0;
  96. }
  97. DEFINE_SUITE("Synthesize attr update", event_update);