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" |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame] | 30 | #include "src/profiling/memory/wire_protocol.h" |
Florian Mayer | 824274d | 2018-09-17 11:33:45 +0100 | [diff] [blame] | 31 | |
| 32 | namespace perfetto { |
Florian Mayer | 8c0d048 | 2018-10-22 14:23:40 +0100 | [diff] [blame] | 33 | namespace profiling { |
Florian Mayer | 824274d | 2018-09-17 11:33:45 +0100 | [diff] [blame] | 34 | |
Florian Mayer | 5195059 | 2019-03-06 20:05:15 +0000 | [diff] [blame^] | 35 | class Client; |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame] | 36 | |
| 37 | // Cache for frees that have been observed. It is infeasible to send every |
| 38 | // free separately, so we batch and send the whole buffer once it is full. |
| 39 | class FreePage { |
| 40 | public: |
Florian Mayer | 5195059 | 2019-03-06 20:05:15 +0000 | [diff] [blame^] | 41 | FreePage() { free_page_.num_entries = 0; } |
Florian Mayer | 192136e | 2019-01-30 18:04:34 +0000 | [diff] [blame] | 42 | |
Florian Mayer | 5195059 | 2019-03-06 20:05:15 +0000 | [diff] [blame^] | 43 | // Add address to buffer. Flush if necessary. |
Ryan Savitski | a502bde | 2019-02-26 21:34:03 +0000 | [diff] [blame] | 44 | // Can be called from any thread. Must not hold mutex_. |
Florian Mayer | 5195059 | 2019-03-06 20:05:15 +0000 | [diff] [blame^] | 45 | bool Add(const uint64_t addr, uint64_t sequence_number, Client* client); |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame] | 46 | |
| 47 | private: |
Florian Mayer | 5195059 | 2019-03-06 20:05:15 +0000 | [diff] [blame^] | 48 | // TODO(fmayer): Sort out naming. It's confusing data FreePage has a member |
| 49 | // called free_page_ that is of type FreeMetadata. |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame] | 50 | FreeMetadata free_page_; |
Florian Mayer | 6508157 | 2018-12-18 13:41:08 +0000 | [diff] [blame] | 51 | std::timed_mutex mutex_; |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | const char* GetThreadStackBase(); |
| 55 | |
Florian Mayer | dac3127 | 2019-02-26 12:07:02 +0000 | [diff] [blame] | 56 | constexpr uint32_t kClientSockTimeoutMs = 1000; |
Ryan Savitski | 14c12d6 | 2019-01-08 22:38:53 +0000 | [diff] [blame] | 57 | |
Ryan Savitski | a502bde | 2019-02-26 21:34:03 +0000 | [diff] [blame] | 58 | // Profiling client, used to sample and record the malloc/free family of calls, |
| 59 | // and communicate the necessary state to a separate profiling daemon process. |
| 60 | // |
| 61 | // Created and owned by the malloc hooks. |
| 62 | // |
| 63 | // Methods of this class are thread-safe unless otherwise stated, in which case |
| 64 | // the caller needs to synchronize calls behind a mutex or similar. |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame] | 65 | class Client { |
| 66 | public: |
Florian Mayer | 5195059 | 2019-03-06 20:05:15 +0000 | [diff] [blame^] | 67 | Client(base::Optional<base::UnixSocketRaw> sock); |
| 68 | Client(const std::string& sock_name); |
Florian Mayer | 20c4b3c | 2019-01-24 18:25:35 +0000 | [diff] [blame] | 69 | bool RecordMalloc(uint64_t alloc_size, |
Florian Mayer | 9a7fb2b | 2018-10-17 18:17:40 +0100 | [diff] [blame] | 70 | uint64_t total_size, |
| 71 | uint64_t alloc_address); |
Florian Mayer | 20c4b3c | 2019-01-24 18:25:35 +0000 | [diff] [blame] | 72 | bool RecordFree(uint64_t alloc_address); |
Florian Mayer | 2da2841 | 2018-10-30 17:33:10 +0000 | [diff] [blame] | 73 | void Shutdown(); |
Florian Mayer | 5195059 | 2019-03-06 20:05:15 +0000 | [diff] [blame^] | 74 | bool FlushFrees(FreeMetadata* free_metadata); |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame] | 75 | |
Ryan Savitski | a502bde | 2019-02-26 21:34:03 +0000 | [diff] [blame] | 76 | // Returns the number of bytes to assign to an allocation with the given |
| 77 | // |alloc_size|, based on the current sampling rate. A return value of zero |
| 78 | // means that the allocation should not be recorded. Not idempotent, each |
| 79 | // invocation mutates the sampler state. |
| 80 | // |
| 81 | // Not thread-safe. |
| 82 | size_t GetSampleSizeLocked(size_t alloc_size) { |
| 83 | if (!inited_.load(std::memory_order_acquire)) |
| 84 | return 0; |
| 85 | return sampler_.SampleSize(alloc_size); |
| 86 | } |
| 87 | |
Florian Mayer | 1fe7773 | 2018-10-02 17:25:53 +0100 | [diff] [blame] | 88 | ClientConfiguration client_config_for_testing() { return client_config_; } |
Florian Mayer | b12c4f2 | 2018-11-14 16:13:02 +0000 | [diff] [blame] | 89 | bool inited() { return inited_; } |
Florian Mayer | 1fe7773 | 2018-10-02 17:25:53 +0100 | [diff] [blame] | 90 | |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame] | 91 | private: |
| 92 | const char* GetStackBase(); |
| 93 | |
Ryan Savitski | a502bde | 2019-02-26 21:34:03 +0000 | [diff] [blame] | 94 | // TODO(rsavitski): used to check if the client is completely initialized |
| 95 | // after construction. The reads in RecordFree & GetSampleSizeLocked are no |
| 96 | // longer necessary (was an optimization to not do redundant work after |
| 97 | // shutdown). Turn into a normal bool, or indicate construction failures |
| 98 | // differently. |
Florian Mayer | f086799 | 2018-11-16 10:12:18 +0000 | [diff] [blame] | 99 | std::atomic<bool> inited_{false}; |
Florian Mayer | 1fe7773 | 2018-10-02 17:25:53 +0100 | [diff] [blame] | 100 | ClientConfiguration client_config_; |
Ryan Savitski | a502bde | 2019-02-26 21:34:03 +0000 | [diff] [blame] | 101 | // sampler_ operations are not thread-safe. |
| 102 | Sampler sampler_; |
Florian Mayer | 5195059 | 2019-03-06 20:05:15 +0000 | [diff] [blame^] | 103 | base::UnixSocketRaw sock_; |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame] | 104 | FreePage free_page_; |
| 105 | const char* main_thread_stack_base_ = nullptr; |
| 106 | std::atomic<uint64_t> sequence_number_{0}; |
Florian Mayer | 5195059 | 2019-03-06 20:05:15 +0000 | [diff] [blame^] | 107 | SharedRingBuffer shmem_; |
Florian Mayer | b85a938 | 2018-09-27 13:59:01 +0100 | [diff] [blame] | 108 | }; |
| 109 | |
Florian Mayer | 8c0d048 | 2018-10-22 14:23:40 +0100 | [diff] [blame] | 110 | } // namespace profiling |
Florian Mayer | 824274d | 2018-09-17 11:33:45 +0100 | [diff] [blame] | 111 | } // namespace perfetto |
| 112 | |
| 113 | #endif // SRC_PROFILING_MEMORY_CLIENT_H_ |