0

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:
Patrick Monette
2022-08-18 16:19:53 +00:00
committed by Chromium LUCI CQ
parent 4812d200bc
commit b4a569eca1
13 changed files with 99 additions and 105 deletions

@ -553,6 +553,10 @@ mixed_component("base") {
"power_monitor/power_monitor_source.cc",
"power_monitor/power_monitor_source.h",
"power_monitor/power_observer.h",
"power_monitor/sampling_event_source.cc",
"power_monitor/sampling_event_source.h",
"power_monitor/timer_sampling_event_source.cc",
"power_monitor/timer_sampling_event_source.h",
"process/environment_internal.cc",
"process/environment_internal.h",
"process/kill.cc",
@ -1289,6 +1293,8 @@ mixed_component("base") {
"message_loop/message_pump_mac.h",
"message_loop/message_pump_mac.mm",
"native_library_mac.mm",
"power_monitor/iopm_power_source_sampling_event_source.cc",
"power_monitor/iopm_power_source_sampling_event_source.h",
"process/kill_mac.cc",
"process/launch_mac.cc",
"process/memory_mac.mm",
@ -3222,6 +3228,7 @@ test("base_unittests") {
"power_monitor/moving_average_unittest.cc",
"power_monitor/power_monitor_device_source_unittest.cc",
"power_monitor/power_monitor_unittest.cc",
"power_monitor/timer_sampling_event_source_unittest.cc",
"process/environment_internal_unittest.cc",
"process/memory_unittest.cc",
"process/process_metrics_unittest.cc",

@ -2,7 +2,7 @@
// 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 "base/power_monitor/iopm_power_source_sampling_event_source.h"
#include <IOKit/IOMessage.h>
#include <dispatch/queue.h>
@ -10,7 +10,7 @@
#include "base/check.h"
#include "base/logging.h"
namespace power_metrics {
namespace base {
IOPMPowerSourceSamplingEventSource::IOPMPowerSourceSamplingEventSource() =
default;
@ -64,4 +64,4 @@ void IOPMPowerSourceSamplingEventSource::OnNotification(
self->callback_.Run();
}
} // namespace power_metrics
} // namespace base

@ -2,19 +2,21 @@
// 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_
#ifndef BASE_POWER_MONITOR_IOPM_POWER_SOURCE_SAMPLING_EVENT_SOURCE_H_
#define BASE_POWER_MONITOR_IOPM_POWER_SOURCE_SAMPLING_EVENT_SOURCE_H_
#include "base/base_export.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"
#include "base/power_monitor/sampling_event_source.h"
namespace power_metrics {
namespace base {
// Generates a sampling event when a state change notification is dispatched by
// the IOPMPowerSource service.
class IOPMPowerSourceSamplingEventSource : public SamplingEventSource {
class BASE_EXPORT IOPMPowerSourceSamplingEventSource
: public SamplingEventSource {
public:
IOPMPowerSourceSamplingEventSource();
@ -29,12 +31,12 @@ class IOPMPowerSourceSamplingEventSource : public SamplingEventSource {
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_;
mac::ScopedIONotificationPortRef notify_port_;
mac::ScopedIOObject<io_service_t> service_;
mac::ScopedIOObject<io_object_t> notification_;
SamplingEventCallback callback_;
};
} // namespace power_metrics
} // namespace base
#endif // COMPONENTS_POWER_METRICS_IOPM_POWER_SOURCE_SAMPLING_EVENT_SOURCE_H_
#endif // BASE_POWER_MONITOR_IOPM_POWER_SOURCE_SAMPLING_EVENT_SOURCE_H_

@ -2,10 +2,10 @@
// 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"
#include "base/power_monitor/sampling_event_source.h"
namespace power_metrics {
namespace base {
SamplingEventSource::~SamplingEventSource() = default;
} // namespace power_metrics
} // namespace base

@ -2,17 +2,18 @@
// 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_
#ifndef BASE_POWER_MONITOR_SAMPLING_EVENT_SOURCE_H_
#define BASE_POWER_MONITOR_SAMPLING_EVENT_SOURCE_H_
#include "base/base_export.h"
#include "base/callback_forward.h"
namespace power_metrics {
namespace base {
// Invokes a callback when a Sample should be requested from all Samplers.
class SamplingEventSource {
class BASE_EXPORT SamplingEventSource {
public:
using SamplingEventCallback = base::RepeatingClosure;
using SamplingEventCallback = RepeatingClosure;
virtual ~SamplingEventSource() = 0;
@ -21,6 +22,6 @@ class SamplingEventSource {
virtual bool Start(SamplingEventCallback callback) = 0;
};
} // namespace power_metrics
} // namespace base
#endif // COMPONENTS_POWER_METRICS_SAMPLING_EVENT_SOURCE_H_
#endif // BASE_POWER_MONITOR_SAMPLING_EVENT_SOURCE_H_

@ -2,13 +2,13 @@
// 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/power_monitor/timer_sampling_event_source.h"
#include "base/check.h"
namespace power_metrics {
namespace base {
TimerSamplingEventSource::TimerSamplingEventSource(base::TimeDelta interval)
TimerSamplingEventSource::TimerSamplingEventSource(TimeDelta interval)
: interval_(interval) {}
TimerSamplingEventSource::~TimerSamplingEventSource() = default;
@ -19,4 +19,4 @@ bool TimerSamplingEventSource::Start(SamplingEventCallback callback) {
return true;
}
} // namespace power_metrics
} // namespace base

@ -0,0 +1,33 @@
// 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 BASE_POWER_MONITOR_TIMER_SAMPLING_EVENT_SOURCE_H_
#define BASE_POWER_MONITOR_TIMER_SAMPLING_EVENT_SOURCE_H_
#include "base/base_export.h"
#include "base/power_monitor/sampling_event_source.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
namespace base {
// Generates a sampling event at regular time intervals.
class BASE_EXPORT TimerSamplingEventSource : public SamplingEventSource {
public:
// |interval| is the time interval between sampling events.
explicit TimerSamplingEventSource(TimeDelta interval);
~TimerSamplingEventSource() override;
// SamplingEventSource:
bool Start(SamplingEventCallback callback) override;
private:
const TimeDelta interval_;
RepeatingTimer timer_;
};
} // namespace base
#endif // BASE_POWER_MONITOR_TIMER_SAMPLING_EVENT_SOURCE_H_

@ -2,23 +2,22 @@
// 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/power_monitor/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 {
namespace base {
TEST(TimerSamplingEventSourceTest, Basic) {
constexpr base::TimeDelta kDelay = base::Seconds(1);
constexpr TimeDelta kDelay = Seconds(1);
int num_callbacks = 0;
base::test::SingleThreadTaskEnvironment task_environment(
base::test::TaskEnvironment::TimeSource::MOCK_TIME);
test::SingleThreadTaskEnvironment task_environment(
test::TaskEnvironment::TimeSource::MOCK_TIME);
TimerSamplingEventSource source(kDelay);
EXPECT_TRUE(
source.Start(base::BindLambdaForTesting([&]() { ++num_callbacks; })));
EXPECT_TRUE(source.Start(BindLambdaForTesting([&]() { ++num_callbacks; })));
EXPECT_EQ(0, num_callbacks);
task_environment.FastForwardBy(kDelay / 2);
EXPECT_EQ(0, num_callbacks);
@ -28,4 +27,4 @@ TEST(TimerSamplingEventSourceTest, Basic) {
EXPECT_EQ(11, num_callbacks);
}
} // namespace power_metrics
} // namespace base

@ -20,8 +20,8 @@
#include "third_party/abseil-cpp/absl/types/optional.h"
#if BUILDFLAG(IS_MAC)
#include "base/power_monitor/iopm_power_source_sampling_event_source.h"
#include "chrome/browser/metrics/power/coalition_resource_usage_provider_mac.h"
#include "components/power_metrics/iopm_power_source_sampling_event_source.h"
#endif // BUILDFLAG(IS_MAC)
// Reports metrics related to power (battery discharge, cpu time, etc.).
@ -173,7 +173,7 @@ class PowerMetricsReporter : public ProcessMonitor::Observer {
// (MaybeEmitHighCPUTraceEvent()).
base::TimeTicks short_interval_begin_time_;
power_metrics::IOPMPowerSourceSamplingEventSource
base::IOPMPowerSourceSamplingEventSource
iopm_power_source_sampling_event_source_;
// The time ticks from when the last IOPMPowerSource event was received.

@ -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,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_

@ -72,7 +72,6 @@ executable("power_sampler") {
deps = [
":power_sampler_lib",
"//base",
"//components/power_metrics",
]
}

@ -11,14 +11,14 @@
#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/power_monitor/iopm_power_source_sampling_event_source.h"
#include "base/power_monitor/timer_sampling_event_source.h"
#include "base/process/process_handle.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/task/single_thread_task_executor.h"
#include "base/time/time.h"
#include "components/power_metrics/iopm_power_source_sampling_event_source.h"
#include "components/power_metrics/timer_sampling_event_source.h"
#include "tools/mac/power/power_sampler/battery_sampler.h"
#include "tools/mac/power/power_sampler/csv_exporter.h"
#include "tools/mac/power/power_sampler/json_exporter.h"
@ -211,13 +211,12 @@ int main(int argc, char** argv) {
}
}
std::unique_ptr<power_metrics::SamplingEventSource> event_source;
std::unique_ptr<base::SamplingEventSource> event_source;
if (command_line.HasSwitch(kSwitchSampleOnNotification)) {
event_source =
std::make_unique<power_metrics::IOPMPowerSourceSamplingEventSource>();
event_source = std::make_unique<base::IOPMPowerSourceSamplingEventSource>();
} else {
event_source = std::make_unique<power_metrics::TimerSamplingEventSource>(
sampling_interval);
event_source =
std::make_unique<base::TimerSamplingEventSource>(sampling_interval);
}
base::SingleThreadTaskExecutor executor(base::MessagePumpType::NS_RUNLOOP);