YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/util/high_water_mark.h
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
//
18
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
#ifndef YB_UTIL_HIGH_WATER_MARK_H
33
#define YB_UTIL_HIGH_WATER_MARK_H
34
35
#include <atomic>
36
37
#include "yb/gutil/macros.h"
38
39
namespace yb {
40
41
// Lock-free integer that keeps track of the highest value seen.
42
// Similar to Impala's RuntimeProfile::HighWaterMarkCounter.
43
// HighWaterMark::max_value() returns the highest value seen;
44
// HighWaterMark::current_value() returns the current value.
45
class HighWaterMark {
46
 public:
47
  explicit HighWaterMark(int64_t initial_value)
48
    : current_value_(initial_value),
49
1.75M
      max_value_(initial_value) {
50
1.75M
  }
51
52
  // Return the current value.
53
1.00G
  int64_t current_value() const {
54
1.00G
    return current_value_.load(std::memory_order_acquire);
55
1.00G
  }
56
57
  // Return the max value.
58
94
  int64_t max_value() const {
59
94
    return max_value_.load(std::memory_order_acquire);
60
94
  }
61
62
  // If current value + 'delta' is <= 'max', increment current value
63
  // by 'delta' and return true; return false otherwise.
64
20.0M
  bool TryIncrementBy(int64_t delta, int64_t max) {
65
20.0M
    while (true) {
66
20.0M
      int64_t old_val = current_value();
67
20.0M
      int64_t new_val = old_val + delta;
68
20.0M
      if (new_val > max) {
69
4.25k
        return false;
70
4.25k
      }
71
20.0M
      if (PREDICT_TRUE(current_value_.compare_exchange_weak(
72
20.0M
          old_val, new_val, std::memory_order_acq_rel))) {
73
20.0M
        UpdateMax(new_val);
74
20.0M
        return true;
75
20.0M
      }
76
20.0M
    }
77
20.0M
  }
78
79
990M
  void IncrementBy(int64_t amount) {
80
990M
    UpdateMax(current_value_.fetch_add(amount, std::memory_order_acq_rel) + amount);
81
990M
  }
82
83
0
  void set_value(int64_t v) {
84
0
    current_value_.store(v, std::memory_order_release);
85
0
    UpdateMax(v);
86
0
  }
87
88
 private:
89
1.01G
  void UpdateMax(int64_t value) {
90
1.01G
    int64_t old_value = max_value_.load(std::memory_order_acquire);
91
1.01G
    while (old_value < value) {
92
7.50M
      if (max_value_.compare_exchange_weak(old_value, value, std::memory_order_acq_rel)) {
93
7.49M
        break;
94
7.49M
      }
95
7.50M
    }
96
1.01G
  }
97
98
  std::atomic<int64_t> current_value_;
99
  std::atomic<int64_t> max_value_;
100
};
101
102
} // namespace yb
103
#endif /* YB_UTIL_HIGH_WATER_MARK_H */