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