event_update.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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(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->data, "KRAVA"));
  23. return 0;
  24. }
  25. static int process_event_scale(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. struct perf_record_event_update_scale *ev_data;
  32. ev_data = (struct perf_record_event_update_scale *)ev->data;
  33. TEST_ASSERT_VAL("wrong id", ev->id == 123);
  34. TEST_ASSERT_VAL("wrong id", ev->type == PERF_EVENT_UPDATE__SCALE);
  35. TEST_ASSERT_VAL("wrong scale", ev_data->scale == 0.123);
  36. return 0;
  37. }
  38. struct event_name {
  39. struct perf_tool tool;
  40. const char *name;
  41. };
  42. static int process_event_name(struct perf_tool *tool,
  43. union perf_event *event,
  44. struct perf_sample *sample __maybe_unused,
  45. struct machine *machine __maybe_unused)
  46. {
  47. struct event_name *tmp = container_of(tool, struct event_name, tool);
  48. struct perf_record_event_update *ev = (struct perf_record_event_update *)event;
  49. TEST_ASSERT_VAL("wrong id", ev->id == 123);
  50. TEST_ASSERT_VAL("wrong id", ev->type == PERF_EVENT_UPDATE__NAME);
  51. TEST_ASSERT_VAL("wrong name", !strcmp(ev->data, tmp->name));
  52. return 0;
  53. }
  54. static int process_event_cpus(struct perf_tool *tool __maybe_unused,
  55. union perf_event *event,
  56. struct perf_sample *sample __maybe_unused,
  57. struct machine *machine __maybe_unused)
  58. {
  59. struct perf_record_event_update *ev = (struct perf_record_event_update *)event;
  60. struct perf_record_event_update_cpus *ev_data;
  61. struct perf_cpu_map *map;
  62. ev_data = (struct perf_record_event_update_cpus *) ev->data;
  63. map = cpu_map__new_data(&ev_data->cpus);
  64. TEST_ASSERT_VAL("wrong id", ev->id == 123);
  65. TEST_ASSERT_VAL("wrong type", ev->type == PERF_EVENT_UPDATE__CPUS);
  66. TEST_ASSERT_VAL("wrong cpus", map->nr == 3);
  67. TEST_ASSERT_VAL("wrong cpus", map->map[0] == 1);
  68. TEST_ASSERT_VAL("wrong cpus", map->map[1] == 2);
  69. TEST_ASSERT_VAL("wrong cpus", map->map[2] == 3);
  70. perf_cpu_map__put(map);
  71. return 0;
  72. }
  73. int test__event_update(struct test *test __maybe_unused, int subtest __maybe_unused)
  74. {
  75. struct evlist *evlist;
  76. struct evsel *evsel;
  77. struct event_name tmp;
  78. evlist = perf_evlist__new_default();
  79. TEST_ASSERT_VAL("failed to get evlist", evlist);
  80. evsel = evlist__first(evlist);
  81. TEST_ASSERT_VAL("failed to allocate ids",
  82. !perf_evsel__alloc_id(&evsel->core, 1, 1));
  83. perf_evlist__id_add(&evlist->core, &evsel->core, 0, 0, 123);
  84. evsel->unit = strdup("KRAVA");
  85. TEST_ASSERT_VAL("failed to synthesize attr update unit",
  86. !perf_event__synthesize_event_update_unit(NULL, evsel, process_event_unit));
  87. evsel->scale = 0.123;
  88. TEST_ASSERT_VAL("failed to synthesize attr update scale",
  89. !perf_event__synthesize_event_update_scale(NULL, evsel, process_event_scale));
  90. tmp.name = evsel__name(evsel);
  91. TEST_ASSERT_VAL("failed to synthesize attr update name",
  92. !perf_event__synthesize_event_update_name(&tmp.tool, evsel, process_event_name));
  93. evsel->core.own_cpus = perf_cpu_map__new("1,2,3");
  94. TEST_ASSERT_VAL("failed to synthesize attr update cpus",
  95. !perf_event__synthesize_event_update_cpus(&tmp.tool, evsel, process_event_cpus));
  96. evlist__delete(evlist);
  97. return 0;
  98. }