drive_metrics_provider.h 3.2 KB

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