persistent_histogram_storage.h 2.7 KB

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