xref: /aosp_15_r20/external/cronet/components/metrics/persistent_histograms.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2018 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker 
5*6777b538SAndroid Build Coastguard Worker #ifndef COMPONENTS_METRICS_PERSISTENT_HISTOGRAMS_H_
6*6777b538SAndroid Build Coastguard Worker #define COMPONENTS_METRICS_PERSISTENT_HISTOGRAMS_H_
7*6777b538SAndroid Build Coastguard Worker 
8*6777b538SAndroid Build Coastguard Worker #include <string>
9*6777b538SAndroid Build Coastguard Worker 
10*6777b538SAndroid Build Coastguard Worker #include "base/feature_list.h"
11*6777b538SAndroid Build Coastguard Worker #include "base/files/file_path.h"
12*6777b538SAndroid Build Coastguard Worker #include "base/metrics/field_trial_params.h"
13*6777b538SAndroid Build Coastguard Worker #include "base/strings/string_piece.h"
14*6777b538SAndroid Build Coastguard Worker 
15*6777b538SAndroid Build Coastguard Worker // Feature definition for enabling histogram persistence. Note that this feature
16*6777b538SAndroid Build Coastguard Worker // (along with its param `kPersistentHistogramsStorage`, declared below) is not
17*6777b538SAndroid Build Coastguard Worker // used for Chrome on Linux, ChromeOS, Windows, macOS, and Android. Instead,
18*6777b538SAndroid Build Coastguard Worker // histograms are persisted to a memory-mapped file, and set up before field
19*6777b538SAndroid Build Coastguard Worker // trial initialization (see //chrome/app/chrome_main_delegate.cc).
20*6777b538SAndroid Build Coastguard Worker BASE_DECLARE_FEATURE(kPersistentHistogramsFeature);
21*6777b538SAndroid Build Coastguard Worker 
22*6777b538SAndroid Build Coastguard Worker // If `kPersistentHistogramsStorage` is set to this, histograms will be
23*6777b538SAndroid Build Coastguard Worker // allocated in a memory region backed by a file.
24*6777b538SAndroid Build Coastguard Worker extern const char kPersistentHistogramStorageMappedFile[];
25*6777b538SAndroid Build Coastguard Worker 
26*6777b538SAndroid Build Coastguard Worker // If `kPersistentHistogramsStorage` is set to this, histograms will be
27*6777b538SAndroid Build Coastguard Worker // allocated on the heap, but using the same allocator as the one used for
28*6777b538SAndroid Build Coastguard Worker // file-backed persistent histograms.
29*6777b538SAndroid Build Coastguard Worker extern const char kPersistentHistogramStorageLocalMemory[];
30*6777b538SAndroid Build Coastguard Worker 
31*6777b538SAndroid Build Coastguard Worker // Determines where histograms will be allocated (should either be
32*6777b538SAndroid Build Coastguard Worker // `kPersistentHistogramStorageMappedFile` or
33*6777b538SAndroid Build Coastguard Worker // `kPersistentHistogramStorageLocalMemory`).
34*6777b538SAndroid Build Coastguard Worker extern const base::FeatureParam<std::string> kPersistentHistogramsStorage;
35*6777b538SAndroid Build Coastguard Worker 
36*6777b538SAndroid Build Coastguard Worker // Persistent browser metrics need to be persisted somewhere. This constant
37*6777b538SAndroid Build Coastguard Worker // provides a known string to be used for both the allocator's internal name and
38*6777b538SAndroid Build Coastguard Worker // for a file on disk (relative to metrics_dir) to which they can be saved. This
39*6777b538SAndroid Build Coastguard Worker // is exported so the name can also be used as a "pref" during configuration.
40*6777b538SAndroid Build Coastguard Worker extern const char kBrowserMetricsName[];
41*6777b538SAndroid Build Coastguard Worker 
42*6777b538SAndroid Build Coastguard Worker // Like above, this provides a known string to be used for persistent browser
43*6777b538SAndroid Build Coastguard Worker // metrics. However, metrics under this are "deferred" and sent along with a
44*6777b538SAndroid Build Coastguard Worker // future session's metrics instead of independently.
45*6777b538SAndroid Build Coastguard Worker extern const char kDeferredBrowserMetricsName[];
46*6777b538SAndroid Build Coastguard Worker 
47*6777b538SAndroid Build Coastguard Worker // Do all the checking and work necessary to enable persistent histograms.
48*6777b538SAndroid Build Coastguard Worker // `metrics_dir` specifies the root directory where persistent histograms will
49*6777b538SAndroid Build Coastguard Worker // live. If `persistent_histograms_enabled` is false, this is essentially a
50*6777b538SAndroid Build Coastguard Worker // no-op (histograms will continue being allocated on the heap). Otherwise,
51*6777b538SAndroid Build Coastguard Worker // `storage`, which should be either `kPersistentHistogramStorageMappedFile` or
52*6777b538SAndroid Build Coastguard Worker // `kPersistentHistogramStorageLocalMemory`, determines where histograms will be
53*6777b538SAndroid Build Coastguard Worker // allocated.
54*6777b538SAndroid Build Coastguard Worker // Note: After a call to this, a call toPersistentHistogramsCleanup() below
55*6777b538SAndroid Build Coastguard Worker // should be made when appropriate.
56*6777b538SAndroid Build Coastguard Worker void InstantiatePersistentHistograms(const base::FilePath& metrics_dir,
57*6777b538SAndroid Build Coastguard Worker                                      bool persistent_histograms_enabled,
58*6777b538SAndroid Build Coastguard Worker                                      base::StringPiece storage);
59*6777b538SAndroid Build Coastguard Worker 
60*6777b538SAndroid Build Coastguard Worker // Schedule the tasks required to cleanup the persistent metrics files.
61*6777b538SAndroid Build Coastguard Worker void PersistentHistogramsCleanup(const base::FilePath& metrics_dir);
62*6777b538SAndroid Build Coastguard Worker 
63*6777b538SAndroid Build Coastguard Worker // Calls InstantiatePersistentHistograms() using `kPersistentHistogramsFeature`
64*6777b538SAndroid Build Coastguard Worker // and `kPersistentHistogramsStorage` as params. PersistentHistogramsCleanup()
65*6777b538SAndroid Build Coastguard Worker // is also called immediately after.
66*6777b538SAndroid Build Coastguard Worker void InstantiatePersistentHistogramsWithFeaturesAndCleanup(
67*6777b538SAndroid Build Coastguard Worker     const base::FilePath& metrics_dir);
68*6777b538SAndroid Build Coastguard Worker 
69*6777b538SAndroid Build Coastguard Worker // After calling this, histograms from this session that were not sent (i.e.,
70*6777b538SAndroid Build Coastguard Worker // unlogged samples) will be sent along with a future session's metrics.
71*6777b538SAndroid Build Coastguard Worker // Normally, those unlogged samples are sent as an "independent log", which uses
72*6777b538SAndroid Build Coastguard Worker // the system profile in the persistent file. However, there are scenarios where
73*6777b538SAndroid Build Coastguard Worker // the browser must exit before a system profile is written to the file, which
74*6777b538SAndroid Build Coastguard Worker // results in the metrics from that session being lost. Calling this function
75*6777b538SAndroid Build Coastguard Worker // will make so that the unlogged samples are sent with a future session's
76*6777b538SAndroid Build Coastguard Worker // metrics (whichever is the first to read the file). Note that this may come at
77*6777b538SAndroid Build Coastguard Worker // the cost of associating the metrics with an incorrect system profile. Returns
78*6777b538SAndroid Build Coastguard Worker // whether the operation was successful.
79*6777b538SAndroid Build Coastguard Worker // Note: If you plan on using this, please make sure to get a review from the
80*6777b538SAndroid Build Coastguard Worker // metrics team. Using this may lead to reporting additional histograms that
81*6777b538SAndroid Build Coastguard Worker // may not be relevant to what is being experimented with, which can cause
82*6777b538SAndroid Build Coastguard Worker // confusing/disruptive data.
83*6777b538SAndroid Build Coastguard Worker bool DeferBrowserMetrics(const base::FilePath& metrics_dir);
84*6777b538SAndroid Build Coastguard Worker 
85*6777b538SAndroid Build Coastguard Worker #endif  // COMPONENTS_METRICS_PERSISTENT_HISTOGRAMS_H_
86