persistent_histogram_storage.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2018 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 BASE_METRICS_PERSISTENT_HISTOGRAM_STORAGE_H_
  5. #define BASE_METRICS_PERSISTENT_HISTOGRAM_STORAGE_H_
  6. #include "base/base_export.h"
  7. #include "base/files/file_path.h"
  8. #include "base/strings/string_piece.h"
  9. namespace base {
  10. // This class creates a fixed sized persistent memory to allow histograms to be
  11. // stored in it. When a PersistentHistogramStorage is destructed, histograms
  12. // recorded during its lifetime are persisted in the directory
  13. // |storage_base_dir_|/|allocator_name| (see the ctor for allocator_name).
  14. // Histograms are not persisted if the storage directory does not exist on
  15. // destruction. PersistentHistogramStorage should be instantiated as early as
  16. // possible in the process lifetime and should never be instantiated again.
  17. // Persisted histograms will eventually be reported by Chrome.
  18. class BASE_EXPORT PersistentHistogramStorage {
  19. public:
  20. enum class StorageDirManagement { kCreate, kUseExisting };
  21. // Creates a process-wide storage location for histograms that will be written
  22. // to a file within a directory provided by |set_storage_base_dir()| on
  23. // destruction.
  24. // The |allocator_name| is used both as an internal name for the allocator,
  25. // well as the leaf directory name for the file to which the histograms are
  26. // persisted. The string must be ASCII.
  27. // |storage_dir_management| specifies if this instance reuses an existing
  28. // storage directory, or is responsible for creating one.
  29. PersistentHistogramStorage(StringPiece allocator_name,
  30. StorageDirManagement storage_dir_management);
  31. PersistentHistogramStorage(const PersistentHistogramStorage&) = delete;
  32. PersistentHistogramStorage& operator=(const PersistentHistogramStorage&) =
  33. delete;
  34. ~PersistentHistogramStorage();
  35. // The storage directory isn't always known during initial construction so
  36. // it's set separately. The last one wins if there are multiple calls to this
  37. // method.
  38. void set_storage_base_dir(const FilePath& storage_base_dir) {
  39. storage_base_dir_ = storage_base_dir;
  40. }
  41. // Disables histogram storage.
  42. void Disable() { disabled_ = true; }
  43. private:
  44. // Metrics files are written into directory
  45. // |storage_base_dir_|/|allocator_name| (see the ctor for allocator_name).
  46. FilePath storage_base_dir_;
  47. // The setting of the storage directory management.
  48. const StorageDirManagement storage_dir_management_;
  49. // A flag indicating if histogram storage is disabled. It starts with false,
  50. // but can be set to true by the caller who decides to throw away its
  51. // histogram data.
  52. bool disabled_ = false;
  53. };
  54. } // namespace base
  55. #endif // BASE_METRICS_PERSISTENT_HISTOGRAM_STORAGE_H_