OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/metrics/chrome_metrics_service_client.h" | 5 #include "chrome/browser/metrics/chrome_metrics_service_client.h" |
6 | 6 |
7 #include "base/bind.h" | |
7 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/time/time.h" | |
8 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
9 #include "chrome/browser/google/google_util.h" | 11 #include "chrome/browser/google/google_util.h" |
12 #include "chrome/browser/memory_details.h" | |
10 #include "chrome/browser/ui/browser_otr_state.h" | 13 #include "chrome/browser/ui/browser_otr_state.h" |
11 #include "chrome/common/chrome_version_info.h" | 14 #include "chrome/common/chrome_version_info.h" |
12 #include "chrome/common/crash_keys.h" | 15 #include "chrome/common/crash_keys.h" |
16 #include "chrome/common/render_messages.h" | |
17 #include "content/public/browser/histogram_fetcher.h" | |
18 #include "content/public/browser/render_process_host.h" | |
19 | |
20 #if !defined(OS_ANDROID) | |
21 #include "chrome/browser/service_process/service_process_control.h" | |
22 #endif | |
13 | 23 |
14 namespace { | 24 namespace { |
15 | 25 |
26 // This specifies the amount of time to wait for all renderers to send their | |
27 // data. | |
28 const int kMaxHistogramGatheringWaitDuration = 60000; // 60 seconds. | |
29 | |
16 metrics::SystemProfileProto::Channel AsProtobufChannel( | 30 metrics::SystemProfileProto::Channel AsProtobufChannel( |
17 chrome::VersionInfo::Channel channel) { | 31 chrome::VersionInfo::Channel channel) { |
18 switch (channel) { | 32 switch (channel) { |
19 case chrome::VersionInfo::CHANNEL_UNKNOWN: | 33 case chrome::VersionInfo::CHANNEL_UNKNOWN: |
20 return metrics::SystemProfileProto::CHANNEL_UNKNOWN; | 34 return metrics::SystemProfileProto::CHANNEL_UNKNOWN; |
21 case chrome::VersionInfo::CHANNEL_CANARY: | 35 case chrome::VersionInfo::CHANNEL_CANARY: |
22 return metrics::SystemProfileProto::CHANNEL_CANARY; | 36 return metrics::SystemProfileProto::CHANNEL_CANARY; |
23 case chrome::VersionInfo::CHANNEL_DEV: | 37 case chrome::VersionInfo::CHANNEL_DEV: |
24 return metrics::SystemProfileProto::CHANNEL_DEV; | 38 return metrics::SystemProfileProto::CHANNEL_DEV; |
25 case chrome::VersionInfo::CHANNEL_BETA: | 39 case chrome::VersionInfo::CHANNEL_BETA: |
26 return metrics::SystemProfileProto::CHANNEL_BETA; | 40 return metrics::SystemProfileProto::CHANNEL_BETA; |
27 case chrome::VersionInfo::CHANNEL_STABLE: | 41 case chrome::VersionInfo::CHANNEL_STABLE: |
28 return metrics::SystemProfileProto::CHANNEL_STABLE; | 42 return metrics::SystemProfileProto::CHANNEL_STABLE; |
29 } | 43 } |
30 NOTREACHED(); | 44 NOTREACHED(); |
31 return metrics::SystemProfileProto::CHANNEL_UNKNOWN; | 45 return metrics::SystemProfileProto::CHANNEL_UNKNOWN; |
32 } | 46 } |
33 | 47 |
48 // Handles asynchronous fetching of memory details. | |
49 // Will run the provided task after finished. | |
50 class MetricsMemoryDetails : public MemoryDetails { | |
51 public: | |
52 explicit MetricsMemoryDetails(const base::Closure& callback) | |
53 : callback_(callback) {} | |
54 | |
55 virtual void OnDetailsAvailable() OVERRIDE { | |
56 base::MessageLoop::current()->PostTask(FROM_HERE, callback_); | |
57 } | |
58 | |
59 private: | |
60 virtual ~MetricsMemoryDetails() {} | |
61 | |
62 base::Closure callback_; | |
63 | |
64 DISALLOW_COPY_AND_ASSIGN(MetricsMemoryDetails); | |
65 }; | |
66 | |
34 } // namespace | 67 } // namespace |
35 | 68 |
36 ChromeMetricsServiceClient::ChromeMetricsServiceClient() { | 69 ChromeMetricsServiceClient::ChromeMetricsServiceClient() |
70 : waiting_for_asynchronous_reporting_step_(false), | |
Ilya Sherman
2014/05/21 13:37:07
nit: Perhaps we could give this a more descriptive
Alexei Svitkine (slow)
2014/05/21 14:20:15
Done.
| |
71 num_async_histogram_fetches_in_progress_(0), | |
72 weak_ptr_factory_(this) { | |
37 } | 73 } |
38 | 74 |
39 ChromeMetricsServiceClient::~ChromeMetricsServiceClient() { | 75 ChromeMetricsServiceClient::~ChromeMetricsServiceClient() { |
40 } | 76 } |
41 | 77 |
42 void ChromeMetricsServiceClient::SetClientID(const std::string& client_id) { | 78 void ChromeMetricsServiceClient::SetClientID(const std::string& client_id) { |
43 crash_keys::SetClientID(client_id); | 79 crash_keys::SetClientID(client_id); |
44 } | 80 } |
45 | 81 |
46 bool ChromeMetricsServiceClient::IsOffTheRecordSessionActive() { | 82 bool ChromeMetricsServiceClient::IsOffTheRecordSessionActive() { |
47 return !chrome::IsOffTheRecordSessionActive(); | 83 return !chrome::IsOffTheRecordSessionActive(); |
48 } | 84 } |
49 | 85 |
50 std::string ChromeMetricsServiceClient::GetApplicationLocale() { | 86 std::string ChromeMetricsServiceClient::GetApplicationLocale() { |
51 return g_browser_process->GetApplicationLocale(); | 87 return g_browser_process->GetApplicationLocale(); |
52 } | 88 } |
53 | 89 |
54 bool ChromeMetricsServiceClient::GetBrand(std::string* brand_code) { | 90 bool ChromeMetricsServiceClient::GetBrand(std::string* brand_code) { |
55 return google_util::GetBrand(brand_code); | 91 return google_util::GetBrand(brand_code); |
56 } | 92 } |
57 | 93 |
58 metrics::SystemProfileProto::Channel ChromeMetricsServiceClient::GetChannel() { | 94 metrics::SystemProfileProto::Channel ChromeMetricsServiceClient::GetChannel() { |
59 return AsProtobufChannel(chrome::VersionInfo::GetChannel()); | 95 return AsProtobufChannel(chrome::VersionInfo::GetChannel()); |
60 } | 96 } |
61 | 97 |
62 std::string ChromeMetricsServiceClient::GetVersionString() { | 98 std::string ChromeMetricsServiceClient::GetVersionString() { |
63 // TODO(asvitkine): Move over from metrics_log.cc | 99 // TODO(asvitkine): Move over from metrics_log.cc |
64 return std::string(); | 100 return std::string(); |
65 } | 101 } |
102 | |
103 void ChromeMetricsServiceClient::OnFinalLogCollection( | |
104 const base::Closure& done_callback) { | |
105 final_log_collection_done_callback_ = done_callback; | |
106 | |
107 // Begin the multi-step process of collecting memory usage histograms: | |
108 // First spawn a task to collect the memory details; when that task is | |
109 // finished, it will call OnMemoryDetailCollectionDone. That will in turn | |
110 // call HistogramSynchronization to collect histograms from all renderers and | |
111 // then call OnHistogramSynchronizationDone to continue processing. | |
112 DCHECK(!waiting_for_asynchronous_reporting_step_); | |
113 waiting_for_asynchronous_reporting_step_ = true; | |
114 | |
115 base::Closure callback = | |
116 base::Bind(&ChromeMetricsServiceClient::OnMemoryDetailCollectionDone, | |
117 weak_ptr_factory_.GetWeakPtr()); | |
118 | |
119 scoped_refptr<MetricsMemoryDetails> details( | |
120 new MetricsMemoryDetails(callback)); | |
121 details->StartFetch(MemoryDetails::UPDATE_USER_METRICS); | |
122 | |
123 // Collect WebCore cache information to put into a histogram. | |
124 for (content::RenderProcessHost::iterator i( | |
125 content::RenderProcessHost::AllHostsIterator()); | |
126 !i.IsAtEnd(); i.Advance()) { | |
127 i.GetCurrentValue()->Send(new ChromeViewMsg_GetCacheResourceStats()); | |
128 } | |
129 } | |
130 | |
131 void ChromeMetricsServiceClient::OnMemoryDetailCollectionDone() { | |
132 // This function should only be called as the callback from an ansynchronous | |
133 // step. | |
134 DCHECK(waiting_for_asynchronous_reporting_step_); | |
135 | |
136 // Create a callback_task for OnHistogramSynchronizationDone. | |
137 base::Closure callback = base::Bind( | |
138 &ChromeMetricsServiceClient::OnHistogramSynchronizationDone, | |
139 weak_ptr_factory_.GetWeakPtr()); | |
140 | |
141 base::TimeDelta timeout = | |
142 base::TimeDelta::FromMilliseconds(kMaxHistogramGatheringWaitDuration); | |
143 | |
144 DCHECK_EQ(num_async_histogram_fetches_in_progress_, 0); | |
145 | |
146 #if defined(OS_ANDROID) | |
147 // Android has no service process. | |
148 num_async_histogram_fetches_in_progress_ = 1; | |
149 #else // OS_ANDROID | |
150 num_async_histogram_fetches_in_progress_ = 2; | |
151 // Run requests to service and content in parallel. | |
152 if (!ServiceProcessControl::GetInstance()->GetHistograms(callback, timeout)) { | |
153 // Assume |num_async_histogram_fetches_in_progress_| is not changed by | |
154 // |GetHistograms()|. | |
155 DCHECK_EQ(num_async_histogram_fetches_in_progress_, 2); | |
156 // Assign |num_async_histogram_fetches_in_progress_| above and decrement it | |
157 // here to make code work even if |GetHistograms()| fired |callback|. | |
158 --num_async_histogram_fetches_in_progress_; | |
159 } | |
160 #endif // OS_ANDROID | |
161 | |
162 // Set up the callback to task to call after we receive histograms from all | |
163 // child processes. Wait time specifies how long to wait before absolutely | |
Ilya Sherman
2014/05/21 13:37:07
nit: "Wait time" -> "timeout"
Alexei Svitkine (slow)
2014/05/21 14:20:15
Done.
| |
164 // calling us back on the task. | |
165 content::FetchHistogramsAsynchronously(base::MessageLoop::current(), callback, | |
166 timeout); | |
167 } | |
168 | |
169 void ChromeMetricsServiceClient::OnHistogramSynchronizationDone() { | |
170 // This function should only be called as the callback from an ansynchronous | |
171 // step. | |
172 DCHECK(waiting_for_asynchronous_reporting_step_); | |
173 DCHECK_GT(num_async_histogram_fetches_in_progress_, 0); | |
174 | |
175 // Check if all expected requests finished. | |
176 if (--num_async_histogram_fetches_in_progress_ > 0) | |
177 return; | |
178 | |
179 waiting_for_asynchronous_reporting_step_ = false; | |
180 final_log_collection_done_callback_.Run(); | |
181 } | |
182 | |
OLD | NEW |