Coverage Report

Created: 2024-09-19 09:45

/proc/self/cwd/source/common/stats/recent_lookups.h
Line
Count
Source (jump to first uncovered line)
1
#pragma once
2
3
#include <functional>
4
#include <list>
5
#include <utility>
6
7
#include "absl/container/flat_hash_map.h"
8
#include "absl/strings/string_view.h"
9
10
namespace Envoy {
11
namespace Stats {
12
13
// Remembers the last 'Capacity' items passed to lookup().
14
class RecentLookups {
15
public:
16
  /**
17
   * Records a lookup of a string. Only the last 'Capacity' lookups are remembered.
18
   *
19
   * @param str the item being looked up.
20
   */
21
  void lookup(absl::string_view str);
22
23
  using IterFn = std::function<void(absl::string_view, uint64_t)>;
24
25
  /**
26
   * Calls fn(item, count) for each of the remembered lookups.
27
   *
28
   * @param fn The function to call for every recently looked up item.
29
   */
30
  void forEach(const IterFn& fn) const;
31
32
  /**
33
   * @return the total number of lookups since tracking began.
34
   */
35
5.00k
  uint64_t total() const { return total_; }
36
37
  /**
38
   * Clears out all contents.
39
   */
40
0
  void clear() {
41
0
    total_ = 0;
42
0
    map_.clear();
43
0
    list_.clear();
44
0
  }
45
46
  /**
47
   * Controls the maximum number of recent lookups to remember. If set to 0,
48
   * then only lookup counts is tracked.
49
   * @param capacity The number of lookups to remember.
50
   */
51
  void setCapacity(uint64_t capacity);
52
53
  /**
54
   * @return The configured capacity.
55
   */
56
0
  uint64_t capacity() const { return capacity_; }
57
58
private:
59
  void evictOne();
60
61
  struct ItemCount {
62
    std::string item_;
63
    uint64_t count_;
64
  };
65
  using List = std::list<ItemCount>;
66
  List list_;
67
68
  // TODO(jmarantz): we could make this more compact by making this a set of
69
  // list-iterators with heterogeneous hash/compare functors.
70
  using Map = absl::flat_hash_map<absl::string_view, List::iterator>;
71
  Map map_;
72
  uint64_t total_{0};
73
  uint64_t capacity_{0};
74
};
75
76
} // namespace Stats
77
} // namespace Envoy