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

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 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
« no previous file with comments | « base/metrics/persistent_histogram_allocator.h ('k') | chrome/browser/chrome_browser_field_trials.cc » ('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 62e8cd6d68baa4ca567e97f3c095ce093969eb1c..fe3a2059ba21d96a9716915d9eac70cf3c276864 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;
+bool g_allocator_enabled;
Ilya Sherman 2016/04/27 20:31:33 Sorry, I'm going to go back to my previous questio
bcwhite 2016/05/02 14:46:47 It would have to be tested by CreateHistogram() wh
Ilya Sherman 2016/05/05 22:22:52 g_allocator_enabled is only accessed internally to
bcwhite 2016/05/06 16:59:27 That would work just fine. The difference would b
// 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,6 +653,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
@@ -658,6 +672,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();
DLOG_IF(WARNING, existing)
@@ -666,6 +681,11 @@ void GlobalHistogramAllocator::Set(
// static
GlobalHistogramAllocator* GlobalHistogramAllocator::Get() {
+ return g_allocator_enabled ? g_allocator : nullptr;
+}
+
+// static
+GlobalHistogramAllocator* GlobalHistogramAllocator::GetEvenIfDisabled() {
return g_allocator;
}
@@ -704,6 +724,35 @@ GlobalHistogramAllocator::ReleaseForTesting() {
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
Ilya Sherman 2016/04/27 20:31:33 Please DCHECK(g_allocator_enabled) in this code.
bcwhite 2016/05/02 14:46:47 Done.
+ // 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)),
« no previous file with comments | « base/metrics/persistent_histogram_allocator.h ('k') | chrome/browser/chrome_browser_field_trials.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698