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 | |
Ryan Savitski | a502bde | 2019-02-26 21:34:03 +0000 | [diff] [blame] | 22 | #include <atomic> |
Florian Mayer | eff9804 | 2018-12-10 17:44:44 +0000 | [diff] [blame] | 23 | #include <condition_variable> |
Florian Mayer | 824274d | 2018-09-17 11:33:45 +0100 | [diff] [blame] | 24 | #include <mutex> |
| 25 | #include <vector> |
| 26 | |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 27 | #include "perfetto/base/unix_socket.h" |
Ryan Savitski | a502bde | 2019-02-26 21:34:03 +0000 | [diff] [blame] | 28 | #include "src/profiling/memory/sampler.h" |
Florian Mayer | 5195059 | 2019-03-06 20:05:15 +0000 | [diff] [blame] | 29 | #include "src/profiling/memory/shared_ring_buffer.h" |
Ryan Savitski | 646d366 | 2019-04-08 12:53:29 +0100 | [diff] [blame^] | 30 | #include "src/profiling/memory/unhooked_allocator.h" |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame] | 31 | #include "src/profiling/memory/wire_protocol.h" |
Florian Mayer | 824274d | 2018-09-17 11:33:45 +0100 | [diff] [blame] | 32 | |
| 33 | namespace perfetto { |
Florian Mayer | 8c0d048 | 2018-10-22 14:23:40 +0100 | [diff] [blame] | 34 | namespace profiling { |
Florian Mayer | 824274d | 2018-09-17 11:33:45 +0100 | [diff] [blame] | 35 | |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame] | 36 | const char* GetThreadStackBase(); |
| 37 | |
Florian Mayer | dac3127 | 2019-02-26 12:07:02 +0000 | [diff] [blame] | 38 | constexpr uint32_t kClientSockTimeoutMs = 1000; |
Ryan Savitski | 14c12d6 | 2019-01-08 22:38:53 +0000 | [diff] [blame] | 39 | |
Ryan Savitski | a502bde | 2019-02-26 21:34:03 +0000 | [diff] [blame] | 40 | // Profiling client, used to sample and record the malloc/free family of calls, |
| 41 | // and communicate the necessary state to a separate profiling daemon process. |
| 42 | // |
| 43 | // Created and owned by the malloc hooks. |
| 44 | // |
| 45 | // Methods of this class are thread-safe unless otherwise stated, in which case |
| 46 | // the caller needs to synchronize calls behind a mutex or similar. |
Ryan Savitski | 646d366 | 2019-04-08 12:53:29 +0100 | [diff] [blame^] | 47 | // |
| 48 | // Implementation warning: this class should not use any heap, as otherwise its |
| 49 | // destruction would enter the possibly-hooked |free|, which can reference the |
| 50 | // Client itself. If avoiding the heap is not possible, then look at using |
| 51 | // UnhookedAllocator. |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame] | 52 | class Client { |
| 53 | public: |
Ryan Savitski | 4a78317 | 2019-03-21 19:27:12 +0000 | [diff] [blame] | 54 | // Returns a client that is ready for sampling allocations, using the given |
| 55 | // socket (which should already be connected to heapprofd). |
| 56 | // |
| 57 | // Returns a shared_ptr since that is how the client will ultimately be used, |
Ryan Savitski | 646d366 | 2019-04-08 12:53:29 +0100 | [diff] [blame^] | 58 | // and to take advantage of std::allocate_shared putting the object & the |
| 59 | // control block in one block of memory. |
| 60 | static std::shared_ptr<Client> CreateAndHandshake( |
| 61 | base::UnixSocketRaw sock, |
| 62 | UnhookedAllocator<Client> unhooked_allocator); |
| 63 | |
Ryan Savitski | 4a78317 | 2019-03-21 19:27:12 +0000 | [diff] [blame] | 64 | static base::Optional<base::UnixSocketRaw> ConnectToHeapprofd( |
| 65 | const std::string& sock_name); |
| 66 | |
Florian Mayer | 20c4b3c | 2019-01-24 18:25:35 +0000 | [diff] [blame] | 67 | bool RecordMalloc(uint64_t alloc_size, |
Florian Mayer | 9a7fb2b | 2018-10-17 18:17:40 +0100 | [diff] [blame] | 68 | uint64_t total_size, |
| 69 | uint64_t alloc_address); |
Ryan Savitski | 4a78317 | 2019-03-21 19:27:12 +0000 | [diff] [blame] | 70 | |
| 71 | // Add address to buffer of deallocations. Flushes the buffer if necessary. |
Florian Mayer | 20c4b3c | 2019-01-24 18:25:35 +0000 | [diff] [blame] | 72 | bool RecordFree(uint64_t alloc_address); |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame] | 73 | |
Ryan Savitski | a502bde | 2019-02-26 21:34:03 +0000 | [diff] [blame] | 74 | // Returns the number of bytes to assign to an allocation with the given |
| 75 | // |alloc_size|, based on the current sampling rate. A return value of zero |
| 76 | // means that the allocation should not be recorded. Not idempotent, each |
| 77 | // invocation mutates the sampler state. |
| 78 | // |
| 79 | // Not thread-safe. |
| 80 | size_t GetSampleSizeLocked(size_t alloc_size) { |
Ryan Savitski | a502bde | 2019-02-26 21:34:03 +0000 | [diff] [blame] | 81 | return sampler_.SampleSize(alloc_size); |
| 82 | } |
| 83 | |
Ryan Savitski | 646d366 | 2019-04-08 12:53:29 +0100 | [diff] [blame^] | 84 | // Public for std::allocate_shared. Use CreateAndHandshake() to create |
| 85 | // instances instead. |
Ryan Savitski | 4a78317 | 2019-03-21 19:27:12 +0000 | [diff] [blame] | 86 | Client(base::UnixSocketRaw sock, |
| 87 | ClientConfiguration client_config, |
| 88 | SharedRingBuffer shmem, |
| 89 | Sampler sampler, |
| 90 | const char* main_thread_stack_base); |
| 91 | |
Florian Mayer | 1fe7773 | 2018-10-02 17:25:53 +0100 | [diff] [blame] | 92 | ClientConfiguration client_config_for_testing() { return client_config_; } |
| 93 | |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame] | 94 | private: |
| 95 | const char* GetStackBase(); |
Ryan Savitski | 549753e | 2019-03-15 15:52:37 +0000 | [diff] [blame] | 96 | // Flush the contents of free_batch_. Must hold free_batch_lock_. |
| 97 | bool FlushFreesLocked(); |
| 98 | |
Florian Mayer | 1fe7773 | 2018-10-02 17:25:53 +0100 | [diff] [blame] | 99 | ClientConfiguration client_config_; |
Ryan Savitski | a502bde | 2019-02-26 21:34:03 +0000 | [diff] [blame] | 100 | // sampler_ operations are not thread-safe. |
| 101 | Sampler sampler_; |
Florian Mayer | 5195059 | 2019-03-06 20:05:15 +0000 | [diff] [blame] | 102 | base::UnixSocketRaw sock_; |
Ryan Savitski | 549753e | 2019-03-15 15:52:37 +0000 | [diff] [blame] | 103 | |
| 104 | // Protected by free_batch_lock_. |
| 105 | FreeBatch free_batch_; |
| 106 | std::timed_mutex free_batch_lock_; |
| 107 | |
Ryan Savitski | 4a78317 | 2019-03-21 19:27:12 +0000 | [diff] [blame] | 108 | const char* main_thread_stack_base_{nullptr}; |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame] | 109 | std::atomic<uint64_t> sequence_number_{0}; |
Florian Mayer | 5195059 | 2019-03-06 20:05:15 +0000 | [diff] [blame] | 110 | SharedRingBuffer shmem_; |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame] | 111 | }; |
| 112 | |
Florian Mayer | 8c0d048 | 2018-10-22 14:23:40 +0100 | [diff] [blame] | 113 | } // namespace profiling |
Florian Mayer | 824274d | 2018-09-17 11:33:45 +0100 | [diff] [blame] | 114 | } // namespace perfetto |
| 115 | |
| 116 | #endif // SRC_PROFILING_MEMORY_CLIENT_H_ |