1 // Copyright 2014 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 #include "components/metrics/library_support/histogram_manager.h" 6 7 #include <string> 8 #include <vector> 9 10 #include "base/lazy_instance.h" 11 #include "base/metrics/histogram_macros.h" 12 #include "base/metrics/histogram_samples.h" 13 #include "base/metrics/statistics_recorder.h" 14 #include "base/thread_annotations.h" 15 #include "components/metrics/histogram_encoder.h" 16 17 namespace metrics { 18 19 // TODO(rtenneti): move g_histogram_manager into java code. 20 static base::LazyInstance<HistogramManager>::Leaky g_histogram_manager = 21 LAZY_INSTANCE_INITIALIZER; 22 HistogramManager()23HistogramManager::HistogramManager() : histogram_snapshot_manager_(this) {} 24 ~HistogramManager()25HistogramManager::~HistogramManager() {} 26 27 // static GetInstance()28HistogramManager* HistogramManager::GetInstance() { 29 return g_histogram_manager.Pointer(); 30 } 31 RecordDelta(const base::HistogramBase & histogram,const base::HistogramSamples & snapshot)32void HistogramManager::RecordDelta(const base::HistogramBase& histogram, 33 const base::HistogramSamples& snapshot) { 34 EncodeHistogramDelta(histogram.histogram_name(), snapshot, &uma_proto_); 35 } 36 37 // TODO(lukasza): https://crbug.com/881903: NO_THREAD_SAFETY_ANALYSIS below can 38 // be removed once base::Lock::Try is annotated with EXCLUSIVE_TRYLOCK_FUNCTION. GetDeltas(std::vector<uint8_t> * data)39bool HistogramManager::GetDeltas(std::vector<uint8_t>* data) 40 NO_THREAD_SAFETY_ANALYSIS { 41 if (get_deltas_lock_.Try()) { 42 base::AutoLock lock(get_deltas_lock_, base::AutoLock::AlreadyAcquired()); 43 // Clear the protobuf between calls. 44 uma_proto_.Clear(); 45 // "false" indicates to *not* include histograms held in persistent storage 46 // on the assumption that they will be visible to the recipient through 47 // other means. 48 base::StatisticsRecorder::PrepareDeltas( 49 false, base::Histogram::kNoFlags, 50 base::Histogram::kUmaTargetedHistogramFlag, 51 &histogram_snapshot_manager_); 52 int32_t data_size = uma_proto_.ByteSize(); 53 data->resize(data_size); 54 if (data_size == 0 || uma_proto_.SerializeToArray(data->data(), data_size)) 55 return true; 56 } 57 data->clear(); 58 return false; 59 } 60 61 } // namespace metrics 62