blob: 5b4a95ee704e3e63587ab89bf3c508c1d37da476 [file] [log] [blame]
Florian Mayer824274d2018-09-17 11:33:45 +01001/*
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>
Ryan Savitski9eb2d5b2019-04-25 15:02:31 +010021#include <sys/types.h>
Florian Mayeraf266022018-09-17 15:18:15 +010022
Ryan Savitskia502bde2019-02-26 21:34:03 +000023#include <atomic>
Florian Mayereff98042018-12-10 17:44:44 +000024#include <condition_variable>
Florian Mayer824274d2018-09-17 11:33:45 +010025#include <mutex>
26#include <vector>
27
Primiano Tucci2c5488f2019-06-01 03:27:28 +010028#include "perfetto/ext/base/unix_socket.h"
Ryan Savitskia502bde2019-02-26 21:34:03 +000029#include "src/profiling/memory/sampler.h"
Florian Mayer51950592019-03-06 20:05:15 +000030#include "src/profiling/memory/shared_ring_buffer.h"
Ryan Savitski646d3662019-04-08 12:53:29 +010031#include "src/profiling/memory/unhooked_allocator.h"
Florian Mayerb85a9382018-09-27 13:59:01 +010032#include "src/profiling/memory/wire_protocol.h"
Florian Mayer824274d2018-09-17 11:33:45 +010033
34namespace perfetto {
Florian Mayer8c0d0482018-10-22 14:23:40 +010035namespace profiling {
Florian Mayer824274d2018-09-17 11:33:45 +010036
Florian Mayerb85a9382018-09-27 13:59:01 +010037const char* GetThreadStackBase();
38
Florian Mayerdac31272019-02-26 12:07:02 +000039constexpr uint32_t kClientSockTimeoutMs = 1000;
Ryan Savitski14c12d62019-01-08 22:38:53 +000040
Ryan Savitskia502bde2019-02-26 21:34:03 +000041// Profiling client, used to sample and record the malloc/free family of calls,
42// and communicate the necessary state to a separate profiling daemon process.
43//
44// Created and owned by the malloc hooks.
45//
46// Methods of this class are thread-safe unless otherwise stated, in which case
47// the caller needs to synchronize calls behind a mutex or similar.
Ryan Savitski646d3662019-04-08 12:53:29 +010048//
49// Implementation warning: this class should not use any heap, as otherwise its
50// destruction would enter the possibly-hooked |free|, which can reference the
51// Client itself. If avoiding the heap is not possible, then look at using
52// UnhookedAllocator.
Florian Mayerb85a9382018-09-27 13:59:01 +010053class Client {
54 public:
Ryan Savitski4a783172019-03-21 19:27:12 +000055 // Returns a client that is ready for sampling allocations, using the given
56 // socket (which should already be connected to heapprofd).
57 //
58 // Returns a shared_ptr since that is how the client will ultimately be used,
Ryan Savitski646d3662019-04-08 12:53:29 +010059 // and to take advantage of std::allocate_shared putting the object & the
60 // control block in one block of memory.
61 static std::shared_ptr<Client> CreateAndHandshake(
62 base::UnixSocketRaw sock,
63 UnhookedAllocator<Client> unhooked_allocator);
64
Ryan Savitski4a783172019-03-21 19:27:12 +000065 static base::Optional<base::UnixSocketRaw> ConnectToHeapprofd(
66 const std::string& sock_name);
67
Florian Mayer20c4b3c2019-01-24 18:25:35 +000068 bool RecordMalloc(uint64_t alloc_size,
Florian Mayer9a7fb2b2018-10-17 18:17:40 +010069 uint64_t total_size,
70 uint64_t alloc_address);
Ryan Savitski4a783172019-03-21 19:27:12 +000071
72 // Add address to buffer of deallocations. Flushes the buffer if necessary.
Florian Mayer20c4b3c2019-01-24 18:25:35 +000073 bool RecordFree(uint64_t alloc_address);
Florian Mayerb85a9382018-09-27 13:59:01 +010074
Ryan Savitskia502bde2019-02-26 21:34:03 +000075 // Returns the number of bytes to assign to an allocation with the given
76 // |alloc_size|, based on the current sampling rate. A return value of zero
77 // means that the allocation should not be recorded. Not idempotent, each
78 // invocation mutates the sampler state.
79 //
80 // Not thread-safe.
81 size_t GetSampleSizeLocked(size_t alloc_size) {
Ryan Savitskia502bde2019-02-26 21:34:03 +000082 return sampler_.SampleSize(alloc_size);
83 }
84
Ryan Savitski646d3662019-04-08 12:53:29 +010085 // Public for std::allocate_shared. Use CreateAndHandshake() to create
86 // instances instead.
Ryan Savitski4a783172019-03-21 19:27:12 +000087 Client(base::UnixSocketRaw sock,
88 ClientConfiguration client_config,
89 SharedRingBuffer shmem,
90 Sampler sampler,
Ryan Savitski9eb2d5b2019-04-25 15:02:31 +010091 pid_t pid_at_creation,
Ryan Savitski4a783172019-03-21 19:27:12 +000092 const char* main_thread_stack_base);
93
Florian Mayer1fe77732018-10-02 17:25:53 +010094 ClientConfiguration client_config_for_testing() { return client_config_; }
95
Florian Mayerb85a9382018-09-27 13:59:01 +010096 private:
97 const char* GetStackBase();
Ryan Savitski549753e2019-03-15 15:52:37 +000098 // Flush the contents of free_batch_. Must hold free_batch_lock_.
99 bool FlushFreesLocked();
Florian Mayer754b57f2019-04-15 13:17:55 +0100100 bool SendControlSocketByte();
Florian Mayerd6bdb6f2019-05-03 17:53:58 +0100101 bool SendWireMessageWithRetriesIfBlocking(const WireMessage&);
102
103 // This is only valid for non-blocking sockets. This is when
104 // client_config_.block_client is true.
105 bool IsConnected();
Ryan Savitski549753e2019-03-15 15:52:37 +0000106
Florian Mayer1fe77732018-10-02 17:25:53 +0100107 ClientConfiguration client_config_;
Ryan Savitskia502bde2019-02-26 21:34:03 +0000108 // sampler_ operations are not thread-safe.
109 Sampler sampler_;
Florian Mayer51950592019-03-06 20:05:15 +0000110 base::UnixSocketRaw sock_;
Ryan Savitski549753e2019-03-15 15:52:37 +0000111
112 // Protected by free_batch_lock_.
113 FreeBatch free_batch_;
114 std::timed_mutex free_batch_lock_;
115
Ryan Savitski4a783172019-03-21 19:27:12 +0000116 const char* main_thread_stack_base_{nullptr};
Florian Mayerb85a9382018-09-27 13:59:01 +0100117 std::atomic<uint64_t> sequence_number_{0};
Florian Mayer51950592019-03-06 20:05:15 +0000118 SharedRingBuffer shmem_;
Ryan Savitski9eb2d5b2019-04-25 15:02:31 +0100119
120 // Used to detect (during the slow path) the situation where the process has
121 // forked during profiling, and is performing malloc operations in the child.
122 // In this scenario, we want to stop profiling in the child, as otherwise
123 // it'll proceed to write to the same shared buffer & control socket (with
124 // duplicate sequence ids).
125 const pid_t pid_at_creation_;
Florian Mayerb85a9382018-09-27 13:59:01 +0100126};
127
Florian Mayer8c0d0482018-10-22 14:23:40 +0100128} // namespace profiling
Florian Mayer824274d2018-09-17 11:33:45 +0100129} // namespace perfetto
130
131#endif // SRC_PROFILING_MEMORY_CLIENT_H_