blob: 93a262f2e4ff1c495468c35e48e614b4dd9dfd75 [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>
Florian Mayeraf266022018-09-17 15:18:15 +010021
Florian Mayer824274d2018-09-17 11:33:45 +010022#include <mutex>
23#include <vector>
24
25#include "perfetto/base/scoped_file.h"
Florian Mayerb85a9382018-09-27 13:59:01 +010026#include "src/profiling/memory/wire_protocol.h"
Florian Mayer824274d2018-09-17 11:33:45 +010027
28namespace perfetto {
29
Florian Mayerb85a9382018-09-27 13:59:01 +010030class BorrowedSocket;
Florian Mayer824274d2018-09-17 11:33:45 +010031
32class 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 Mayerb85a9382018-09-27 13:59:01 +010041 std::mutex mutex_;
Florian Mayer824274d2018-09-17 11:33:45 +010042 std::condition_variable cv_;
43 std::vector<base::ScopedFile> sockets_;
44 size_t available_sockets_;
Florian Mayeraf266022018-09-17 15:18:15 +010045 size_t dead_sockets_ = 0;
Florian Mayer824274d2018-09-17 11:33:45 +010046};
47
Florian Mayerb85a9382018-09-27 13:59:01 +010048// Socket borrowed from a SocketPool. Gets returned once it goes out of scope.
49class 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.
80class 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
96const char* GetThreadStackBase();
97
98// This is created and owned by the malloc hooks.
99class 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
Florian Mayer1fe77732018-10-02 17:25:53 +0100106 ClientConfiguration client_config_for_testing() { return client_config_; }
107
Florian Mayerb85a9382018-09-27 13:59:01 +0100108 private:
109 const char* GetStackBase();
110
Florian Mayer1fe77732018-10-02 17:25:53 +0100111 ClientConfiguration client_config_;
Florian Mayerb85a9382018-09-27 13:59:01 +0100112 SocketPool socket_pool_;
113 FreePage free_page_;
114 const char* main_thread_stack_base_ = nullptr;
115 std::atomic<uint64_t> sequence_number_{0};
116};
117
Florian Mayer824274d2018-09-17 11:33:45 +0100118} // namespace perfetto
119
120#endif // SRC_PROFILING_MEMORY_CLIENT_H_