xref: /aosp_15_r20/external/cronet/components/metrics/drive_metrics_provider.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef COMPONENTS_METRICS_DRIVE_METRICS_PROVIDER_H_
6 #define COMPONENTS_METRICS_DRIVE_METRICS_PROVIDER_H_
7 
8 #include "base/functional/callback_forward.h"
9 #include "base/gtest_prod_util.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/sequence_checker.h"
12 #include "components/metrics/metrics_provider.h"
13 #include "third_party/metrics_proto/system_profile.pb.h"
14 
15 namespace base {
16 class FilePath;
17 }
18 
19 namespace metrics {
20 
21 // Provides metrics about the local drives on a user's computer. Currently only
22 // checks to see if they incur a seek-time penalty (e.g. if they're SSDs).
23 class DriveMetricsProvider : public metrics::MetricsProvider {
24  public:
25   explicit DriveMetricsProvider(int local_state_path_key);
26 
27   DriveMetricsProvider(const DriveMetricsProvider&) = delete;
28   DriveMetricsProvider& operator=(const DriveMetricsProvider&) = delete;
29 
30   ~DriveMetricsProvider() override;
31 
32   // metrics::MetricsProvider:
33   void AsyncInit(base::OnceClosure done_callback) override;
34   void ProvideSystemProfileMetrics(
35       metrics::SystemProfileProto* system_profile_proto) override;
36 
37  private:
38   FRIEND_TEST_ALL_PREFIXES(DriveMetricsProviderTest, HasSeekPenalty);
39 
40   // A response to querying a drive as to whether it incurs a seek penalty.
41   // |has_seek_penalty| is set if |success| is true.
42   struct SeekPenaltyResponse {
43     SeekPenaltyResponse();
44     bool success;
45     bool has_seek_penalty;
46   };
47 
48   struct DriveMetrics {
49     SeekPenaltyResponse app_drive;
50     SeekPenaltyResponse user_data_drive;
51   };
52 
53   // Determine whether the device that services |path| has a seek penalty.
54   // Returns false if it couldn't be determined (e.g., |path| doesn't exist).
55   static bool HasSeekPenalty(const base::FilePath& path,
56                              bool* has_seek_penalty);
57 
58   // Gather metrics about various drives. Should be run on a background thread.
59   static DriveMetrics GetDriveMetricsOnBackgroundThread(
60       int local_state_path_key);
61 
62   // Tries to determine whether there is a penalty for seeking on the drive that
63   // hosts |path_service_key| (for example: the drive that holds "Local State").
64   static void QuerySeekPenalty(int path_service_key,
65                                SeekPenaltyResponse* response);
66 
67   // Called when metrics are done being gathered asynchronously.
68   // |done_callback| is the callback that should be called once all metrics are
69   // gathered.
70   void GotDriveMetrics(base::OnceClosure done_callback,
71                        const DriveMetrics& metrics);
72 
73   // Fills |drive| with information from successful |response|s.
74   void FillDriveMetrics(const SeekPenaltyResponse& response,
75                         metrics::SystemProfileProto::Hardware::Drive* drive);
76 
77   // The key to give to base::PathService to obtain the path to local state
78   // (supplied by the embedder).
79   int local_state_path_key_;
80 
81   // Information gathered about various important drives.
82   DriveMetrics metrics_;
83 
84   SEQUENCE_CHECKER(sequence_checker_);
85   base::WeakPtrFactory<DriveMetricsProvider> weak_ptr_factory_{this};
86 };
87 
88 }  // namespace metrics
89 
90 #endif  // COMPONENTS_METRICS_DRIVE_METRICS_PROVIDER_H_
91