backtrace_benchmarks.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (C) 2017 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <errno.h>
  17. #include <fcntl.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <sys/prctl.h>
  21. #include <sys/stat.h>
  22. #include <sys/types.h>
  23. #include <sys/wait.h>
  24. #include <unistd.h>
  25. #include <string>
  26. #include <android-base/file.h>
  27. #include <android-base/threads.h>
  28. #include <benchmark/benchmark.h>
  29. #include <backtrace/Backtrace.h>
  30. #include <backtrace/BacktraceMap.h>
  31. #include <unwindstack/Memory.h>
  32. constexpr size_t kNumMaps = 2000;
  33. static bool CountMaps(pid_t pid, size_t* num_maps) {
  34. // Minimize the calls that might allocate memory. If too much memory
  35. // gets allocated, then this routine will add extra maps and the next
  36. // call will fail to get the same number of maps as before.
  37. int fd =
  38. open((std::string("/proc/") + std::to_string(pid) + "/maps").c_str(), O_RDONLY | O_CLOEXEC);
  39. if (fd == -1) {
  40. fprintf(stderr, "Cannot open map file for pid %d: %s\n", pid, strerror(errno));
  41. return false;
  42. }
  43. *num_maps = 0;
  44. while (true) {
  45. char buffer[2048];
  46. ssize_t bytes = read(fd, buffer, sizeof(buffer));
  47. if (bytes <= 0) {
  48. break;
  49. }
  50. // Count the '\n'.
  51. for (size_t i = 0; i < static_cast<size_t>(bytes); i++) {
  52. if (buffer[i] == '\n') {
  53. ++*num_maps;
  54. }
  55. }
  56. }
  57. close(fd);
  58. return true;
  59. }
  60. static void CreateMap(benchmark::State& state, BacktraceMap* (*map_func)(pid_t, bool)) {
  61. // Create a remote process so that the map data is exactly the same.
  62. // Also, so that we can create a set number of maps.
  63. pid_t pid;
  64. if ((pid = fork()) == 0) {
  65. size_t num_maps;
  66. if (!CountMaps(getpid(), &num_maps)) {
  67. exit(1);
  68. }
  69. // Create uniquely named maps.
  70. std::vector<void*> maps;
  71. for (size_t i = num_maps; i < kNumMaps; i++) {
  72. int flags = PROT_READ | PROT_WRITE;
  73. // Alternate page type to make sure a map entry is added for each call.
  74. if ((i % 2) == 0) {
  75. flags |= PROT_EXEC;
  76. }
  77. void* memory = mmap(nullptr, PAGE_SIZE, flags, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  78. if (memory == MAP_FAILED) {
  79. fprintf(stderr, "Failed to create map: %s\n", strerror(errno));
  80. exit(1);
  81. }
  82. memset(memory, 0x1, PAGE_SIZE);
  83. #if defined(PR_SET_VMA)
  84. if (prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, memory, PAGE_SIZE, "test_map") == -1) {
  85. fprintf(stderr, "Failed: %s\n", strerror(errno));
  86. }
  87. #endif
  88. maps.push_back(memory);
  89. }
  90. if (!CountMaps(getpid(), &num_maps)) {
  91. exit(1);
  92. }
  93. if (num_maps < kNumMaps) {
  94. fprintf(stderr, "Maps set incorrectly: %zu found, %zu expected at least.\n", num_maps,
  95. kNumMaps);
  96. std::string str;
  97. android::base::ReadFileToString("/proc/self/maps", &str);
  98. fprintf(stderr, "%s\n", str.c_str());
  99. exit(1);
  100. }
  101. // Wait for an hour at most.
  102. sleep(3600);
  103. exit(1);
  104. } else if (pid < 0) {
  105. fprintf(stderr, "Fork failed: %s\n", strerror(errno));
  106. return;
  107. }
  108. size_t num_maps = 0;
  109. for (size_t i = 0; i < 2000; i++) {
  110. if (CountMaps(pid, &num_maps) && num_maps >= kNumMaps) {
  111. break;
  112. }
  113. usleep(1000);
  114. }
  115. if (num_maps < kNumMaps) {
  116. fprintf(stderr, "Timed out waiting for the number of maps available: %zu\n", num_maps);
  117. return;
  118. }
  119. while (state.KeepRunning()) {
  120. BacktraceMap* map = map_func(pid, false);
  121. if (map == nullptr) {
  122. fprintf(stderr, "Failed to create map\n");
  123. return;
  124. }
  125. delete map;
  126. }
  127. kill(pid, SIGKILL);
  128. waitpid(pid, nullptr, 0);
  129. }
  130. static void BM_create_map(benchmark::State& state) {
  131. CreateMap(state, BacktraceMap::Create);
  132. }
  133. BENCHMARK(BM_create_map);
  134. using BacktraceCreateFn = decltype(Backtrace::Create);
  135. static void CreateBacktrace(benchmark::State& state, BacktraceMap* map, BacktraceCreateFn fn) {
  136. while (state.KeepRunning()) {
  137. std::unique_ptr<Backtrace> backtrace(fn(getpid(), android::base::GetThreadId(), map));
  138. backtrace->Unwind(0);
  139. }
  140. }
  141. static void BM_create_backtrace(benchmark::State& state) {
  142. std::unique_ptr<BacktraceMap> backtrace_map(BacktraceMap::Create(getpid()));
  143. CreateBacktrace(state, backtrace_map.get(), Backtrace::Create);
  144. }
  145. BENCHMARK(BM_create_backtrace);
  146. BENCHMARK_MAIN();