/Users/deen/code/yugabyte-db/src/yb/rocksdb/db/writebuffer.h
Line | Count | Source |
1 | | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | | // This source code is licensed under the BSD-style license found in the |
3 | | // LICENSE file in the root directory of this source tree. An additional grant |
4 | | // of patent rights can be found in the PATENTS file in the same directory. |
5 | | // |
6 | | // The following only applies to changes made to this file as part of YugaByte development. |
7 | | // |
8 | | // Portions Copyright (c) YugaByte, Inc. |
9 | | // |
10 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
11 | | // in compliance with the License. You may obtain a copy of the License at |
12 | | // |
13 | | // http://www.apache.org/licenses/LICENSE-2.0 |
14 | | // |
15 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
16 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
17 | | // or implied. See the License for the specific language governing permissions and limitations |
18 | | // under the License. |
19 | | // |
20 | | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. |
21 | | // Use of this source code is governed by a BSD-style license that can be |
22 | | // found in the LICENSE file. See the AUTHORS file for names of contributors. |
23 | | // |
24 | | // WriteBuffer is for managing memory allocation for one or more MemTables. |
25 | | |
26 | | #pragma once |
27 | | |
28 | | #include <atomic> |
29 | | #include <memory> |
30 | | |
31 | | #include "yb/rocksdb/memory_monitor.h" |
32 | | |
33 | | namespace rocksdb { |
34 | | |
35 | | class WriteBuffer { |
36 | | public: |
37 | | WriteBuffer(size_t _buffer_size, |
38 | | std::shared_ptr<MemoryMonitor> memory_monitor = nullptr) |
39 | 343k | : buffer_size_(_buffer_size), memory_monitor_(memory_monitor) {} |
40 | | |
41 | 324k | ~WriteBuffer() {} |
42 | | |
43 | 26.6k | size_t memory_usage() const { |
44 | 26.6k | return memory_used_.load(std::memory_order_relaxed); |
45 | 26.6k | } |
46 | 22.7M | size_t buffer_size() const { return buffer_size_; } |
47 | | |
48 | | // Should only be called from write thread |
49 | 22.7M | bool ShouldFlush() const { |
50 | 22.7M | return buffer_size() > 0 && memory_usage() >= buffer_size(); |
51 | 22.7M | } |
52 | | |
53 | | // Should only be called from write thread |
54 | 152M | void ReserveMem(size_t mem) { |
55 | 152M | memory_used_.fetch_add(mem, std::memory_order_relaxed); |
56 | 152M | if (memory_monitor_) { |
57 | 113M | memory_monitor_->ReservedMem(mem); |
58 | 113M | } |
59 | 152M | } |
60 | 361k | void FreeMem(size_t mem) { |
61 | 361k | memory_used_.fetch_sub(mem, std::memory_order_relaxed); |
62 | 361k | if (memory_monitor_) { |
63 | 311k | memory_monitor_->FreedMem(mem); |
64 | 311k | } |
65 | 361k | } |
66 | | |
67 | | private: |
68 | | const size_t buffer_size_; |
69 | | std::atomic<size_t> memory_used_{0}; |
70 | | std::shared_ptr<MemoryMonitor> memory_monitor_; |
71 | | |
72 | | // No copying allowed |
73 | | WriteBuffer(const WriteBuffer&); |
74 | | void operator=(const WriteBuffer&); |
75 | | }; |
76 | | |
77 | | } // namespace rocksdb |