blob: 7a4e459ad1adc3281e0f2716703413da4da90861 [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"
26
27namespace perfetto {
28
29class SocketPool;
30
Florian Mayeraf266022018-09-17 15:18:15 +010031class FreePage {
32 public:
33 FreePage();
34
35 // Can be called from any thread. Must not hold mtx_.`
36 void Add(const uint64_t addr, SocketPool* pool);
37
38 private:
39 // Needs to be called holding mtx_.
40 void Flush(SocketPool* pool);
41
42 std::vector<uint64_t> free_page_;
43 std::mutex mtx_;
44 size_t offset_;
45};
46
Florian Mayer824274d2018-09-17 11:33:45 +010047class BorrowedSocket {
48 public:
49 BorrowedSocket(const BorrowedSocket&) = delete;
50 BorrowedSocket& operator=(const BorrowedSocket&) = delete;
51 BorrowedSocket(BorrowedSocket&& other) {
52 fd_ = std::move(other.fd_);
53 socket_pool_ = other.socket_pool_;
54 other.socket_pool_ = nullptr;
55 }
56
57 BorrowedSocket(base::ScopedFile fd, SocketPool* socket_pool);
58 int operator*();
59 int get();
60 void Close();
61 ~BorrowedSocket();
62
63 private:
64 base::ScopedFile fd_;
65 SocketPool* socket_pool_ = nullptr;
66};
67
68class SocketPool {
69 public:
70 friend class BorrowedSocket;
71 SocketPool(std::vector<base::ScopedFile> sockets);
72
73 BorrowedSocket Borrow();
74
75 private:
76 void Return(base::ScopedFile fd);
77 std::mutex mtx_;
78 std::condition_variable cv_;
79 std::vector<base::ScopedFile> sockets_;
80 size_t available_sockets_;
Florian Mayeraf266022018-09-17 15:18:15 +010081 size_t dead_sockets_ = 0;
Florian Mayer824274d2018-09-17 11:33:45 +010082};
83
84} // namespace perfetto
85
86#endif // SRC_PROFILING_MEMORY_CLIENT_H_