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 | #ifndef SRC_PROFILING_MEMORY_CLIENT_H_ |
| 18 | #define SRC_PROFILING_MEMORY_CLIENT_H_ |
| 19 | |
| 20 | #include <stddef.h> |
Florian Mayer | af26602 | 2018-09-17 15:18:15 +0100 | [diff] [blame] | 21 | |
Florian Mayer | 824274d | 2018-09-17 11:33:45 +0100 | [diff] [blame] | 22 | #include <mutex> |
| 23 | #include <vector> |
| 24 | |
| 25 | #include "perfetto/base/scoped_file.h" |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame^] | 26 | #include "src/profiling/memory/wire_protocol.h" |
Florian Mayer | 824274d | 2018-09-17 11:33:45 +0100 | [diff] [blame] | 27 | |
| 28 | namespace perfetto { |
| 29 | |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame^] | 30 | class BorrowedSocket; |
Florian Mayer | 824274d | 2018-09-17 11:33:45 +0100 | [diff] [blame] | 31 | |
| 32 | class SocketPool { |
| 33 | public: |
| 34 | friend class BorrowedSocket; |
| 35 | SocketPool(std::vector<base::ScopedFile> sockets); |
| 36 | |
| 37 | BorrowedSocket Borrow(); |
| 38 | |
| 39 | private: |
| 40 | void Return(base::ScopedFile fd); |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame^] | 41 | std::mutex mutex_; |
Florian Mayer | 824274d | 2018-09-17 11:33:45 +0100 | [diff] [blame] | 42 | std::condition_variable cv_; |
| 43 | std::vector<base::ScopedFile> sockets_; |
| 44 | size_t available_sockets_; |
Florian Mayer | af26602 | 2018-09-17 15:18:15 +0100 | [diff] [blame] | 45 | size_t dead_sockets_ = 0; |
Florian Mayer | 824274d | 2018-09-17 11:33:45 +0100 | [diff] [blame] | 46 | }; |
| 47 | |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame^] | 48 | // Socket borrowed from a SocketPool. Gets returned once it goes out of scope. |
| 49 | class BorrowedSocket { |
| 50 | public: |
| 51 | BorrowedSocket(const BorrowedSocket&) = delete; |
| 52 | BorrowedSocket& operator=(const BorrowedSocket&) = delete; |
| 53 | BorrowedSocket(BorrowedSocket&& other) noexcept { |
| 54 | fd_ = std::move(other.fd_); |
| 55 | socket_pool_ = other.socket_pool_; |
| 56 | other.socket_pool_ = nullptr; |
| 57 | } |
| 58 | |
| 59 | BorrowedSocket(base::ScopedFile fd, SocketPool* socket_pool) |
| 60 | : fd_(std::move(fd)), socket_pool_(socket_pool) {} |
| 61 | |
| 62 | ~BorrowedSocket() { |
| 63 | if (socket_pool_ != nullptr) |
| 64 | socket_pool_->Return(std::move(fd_)); |
| 65 | } |
| 66 | |
| 67 | int operator*() { return get(); } |
| 68 | |
| 69 | int get() { return *fd_; } |
| 70 | |
| 71 | void Close() { fd_.reset(); } |
| 72 | |
| 73 | private: |
| 74 | base::ScopedFile fd_; |
| 75 | SocketPool* socket_pool_ = nullptr; |
| 76 | }; |
| 77 | |
| 78 | // Cache for frees that have been observed. It is infeasible to send every |
| 79 | // free separately, so we batch and send the whole buffer once it is full. |
| 80 | class FreePage { |
| 81 | public: |
| 82 | // Add address to buffer. Flush if necessary using a socket borrowed from |
| 83 | // pool. |
| 84 | // Can be called from any thread. Must not hold mutex_.` |
| 85 | void Add(const uint64_t addr, uint64_t sequence_number, SocketPool* pool); |
| 86 | |
| 87 | private: |
| 88 | // Needs to be called holding mutex_. |
| 89 | void FlushLocked(SocketPool* pool); |
| 90 | |
| 91 | FreeMetadata free_page_; |
| 92 | std::mutex mutex_; |
| 93 | size_t offset_ = 0; |
| 94 | }; |
| 95 | |
| 96 | const char* GetThreadStackBase(); |
| 97 | |
| 98 | // This is created and owned by the malloc hooks. |
| 99 | class Client { |
| 100 | public: |
| 101 | Client(std::vector<base::ScopedFile> sockets); |
| 102 | Client(const std::string& sock_name, size_t conns); |
| 103 | void RecordMalloc(uint64_t alloc_size, uint64_t alloc_address); |
| 104 | void RecordFree(uint64_t alloc_address); |
| 105 | |
| 106 | private: |
| 107 | const char* GetStackBase(); |
| 108 | |
| 109 | SocketPool socket_pool_; |
| 110 | FreePage free_page_; |
| 111 | const char* main_thread_stack_base_ = nullptr; |
| 112 | std::atomic<uint64_t> sequence_number_{0}; |
| 113 | }; |
| 114 | |
Florian Mayer | 824274d | 2018-09-17 11:33:45 +0100 | [diff] [blame] | 115 | } // namespace perfetto |
| 116 | |
| 117 | #endif // SRC_PROFILING_MEMORY_CLIENT_H_ |