backtrace_read_benchmarks.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 <signal.h>
  18. #include <stdint.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <sys/mman.h>
  23. #include <sys/ptrace.h>
  24. #include <sys/types.h>
  25. #include <sys/uio.h>
  26. #include <sys/wait.h>
  27. #include <unistd.h>
  28. #include <memory>
  29. #include <vector>
  30. #include <benchmark/benchmark.h>
  31. #include <backtrace/Backtrace.h>
  32. #define AT_COMMON_SIZES Arg(1)->Arg(4)->Arg(8)->Arg(16)->Arg(100)->Arg(200)->Arg(500)->Arg(1024)
  33. static void Attach(pid_t pid) {
  34. if (ptrace(PTRACE_ATTACH, pid, 0, 0) == -1) {
  35. perror("Failed to attach");
  36. abort();
  37. }
  38. siginfo_t si;
  39. // Wait for up to 5 seconds.
  40. for (size_t i = 0; i < 5000; i++) {
  41. if (ptrace(PTRACE_GETSIGINFO, pid, 0, &si) == 0) {
  42. return;
  43. }
  44. usleep(1000);
  45. }
  46. printf("Remote process failed to stop in five seconds.\n");
  47. abort();
  48. }
  49. class ScopedPidReaper {
  50. public:
  51. ScopedPidReaper(pid_t pid) : pid_(pid) {}
  52. ~ScopedPidReaper() {
  53. kill(pid_, SIGKILL);
  54. waitpid(pid_, nullptr, 0);
  55. }
  56. private:
  57. pid_t pid_;
  58. };
  59. static size_t ProcessVmRead(pid_t pid, uint64_t remote_src, void* dst, size_t len) {
  60. struct iovec dst_iov = {
  61. .iov_base = dst, .iov_len = len,
  62. };
  63. struct iovec src_iov = {
  64. .iov_base = reinterpret_cast<void*>(remote_src), .iov_len = len,
  65. };
  66. ssize_t rc = process_vm_readv(pid, &dst_iov, 1, &src_iov, 1, 0);
  67. return rc == -1 ? 0 : rc;
  68. }
  69. static bool PtraceReadLong(pid_t pid, uint64_t addr, long* value) {
  70. // ptrace() returns -1 and sets errno when the operation fails.
  71. // To disambiguate -1 from a valid result, we clear errno beforehand.
  72. errno = 0;
  73. *value = ptrace(PTRACE_PEEKTEXT, pid, reinterpret_cast<void*>(addr), nullptr);
  74. if (*value == -1 && errno) {
  75. return false;
  76. }
  77. return true;
  78. }
  79. static size_t PtraceRead(pid_t pid, uint64_t addr, void* dst, size_t bytes) {
  80. size_t bytes_read = 0;
  81. long data;
  82. for (size_t i = 0; i < bytes / sizeof(long); i++) {
  83. if (!PtraceReadLong(pid, addr, &data)) {
  84. return bytes_read;
  85. }
  86. memcpy(dst, &data, sizeof(long));
  87. dst = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(dst) + sizeof(long));
  88. addr += sizeof(long);
  89. bytes_read += sizeof(long);
  90. }
  91. size_t left_over = bytes & (sizeof(long) - 1);
  92. if (left_over) {
  93. if (!PtraceReadLong(pid, addr, &data)) {
  94. return bytes_read;
  95. }
  96. memcpy(dst, &data, left_over);
  97. bytes_read += left_over;
  98. }
  99. return bytes_read;
  100. }
  101. static void CreateRemoteProcess(size_t size, void** map, pid_t* pid) {
  102. *map = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  103. if (*map == MAP_FAILED) {
  104. perror("Can't allocate memory");
  105. abort();
  106. }
  107. memset(*map, 0xaa, size);
  108. if ((*pid = fork()) == 0) {
  109. for (volatile int i = 0;; i++)
  110. ;
  111. exit(1);
  112. }
  113. if (*pid < 0) {
  114. perror("Failed to fork");
  115. abort();
  116. }
  117. Attach(*pid);
  118. // Don't need this map in the current process any more.
  119. munmap(*map, size);
  120. }
  121. static void BM_read_with_ptrace(benchmark::State& state) {
  122. void* map;
  123. pid_t pid;
  124. CreateRemoteProcess(state.range(0), &map, &pid);
  125. ScopedPidReaper reap(pid);
  126. std::vector<uint8_t> read_buffer(state.range(0));
  127. uint64_t addr = reinterpret_cast<uint64_t>(map);
  128. while (state.KeepRunning()) {
  129. if (PtraceRead(pid, addr, read_buffer.data(), read_buffer.size()) != read_buffer.size()) {
  130. printf("Unexpected bad read.\n");
  131. abort();
  132. }
  133. }
  134. ptrace(PTRACE_DETACH, pid, 0, 0);
  135. }
  136. BENCHMARK(BM_read_with_ptrace)->AT_COMMON_SIZES;
  137. static void BM_read_with_process_vm_read(benchmark::State& state) {
  138. void* map;
  139. pid_t pid;
  140. CreateRemoteProcess(state.range(0), &map, &pid);
  141. ScopedPidReaper reap(pid);
  142. std::vector<uint8_t> read_buffer(state.range(0));
  143. uint64_t addr = reinterpret_cast<uint64_t>(map);
  144. while (state.KeepRunning()) {
  145. if (ProcessVmRead(pid, addr, read_buffer.data(), read_buffer.size()) != read_buffer.size()) {
  146. printf("Unexpected bad read.\n");
  147. abort();
  148. }
  149. }
  150. ptrace(PTRACE_DETACH, pid, 0, 0);
  151. }
  152. BENCHMARK(BM_read_with_process_vm_read)->AT_COMMON_SIZES;
  153. static void BM_read_with_backtrace_object(benchmark::State& state) {
  154. void* map;
  155. pid_t pid;
  156. CreateRemoteProcess(state.range(0), &map, &pid);
  157. ScopedPidReaper reap(pid);
  158. std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, BACKTRACE_CURRENT_THREAD));
  159. if (backtrace.get() == nullptr) {
  160. printf("Failed to create backtrace.\n");
  161. abort();
  162. }
  163. uint64_t addr = reinterpret_cast<uint64_t>(map);
  164. std::vector<uint8_t> read_buffer(state.range(0));
  165. while (state.KeepRunning()) {
  166. if (backtrace->Read(addr, read_buffer.data(), read_buffer.size()) != read_buffer.size()) {
  167. printf("Unexpected bad read.\n");
  168. abort();
  169. }
  170. }
  171. ptrace(PTRACE_DETACH, pid, 0, 0);
  172. }
  173. BENCHMARK(BM_read_with_backtrace_object)->AT_COMMON_SIZES;