Florian Mayer | 824274d | 2018-09-17 11:33:45 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
| 17 | #include "src/profiling/memory/client.h" |
| 18 | |
| 19 | #include <inttypes.h> |
Ryan Savitski | 03693fb | 2019-01-18 17:07:06 +0000 | [diff] [blame] | 20 | #include <sys/prctl.h> |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame] | 21 | #include <sys/syscall.h> |
Ryan Savitski | 9eb2d5b | 2019-04-25 15:02:31 +0100 | [diff] [blame] | 22 | #include <sys/types.h> |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame] | 23 | #include <unistd.h> |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame] | 24 | #include <unwindstack/MachineArm.h> |
| 25 | #include <unwindstack/MachineArm64.h> |
| 26 | #include <unwindstack/MachineMips.h> |
| 27 | #include <unwindstack/MachineMips64.h> |
| 28 | #include <unwindstack/MachineX86.h> |
| 29 | #include <unwindstack/MachineX86_64.h> |
| 30 | #include <unwindstack/Regs.h> |
| 31 | #include <unwindstack/RegsGetLocal.h> |
Florian Mayer | 824274d | 2018-09-17 11:33:45 +0100 | [diff] [blame] | 32 | |
Ryan Savitski | 9eb2d5b | 2019-04-25 15:02:31 +0100 | [diff] [blame] | 33 | #include <atomic> |
| 34 | #include <new> |
| 35 | |
Florian Mayer | 824274d | 2018-09-17 11:33:45 +0100 | [diff] [blame] | 36 | #include "perfetto/base/logging.h" |
Eric Seckler | 83dcc8c | 2019-08-21 12:18:43 +0100 | [diff] [blame] | 37 | #include "perfetto/base/time.h" |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame] | 38 | #include "perfetto/ext/base/scoped_file.h" |
| 39 | #include "perfetto/ext/base/thread_utils.h" |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame] | 40 | #include "perfetto/ext/base/unix_socket.h" |
| 41 | #include "perfetto/ext/base/utils.h" |
Florian Mayer | 88665be | 2018-10-03 13:15:38 +0100 | [diff] [blame] | 42 | #include "src/profiling/memory/sampler.h" |
Ryan Savitski | a502bde | 2019-02-26 21:34:03 +0000 | [diff] [blame] | 43 | #include "src/profiling/memory/scoped_spinlock.h" |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame] | 44 | #include "src/profiling/memory/wire_protocol.h" |
Florian Mayer | 824274d | 2018-09-17 11:33:45 +0100 | [diff] [blame] | 45 | |
| 46 | namespace perfetto { |
Florian Mayer | 8c0d048 | 2018-10-22 14:23:40 +0100 | [diff] [blame] | 47 | namespace profiling { |
Florian Mayer | af26602 | 2018-09-17 15:18:15 +0100 | [diff] [blame] | 48 | namespace { |
| 49 | |
Florian Mayer | 5195059 | 2019-03-06 20:05:15 +0000 | [diff] [blame] | 50 | const char kSingleByte[1] = {'x'}; |
Florian Mayer | 6508157 | 2018-12-18 13:41:08 +0000 | [diff] [blame] | 51 | constexpr std::chrono::seconds kLockTimeout{1}; |
Florian Mayer | d6bdb6f | 2019-05-03 17:53:58 +0100 | [diff] [blame] | 52 | constexpr auto kResendBackoffUs = 100; |
Florian Mayer | 15e71f8 | 2018-10-17 15:11:49 +0100 | [diff] [blame] | 53 | |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame] | 54 | inline bool IsMainThread() { |
Primiano Tucci | 5e6bc99 | 2018-12-12 15:04:36 +0000 | [diff] [blame] | 55 | return getpid() == base::GetThreadId(); |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame] | 56 | } |
Florian Mayer | af26602 | 2018-09-17 15:18:15 +0100 | [diff] [blame] | 57 | |
Florian Mayer | 88665be | 2018-10-03 13:15:38 +0100 | [diff] [blame] | 58 | // TODO(b/117203899): Remove this after making bionic implementation safe to |
| 59 | // use. |
| 60 | char* FindMainThreadStack() { |
| 61 | base::ScopedFstream maps(fopen("/proc/self/maps", "r")); |
| 62 | if (!maps) { |
| 63 | return nullptr; |
| 64 | } |
| 65 | while (!feof(*maps)) { |
| 66 | char line[1024]; |
| 67 | char* data = fgets(line, sizeof(line), *maps); |
| 68 | if (data != nullptr && strstr(data, "[stack]")) { |
| 69 | char* sep = strstr(data, "-"); |
| 70 | if (sep == nullptr) |
| 71 | continue; |
| 72 | sep++; |
| 73 | return reinterpret_cast<char*>(strtoll(sep, nullptr, 16)); |
| 74 | } |
| 75 | } |
| 76 | return nullptr; |
| 77 | } |
| 78 | |
Ryan Savitski | 03693fb | 2019-01-18 17:07:06 +0000 | [diff] [blame] | 79 | int UnsetDumpable(int) { |
| 80 | prctl(PR_SET_DUMPABLE, 0); |
| 81 | return 0; |
| 82 | } |
| 83 | |
Florian Mayer | af26602 | 2018-09-17 15:18:15 +0100 | [diff] [blame] | 84 | } // namespace |
| 85 | |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame] | 86 | const char* GetThreadStackBase() { |
| 87 | pthread_attr_t attr; |
| 88 | if (pthread_getattr_np(pthread_self(), &attr) != 0) |
| 89 | return nullptr; |
| 90 | base::ScopedResource<pthread_attr_t*, pthread_attr_destroy, nullptr> cleanup( |
| 91 | &attr); |
| 92 | |
| 93 | char* stackaddr; |
| 94 | size_t stacksize; |
| 95 | if (pthread_attr_getstack(&attr, reinterpret_cast<void**>(&stackaddr), |
| 96 | &stacksize) != 0) |
| 97 | return nullptr; |
| 98 | return stackaddr + stacksize; |
| 99 | } |
| 100 | |
Ryan Savitski | 4a78317 | 2019-03-21 19:27:12 +0000 | [diff] [blame] | 101 | // static |
| 102 | base::Optional<base::UnixSocketRaw> Client::ConnectToHeapprofd( |
| 103 | const std::string& sock_name) { |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 104 | auto sock = base::UnixSocketRaw::CreateMayFail(base::SockFamily::kUnix, |
| 105 | base::SockType::kStream); |
Ryan Savitski | 4a78317 | 2019-03-21 19:27:12 +0000 | [diff] [blame] | 106 | if (!sock || !sock.Connect(sock_name)) { |
| 107 | PERFETTO_PLOG("Failed to connect to %s", sock_name.c_str()); |
| 108 | return base::nullopt; |
Florian Mayer | 5195059 | 2019-03-06 20:05:15 +0000 | [diff] [blame] | 109 | } |
Ryan Savitski | 4a78317 | 2019-03-21 19:27:12 +0000 | [diff] [blame] | 110 | if (!sock.SetTxTimeout(kClientSockTimeoutMs)) { |
| 111 | PERFETTO_PLOG("Failed to set send timeout for %s", sock_name.c_str()); |
| 112 | return base::nullopt; |
| 113 | } |
| 114 | if (!sock.SetRxTimeout(kClientSockTimeoutMs)) { |
| 115 | PERFETTO_PLOG("Failed to set receive timeout for %s", sock_name.c_str()); |
| 116 | return base::nullopt; |
| 117 | } |
| 118 | return std::move(sock); |
| 119 | } |
| 120 | |
| 121 | // static |
Ryan Savitski | 646d366 | 2019-04-08 12:53:29 +0100 | [diff] [blame] | 122 | std::shared_ptr<Client> Client::CreateAndHandshake( |
| 123 | base::UnixSocketRaw sock, |
| 124 | UnhookedAllocator<Client> unhooked_allocator) { |
Ryan Savitski | 4a78317 | 2019-03-21 19:27:12 +0000 | [diff] [blame] | 125 | if (!sock) { |
Florian Mayer | 33d8ea2 | 2019-05-09 19:57:07 +0100 | [diff] [blame] | 126 | PERFETTO_DFATAL_OR_ELOG("Socket not connected."); |
Ryan Savitski | 4a78317 | 2019-03-21 19:27:12 +0000 | [diff] [blame] | 127 | return nullptr; |
| 128 | } |
Florian Mayer | 5195059 | 2019-03-06 20:05:15 +0000 | [diff] [blame] | 129 | |
Florian Mayer | 754b57f | 2019-04-15 13:17:55 +0100 | [diff] [blame] | 130 | PERFETTO_DCHECK(sock.IsBlocking()); |
| 131 | |
Ryan Savitski | 03693fb | 2019-01-18 17:07:06 +0000 | [diff] [blame] | 132 | // We might be running in a process that is not dumpable (such as app |
| 133 | // processes on user builds), in which case the /proc/self/mem will be chown'd |
| 134 | // to root:root, and will not be accessible even to the process itself (see |
| 135 | // man 5 proc). In such situations, temporarily mark the process dumpable to |
| 136 | // be able to open the files, unsetting dumpability immediately afterwards. |
| 137 | int orig_dumpable = prctl(PR_GET_DUMPABLE); |
| 138 | |
| 139 | enum { kNop, kDoUnset }; |
| 140 | base::ScopedResource<int, UnsetDumpable, kNop, false> unset_dumpable(kNop); |
| 141 | if (orig_dumpable == 0) { |
| 142 | unset_dumpable.reset(kDoUnset); |
| 143 | prctl(PR_SET_DUMPABLE, 1); |
| 144 | } |
| 145 | |
Florian Mayer | 1b489ad | 2019-06-26 22:18:46 -0700 | [diff] [blame] | 146 | size_t num_send_fds = kHandshakeSize; |
| 147 | |
Florian Mayer | b03fd28 | 2018-10-03 16:05:16 +0100 | [diff] [blame] | 148 | base::ScopedFile maps(base::OpenFile("/proc/self/maps", O_RDONLY)); |
Ryan Savitski | 03693fb | 2019-01-18 17:07:06 +0000 | [diff] [blame] | 149 | if (!maps) { |
Florian Mayer | 33d8ea2 | 2019-05-09 19:57:07 +0100 | [diff] [blame] | 150 | PERFETTO_DFATAL_OR_ELOG("Failed to open /proc/self/maps"); |
Ryan Savitski | 4a78317 | 2019-03-21 19:27:12 +0000 | [diff] [blame] | 151 | return nullptr; |
Florian Mayer | 0ff382c | 2018-11-13 16:19:16 +0000 | [diff] [blame] | 152 | } |
Ryan Savitski | 03693fb | 2019-01-18 17:07:06 +0000 | [diff] [blame] | 153 | base::ScopedFile mem(base::OpenFile("/proc/self/mem", O_RDONLY)); |
| 154 | if (!mem) { |
Florian Mayer | 33d8ea2 | 2019-05-09 19:57:07 +0100 | [diff] [blame] | 155 | PERFETTO_DFATAL_OR_ELOG("Failed to open /proc/self/mem"); |
Ryan Savitski | 4a78317 | 2019-03-21 19:27:12 +0000 | [diff] [blame] | 156 | return nullptr; |
Ryan Savitski | 03693fb | 2019-01-18 17:07:06 +0000 | [diff] [blame] | 157 | } |
Florian Mayer | 7142c7c | 2019-05-20 18:11:41 +0100 | [diff] [blame] | 158 | |
Florian Mayer | 1b489ad | 2019-06-26 22:18:46 -0700 | [diff] [blame] | 159 | base::ScopedFile page_idle(base::OpenFile("/proc/self/page_idle", O_RDWR)); |
| 160 | if (!page_idle) { |
| 161 | PERFETTO_LOG("Failed to open /proc/self/page_idle. Continuing."); |
| 162 | num_send_fds = kHandshakeSize - 1; |
| 163 | } |
| 164 | |
Ryan Savitski | 03693fb | 2019-01-18 17:07:06 +0000 | [diff] [blame] | 165 | // Restore original dumpability value if we overrode it. |
| 166 | unset_dumpable.reset(); |
| 167 | |
Florian Mayer | 5195059 | 2019-03-06 20:05:15 +0000 | [diff] [blame] | 168 | int fds[kHandshakeSize]; |
| 169 | fds[kHandshakeMaps] = *maps; |
| 170 | fds[kHandshakeMem] = *mem; |
Florian Mayer | 1b489ad | 2019-06-26 22:18:46 -0700 | [diff] [blame] | 171 | fds[kHandshakePageIdle] = *page_idle; |
Florian Mayer | 5195059 | 2019-03-06 20:05:15 +0000 | [diff] [blame] | 172 | |
Florian Mayer | 1fe7773 | 2018-10-02 17:25:53 +0100 | [diff] [blame] | 173 | // Send an empty record to transfer fds for /proc/self/maps and |
| 174 | // /proc/self/mem. |
Florian Mayer | 1b489ad | 2019-06-26 22:18:46 -0700 | [diff] [blame] | 175 | if (sock.Send(kSingleByte, sizeof(kSingleByte), fds, num_send_fds) != |
Florian Mayer | 5195059 | 2019-03-06 20:05:15 +0000 | [diff] [blame] | 176 | sizeof(kSingleByte)) { |
Florian Mayer | 33d8ea2 | 2019-05-09 19:57:07 +0100 | [diff] [blame] | 177 | PERFETTO_DFATAL_OR_ELOG("Failed to send file descriptors."); |
Ryan Savitski | 4a78317 | 2019-03-21 19:27:12 +0000 | [diff] [blame] | 178 | return nullptr; |
Florian Mayer | f1510c4 | 2018-10-17 11:40:52 +0100 | [diff] [blame] | 179 | } |
Florian Mayer | 5195059 | 2019-03-06 20:05:15 +0000 | [diff] [blame] | 180 | |
Ryan Savitski | 4a78317 | 2019-03-21 19:27:12 +0000 | [diff] [blame] | 181 | ClientConfiguration client_config; |
Florian Mayer | 5195059 | 2019-03-06 20:05:15 +0000 | [diff] [blame] | 182 | base::ScopedFile shmem_fd; |
Florian Mayer | 0e26d71 | 2019-03-26 14:18:14 +0000 | [diff] [blame] | 183 | size_t recv = 0; |
| 184 | while (recv < sizeof(client_config)) { |
| 185 | size_t num_fds = 0; |
| 186 | base::ScopedFile* fd = nullptr; |
| 187 | if (!shmem_fd) { |
| 188 | num_fds = 1; |
| 189 | fd = &shmem_fd; |
| 190 | } |
| 191 | ssize_t rd = sock.Receive(reinterpret_cast<char*>(&client_config) + recv, |
| 192 | sizeof(client_config) - recv, fd, num_fds); |
| 193 | if (rd == -1) { |
| 194 | PERFETTO_PLOG("Failed to receive ClientConfiguration."); |
| 195 | return nullptr; |
| 196 | } |
| 197 | if (rd == 0) { |
| 198 | PERFETTO_LOG("Server disconnected while sending ClientConfiguration."); |
| 199 | return nullptr; |
| 200 | } |
| 201 | recv += static_cast<size_t>(rd); |
| 202 | } |
| 203 | |
| 204 | if (!shmem_fd) { |
Florian Mayer | 33d8ea2 | 2019-05-09 19:57:07 +0100 | [diff] [blame] | 205 | PERFETTO_DFATAL_OR_ELOG("Did not receive shmem fd."); |
Ryan Savitski | 4a78317 | 2019-03-21 19:27:12 +0000 | [diff] [blame] | 206 | return nullptr; |
Florian Mayer | f1510c4 | 2018-10-17 11:40:52 +0100 | [diff] [blame] | 207 | } |
Florian Mayer | 5195059 | 2019-03-06 20:05:15 +0000 | [diff] [blame] | 208 | |
| 209 | auto shmem = SharedRingBuffer::Attach(std::move(shmem_fd)); |
| 210 | if (!shmem || !shmem->is_valid()) { |
Florian Mayer | 33d8ea2 | 2019-05-09 19:57:07 +0100 | [diff] [blame] | 211 | PERFETTO_DFATAL_OR_ELOG("Failed to attach to shmem."); |
Ryan Savitski | 4a78317 | 2019-03-21 19:27:12 +0000 | [diff] [blame] | 212 | return nullptr; |
Florian Mayer | 5195059 | 2019-03-06 20:05:15 +0000 | [diff] [blame] | 213 | } |
Florian Mayer | 5195059 | 2019-03-06 20:05:15 +0000 | [diff] [blame] | 214 | |
Ryan Savitski | 4a78317 | 2019-03-21 19:27:12 +0000 | [diff] [blame] | 215 | PERFETTO_DCHECK(client_config.interval >= 1); |
Florian Mayer | d6bdb6f | 2019-05-03 17:53:58 +0100 | [diff] [blame] | 216 | // TODO(fmayer): Always make this nonblocking. |
| 217 | // This is so that without block_client, we get the old behaviour that rate |
| 218 | // limits using the blocking socket. We do not want to change that for Q. |
| 219 | sock.SetBlocking(!client_config.block_client); |
Ryan Savitski | 4a78317 | 2019-03-21 19:27:12 +0000 | [diff] [blame] | 220 | Sampler sampler{client_config.interval}; |
Ryan Savitski | 646d366 | 2019-04-08 12:53:29 +0100 | [diff] [blame] | 221 | // note: the shared_ptr will retain a copy of the unhooked_allocator |
Ryan Savitski | 9eb2d5b | 2019-04-25 15:02:31 +0100 | [diff] [blame] | 222 | return std::allocate_shared<Client>(unhooked_allocator, std::move(sock), |
| 223 | client_config, std::move(shmem.value()), |
| 224 | std::move(sampler), getpid(), |
| 225 | FindMainThreadStack()); |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame] | 226 | } |
| 227 | |
Ryan Savitski | 4a78317 | 2019-03-21 19:27:12 +0000 | [diff] [blame] | 228 | Client::Client(base::UnixSocketRaw sock, |
| 229 | ClientConfiguration client_config, |
| 230 | SharedRingBuffer shmem, |
| 231 | Sampler sampler, |
Ryan Savitski | 9eb2d5b | 2019-04-25 15:02:31 +0100 | [diff] [blame] | 232 | pid_t pid_at_creation, |
Ryan Savitski | 4a78317 | 2019-03-21 19:27:12 +0000 | [diff] [blame] | 233 | const char* main_thread_stack_base) |
| 234 | : client_config_(client_config), |
| 235 | sampler_(std::move(sampler)), |
| 236 | sock_(std::move(sock)), |
| 237 | main_thread_stack_base_(main_thread_stack_base), |
Ryan Savitski | 9eb2d5b | 2019-04-25 15:02:31 +0100 | [diff] [blame] | 238 | shmem_(std::move(shmem)), |
| 239 | pid_at_creation_(pid_at_creation) {} |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame] | 240 | |
| 241 | const char* Client::GetStackBase() { |
| 242 | if (IsMainThread()) { |
| 243 | if (!main_thread_stack_base_) |
| 244 | // Because pthread_attr_getstack reads and parses /proc/self/maps and |
| 245 | // /proc/self/stat, we have to cache the result here. |
| 246 | main_thread_stack_base_ = GetThreadStackBase(); |
| 247 | return main_thread_stack_base_; |
| 248 | } |
| 249 | return GetThreadStackBase(); |
| 250 | } |
| 251 | |
| 252 | // The stack grows towards numerically smaller addresses, so the stack layout |
| 253 | // of main calling malloc is as follows. |
| 254 | // |
| 255 | // +------------+ |
| 256 | // |SendWireMsg | |
| 257 | // stacktop +--> +------------+ 0x1000 |
| 258 | // |RecordMalloc| + |
| 259 | // +------------+ | |
| 260 | // | malloc | | |
| 261 | // +------------+ | |
| 262 | // | main | v |
| 263 | // stackbase +-> +------------+ 0xffff |
Florian Mayer | 6eb891b | 2019-06-25 13:57:59 -0700 | [diff] [blame] | 264 | bool Client::RecordMalloc(uint64_t sample_size, |
| 265 | uint64_t alloc_size, |
Florian Mayer | 9a7fb2b | 2018-10-17 18:17:40 +0100 | [diff] [blame] | 266 | uint64_t alloc_address) { |
Florian Mayer | 8525eb4 | 2019-04-30 13:09:33 +0100 | [diff] [blame] | 267 | if (PERFETTO_UNLIKELY(getpid() != pid_at_creation_)) { |
Ryan Savitski | 9eb2d5b | 2019-04-25 15:02:31 +0100 | [diff] [blame] | 268 | PERFETTO_LOG("Detected post-fork child situation, stopping profiling."); |
| 269 | return false; |
| 270 | } |
| 271 | |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame] | 272 | AllocMetadata metadata; |
| 273 | const char* stackbase = GetStackBase(); |
| 274 | const char* stacktop = reinterpret_cast<char*>(__builtin_frame_address(0)); |
| 275 | unwindstack::AsmGetRegs(metadata.register_data); |
| 276 | |
Florian Mayer | 33d8ea2 | 2019-05-09 19:57:07 +0100 | [diff] [blame] | 277 | if (PERFETTO_UNLIKELY(stackbase < stacktop)) { |
| 278 | PERFETTO_DFATAL_OR_ELOG("Stackbase >= stacktop."); |
Florian Mayer | 20c4b3c | 2019-01-24 18:25:35 +0000 | [diff] [blame] | 279 | return false; |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | uint64_t stack_size = static_cast<uint64_t>(stackbase - stacktop); |
Florian Mayer | 6eb891b | 2019-06-25 13:57:59 -0700 | [diff] [blame] | 283 | metadata.sample_size = sample_size; |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame] | 284 | metadata.alloc_size = alloc_size; |
| 285 | metadata.alloc_address = alloc_address; |
| 286 | metadata.stack_pointer = reinterpret_cast<uint64_t>(stacktop); |
| 287 | metadata.stack_pointer_offset = sizeof(AllocMetadata); |
| 288 | metadata.arch = unwindstack::Regs::CurrentArch(); |
Florian Mayer | c40573e | 2018-11-16 14:27:28 +0000 | [diff] [blame] | 289 | metadata.sequence_number = |
| 290 | 1 + sequence_number_.fetch_add(1, std::memory_order_acq_rel); |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame] | 291 | |
Florian Mayer | 5bd65f4 | 2019-05-02 16:20:38 +0100 | [diff] [blame] | 292 | struct timespec ts; |
| 293 | if (clock_gettime(CLOCK_MONOTONIC_COARSE, &ts) == 0) { |
| 294 | metadata.clock_monotonic_coarse_timestamp = |
| 295 | static_cast<uint64_t>(base::FromPosixTimespec(ts).count()); |
| 296 | } else { |
| 297 | metadata.clock_monotonic_coarse_timestamp = 0; |
| 298 | } |
| 299 | |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame] | 300 | WireMessage msg{}; |
Florian Mayer | 88665be | 2018-10-03 13:15:38 +0100 | [diff] [blame] | 301 | msg.record_type = RecordType::Malloc; |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame] | 302 | msg.alloc_header = &metadata; |
| 303 | msg.payload = const_cast<char*>(stacktop); |
| 304 | msg.payload_size = static_cast<size_t>(stack_size); |
| 305 | |
Florian Mayer | d6bdb6f | 2019-05-03 17:53:58 +0100 | [diff] [blame] | 306 | if (!SendWireMessageWithRetriesIfBlocking(msg)) |
| 307 | return false; |
| 308 | |
| 309 | return SendControlSocketByte(); |
| 310 | } |
| 311 | |
| 312 | bool Client::SendWireMessageWithRetriesIfBlocking(const WireMessage& msg) { |
| 313 | for (;;) { |
| 314 | if (PERFETTO_LIKELY(SendWireMessage(&shmem_, msg))) |
| 315 | return true; |
| 316 | // retry if in blocking mode and still connected |
| 317 | if (client_config_.block_client && base::IsAgain(errno) && IsConnected()) { |
| 318 | usleep(kResendBackoffUs); |
| 319 | continue; |
| 320 | } |
| 321 | PERFETTO_PLOG("Failed to write to shared ring buffer. Disconnecting."); |
Florian Mayer | 5195059 | 2019-03-06 20:05:15 +0000 | [diff] [blame] | 322 | return false; |
| 323 | } |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame] | 324 | } |
| 325 | |
Ryan Savitski | 549753e | 2019-03-15 15:52:37 +0000 | [diff] [blame] | 326 | bool Client::RecordFree(const uint64_t alloc_address) { |
Ryan Savitski | 4a78317 | 2019-03-21 19:27:12 +0000 | [diff] [blame] | 327 | uint64_t sequence_number = |
| 328 | 1 + sequence_number_.fetch_add(1, std::memory_order_acq_rel); |
Florian Mayer | 9a7fb2b | 2018-10-17 18:17:40 +0100 | [diff] [blame] | 329 | |
Ryan Savitski | 549753e | 2019-03-15 15:52:37 +0000 | [diff] [blame] | 330 | std::unique_lock<std::timed_mutex> l(free_batch_lock_, kLockTimeout); |
| 331 | if (!l.owns_lock()) |
| 332 | return false; |
| 333 | if (free_batch_.num_entries == kFreeBatchSize) { |
| 334 | if (!FlushFreesLocked()) |
| 335 | return false; |
| 336 | // Flushed the contents of the buffer, reset it for reuse. |
| 337 | free_batch_.num_entries = 0; |
| 338 | } |
| 339 | FreeBatchEntry& current_entry = |
| 340 | free_batch_.entries[free_batch_.num_entries++]; |
| 341 | current_entry.sequence_number = sequence_number; |
Ryan Savitski | 4a78317 | 2019-03-21 19:27:12 +0000 | [diff] [blame] | 342 | current_entry.addr = alloc_address; |
Ryan Savitski | 549753e | 2019-03-15 15:52:37 +0000 | [diff] [blame] | 343 | return true; |
| 344 | } |
| 345 | |
| 346 | bool Client::FlushFreesLocked() { |
Florian Mayer | 8525eb4 | 2019-04-30 13:09:33 +0100 | [diff] [blame] | 347 | if (PERFETTO_UNLIKELY(getpid() != pid_at_creation_)) { |
Ryan Savitski | 9eb2d5b | 2019-04-25 15:02:31 +0100 | [diff] [blame] | 348 | PERFETTO_LOG("Detected post-fork child situation, stopping profiling."); |
| 349 | return false; |
| 350 | } |
| 351 | |
Ryan Savitski | 549753e | 2019-03-15 15:52:37 +0000 | [diff] [blame] | 352 | WireMessage msg = {}; |
| 353 | msg.record_type = RecordType::Free; |
| 354 | msg.free_header = &free_batch_; |
Florian Mayer | 5bd65f4 | 2019-05-02 16:20:38 +0100 | [diff] [blame] | 355 | struct timespec ts; |
| 356 | if (clock_gettime(CLOCK_MONOTONIC_COARSE, &ts) == 0) { |
| 357 | free_batch_.clock_monotonic_coarse_timestamp = |
| 358 | static_cast<uint64_t>(base::FromPosixTimespec(ts).count()); |
| 359 | } else { |
| 360 | free_batch_.clock_monotonic_coarse_timestamp = 0; |
| 361 | } |
| 362 | |
Florian Mayer | d6bdb6f | 2019-05-03 17:53:58 +0100 | [diff] [blame] | 363 | if (!SendWireMessageWithRetriesIfBlocking(msg)) |
Ryan Savitski | 549753e | 2019-03-15 15:52:37 +0000 | [diff] [blame] | 364 | return false; |
Florian Mayer | 754b57f | 2019-04-15 13:17:55 +0100 | [diff] [blame] | 365 | return SendControlSocketByte(); |
| 366 | } |
| 367 | |
Florian Mayer | d6bdb6f | 2019-05-03 17:53:58 +0100 | [diff] [blame] | 368 | bool Client::IsConnected() { |
| 369 | PERFETTO_DCHECK(!sock_.IsBlocking()); |
| 370 | char buf[1]; |
| 371 | ssize_t recv_bytes = sock_.Receive(buf, sizeof(buf), nullptr, 0); |
| 372 | if (recv_bytes == 0) |
| 373 | return false; |
| 374 | // This is not supposed to happen because currently heapprofd does not send |
| 375 | // data to the client. Here for generality's sake. |
| 376 | if (recv_bytes > 0) |
| 377 | return true; |
| 378 | return base::IsAgain(errno); |
| 379 | } |
| 380 | |
Florian Mayer | 754b57f | 2019-04-15 13:17:55 +0100 | [diff] [blame] | 381 | bool Client::SendControlSocketByte() { |
Florian Mayer | d6bdb6f | 2019-05-03 17:53:58 +0100 | [diff] [blame] | 382 | // TODO(fmayer): Fix the special casing that only block_client uses a |
| 383 | // nonblocking socket. |
| 384 | if (sock_.Send(kSingleByte, sizeof(kSingleByte)) == -1 && |
| 385 | (!client_config_.block_client || !base::IsAgain(errno))) { |
Ryan Savitski | 4a78317 | 2019-03-21 19:27:12 +0000 | [diff] [blame] | 386 | PERFETTO_PLOG("Failed to send control socket byte."); |
Ryan Savitski | 549753e | 2019-03-15 15:52:37 +0000 | [diff] [blame] | 387 | return false; |
| 388 | } |
| 389 | return true; |
| 390 | } |
| 391 | |
Florian Mayer | 8c0d048 | 2018-10-22 14:23:40 +0100 | [diff] [blame] | 392 | } // namespace profiling |
Florian Mayer | 824274d | 2018-09-17 11:33:45 +0100 | [diff] [blame] | 393 | } // namespace perfetto |