0

Add power events check in EnergyMetricsProviderLinux::Initialize()

Some Linux Platforms do not have power events, on which energy metrics
are unavailable. Check if power events exist to reduce unnecessary code
execution and get correct warning.

Bug: 1385251
Change-Id: I99bc4f51981f71f0b0a2f266895f0ad8fa9434a7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4576801
Reviewed-by: Francois Pierre Doray <fdoray@chromium.org>
Commit-Queue: Shiyi Zou <shiyi.zou@intel.com>
Cr-Commit-Position: refs/heads/main@{#1152341}
This commit is contained in:
Shiyi Zou
2023-06-02 03:09:03 +00:00
committed by Chromium LUCI CQ
parent dc8656a289
commit f1f294374d

@ -14,6 +14,7 @@
#include "base/files/scoped_file.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
@ -21,6 +22,8 @@ namespace power_metrics {
namespace {
constexpr const char* kPowerEventPath = "/sys/bus/event_source/devices/power";
// Existing metrics that can be read via perf event.
constexpr std::array<const char*, 5> kMetrics{
"energy-pkg", "energy-cores", "energy-gpu", "energy-ram", "energy-psys"};
@ -134,6 +137,12 @@ bool EnergyMetricsProviderLinux::Initialize() {
is_initialized_ = true;
// Check if there are available power-related events on local platform.
if (!base::PathExists(base::FilePath(kPowerEventPath))) {
LOG(WARNING) << "No available power event";
return false;
}
// Check if perf_event_paranoid is set to 0 as required.
uint64_t perf_event_paranoid;
if (!ReadUint64FromFile(
@ -151,7 +160,7 @@ bool EnergyMetricsProviderLinux::Initialize() {
// type for perf_event_attr from /sys/bus/event_source/devices/power/type.
uint64_t attr_type;
if (!ReadUint64FromFile(
base::FilePath("/sys/bus/event_source/devices/power/type"),
base::FilePath(base::StrCat({kPowerEventPath, "/type"})),
&attr_type)) {
LOG(WARNING) << "Failed to get perf event type";
return false;
@ -160,11 +169,9 @@ bool EnergyMetricsProviderLinux::Initialize() {
// For each metric, get their file descriptors.
for (auto* const metric : kMetrics) {
base::FilePath config_path =
base::FilePath("/sys/bus/event_source/devices/power/events")
.Append(FILE_PATH_LITERAL(metric));
base::FilePath scale_path =
base::FilePath("/sys/bus/event_source/devices/power/events")
.Append(FILE_PATH_LITERAL(metric + std::string(".scale")));
base::FilePath(base::StrCat({kPowerEventPath, "/events/", metric}));
base::FilePath scale_path = base::FilePath(
base::StrCat({kPowerEventPath, "/events/", metric, ".scale"}));
// Some energy metrics may be unavailable on different platforms, so the
// corresponding file path does not exist, which is normal.
if (!base::PathExists(config_path) || !base::PathExists(scale_path)) {