1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome_frame/chrome_frame_histograms.h"
7 #include "base/histogram.h"
8 #include "base/lazy_instance.h"
9 #include "base/logging.h"
10 #include "base/message_loop.h"
11 #include "base/pickle.h"
13 // Initialize histogram statistics gathering system.
14 base::LazyInstance
<StatisticsRecorder
>
15 g_statistics_recorder_(base::LINKER_INITIALIZED
);
17 ChromeFrameHistogramSnapshots::ChromeFrameHistogramSnapshots() {
18 // Ensure that an instance of the StatisticsRecorder object is created.
19 g_statistics_recorder_
.Get();
22 ChromeFrameHistogramSnapshots::HistogramPickledList
23 ChromeFrameHistogramSnapshots::GatherAllHistograms() {
25 AutoLock
auto_lock(lock_
);
27 StatisticsRecorder::Histograms histograms
;
28 StatisticsRecorder::GetHistograms(&histograms
);
30 HistogramPickledList pickled_histograms
;
32 for (StatisticsRecorder::Histograms::iterator it
= histograms
.begin();
33 histograms
.end() != it
;
35 (*it
)->SetFlags(Histogram::kIPCSerializationSourceFlag
);
36 GatherHistogram(**it
, &pickled_histograms
);
39 return pickled_histograms
;
42 void ChromeFrameHistogramSnapshots::GatherHistogram(
43 const Histogram
& histogram
,
44 HistogramPickledList
* pickled_histograms
) {
46 // Get up-to-date snapshot of sample stats.
47 Histogram::SampleSet snapshot
;
48 histogram
.SnapshotSample(&snapshot
);
49 const std::string
& histogram_name
= histogram
.histogram_name();
51 // Check if we already have a log of this histogram and if not create an
53 LoggedSampleMap::iterator it
= logged_samples_
.find(histogram_name
);
54 Histogram::SampleSet
* already_logged
;
55 if (logged_samples_
.end() == it
) {
57 already_logged
= &logged_samples_
[histogram
.histogram_name()];
58 already_logged
->Resize(histogram
); // Complete initialization.
60 already_logged
= &(it
->second
);
61 // Deduct any stats we've already logged from our snapshot.
62 snapshot
.Subtract(*already_logged
);
65 // Snapshot now contains only a delta to what we've already_logged.
66 if (snapshot
.TotalCount() > 0) {
67 GatherHistogramDelta(histogram
, snapshot
, pickled_histograms
);
68 // Add new data into our running total.
69 already_logged
->Add(snapshot
);
73 void ChromeFrameHistogramSnapshots::GatherHistogramDelta(
74 const Histogram
& histogram
,
75 const Histogram::SampleSet
& snapshot
,
76 HistogramPickledList
* pickled_histograms
) {
78 DCHECK(0 != snapshot
.TotalCount());
79 snapshot
.CheckSize(histogram
);
81 std::string histogram_info
=
82 Histogram::SerializeHistogramInfo(histogram
, snapshot
);
83 pickled_histograms
->push_back(histogram_info
);