Commit a0896c26 authored by Nikhil Athreya's avatar Nikhil Athreya Committed by Commit Bot

power: Pass process usage data from c++ to js

Recognizes a request for process usage data from ProcessDataCollector
when an IPC message from the render (invoked from Javascript) is
invoked and passes the information in the
ProcessDataCollector::ProcessUsageData struct's to the JS code.

requestProcessUsageData from the JS console.

BUG: crbug.com/851767
TEST: On a Chrome OS device, go to chrome://power and call
Change-Id: Ief597fd97032254c3675371d6a0419cdea0e49f1
Reviewed-on: https://chromium-review.googlesource.com/1157133
Commit-Queue: Nikhil Athreya <nathreya@google.com>
Reviewed-by: default avatarDemetrios Papadopoulos <dpapad@chromium.org>
Reviewed-by: default avatarMichael Giuffrida <michaelpg@chromium.org>
Reviewed-by: default avatarDan Erat <derat@chromium.org>
Cr-Commit-Position: refs/heads/master@{#580061}
parent 8e837e57
...@@ -37,14 +37,15 @@ namespace chromeos { ...@@ -37,14 +37,15 @@ namespace chromeos {
// and is implemented as a global singleton. // and is implemented as a global singleton.
class CHROMEOS_EXPORT ProcessDataCollector { class CHROMEOS_EXPORT ProcessDataCollector {
public: public:
// The different sources of power consumption being tracked. // The different sources of power consumption being tracked. This should be
// kept in sync with the |PowerConsumerType| in power.js.
enum class PowerConsumerType { enum class PowerConsumerType {
SCREEN, SCREEN = 0,
KEYBOARD, KEYBOARD = 1,
CROSTINI, CROSTINI = 2,
ARC, ARC = 3,
CHROME, CHROME = 4,
SYSTEM SYSTEM = 5
}; };
// Contains basic information about a process like its PID, its name, etc. The // Contains basic information about a process like its PID, its name, etc. The
......
...@@ -4,11 +4,13 @@ ...@@ -4,11 +4,13 @@
<meta charset="utf-8"> <meta charset="utf-8">
<title>$i18n{titleText}</title> <title>$i18n{titleText}</title>
<link rel="stylesheet" href="chrome://resources/css/text_defaults.css"> <link rel="stylesheet" href="chrome://resources/css/text_defaults.css">
<link rel="stylesheet" href="chrome://power/power.css"> <link rel="stylesheet" href="power.css">
<script src="chrome://resources/js/promise_resolver.js"></script>
<script src="chrome://resources/js/cr.js"></script>
<script src="chrome://resources/js/load_time_data.js"></script> <script src="chrome://resources/js/load_time_data.js"></script>
<script src="chrome://resources/js/util.js"></script> <script src="chrome://resources/js/util.js"></script>
<script src="chrome://power/strings.js"></script> <script src="strings.js"></script>
<script src="chrome://power/power.js"></script> <script src="power.js"></script>
</head> </head>
<body> <body>
<table id="main-table"> <table id="main-table">
......
...@@ -2,6 +2,20 @@ ...@@ -2,6 +2,20 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
/**
* The different types of power consumer types. Should be kept in sync with the
* PowerConsumerType in ProcessDataCollector.
* @enum {number}
*/
const PowerConsumerType = {
SCREEN: 0,
KEYBOARD: 1,
CROSTINI: 2,
ARC: 3,
CHROME: 4,
SYSTEM: 5
};
/** /**
* Plot a line graph of data versus time on a HTML canvas element. * Plot a line graph of data versus time on a HTML canvas element.
* *
...@@ -763,6 +777,11 @@ function showCpuFreqData(freqStateData, systemResumedArray) { ...@@ -763,6 +777,11 @@ function showCpuFreqData(freqStateData, systemResumedArray) {
'frequencyStateOccupancyPercentageHeader', 'MHz', 'cpu-freq-plots-div'); 'frequencyStateOccupancyPercentageHeader', 'MHz', 'cpu-freq-plots-div');
} }
function showProcessUsageData(processUsageData) {
// TODO(crbug.com/851767): Add the code to create a suitable UI for this
// information.
}
function requestBatteryChargeData() { function requestBatteryChargeData() {
chrome.send('requestBatteryChargeData'); chrome.send('requestBatteryChargeData');
} }
...@@ -775,6 +794,10 @@ function requestCpuFreqData() { ...@@ -775,6 +794,10 @@ function requestCpuFreqData() {
chrome.send('requestCpuFreqData'); chrome.send('requestCpuFreqData');
} }
function requestProcessUsageData() {
cr.sendWithPromise('requestProcessUsageData').then(showProcessUsageData);
}
/** /**
* Return a callback for the 'Show'/'Hide' buttons for each section of the * Return a callback for the 'Show'/'Hide' buttons for each section of the
* about:power page. * about:power page.
......
...@@ -7,7 +7,9 @@ ...@@ -7,7 +7,9 @@
#include <stddef.h> #include <stddef.h>
#include <memory> #include <memory>
#include <string>
#include <utility> #include <utility>
#include <vector>
#include "base/bind.h" #include "base/bind.h"
#include "base/bind_helpers.h" #include "base/bind_helpers.h"
...@@ -17,6 +19,7 @@ ...@@ -17,6 +19,7 @@
#include "base/time/time.h" #include "base/time/time.h"
#include "base/values.h" #include "base/values.h"
#include "chrome/browser/chromeos/power/power_data_collector.h" #include "chrome/browser/chromeos/power/power_data_collector.h"
#include "chrome/browser/chromeos/power/process_data_collector.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/common/url_constants.h" #include "chrome/common/url_constants.h"
#include "chrome/grit/browser_resources.h" #include "chrome/grit/browser_resources.h"
...@@ -43,6 +46,8 @@ const char kRequestCpuFreqDataCallback[] = "requestCpuFreqData"; ...@@ -43,6 +46,8 @@ const char kRequestCpuFreqDataCallback[] = "requestCpuFreqData";
const char kOnRequestCpuFreqDataFunction[] = const char kOnRequestCpuFreqDataFunction[] =
"powerUI.showCpuFreqData"; "powerUI.showCpuFreqData";
const char kRequestProcessUsageDataCallback[] = "requestProcessUsageData";
class PowerMessageHandler : public content::WebUIMessageHandler { class PowerMessageHandler : public content::WebUIMessageHandler {
public: public:
PowerMessageHandler(); PowerMessageHandler();
...@@ -55,6 +60,7 @@ class PowerMessageHandler : public content::WebUIMessageHandler { ...@@ -55,6 +60,7 @@ class PowerMessageHandler : public content::WebUIMessageHandler {
void OnGetBatteryChargeData(const base::ListValue* value); void OnGetBatteryChargeData(const base::ListValue* value);
void OnGetCpuIdleData(const base::ListValue* value); void OnGetCpuIdleData(const base::ListValue* value);
void OnGetCpuFreqData(const base::ListValue* value); void OnGetCpuFreqData(const base::ListValue* value);
void OnGetProcessUsageData(const base::ListValue* value);
void GetJsStateOccupancyData( void GetJsStateOccupancyData(
const std::vector<CpuDataCollector::StateOccupancySampleDeque>& data, const std::vector<CpuDataCollector::StateOccupancySampleDeque>& data,
const std::vector<std::string>& state_names, const std::vector<std::string>& state_names,
...@@ -81,6 +87,10 @@ void PowerMessageHandler::RegisterMessages() { ...@@ -81,6 +87,10 @@ void PowerMessageHandler::RegisterMessages() {
kRequestCpuFreqDataCallback, kRequestCpuFreqDataCallback,
base::BindRepeating(&PowerMessageHandler::OnGetCpuFreqData, base::BindRepeating(&PowerMessageHandler::OnGetCpuFreqData,
base::Unretained(this))); base::Unretained(this)));
web_ui()->RegisterMessageCallback(
kRequestProcessUsageDataCallback,
base::BindRepeating(&PowerMessageHandler::OnGetProcessUsageData,
base::Unretained(this)));
} }
void PowerMessageHandler::OnGetBatteryChargeData(const base::ListValue* value) { void PowerMessageHandler::OnGetBatteryChargeData(const base::ListValue* value) {
...@@ -142,6 +152,32 @@ void PowerMessageHandler::OnGetCpuFreqData(const base::ListValue* value) { ...@@ -142,6 +152,32 @@ void PowerMessageHandler::OnGetCpuFreqData(const base::ListValue* value) {
js_freq_data, js_system_resumed_data); js_freq_data, js_system_resumed_data);
} }
void PowerMessageHandler::OnGetProcessUsageData(const base::ListValue* args) {
AllowJavascript();
CHECK_EQ(1U, args->GetSize());
const base::Value* callback_id;
CHECK(args->Get(0, &callback_id));
const std::vector<ProcessDataCollector::ProcessUsageData>& process_list =
ProcessDataCollector::Get()->GetProcessUsages();
base::ListValue js_process_usages;
for (const auto& process_info : process_list) {
std::unique_ptr<base::DictionaryValue> element =
std::make_unique<base::DictionaryValue>();
element->SetInteger("pid", process_info.process_data.pid);
element->SetString("name", process_info.process_data.name);
element->SetString("cmdline", process_info.process_data.cmdline);
element->SetInteger("type",
static_cast<int>(process_info.process_data.type));
element->SetDouble("powerUsageFraction", process_info.power_usage_fraction);
js_process_usages.Append(std::move(element));
}
ResolveJavascriptCallback(*callback_id, js_process_usages);
}
void PowerMessageHandler::GetJsSystemResumedData(base::ListValue *data) { void PowerMessageHandler::GetJsSystemResumedData(base::ListValue *data) {
DCHECK(data); DCHECK(data);
......
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