File: limited_layer_entropy_cost_tracker.h

package info (click to toggle)
chromium 140.0.7339.127-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,192,880 kB
  • sloc: cpp: 35,093,808; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,797; asm: 949,904; xml: 747,503; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,114; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (107 lines) | stat: -rw-r--r-- 4,456 bytes parent folder | download | duplicates (4)
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
// 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 COMPONENTS_VARIATIONS_LIMITED_LAYER_ENTROPY_COST_TRACKER_H_
#define COMPONENTS_VARIATIONS_LIMITED_LAYER_ENTROPY_COST_TRACKER_H_

#include <cstdint>

#include "base/component_export.h"
#include "base/gtest_prod_util.h"
#include "third_party/abseil-cpp/absl/container/flat_hash_map.h"

namespace variations {

class Layer;
class Study;

// Provides methods to calculate and monitor the total entropy used by studies
// assigned to a limited layer.
class COMPONENT_EXPORT(VARIATIONS) LimitedLayerEntropyCostTracker {
 public:
  // Tracks the entropy consumed by studies that reference the given layer
  // (i.e. studies the caller passes to AddEntropyUsedByStudy()).
  //
  //   * layer - The layer whose entropy is being tracked.
  //   * entropy_limit_in_bits - The maximum allowed entropy limit for any
  //   member of the limited layer.
  //
  // The tracker expects that the layer and study data passed to its constructor
  // and methods are valid. However, as this data comes from external sources
  // (i.e. the seed), the tracker performs additional validation on its input
  // and will DumpWithoutCrashing() to log the stack trace by which the invalid
  // input was provided. If this occurs, the tracker will be invalidated and
  // the seed from which the tracker is derived should be rejected.
  LimitedLayerEntropyCostTracker(const Layer& layer,
                                 double entropy_limit_in_bits);
  ~LimitedLayerEntropyCostTracker();

  LimitedLayerEntropyCostTracker(const LimitedLayerEntropyCostTracker&) =
      delete;
  LimitedLayerEntropyCostTracker& operator=(
      const LimitedLayerEntropyCostTracker&) = delete;

  // Returns true if the tracker is valid.
  bool IsValid() const { return is_valid_; }

  // Calculates the entropy used by the study and adds it to the total entropy
  // used by the layer. This method returns true if there is enough entropy
  // remaining to handle the study assignment or if the study does not consume
  // entropy on the limited layer.
  //
  // Note that this expects the study to be assigned to the same limited layer
  // given in the constructor.
  bool AddEntropyUsedByStudy(const Study& study);

  // Returns the maximum member-level entropy used by studies currently assigned
  // to the limited layer.
  double GetMaxEntropyUsedForTesting() const;

  // Returns true if the total entropy currently used by the limited layer is
  // over the allowed entropy limit.
  bool IsEntropyLimitExceeded() const { return entropy_limit_exceeded_; }

 private:
  FRIEND_TEST_ALL_PREFIXES(LimitedLayerEntropyCostTrackerTest,
                           TestConstructor_WithLimitedLayer);
  FRIEND_TEST_ALL_PREFIXES(LimitedLayerEntropyCostTrackerTest,
                           TestConstructor_LayerMembersUsingEntropyAboveLimit);
  FRIEND_TEST_ALL_PREFIXES(LimitedLayerEntropyCostTrackerTest,
                           TestAddEntropyUsedByStudy_MultipleStudies);
  FRIEND_TEST_ALL_PREFIXES(
      LimitedLayerEntropyCostTrackerTest,
      TestAddEntropyUsedByStudy_LaunchedAndActiveStudies);

  // Invalidates the tracker on bad input. Note that this is a terminal state
  // for the tracker. Once the tracker is invalidated, it cannot be made valid
  // again.
  void Invalidate();

  // The maximum allowed entropy limit for any member of the limited layer.
  const double entropy_limit_in_bits_;

  // ID of the active limited layer for this client. This is used to sanity
  // check the studies whose entropy is being tracked (they should all refer
  // to the same limited layer ID).
  const uint32_t limited_layer_id_;

  // Entropy used by each layer member keyed by its ID.
  absl::flat_hash_map<uint32_t, double> entropy_used_by_member_id_;

  // Whether the entropy limit has been exceeded.
  bool entropy_limit_exceeded_ = false;

  // Whether the tracker has had non-zero entropy added for at least one study.
  // i.e., the entropy is not solely based on the layer member sizes.
  bool includes_study_entropy_ = false;

  // Whether all input given to the tracker has been valid. If the tracker is
  // invalidated by bad input, the seed from which the input is derived should
  // be rejected.
  bool is_valid_ = true;
};

}  // namespace variations

#endif  // COMPONENTS_VARIATIONS_LIMITED_LAYER_ENTROPY_COST_TRACKER_H_