YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/server/total_mem_watcher.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) YugaByte, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4
// in compliance with the License.  You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software distributed under the License
9
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10
// or implied.  See the License for the specific language governing permissions and limitations
11
// under the License.
12
//
13
14
#ifndef YB_SERVER_TOTAL_MEM_WATCHER_H
15
#define YB_SERVER_TOTAL_MEM_WATCHER_H
16
17
#include <functional>
18
#include <memory>
19
20
#include "yb/gutil/macros.h"
21
22
#include "yb/util/status_fwd.h"
23
24
namespace yb {
25
namespace server {
26
27
// Allows to monitor total memory usage (resident set size, or RSS) of the current process as seen
28
// by the operating system, and decide whether the process should exit if it allocates too much
29
// memory.
30
//
31
// This class is not thread-safe.
32
class TotalMemWatcher {
33
 public:
34
  virtual ~TotalMemWatcher();
35
36
  // Re-check the total memory usage and populate the internal state of this watcher object.
37
  virtual CHECKED_STATUS Check() = 0;
38
39
  // Determines whether the program should terminate based on the most recent check. Returns a
40
  // non-empty string with the termination reason if the program should terminate, or or an empty
41
  // string if it should not.
42
130k
  virtual std::string GetTerminationExplanation() { return std::string(); }
43
44
  // Returns a human-readable representation of memory usage details to be printed before program
45
  // terminates due to exceeding the memory limit.
46
0
  virtual std::string GetMemoryUsageDetails() { return "N/A"; }
47
48
  static std::unique_ptr<TotalMemWatcher> Create();
49
50
  // Enters an infinite loop monitoring memory usage. Exits from the loop if the memory usage limit
51
  // has been exceeded. Uses the provided functions to initiate server shutdown and check if
52
  // shutdown has completed.
53
  void MemoryMonitoringLoop(std::function<void()> shutdown_fn,
54
                            std::function<bool()> is_shutdown_finished_fn);
55
56
 protected:
57
  TotalMemWatcher();
58
59
  size_t rss_termination_limit_bytes_;
60
61
 private:
62
  DISALLOW_COPY_AND_ASSIGN(TotalMemWatcher);
63
};
64
65
}  // namespace server
66
}  // namespace yb
67
68
#endif  // YB_SERVER_TOTAL_MEM_WATCHER_H