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