File: limited_layer_entropy_cost_tracker.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 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 (92 lines) | stat: -rw-r--r-- 3,941 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
// 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 <map>
#include <set>

#include "base/component_export.h"
#include "base/gtest_prod_util.h"

namespace variations {

class Study;
class VariationsLayers;
class VariationsSeed;

// Provides methods to calculate and monitor the total entropy used by studies
// assigned to a limited layer.
class COMPONENT_EXPORT(VARIATIONS) LimitedLayerEntropyCostTracker {
 public:
  LimitedLayerEntropyCostTracker(const VariationsLayers& layers,
                                 const VariationsSeed& seed,
                                 double entropy_limit_in_bits);
  ~LimitedLayerEntropyCostTracker();

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

  // 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.
  bool TryAddEntropyUsedByStudy(const Study& study);

  // Returns the total entropy used by studies currently assigned to the limited
  // layer.
  double GetTotalEntropyUsedForTesting();

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

 private:
  FRIEND_TEST_ALL_PREFIXES(LimitedLayerEntropyCostTrackerTest,
                           TestConstructor_WithNoLimitedLayer);
  FRIEND_TEST_ALL_PREFIXES(LimitedLayerEntropyCostTrackerTest,
                           TestConstructor_WithNonLimitedEntropyMode);
  FRIEND_TEST_ALL_PREFIXES(LimitedLayerEntropyCostTrackerTest,
                           TestConstructor_WithNoLayerMembers);
  FRIEND_TEST_ALL_PREFIXES(LimitedLayerEntropyCostTrackerTest,
                           TestConstructor_WithManyLimitedLayers);
  FRIEND_TEST_ALL_PREFIXES(LimitedLayerEntropyCostTrackerTest,
                           TestConstructor_WithMisconfiguredLimitedLayer);
  FRIEND_TEST_ALL_PREFIXES(LimitedLayerEntropyCostTrackerTest,
                           TestConstructor_WithLimitedLayer);
  FRIEND_TEST_ALL_PREFIXES(LimitedLayerEntropyCostTrackerTest,
                           TestConstructor_LayerMembersUsingEntropyAboveLimit);

  static constexpr uint64_t kInvalidLayerId = 0;

  // Id of the active limited layer for this client. A layer is "active" for a
  // client if the client's slot for that layer is associated with a layer
  // member.
  uint32_t active_limited_layer_id_ = kInvalidLayerId;
  const double entropy_limit_in_bits_;
  bool includes_entropy_used_by_studies_ = false;
  bool entropy_limit_reached_ = false;

  // Entropy used by each layer member keyed by its ID. Using uint32_t as the
  // key type since the ID of a layer member proto is a uint32_t.
  std::map<uint32_t, double> entropy_used_by_layer_members_;

  // Contains the Ids of all limited layers in the seed. This field is used in
  // `TryAddEntropyUsedByStudy` to check if a study references a limited layer
  // because the Study class does not contain enough information to perform that
  // check.
  // TODO(siakabaro): If a study references a layer, cache the layer information
  // in the ProcessedStudy class and update `TryAddEntropyUsedByStudy` to take a
  // ProcessedStudy object as input instead of a Study object. Remove this field
  // after the update.
  std::set<uint32_t> limited_layers_ids_;
};

}  // namespace variations

#endif  // COMPONENTS_VARIATIONS_LIMITED_LAYER_ENTROPY_COST_TRACKER_H_