Move SamplingEventSource to base/power_monitor/
This move is done in preparation for adding BatteryStateSampler in the same directory, which will depend on SamplingEventSource. Bug: 1248057 Change-Id: I91022e465614d1fbcbccfaa4fd782349a83e3534 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3819886 Reviewed-by: Etienne Pierre-Doray <etiennep@chromium.org> Commit-Queue: Patrick Monette <pmonette@chromium.org> Reviewed-by: Daniel Cheng <dcheng@chromium.org> Cr-Commit-Position: refs/heads/main@{#1036633}
This commit is contained in:

committed by
Chromium LUCI CQ

parent
4812d200bc
commit
b4a569eca1
@ -2,20 +2,11 @@
|
||||
# Use of this source code is governed by a BSD-style license that can be
|
||||
# found in the LICENSE file.
|
||||
|
||||
static_library("power_metrics") {
|
||||
sources = [
|
||||
"sampling_event_source.cc",
|
||||
"sampling_event_source.h",
|
||||
"timer_sampling_event_source.cc",
|
||||
"timer_sampling_event_source.h",
|
||||
]
|
||||
|
||||
if (is_mac) {
|
||||
sources += [
|
||||
if (is_mac) {
|
||||
static_library("power_metrics") {
|
||||
sources = [
|
||||
"energy_impact_mac.h",
|
||||
"energy_impact_mac.mm",
|
||||
"iopm_power_source_sampling_event_source.cc",
|
||||
"iopm_power_source_sampling_event_source.h",
|
||||
"m1_sensors_internal_types_mac.h",
|
||||
"m1_sensors_mac.h",
|
||||
"m1_sensors_mac.mm",
|
||||
@ -28,29 +19,24 @@ static_library("power_metrics") {
|
||||
"smc_mac.h",
|
||||
"smc_mac.mm",
|
||||
]
|
||||
|
||||
deps = [ "//base" ]
|
||||
}
|
||||
|
||||
deps = [ "//base" ]
|
||||
}
|
||||
source_set("unit_tests") {
|
||||
testonly = true
|
||||
|
||||
source_set("unit_tests") {
|
||||
testonly = true
|
||||
|
||||
sources = [ "timer_sampling_event_source_unittest.cc" ]
|
||||
|
||||
deps = [
|
||||
":power_metrics",
|
||||
"//base",
|
||||
"//base/test:test_support",
|
||||
"//testing/gtest",
|
||||
]
|
||||
|
||||
if (is_mac) {
|
||||
sources += [
|
||||
sources = [
|
||||
"energy_impact_mac_unittest.mm",
|
||||
"resource_coalition_mac_unittest.mm",
|
||||
]
|
||||
|
||||
data = [ "test/data/" ]
|
||||
|
||||
deps = [
|
||||
":power_metrics",
|
||||
"//base",
|
||||
"//testing/gtest",
|
||||
]
|
||||
}
|
||||
}
|
||||
|
@ -1,67 +0,0 @@
|
||||
// Copyright 2021 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 "components/power_metrics/iopm_power_source_sampling_event_source.h"
|
||||
|
||||
#include <IOKit/IOMessage.h>
|
||||
#include <dispatch/queue.h>
|
||||
|
||||
#include "base/check.h"
|
||||
#include "base/logging.h"
|
||||
|
||||
namespace power_metrics {
|
||||
|
||||
IOPMPowerSourceSamplingEventSource::IOPMPowerSourceSamplingEventSource() =
|
||||
default;
|
||||
|
||||
IOPMPowerSourceSamplingEventSource::~IOPMPowerSourceSamplingEventSource() =
|
||||
default;
|
||||
|
||||
bool IOPMPowerSourceSamplingEventSource::Start(SamplingEventCallback callback) {
|
||||
DCHECK(!callback_);
|
||||
DCHECK(callback);
|
||||
|
||||
callback_ = callback;
|
||||
|
||||
service_.reset(IOServiceGetMatchingService(
|
||||
kIOMasterPortDefault, IOServiceMatching("IOPMPowerSource")));
|
||||
|
||||
if (!service_) {
|
||||
LOG(ERROR) << "IOPMPowerSource service not found";
|
||||
return false;
|
||||
}
|
||||
|
||||
notify_port_.reset(IONotificationPortCreate(kIOMasterPortDefault));
|
||||
if (!notify_port_.is_valid()) {
|
||||
LOG(ERROR) << "Could not create a notification port";
|
||||
return false;
|
||||
}
|
||||
|
||||
IONotificationPortSetDispatchQueue(notify_port_.get(),
|
||||
dispatch_get_main_queue());
|
||||
|
||||
kern_return_t result = IOServiceAddInterestNotification(
|
||||
notify_port_.get(), service_, kIOGeneralInterest, OnNotification, this,
|
||||
notification_.InitializeInto());
|
||||
|
||||
if (result != KERN_SUCCESS) {
|
||||
LOG(ERROR) << "Could not register to IOPMPowerSource notifications";
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// static
|
||||
void IOPMPowerSourceSamplingEventSource::OnNotification(
|
||||
void* context,
|
||||
io_service_t service,
|
||||
natural_t message_type,
|
||||
void* message_argument) {
|
||||
IOPMPowerSourceSamplingEventSource* self =
|
||||
static_cast<IOPMPowerSourceSamplingEventSource*>(context);
|
||||
self->callback_.Run();
|
||||
}
|
||||
|
||||
} // namespace power_metrics
|
@ -1,40 +0,0 @@
|
||||
// Copyright 2021 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 COMPONENTS_POWER_METRICS_IOPM_POWER_SOURCE_SAMPLING_EVENT_SOURCE_H_
|
||||
#define COMPONENTS_POWER_METRICS_IOPM_POWER_SOURCE_SAMPLING_EVENT_SOURCE_H_
|
||||
|
||||
#include "base/callback.h"
|
||||
#include "base/mac/scoped_ionotificationportref.h"
|
||||
#include "base/mac/scoped_ioobject.h"
|
||||
#include "components/power_metrics/sampling_event_source.h"
|
||||
|
||||
namespace power_metrics {
|
||||
|
||||
// Generates a sampling event when a state change notification is dispatched by
|
||||
// the IOPMPowerSource service.
|
||||
class IOPMPowerSourceSamplingEventSource : public SamplingEventSource {
|
||||
public:
|
||||
IOPMPowerSourceSamplingEventSource();
|
||||
|
||||
~IOPMPowerSourceSamplingEventSource() override;
|
||||
|
||||
// SamplingEventSource:
|
||||
bool Start(SamplingEventCallback callback) override;
|
||||
|
||||
private:
|
||||
static void OnNotification(void* context,
|
||||
io_service_t service,
|
||||
natural_t message_type,
|
||||
void* message_argument);
|
||||
|
||||
base::mac::ScopedIONotificationPortRef notify_port_;
|
||||
base::mac::ScopedIOObject<io_service_t> service_;
|
||||
base::mac::ScopedIOObject<io_object_t> notification_;
|
||||
SamplingEventCallback callback_;
|
||||
};
|
||||
|
||||
} // namespace power_metrics
|
||||
|
||||
#endif // COMPONENTS_POWER_METRICS_IOPM_POWER_SOURCE_SAMPLING_EVENT_SOURCE_H_
|
@ -1,11 +0,0 @@
|
||||
// Copyright 2021 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 "components/power_metrics/sampling_event_source.h"
|
||||
|
||||
namespace power_metrics {
|
||||
|
||||
SamplingEventSource::~SamplingEventSource() = default;
|
||||
|
||||
} // namespace power_metrics
|
@ -1,26 +0,0 @@
|
||||
// Copyright 2021 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 COMPONENTS_POWER_METRICS_SAMPLING_EVENT_SOURCE_H_
|
||||
#define COMPONENTS_POWER_METRICS_SAMPLING_EVENT_SOURCE_H_
|
||||
|
||||
#include "base/callback_forward.h"
|
||||
|
||||
namespace power_metrics {
|
||||
|
||||
// Invokes a callback when a Sample should be requested from all Samplers.
|
||||
class SamplingEventSource {
|
||||
public:
|
||||
using SamplingEventCallback = base::RepeatingClosure;
|
||||
|
||||
virtual ~SamplingEventSource() = 0;
|
||||
|
||||
// Starts generating sampling events. Returns whether the operation succeeded.
|
||||
// |callback| is invoked for every sampling event.
|
||||
virtual bool Start(SamplingEventCallback callback) = 0;
|
||||
};
|
||||
|
||||
} // namespace power_metrics
|
||||
|
||||
#endif // COMPONENTS_POWER_METRICS_SAMPLING_EVENT_SOURCE_H_
|
@ -1,22 +0,0 @@
|
||||
// Copyright 2021 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 "components/power_metrics/timer_sampling_event_source.h"
|
||||
|
||||
#include "base/check.h"
|
||||
|
||||
namespace power_metrics {
|
||||
|
||||
TimerSamplingEventSource::TimerSamplingEventSource(base::TimeDelta interval)
|
||||
: interval_(interval) {}
|
||||
|
||||
TimerSamplingEventSource::~TimerSamplingEventSource() = default;
|
||||
|
||||
bool TimerSamplingEventSource::Start(SamplingEventCallback callback) {
|
||||
DCHECK(callback);
|
||||
timer_.Start(FROM_HERE, interval_, std::move(callback));
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace power_metrics
|
@ -1,32 +0,0 @@
|
||||
// Copyright 2021 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 COMPONENTS_POWER_METRICS_TIMER_SAMPLING_EVENT_SOURCE_H_
|
||||
#define COMPONENTS_POWER_METRICS_TIMER_SAMPLING_EVENT_SOURCE_H_
|
||||
|
||||
#include "base/time/time.h"
|
||||
#include "base/timer/timer.h"
|
||||
#include "components/power_metrics/sampling_event_source.h"
|
||||
|
||||
namespace power_metrics {
|
||||
|
||||
// Generates a sampling event at regular time intervals.
|
||||
class TimerSamplingEventSource : public SamplingEventSource {
|
||||
public:
|
||||
// |interval| is the time interval between sampling events.
|
||||
explicit TimerSamplingEventSource(base::TimeDelta interval);
|
||||
|
||||
~TimerSamplingEventSource() override;
|
||||
|
||||
// SamplingEventSource:
|
||||
bool Start(SamplingEventCallback callback) override;
|
||||
|
||||
private:
|
||||
const base::TimeDelta interval_;
|
||||
base::RepeatingTimer timer_;
|
||||
};
|
||||
|
||||
} // namespace power_metrics
|
||||
|
||||
#endif // COMPONENTS_POWER_METRICS_TIMER_SAMPLING_EVENT_SOURCE_H_
|
@ -1,31 +0,0 @@
|
||||
// Copyright 2021 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 "components/power_metrics/timer_sampling_event_source.h"
|
||||
|
||||
#include "base/test/bind.h"
|
||||
#include "base/test/task_environment.h"
|
||||
#include "testing/gtest/include/gtest/gtest.h"
|
||||
|
||||
namespace power_metrics {
|
||||
|
||||
TEST(TimerSamplingEventSourceTest, Basic) {
|
||||
constexpr base::TimeDelta kDelay = base::Seconds(1);
|
||||
int num_callbacks = 0;
|
||||
base::test::SingleThreadTaskEnvironment task_environment(
|
||||
base::test::TaskEnvironment::TimeSource::MOCK_TIME);
|
||||
|
||||
TimerSamplingEventSource source(kDelay);
|
||||
EXPECT_TRUE(
|
||||
source.Start(base::BindLambdaForTesting([&]() { ++num_callbacks; })));
|
||||
EXPECT_EQ(0, num_callbacks);
|
||||
task_environment.FastForwardBy(kDelay / 2);
|
||||
EXPECT_EQ(0, num_callbacks);
|
||||
task_environment.FastForwardBy(kDelay / 2);
|
||||
EXPECT_EQ(1, num_callbacks);
|
||||
task_environment.FastForwardBy(kDelay * 10);
|
||||
EXPECT_EQ(11, num_callbacks);
|
||||
}
|
||||
|
||||
} // namespace power_metrics
|
Reference in New Issue
Block a user