cpumap.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "tests.h"
  3. #include <stdio.h>
  4. #include "cpumap.h"
  5. #include "event.h"
  6. #include <string.h>
  7. #include <linux/bitops.h>
  8. #include "debug.h"
  9. struct machine;
  10. static int process_event_mask(struct perf_tool *tool __maybe_unused,
  11. union perf_event *event,
  12. struct perf_sample *sample __maybe_unused,
  13. struct machine *machine __maybe_unused)
  14. {
  15. struct cpu_map_event *map_event = &event->cpu_map;
  16. struct cpu_map_mask *mask;
  17. struct cpu_map_data *data;
  18. struct cpu_map *map;
  19. int i;
  20. data = &map_event->data;
  21. TEST_ASSERT_VAL("wrong type", data->type == PERF_CPU_MAP__MASK);
  22. mask = (struct cpu_map_mask *)data->data;
  23. TEST_ASSERT_VAL("wrong nr", mask->nr == 1);
  24. for (i = 0; i < 20; i++) {
  25. TEST_ASSERT_VAL("wrong cpu", test_bit(i, mask->mask));
  26. }
  27. map = cpu_map__new_data(data);
  28. TEST_ASSERT_VAL("wrong nr", map->nr == 20);
  29. for (i = 0; i < 20; i++) {
  30. TEST_ASSERT_VAL("wrong cpu", map->map[i] == i);
  31. }
  32. cpu_map__put(map);
  33. return 0;
  34. }
  35. static int process_event_cpus(struct perf_tool *tool __maybe_unused,
  36. union perf_event *event,
  37. struct perf_sample *sample __maybe_unused,
  38. struct machine *machine __maybe_unused)
  39. {
  40. struct cpu_map_event *map_event = &event->cpu_map;
  41. struct cpu_map_entries *cpus;
  42. struct cpu_map_data *data;
  43. struct cpu_map *map;
  44. data = &map_event->data;
  45. TEST_ASSERT_VAL("wrong type", data->type == PERF_CPU_MAP__CPUS);
  46. cpus = (struct cpu_map_entries *)data->data;
  47. TEST_ASSERT_VAL("wrong nr", cpus->nr == 2);
  48. TEST_ASSERT_VAL("wrong cpu", cpus->cpu[0] == 1);
  49. TEST_ASSERT_VAL("wrong cpu", cpus->cpu[1] == 256);
  50. map = cpu_map__new_data(data);
  51. TEST_ASSERT_VAL("wrong nr", map->nr == 2);
  52. TEST_ASSERT_VAL("wrong cpu", map->map[0] == 1);
  53. TEST_ASSERT_VAL("wrong cpu", map->map[1] == 256);
  54. TEST_ASSERT_VAL("wrong refcnt", refcount_read(&map->refcnt) == 1);
  55. cpu_map__put(map);
  56. return 0;
  57. }
  58. int test__cpu_map_synthesize(struct test *test __maybe_unused, int subtest __maybe_unused)
  59. {
  60. struct cpu_map *cpus;
  61. /* This one is better stores in mask. */
  62. cpus = cpu_map__new("0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19");
  63. TEST_ASSERT_VAL("failed to synthesize map",
  64. !perf_event__synthesize_cpu_map(NULL, cpus, process_event_mask, NULL));
  65. cpu_map__put(cpus);
  66. /* This one is better stores in cpu values. */
  67. cpus = cpu_map__new("1,256");
  68. TEST_ASSERT_VAL("failed to synthesize map",
  69. !perf_event__synthesize_cpu_map(NULL, cpus, process_event_cpus, NULL));
  70. cpu_map__put(cpus);
  71. return 0;
  72. }
  73. static int cpu_map_print(const char *str)
  74. {
  75. struct cpu_map *map = cpu_map__new(str);
  76. char buf[100];
  77. if (!map)
  78. return -1;
  79. cpu_map__snprint(map, buf, sizeof(buf));
  80. return !strcmp(buf, str);
  81. }
  82. int test__cpu_map_print(struct test *test __maybe_unused, int subtest __maybe_unused)
  83. {
  84. TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1"));
  85. TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,5"));
  86. TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3,5,7,9,11,13,15,17,19,21-40"));
  87. TEST_ASSERT_VAL("failed to convert map", cpu_map_print("2-5"));
  88. TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3-6,8-10,24,35-37"));
  89. TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3-6,8-10,24,35-37"));
  90. TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1-10,12-20,22-30,32-40"));
  91. return 0;
  92. }