topology.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include "tests.h"
  5. #include "util.h"
  6. #include "session.h"
  7. #include "evlist.h"
  8. #include "debug.h"
  9. #define TEMPL "/tmp/perf-test-XXXXXX"
  10. #define DATA_SIZE 10
  11. static int get_temp(char *path)
  12. {
  13. int fd;
  14. strcpy(path, TEMPL);
  15. fd = mkstemp(path);
  16. if (fd < 0) {
  17. perror("mkstemp failed");
  18. return -1;
  19. }
  20. close(fd);
  21. return 0;
  22. }
  23. static int session_write_header(char *path)
  24. {
  25. struct perf_session *session;
  26. struct perf_data_file file = {
  27. .path = path,
  28. .mode = PERF_DATA_MODE_WRITE,
  29. };
  30. session = perf_session__new(&file, false, NULL);
  31. TEST_ASSERT_VAL("can't get session", session);
  32. session->evlist = perf_evlist__new_default();
  33. TEST_ASSERT_VAL("can't get evlist", session->evlist);
  34. perf_header__set_feat(&session->header, HEADER_CPU_TOPOLOGY);
  35. perf_header__set_feat(&session->header, HEADER_NRCPUS);
  36. session->header.data_size += DATA_SIZE;
  37. TEST_ASSERT_VAL("failed to write header",
  38. !perf_session__write_header(session, session->evlist, file.fd, true));
  39. perf_session__delete(session);
  40. return 0;
  41. }
  42. static int check_cpu_topology(char *path, struct cpu_map *map)
  43. {
  44. struct perf_session *session;
  45. struct perf_data_file file = {
  46. .path = path,
  47. .mode = PERF_DATA_MODE_READ,
  48. };
  49. int i;
  50. session = perf_session__new(&file, false, NULL);
  51. TEST_ASSERT_VAL("can't get session", session);
  52. for (i = 0; i < session->header.env.nr_cpus_online; i++) {
  53. pr_debug("CPU %d, core %d, socket %d\n", i,
  54. session->header.env.cpu[i].core_id,
  55. session->header.env.cpu[i].socket_id);
  56. }
  57. for (i = 0; i < map->nr; i++) {
  58. TEST_ASSERT_VAL("Core ID doesn't match",
  59. (session->header.env.cpu[map->map[i]].core_id == (cpu_map__get_core(map, i, NULL) & 0xffff)));
  60. TEST_ASSERT_VAL("Socket ID doesn't match",
  61. (session->header.env.cpu[map->map[i]].socket_id == cpu_map__get_socket(map, i, NULL)));
  62. }
  63. perf_session__delete(session);
  64. return 0;
  65. }
  66. int test_session_topology(int subtest __maybe_unused)
  67. {
  68. char path[PATH_MAX];
  69. struct cpu_map *map;
  70. int ret = -1;
  71. TEST_ASSERT_VAL("can't get templ file", !get_temp(path));
  72. pr_debug("templ file: %s\n", path);
  73. if (session_write_header(path))
  74. goto free_path;
  75. map = cpu_map__new(NULL);
  76. if (map == NULL) {
  77. pr_debug("failed to get system cpumap\n");
  78. goto free_path;
  79. }
  80. if (check_cpu_topology(path, map))
  81. goto free_map;
  82. ret = 0;
  83. free_map:
  84. cpu_map__put(map);
  85. free_path:
  86. unlink(path);
  87. return ret;
  88. }