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

Unified Diff: chrome/browser/chromeos/resource_reporter/resource_reporter.h

Issue 1374283003: Reporting top cpu and memory consumers via rappor on chromeos (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Using literals for UMA histograms Created 5 years, 1 month 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/chromeos/resource_reporter/resource_reporter.h
diff --git a/chrome/browser/chromeos/resource_reporter/resource_reporter.h b/chrome/browser/chromeos/resource_reporter/resource_reporter.h
new file mode 100644
index 0000000000000000000000000000000000000000..d92cbcc20e75e7a703d2b8449a1dfc6e3ba8cefe
--- /dev/null
+++ b/chrome/browser/chromeos/resource_reporter/resource_reporter.h
@@ -0,0 +1,193 @@
+// Copyright 2015 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_CHROMEOS_RESOURCE_REPORTER_RESOURCE_REPORTER_H_
+#define CHROME_BROWSER_CHROMEOS_RESOURCE_REPORTER_RESOURCE_REPORTER_H_
+
+#include <map>
+#include <vector>
ncarter (slow) 2015/11/13 22:44:32 #include <string> here (& can remove from the .cc)
afakhry 2015/11/16 18:44:20 Done.
+
+#include "base/gtest_prod_util.h"
+#include "base/macros.h"
+#include "base/memory/memory_pressure_listener.h"
+#include "chrome/browser/task_management/task_manager_observer.h"
+#include "components/metrics/metrics_service.h"
+#include "components/rappor/sample.h"
+
+namespace base {
+template <typename T>
+struct DefaultSingletonTraits;
+}
+
+using MemoryPressureLevel = base::MemoryPressureListener::MemoryPressureLevel;
+
+namespace chromeos {
+
+// A system that tracks the top |kTopConsumersCount| CPU and memory consumer
+// Chrome tasks and reports them via Rappor whenever memory pressure is moderate
+// or higher.
+class ResourceReporter
+ : public task_management::TaskManagerObserver,
+ public metrics::MetricsServiceObserver {
+ public:
+ // A collection of the data of a task manager's task that the ResourceReporter
+ // is interested in.
+ struct TaskRecord {
+ // The ID of the task.
+ task_management::TaskId id;
+
+ // The name of the Rappor sample to be used for recording a sample
+ // representing this task.
+ std::string rappor_sample;
Steven Holte 2015/11/13 21:42:18 Similar to my other comment, this should probably
afakhry 2015/11/16 18:44:20 Done.
+
+ // The CPU usage of the task from the most recent task manager refresh in
+ // percentage.
+ double cpu;
+
+ // The physical memory usage of the task from the most recent task manager
+ // refresh in bytes. It doesn't include shared memory. A value of -1 is
+ // invalid. See TaskManagerInterface::GetPhysicalMemoryUsage().
+ int64_t memory;
+
+ // True if the task is running on a process at background priority.
+ bool is_background;
+
+ explicit TaskRecord(task_management::TaskId the_id);
+
+ TaskRecord(task_management::TaskId the_id,
ncarter (slow) 2015/11/13 22:44:32 ctors before data members https://google.github.io
afakhry 2015/11/16 18:44:19 I thought in structs we put the member variable fi
+ const std::string& the_sample,
+ double the_cpu,
+ int64_t the_memory,
+ bool background);
+ };
+
+ ~ResourceReporter() override;
+
+ // The system runs only if the user has opted-in to send anonymous usage
+ // stats. We observe state changes in the MetricsService which will determine
+ // if the ResourceReporter needs to observe the task manager and collect
+ // resource usage stats.
+ static void StartObservingMetricsService();
+ static void StopObservingMetricsService();
+
+ // The singleton instance.
+ static ResourceReporter* GetInstance();
+
+ // task_management::TaskManagerObserver:
+ void OnTaskAdded(task_management::TaskId id) override;
+ void OnTaskToBeRemoved(task_management::TaskId id) override;
+ void OnTasksRefreshed(const task_management::TaskIdList& task_ids) override;
+
+ // metrics::MetricsServiceObserver:
+ void OnMetricsServiceStart() override;
+ void OnMetricsServiceStop() override;
+
+ private:
+ friend struct base::DefaultSingletonTraits<ResourceReporter>;
+ friend class ResourceReporterTest;
+ FRIEND_TEST_ALL_PREFIXES(ResourceReporterTest, TestGetCpuRapporMetricName);
+ FRIEND_TEST_ALL_PREFIXES(ResourceReporterTest, TestGetMemoryRapporMetricName);
+ FRIEND_TEST_ALL_PREFIXES(ResourceReporterTest, TestAll);
+
+ enum TaskProcessPriority {
+ FOREGROUND = 0,
+ BACKGROUND,
+ PRIORITIES_NUM,
+ };
+
+ enum CpuUsageRange {
+ RANGE_0_TO_10_PERCENT = 0,
+ RANGE_10_TO_30_PERCENT,
+ RANGE_30_TO_60_PERCENT,
+ RANGE_ABOVE_60_PERCENT,
+ CPU_RANGES_NUM,
+ };
+
+ enum MemoryUsageRange {
+ RANGE_0_TO_200_MB = 0,
+ RANGE_200_TO_400_MB,
+ RANGE_400_TO_600_MB,
+ RANGE_600_TO_800_MB,
+ RANGE_800_TO_1_GB,
+ RANGE_ABOVE_1_GB,
+ MEMORY_RANGES_NUM,
+ };
+
+ enum CpuCoresNumberRange {
+ RANGE_CORES_NA = 0,
+ RANGE_CORES_1_CORE,
+ RANGE_CORES_2_CORES,
+ RANGE_CORES_3_TO_4_CORES,
+ RANGE_CORES_5_TO_8_CORES,
+ RANGE_CORES_9_TO_16_CORES,
+ RANGE_CORES_ABOVE_16_CORES,
+ CORES_RANGES_NUM,
+ };
+
+ // The maximum number of top consumer tasks of each resource that we're
+ // interested in reporting.
+ static const size_t kTopConsumersCount;
+
+ ResourceReporter();
+
+ // Creates a Rappor sample for the given |task_record|.
+ static scoped_ptr<rappor::Sample> CreateRapporSample(
+ rappor::RapporService* rappor_service,
+ const TaskRecord& task_record);
+
+ // Gets the CPU/memory usage ranges given the |cpu| / |memory_in_bytes|
+ // values.
+ static CpuUsageRange GetCpuUsageRange(double cpu);
+ static MemoryUsageRange GetMemoryUsageRange(int64_t memory_in_bytes);
+
+ // Gets the bucket in which the current system's number of CPU cores fall
+ // into.
+ static CpuCoresNumberRange GetCurrentSystemCpuCoresRange();
+
+ // Start / stop observing the task manager and the memory pressure events.
+ void StartMonitoring();
+ void StopMonitoring();
+
+ // The callback function that will be invoked on memory pressure events.
+ void OnMemoryPressure(MemoryPressureLevel memory_pressure_level);
+
+ // We'll use this to watch for memory pressure events so that we can trigger
+ // Rappor sampling at moderate memory pressure or higher.
+ scoped_ptr<base::MemoryPressureListener> memory_pressure_listener_;
+
+ // Contains the collected data about the currently running tasks from the most
+ // recent task manager refresh.
+ std::map<task_management::TaskId, scoped_ptr<TaskRecord>> task_records_;
+
+ // Contains the top |kTopConsumerCount| CPU consumer tasks sorted in a
+ // descending order by their CPU usage.
+ std::vector<TaskRecord*> task_records_by_cpu_;
+
+ // Contains the top |kTopConsumerCount| memory consumer tasks sorted in a
+ // descending order by their memory usage.
+ std::vector<TaskRecord*> task_records_by_memory_;
+
+ const CpuCoresNumberRange system_cpu_cores_range_;
+
+ // the most recent reading for the browser and GPU processes to be reported as
+ // UMA histograms when the system is under moderate memory pressure or higher.
+ double last_browser_process_cpu_ = 0.0;
+ double last_gpu_process_cpu_ = 0.0;
+ int64_t last_browser_process_memory_ = 0;
+ int64_t last_gpu_process_memory_ = 0;
+
+ MemoryPressureLevel previous_memory_pressure_level_ =
+ MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_NONE;
+
+ // MetricsService::Start()/Stop() can be called multiple times which can cause
+ // all sort of trouble. We need to track whether we have already started
+ // monitoring or not using the below flag.
+ bool is_monitoring_;
+
+ DISALLOW_COPY_AND_ASSIGN(ResourceReporter);
+};
+
+} // namespace chromeos
+
+#endif // CHROME_BROWSER_CHROMEOS_RESOURCE_REPORTER_RESOURCE_REPORTER_H_

Powered by Google App Engine
This is Rietveld 408576698