YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/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
437k
    : buffer_size_(_buffer_size), memory_monitor_(memory_monitor) {}
40
41
398k
  ~WriteBuffer() {}
42
43
26.9k
  size_t memory_usage() const {
44
26.9k
    return memory_used_.load(std::memory_order_relaxed);
45
26.9k
  }
46
28.8M
  size_t buffer_size() const { return buffer_size_; }
47
48
  // Should only be called from write thread
49
28.7M
  bool ShouldFlush() const {
50
28.7M
    return buffer_size() > 0 && 
memory_usage() >= buffer_size()25.4k
;
51
28.7M
  }
52
53
  // Should only be called from write thread
54
401M
  void ReserveMem(size_t mem) {
55
401M
    memory_used_.fetch_add(mem, std::memory_order_relaxed);
56
401M
    if (memory_monitor_) {
57
360M
      memory_monitor_->ReservedMem(mem);
58
360M
    }
59
401M
  }
60
438k
  void FreeMem(size_t mem) {
61
438k
    memory_used_.fetch_sub(mem, std::memory_order_relaxed);
62
438k
    if (memory_monitor_) {
63
385k
      memory_monitor_->FreedMem(mem);
64
385k
    }
65
438k
  }
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