File: sample_map_iterator.h

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (111 lines) | stat: -rw-r--r-- 3,164 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_METRICS_SAMPLE_MAP_ITERATOR_H_
#define BASE_METRICS_SAMPLE_MAP_ITERATOR_H_

#include <stdint.h>

#include <atomic>
#include <type_traits>
#include <utility>

#include "base/check.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram_base.h"
#include "base/metrics/histogram_samples.h"
#include "base/types/to_address.h"

namespace base {

// An iterator for going through a SampleMap. `MapT` is the underlying map type
// that stores the counts. `support_extraction` should be true iff the caller
// wants this iterator to support extracting the values. If the counts are
// pointers, accesses to them will be atomic; see `kUseAtomicOps` below.
template <typename MapT, bool support_extraction>
class SampleMapIterator : public SampleCountIterator {
 private:
  using T = std::conditional_t<support_extraction, MapT, const MapT>;

 public:
  explicit SampleMapIterator(T& sample_counts)
      : iter_(sample_counts.begin()), end_(sample_counts.end()) {
    SkipEmptyBuckets();
  }

  ~SampleMapIterator() override {
    if constexpr (support_extraction) {
      // Ensure that the user has consumed all the samples in order to ensure no
      // samples are lost.
      DCHECK(Done());
    }
  }

  // SampleCountIterator:
  bool Done() const override { return iter_ == end_; }

  void Next() override {
    DCHECK(!Done());
    ++iter_;
    SkipEmptyBuckets();
  }

  void Get(HistogramBase::Sample32* min,
           int64_t* max,
           HistogramBase::Count32* count) override {
    DCHECK(!Done());
    *min = iter_->first;
    *max = int64_t{iter_->first} + 1;
    if constexpr (support_extraction) {
      *count = Exchange();
    } else {
      *count = Load();
    }
  }

 private:
  using I = std::conditional_t<support_extraction,
                               typename T::iterator,
                               typename T::const_iterator>;

  // If the counts are pointers, assume they may live in shared memory, which
  // means accesses to them must be atomic, since other processes may attempt to
  // concurrently modify their values. (Note that a lock wouldn't help here,
  // since said other processes would not be aware of our lock.) If they are
  // values, we don't bother with atomic ops; callers who want thread-safety can
  // use locking.
  static constexpr bool kUseAtomicOps =
      IsPointerOrRawPtr<typename T::mapped_type>;

  void SkipEmptyBuckets() {
    while (!Done() && Load() == 0) {
      ++iter_;
    }
  }

  HistogramBase::Count32 Load() const {
    if constexpr (kUseAtomicOps) {
      return iter_->second->load(std::memory_order_relaxed);
    } else {
      return iter_->second;
    }
  }

  HistogramBase::Count32 Exchange() const
    requires support_extraction
  {
    if constexpr (kUseAtomicOps) {
      return iter_->second->exchange(0, std::memory_order_relaxed);
    } else {
      return std::exchange(iter_->second, 0);
    }
  }

  I iter_;
  const I end_;
};

}  // namespace base

#endif  // BASE_METRICS_SAMPLE_MAP_ITERATOR_H_