Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(238)

Unified Diff: base/metrics/persistent_histogram_allocator.cc

Issue 1891913002: Support saving browser metrics to disk and reading them during next run. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: removed some unnecessary includes Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/metrics/persistent_histogram_allocator.h ('k') | base/metrics/persistent_memory_allocator.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/metrics/persistent_histogram_allocator.cc
diff --git a/base/metrics/persistent_histogram_allocator.cc b/base/metrics/persistent_histogram_allocator.cc
index c29aa6feb127cb6967658cc18cdda8e8359d94ef..2a090366a8b4d90dde75b2c9621c2aaf94f6f885 100644
--- a/base/metrics/persistent_histogram_allocator.cc
+++ b/base/metrics/persistent_histogram_allocator.cc
@@ -6,6 +6,7 @@
#include <memory>
+#include "base/files/important_file_writer.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
@@ -45,7 +46,8 @@ enum : uint32_t {
// GlobalHistogramAllocator objects) are explicitly forbidden from doing
// anything essential at exit anyway due to the fact that they depend on data
// managed elsewhere and which could be destructed first.
-GlobalHistogramAllocator* g_allocator;
+GlobalHistogramAllocator* g_allocator = nullptr;
+bool g_allocator_enabled = false;
// Take an array of range boundaries and create a proper BucketRanges object
// which is returned to the caller. A return of nullptr indicates that the
@@ -655,6 +657,18 @@ void GlobalHistogramAllocator::CreateWithSharedMemoryHandle(
}
// static
+void GlobalHistogramAllocator::Enable() {
+ DCHECK(g_allocator);
+ g_allocator_enabled = true;
+}
+
+// static
+void GlobalHistogramAllocator::Disable() {
+ DCHECK(g_allocator);
+ g_allocator_enabled = false;
+}
+
+// static
void GlobalHistogramAllocator::Set(
std::unique_ptr<GlobalHistogramAllocator> allocator) {
// Releasing or changing an allocator is extremely dangerous because it
@@ -662,6 +676,7 @@ void GlobalHistogramAllocator::Set(
// also released, future accesses to those histograms will seg-fault.
CHECK(!g_allocator);
g_allocator = allocator.release();
+ g_allocator_enabled = true;
size_t existing = StatisticsRecorder::GetHistogramCount();
DVLOG_IF(1, existing)
@@ -670,6 +685,11 @@ void GlobalHistogramAllocator::Set(
// static
GlobalHistogramAllocator* GlobalHistogramAllocator::Get() {
+ return g_allocator_enabled ? g_allocator : nullptr;
+}
+
+// static
+GlobalHistogramAllocator* GlobalHistogramAllocator::GetEvenIfDisabled() {
return g_allocator;
}
@@ -708,6 +728,37 @@ GlobalHistogramAllocator::ReleaseForTesting() {
return WrapUnique(histogram_allocator);
};
+void GlobalHistogramAllocator::SetPersistentLocation(const FilePath& location) {
+ persistent_location_ = location;
+}
+
+bool GlobalHistogramAllocator::WriteToPersistentLocation() {
+ DCHECK(g_allocator_enabled);
+
+#if defined(OS_NACL)
+ // NACL doesn't support file operations, including ImportantFileWriter.
+ NOTREACHED();
+ return false;
+#else
+ // Stop if no destination is set.
+ if (persistent_location_.empty()) {
+ NOTREACHED() << "Could not write \"" << Name() << "\" persistent histograms"
+ << " to file because no location was set.";
+ return false;
+ }
+
+ StringPiece contents(static_cast<const char*>(data()), used());
+ if (!ImportantFileWriter::WriteFileAtomically(persistent_location_,
+ contents)) {
+ LOG(ERROR) << "Could not write \"" << Name() << "\" persistent histograms"
+ << " to file: " << persistent_location_.value();
+ return false;
+ }
+
+ return true;
+#endif
+}
+
GlobalHistogramAllocator::GlobalHistogramAllocator(
std::unique_ptr<PersistentMemoryAllocator> memory)
: PersistentHistogramAllocator(std::move(memory)),
« no previous file with comments | « base/metrics/persistent_histogram_allocator.h ('k') | base/metrics/persistent_memory_allocator.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698