Commit f4728fa7 authored by Gabriel Marin's avatar Gabriel Marin Committed by Commit Bot

PerfProvider: Add a main profile provider class for Chrome OS

Add ProfileProvider to manage multiple metric collectors and register with the
various Chromium triggers.

Both the implementation and tests are mostly new.

Added rules to compile the class and the associated unit tests, but the
code will not be used in prod until a later CL that adds a perf events
collector to replace PerfProvider.

BUG=b:116527691
TEST=Unit tests pass

Change-Id: Iac745beb5f80ccfb675de581e8ccb5b7304f6114
Reviewed-on: https://chromium-review.googlesource.com/c/1357639
Commit-Queue: Gabriel Marin <gmx@chromium.org>
Reviewed-by: default avatarAlexei Svitkine <asvitkine@chromium.org>
Cr-Commit-Position: refs/heads/master@{#615632}
parent 16401656
...@@ -3162,6 +3162,8 @@ jumbo_split_static_library("browser") { ...@@ -3162,6 +3162,8 @@ jumbo_split_static_library("browser") {
"metrics/perf/metric_collector.h", "metrics/perf/metric_collector.h",
"metrics/perf/perf_output.cc", "metrics/perf/perf_output.cc",
"metrics/perf/perf_output.h", "metrics/perf/perf_output.h",
"metrics/perf/profile_provider_chromeos.cc",
"metrics/perf/profile_provider_chromeos.h",
"metrics/perf/random_selector.cc", "metrics/perf/random_selector.cc",
"metrics/perf/random_selector.h", "metrics/perf/random_selector.h",
"metrics/perf/windowed_incognito_observer.cc", "metrics/perf/windowed_incognito_observer.cc",
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/metrics/perf/profile_provider_chromeos.h"
#include "chromeos/dbus/dbus_thread_manager.h"
namespace metrics {
namespace {
// Returns true if a normal user is logged in. Returns false otherwise (e.g. if
// logged in as a guest or as a kiosk app).
bool IsNormalUserLoggedIn() {
return chromeos::LoginState::Get()->IsUserAuthenticated();
}
} // namespace
ProfileProvider::ProfileProvider() : weak_factory_(this) {}
ProfileProvider::~ProfileProvider() {
chromeos::LoginState::Get()->RemoveObserver(this);
chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(
this);
}
void ProfileProvider::Init() {
for (auto& collector : collectors_) {
collector->Init();
}
// Register as an observer of login state changes.
chromeos::LoginState::Get()->AddObserver(this);
// Register as an observer of power manager events.
chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(
this);
// Register as an observer of session restore.
on_session_restored_callback_subscription_ =
SessionRestore::RegisterOnSessionRestoredCallback(base::BindRepeating(
&ProfileProvider::OnSessionRestoreDone, weak_factory_.GetWeakPtr()));
// Check the login state. At the time of writing, this class is instantiated
// before login. A subsequent login would activate the profiling. However,
// that behavior may change in the future so that the user is already logged
// when this class is instantiated. By calling LoggedInStateChanged() here,
// ProfileProvider will recognize that the system is already logged in.
LoggedInStateChanged();
}
bool ProfileProvider::GetSampledProfiles(
std::vector<SampledProfile>* sampled_profiles) {
bool result = false;
for (auto& collector : collectors_) {
bool written = collector->GetSampledProfiles(sampled_profiles);
result = result || written;
}
return result;
}
void ProfileProvider::LoggedInStateChanged() {
if (IsNormalUserLoggedIn()) {
for (auto& collector : collectors_) {
collector->OnUserLoggedIn();
}
} else {
for (auto& collector : collectors_) {
collector->Deactivate();
}
}
}
void ProfileProvider::SuspendDone(const base::TimeDelta& sleep_duration) {
// A zero value for the suspend duration indicates that the suspend was
// canceled. Do not collect anything if that's the case.
if (sleep_duration.is_zero())
return;
// Do not collect a profile unless logged in. The system behavior when closing
// the lid or idling when not logged in is currently to shut down instead of
// suspending. But it's good to enforce the rule here in case that changes.
if (!IsNormalUserLoggedIn())
return;
// Inform each collector that a successful suspend has completed.
for (auto& collector : collectors_) {
collector->SuspendDone(sleep_duration);
}
}
void ProfileProvider::OnSessionRestoreDone(int num_tabs_restored) {
// Do not collect a profile unless logged in as a normal user.
if (!IsNormalUserLoggedIn())
return;
// Inform each collector of a session restore event.
for (auto& collector : collectors_) {
collector->OnSessionRestoreDone(num_tabs_restored);
}
}
} // namespace metrics
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_METRICS_PERF_PROFILE_PROVIDER_CHROMEOS_H_
#define CHROME_BROWSER_METRICS_PERF_PROFILE_PROVIDER_CHROMEOS_H_
#include <vector>
#include "base/time/time.h"
#include "chrome/browser/metrics/perf/metric_collector.h"
#include "chromeos/dbus/power_manager_client.h"
#include "chromeos/login/login_state.h"
namespace metrics {
// Provides access to ChromeOS profile data using different metric collectors.
// It detects certain system triggers, such as device resuming from suspend
// mode, or user logging in, which it forwards to the registered collectors.
class ProfileProvider : public chromeos::PowerManagerClient::Observer,
public chromeos::LoginState::Observer {
public:
ProfileProvider();
~ProfileProvider() override;
void Init();
// Stores collected perf data protobufs in |sampled_profiles|. Clears all the
// stored profile data. Returns true if it wrote to |sampled_profiles|.
bool GetSampledProfiles(std::vector<SampledProfile>* sampled_profiles);
protected:
// Called when either the login state or the logged in user type changes.
// Activates the registered collectors to start collecting. Inherited from
// LoginState::Observer.
void LoggedInStateChanged() override;
// Called when a suspend finishes. This is either a successful suspend
// followed by a resume, or a suspend that was canceled. Inherited from
// PowerManagerClient::Observer.
void SuspendDone(const base::TimeDelta& sleep_duration) override;
// Called when a session restore has finished.
void OnSessionRestoreDone(int num_tabs_restored);
// Vector of registered metric collectors.
std::vector<std::unique_ptr<MetricCollector>> collectors_;
private:
// Points to the on-session-restored callback that was registered with
// SessionRestore's callback list. When objects of this class are destroyed,
// the subscription object's destructor will automatically unregister the
// callback in SessionRestore, so that the callback list does not contain any
// obsolete callbacks.
SessionRestore::CallbackSubscription
on_session_restored_callback_subscription_;
// To pass around the "this" pointer across threads safely.
base::WeakPtrFactory<ProfileProvider> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(ProfileProvider);
};
} // namespace metrics
#endif // CHROME_BROWSER_METRICS_PERF_PROFILE_PROVIDER_CHROMEOS_H_
...@@ -3477,6 +3477,7 @@ test("unit_tests") { ...@@ -3477,6 +3477,7 @@ test("unit_tests") {
"../browser/google/google_brand_code_map_chromeos_unittest.cc", "../browser/google/google_brand_code_map_chromeos_unittest.cc",
"../browser/media/webrtc/desktop_media_list_ash_unittest.cc", "../browser/media/webrtc/desktop_media_list_ash_unittest.cc",
"../browser/metrics/perf/metric_collector_unittest.cc", "../browser/metrics/perf/metric_collector_unittest.cc",
"../browser/metrics/perf/profile_provider_chromeos_unittest.cc",
"../browser/notifications/chrome_ash_message_center_client_unittest.cc", "../browser/notifications/chrome_ash_message_center_client_unittest.cc",
"../browser/renderer_context_menu/mock_render_view_context_menu.cc", "../browser/renderer_context_menu/mock_render_view_context_menu.cc",
"../browser/renderer_context_menu/mock_render_view_context_menu.h", "../browser/renderer_context_menu/mock_render_view_context_menu.h",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment