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

Side by Side Diff: chrome/browser/metrics/perf_provider_chromeos.cc

Issue 1218583002: metrics: Add dbus interface for GetRandomPerfOutput (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Do not allow both perf_stat and perf_data to be passed in; treat it as an error Created 5 years, 5 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 <string> 5 #include <string>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 65
66 // Enumeration representing success and various failure modes for collecting and 66 // Enumeration representing success and various failure modes for collecting and
67 // sending perf data. 67 // sending perf data.
68 enum GetPerfDataOutcome { 68 enum GetPerfDataOutcome {
69 SUCCESS, 69 SUCCESS,
70 NOT_READY_TO_UPLOAD, 70 NOT_READY_TO_UPLOAD,
71 NOT_READY_TO_COLLECT, 71 NOT_READY_TO_COLLECT,
72 INCOGNITO_ACTIVE, 72 INCOGNITO_ACTIVE,
73 INCOGNITO_LAUNCHED, 73 INCOGNITO_LAUNCHED,
74 PROTOBUF_NOT_PARSED, 74 PROTOBUF_NOT_PARSED,
75 ILLEGAL_DATA_RETURNED,
75 NUM_OUTCOMES 76 NUM_OUTCOMES
76 }; 77 };
77 78
78 // Name of the histogram that represents the success and various failure modes 79 // Name of the histogram that represents the success and various failure modes
79 // for collecting and sending perf data. 80 // for collecting and sending perf data.
80 const char kGetPerfDataOutcomeHistogram[] = "UMA.Perf.GetData"; 81 const char kGetPerfDataOutcomeHistogram[] = "UMA.Perf.GetData";
81 82
82 void AddToPerfHistogram(GetPerfDataOutcome outcome) { 83 void AddToPerfHistogram(GetPerfDataOutcome outcome) {
83 UMA_HISTOGRAM_ENUMERATION(kGetPerfDataOutcomeHistogram, 84 UMA_HISTOGRAM_ENUMERATION(kGetPerfDataOutcomeHistogram,
84 outcome, 85 outcome,
85 NUM_OUTCOMES); 86 NUM_OUTCOMES);
86 } 87 }
87 88
88 // Returns true if a normal user is logged in. Returns false otherwise (e.g. if 89 // Returns true if a normal user is logged in. Returns false otherwise (e.g. if
89 // logged in as a guest or as a kiosk app). 90 // logged in as a guest or as a kiosk app).
90 bool IsNormalUserLoggedIn() { 91 bool IsNormalUserLoggedIn() {
91 return chromeos::LoginState::Get()->IsUserAuthenticated(); 92 return chromeos::LoginState::Get()->IsUserAuthenticated();
92 } 93 }
93 94
94 } // namespace 95 } // namespace
95 96
97 namespace metrics {
96 98
97 namespace metrics { 99 namespace {
98 100
99 // This class must be created and used on the UI thread. It watches for any 101 // This class must be created and used on the UI thread. It watches for any
100 // incognito window being opened from the time it is instantiated to the time it 102 // incognito window being opened from the time it is instantiated to the time it
101 // is destroyed. 103 // is destroyed.
102 class WindowedIncognitoObserver : public chrome::BrowserListObserver { 104 class WindowedIncognitoObserver
105 : public chrome::BrowserListObserver,
106 public PerfProvider::IncognitoObserverInterface {
103 public: 107 public:
104 WindowedIncognitoObserver() : incognito_launched_(false) { 108 WindowedIncognitoObserver() : incognito_launched_(false) {
105 BrowserList::AddObserver(this); 109 BrowserList::AddObserver(this);
106 } 110 }
107 111
108 ~WindowedIncognitoObserver() override { BrowserList::RemoveObserver(this); } 112 ~WindowedIncognitoObserver() override { BrowserList::RemoveObserver(this); }
109 113
110 // This method can be checked to see whether any incognito window has been 114 // This method can be checked to see whether any incognito window has been
111 // opened since the time this object was created. 115 // opened since the time this object was created.
112 bool incognito_launched() { 116 virtual bool incognito_launched() const override {
113 return incognito_launched_; 117 return incognito_launched_;
114 } 118 }
115 119
116 private: 120 private:
117 // chrome::BrowserListObserver implementation. 121 // chrome::BrowserListObserver implementation.
118 void OnBrowserAdded(Browser* browser) override { 122 void OnBrowserAdded(Browser* browser) override {
119 if (browser->profile()->IsOffTheRecord()) 123 if (browser->profile()->IsOffTheRecord())
120 incognito_launched_ = true; 124 incognito_launched_ = true;
121 } 125 }
122 126
123 bool incognito_launched_; 127 bool incognito_launched_;
124 }; 128 };
125 129
130 } // namespace
131
126 PerfProvider::PerfProvider() 132 PerfProvider::PerfProvider()
127 : login_observer_(this), 133 : login_observer_(this),
128 next_profiling_interval_start_(base::TimeTicks::Now()), 134 next_profiling_interval_start_(base::TimeTicks::Now()),
129 weak_factory_(this) { 135 weak_factory_(this) {
130 // Register the login observer with LoginState. 136 // Register the login observer with LoginState.
131 chromeos::LoginState::Get()->AddObserver(&login_observer_); 137 chromeos::LoginState::Get()->AddObserver(&login_observer_);
132 138
133 // Register as an observer of power manager events. 139 // Register as an observer of power manager events.
134 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()-> 140 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
135 AddObserver(this); 141 AddObserver(this);
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 320
315 scoped_ptr<WindowedIncognitoObserver> incognito_observer( 321 scoped_ptr<WindowedIncognitoObserver> incognito_observer(
316 new WindowedIncognitoObserver); 322 new WindowedIncognitoObserver);
317 323
318 chromeos::DebugDaemonClient* client = 324 chromeos::DebugDaemonClient* client =
319 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient(); 325 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient();
320 326
321 base::TimeDelta collection_duration = base::TimeDelta::FromSeconds( 327 base::TimeDelta collection_duration = base::TimeDelta::FromSeconds(
322 kPerfCommandDurationDefaultSeconds); 328 kPerfCommandDurationDefaultSeconds);
323 329
324 client->GetPerfData(collection_duration.InSeconds(), 330 client->GetPerfOutput(
325 base::Bind(&PerfProvider::ParseProtoIfValid, 331 collection_duration.InSeconds(),
326 weak_factory_.GetWeakPtr(), 332 base::Bind(&PerfProvider::ParseOutputProtoIfValid,
327 base::Passed(&incognito_observer), 333 weak_factory_.GetWeakPtr(), base::Passed(&incognito_observer),
328 base::Passed(&sampled_profile))); 334 base::Passed(&sampled_profile)));
329 } 335 }
330 336
331 void PerfProvider::DoPeriodicCollection() { 337 void PerfProvider::DoPeriodicCollection() {
332 scoped_ptr<SampledProfile> sampled_profile(new SampledProfile); 338 scoped_ptr<SampledProfile> sampled_profile(new SampledProfile);
333 sampled_profile->set_trigger_event(SampledProfile::PERIODIC_COLLECTION); 339 sampled_profile->set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
334 340
335 CollectIfNecessary(sampled_profile.Pass()); 341 CollectIfNecessary(sampled_profile.Pass());
336 } 342 }
337 343
338 void PerfProvider::CollectPerfDataAfterResume( 344 void PerfProvider::CollectPerfDataAfterResume(
(...skipping 14 matching lines...) Expand all
353 // Fill out a SampledProfile protobuf that will contain the collected data. 359 // Fill out a SampledProfile protobuf that will contain the collected data.
354 scoped_ptr<SampledProfile> sampled_profile(new SampledProfile); 360 scoped_ptr<SampledProfile> sampled_profile(new SampledProfile);
355 sampled_profile->set_trigger_event(SampledProfile::RESTORE_SESSION); 361 sampled_profile->set_trigger_event(SampledProfile::RESTORE_SESSION);
356 sampled_profile->set_ms_after_restore(time_after_restore.InMilliseconds()); 362 sampled_profile->set_ms_after_restore(time_after_restore.InMilliseconds());
357 sampled_profile->set_num_tabs_restored(num_tabs_restored); 363 sampled_profile->set_num_tabs_restored(num_tabs_restored);
358 364
359 CollectIfNecessary(sampled_profile.Pass()); 365 CollectIfNecessary(sampled_profile.Pass());
360 last_session_restore_collection_time_ = base::TimeTicks::Now(); 366 last_session_restore_collection_time_ = base::TimeTicks::Now();
361 } 367 }
362 368
363 void PerfProvider::ParseProtoIfValid( 369 void PerfProvider::ParseOutputProtoIfValid(
364 scoped_ptr<WindowedIncognitoObserver> incognito_observer, 370 scoped_ptr<IncognitoObserverInterface> incognito_observer,
365 scoped_ptr<SampledProfile> sampled_profile, 371 scoped_ptr<SampledProfile> sampled_profile,
366 const std::vector<uint8>& data) { 372 int result,
373 const std::vector<uint8>& perf_data,
374 const std::vector<uint8>& perf_stat) {
367 DCHECK(CalledOnValidThread()); 375 DCHECK(CalledOnValidThread());
368 376
369 if (incognito_observer->incognito_launched()) { 377 if (incognito_observer->incognito_launched()) {
370 AddToPerfHistogram(INCOGNITO_LAUNCHED); 378 AddToPerfHistogram(INCOGNITO_LAUNCHED);
371 return; 379 return;
372 } 380 }
373 381
374 PerfDataProto perf_data_proto; 382 if (result != 0 || (perf_data.empty() && perf_stat.empty())) {
375 if (!perf_data_proto.ParseFromArray(data.data(), data.size())) {
376 AddToPerfHistogram(PROTOBUF_NOT_PARSED); 383 AddToPerfHistogram(PROTOBUF_NOT_PARSED);
377 return; 384 return;
378 } 385 }
379 386
380 // Populate a profile collection protobuf with the collected perf data and 387 if (!perf_data.empty() && !perf_stat.empty()) {
381 // extra metadata. 388 AddToPerfHistogram(ILLEGAL_DATA_RETURNED);
382 cached_perf_data_.resize(cached_perf_data_.size() + 1); 389 return;
383 SampledProfile& collection_data = cached_perf_data_.back(); 390 }
384 collection_data.Swap(sampled_profile.get());
385 391
386 // Fill out remaining fields of the SampledProfile protobuf. 392 if (!perf_data.empty()) {
387 collection_data.set_ms_after_boot( 393 PerfDataProto perf_data_proto;
388 perf_data_proto.timestamp_sec() * base::Time::kMillisecondsPerSecond); 394 if (!perf_data_proto.ParseFromArray(perf_data.data(), perf_data.size())) {
395 AddToPerfHistogram(PROTOBUF_NOT_PARSED);
396 return;
397 }
398 sampled_profile->set_ms_after_boot(
399 perf_data_proto.timestamp_sec() * base::Time::kMillisecondsPerSecond);
400 sampled_profile->mutable_perf_data()->Swap(&perf_data_proto);
401 }
402
403 if (!perf_stat.empty()) {
Ilya Sherman 2015/07/01 03:09:02 nit: I think it would be clearer to simply write t
Simon Que 2015/07/01 20:22:57 Done.
404 PerfStatProto perf_stat_proto;
405 if (!perf_stat_proto.ParseFromArray(perf_stat.data(), perf_stat.size())) {
406 AddToPerfHistogram(PROTOBUF_NOT_PARSED);
407 return;
408 }
409 sampled_profile->mutable_perf_stat()->Swap(&perf_stat_proto);
410 }
389 411
390 DCHECK(!login_time_.is_null()); 412 DCHECK(!login_time_.is_null());
391 collection_data. 413 sampled_profile->set_ms_after_login(
392 set_ms_after_login((base::TimeTicks::Now() - login_time_) 414 (base::TimeTicks::Now() - login_time_).InMilliseconds());
393 .InMilliseconds());
394 415
395 // Finally, store the perf data itself. 416 // Add the collected data to the container of collected SampledProfiles.
396 collection_data.mutable_perf_data()->Swap(&perf_data_proto); 417 cached_perf_data_.resize(cached_perf_data_.size() + 1);
418 cached_perf_data_.back().Swap(sampled_profile.get());
397 } 419 }
398 420
399 } // namespace metrics 421 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698