File: storage_manager_impl.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (108 lines) | stat: -rw-r--r-- 4,022 bytes parent folder | download | duplicates (5)
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
// 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 CHROME_BROWSER_METRICS_STRUCTURED_STORAGE_MANAGER_IMPL_H_
#define CHROME_BROWSER_METRICS_STRUCTURED_STORAGE_MANAGER_IMPL_H_

#include <atomic>
#include <memory>

#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/task/sequenced_task_runner.h"
#include "components/metrics/metrics_scheduler.h"
#include "components/metrics/structured/flushed_map.h"
#include "components/metrics/structured/lib/event_buffer.h"
#include "components/metrics/structured/storage_manager.h"
#include "third_party/metrics_proto/structured_data.pb.h"

namespace metrics::structured {

// Configuration data needed to create a Storage Manager.
struct StorageManagerConfig {
  // The maximum number of bytes the in-memory events can consume.
  int64_t buffer_max_bytes;
  // The maximum number of bytes the disk events can consume.
  int64_t disk_max_bytes;
};

// Provides a storage of events for Structured Metrics that tries to maintain a
// maximum usage of resources. This is done by in-memory and disk
// resources having a max size. In-memory resource get written to disk and disk
// resources are deleted once the quota is exceeded. This to be used only on
// Ash ChromeOS.
//
// There is no initialization. Events may be recorded immediately.
class StorageManagerImpl : public StorageManager {
 public:
  explicit StorageManagerImpl(const StorageManagerConfig& config);

  // Storage Manager Impl constructor
  // |config| contains how different components are configured.
  // |events_path| path event buffers will be temporarily storing events.
  // |flush_dir| the directory used to write event buffers to disk.
  StorageManagerImpl(const StorageManagerConfig& config,
                     const base::FilePath& events_path,
                     const base::FilePath& flush_dir);

  ~StorageManagerImpl() override;

  // EventStorage:
  void AddEvent(StructuredEventProto event) override;
  ::google::protobuf::RepeatedPtrField<StructuredEventProto> TakeEvents()
      override;
  int RecordedEventsCount() const override;
  void Purge() override;
  void AddBatchEvents(
      const google::protobuf::RepeatedPtrField<StructuredEventProto>& events)
      override;

  // Gets the configuration for the StorageManager using the configuration of
  // the system.
  static StorageManagerConfig GetStorageManagerConfig();

 private:
  // Flushes |event_buffer_| and then adds |event| to |event_buffer_| after.
  void FlushAndAddEvent(StructuredEventProto&& event);

  // Flush |event_buffer_| to disk.
  void FlushBuffer();

  // Handles Flush errors and propagates key to |service_|.
  void OnFlushCompleted(base::expected<FlushedKey, FlushError> key);

  // Retrieves events from the in-memory buffer.
  void TakeFromInMemory(
      google::protobuf::RepeatedPtrField<StructuredEventProto>* output);

  // Retrieves events from disk. All events are loaded.
  void TakeFromDisk(
      google::protobuf::RepeatedPtrField<StructuredEventProto>* output);

  // Cleans up all on-disk events, |storage_service_| is notified of the
  // deletion with |reason|.
  void CleanupFlushed(DeleteReason reason);

  // Deletes on-disk events from |flushed_map_| until it is under the desired
  // quota.
  void DropFlushedUntilUnderQuota();

  // The configuration used to create |this|.
  StorageManagerConfig config_;

  // An EventBuffer to store events in-memory.
  std::unique_ptr<EventBuffer<StructuredEventProto>> event_buffer_;
  // Manages flushed events.
  FlushedMap flushed_map_;

  // Flag denoting if flushed events are being dropped.
  std::atomic_bool dropping_flushed_queued_ = false;

  scoped_refptr<base::SequencedTaskRunner> task_runner_;
  base::WeakPtrFactory<StorageManagerImpl> weak_factory_{this};
};
}  // namespace metrics::structured

#endif  // CHROME_BROWSER_METRICS_STRUCTURED_STORAGE_MANAGER_IMPL_H_