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

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: addressed some review comments by Ilya Created 4 years, 8 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
Index: base/metrics/persistent_histogram_allocator.cc
diff --git a/base/metrics/persistent_histogram_allocator.cc b/base/metrics/persistent_histogram_allocator.cc
index 62e8cd6d68baa4ca567e97f3c095ce093969eb1c..268a7a0a1122f66b2a10af58ebb347db36df882b 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"
@@ -46,6 +47,7 @@ enum : uint32_t {
// 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_disabled;
Ilya Sherman 2016/04/26 20:01:24 Please use a boolean for enabled/disabled state, r
bcwhite 2016/04/26 21:04:26 Done.
// 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
@@ -651,12 +653,36 @@ void GlobalHistogramAllocator::CreateWithSharedMemoryHandle(
}
// static
+void GlobalHistogramAllocator::Enable() {
+ if (g_allocator) {
+ // Already enabled.
+ DCHECK(!g_allocator_disabled);
+ return;
+ }
+
+ DCHECK(g_allocator_disabled);
+ std::swap(g_allocator, g_allocator_disabled);
+}
+
+// static
+void GlobalHistogramAllocator::Disable() {
+ if (g_allocator_disabled) {
+ // Already disabled.
+ DCHECK(!g_allocator);
+ return;
+ }
+
+ DCHECK(g_allocator);
+ std::swap(g_allocator, g_allocator_disabled);
+}
+
+// static
void GlobalHistogramAllocator::Set(
std::unique_ptr<GlobalHistogramAllocator> allocator) {
// Releasing or changing an allocator is extremely dangerous because it
// likely has histograms stored within it. If the backing memory is also
// also released, future accesses to those histograms will seg-fault.
- CHECK(!g_allocator);
+ CHECK(!g_allocator && !g_allocator_disabled);
g_allocator = allocator.release();
size_t existing = StatisticsRecorder::GetHistogramCount();
@@ -670,9 +696,14 @@ GlobalHistogramAllocator* GlobalHistogramAllocator::Get() {
}
// static
+GlobalHistogramAllocator* GlobalHistogramAllocator::GetEvenIfDisabled() {
+ return g_allocator ? g_allocator : g_allocator_disabled;
+}
+
+// static
std::unique_ptr<GlobalHistogramAllocator>
GlobalHistogramAllocator::ReleaseForTesting() {
- GlobalHistogramAllocator* histogram_allocator = g_allocator;
+ GlobalHistogramAllocator* histogram_allocator = GetEvenIfDisabled();
if (!histogram_allocator)
return nullptr;
PersistentMemoryAllocator* memory_allocator =
@@ -700,10 +731,41 @@ GlobalHistogramAllocator::ReleaseForTesting() {
DCHECK_NE(kResultHistogram, histogram_data->name);
}
+ DCHECK_NE(g_allocator != nullptr, g_allocator_disabled != nullptr);
g_allocator = nullptr;
+ g_allocator_disabled = nullptr;
return WrapUnique(histogram_allocator);
};
+void GlobalHistogramAllocator::SetPersistentLocation(const FilePath& location) {
+ persistent_location_ = location;
+}
+
+bool GlobalHistogramAllocator::WriteToPersistentLocation() {
+#if defined(OS_NACL)
+ // NACL doesn't support file operations, including ImportantFileWriter.
+ NOTREACHED();
+ return false;
+#else
+ // Stop if no destination is set, perhaps because it has not been enabled.
+ 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)),

Powered by Google App Engine
This is Rietveld 408576698