0

Rename {absl => std}::optional Final n°2

#cleanup

Automated patch. This is a no-op. Please avoid, as much as possible,
assigning unrelated bugs to this.

Context:
https://groups.google.com/a/chromium.org/g/cxx/c/nBD_1LaanTc/m/ghh-ZZhWAwAJ?utm_medium=email

As of https://crrev.com/1204351, absl::optional is now a type alias
for std::optional. We should migrate toward it.

Script:
```
function replace {
  echo "Replacing $1 by $2"
  git grep -l "$1" \
    | cut -f1 -d: \
    | grep -v \
      -e "third_party/abseil-cpp/*" \
      -e "third_party/googletest/*" \
      -e "third_party/leveldatabase/*" \
      -e "third_party/libaddressinput/" \
      -e "third_party/liburlpattern/*" \
      -e "third_party/lzma_sdk/*" \
      -e "third_party/maldoca/*" \
      -e "third_party/mediapipe/*" \
      -e "third_party/shell-encryption/*"\
      -e "third_party/tflite_support/*" \
      -e "third_party/webrtc_overrides/*" \
      -e "third_party/zxcvbn-cpp/*" \
    | grep \
      -e "\.h" \
      -e "\.cc" \
      -e "\.mm" \
      -e "\.pidl" \
    | sort \
    | uniq \
    | xargs sed -i "s/$1/$2/g"
}
replace "absl::make_optional" "std::make_optional"
replace "absl::optional" "std::optional"
replace "absl::nullopt" "std::nullopt"
replace "absl::in_place," "std::in_place,"
replace "absl::in_place_t," "std::in_place_t,"
replace "absl::in_place)" "std::in_place)"
replace "absl::in_place_t)" "std::in_place_t)"
replace "\"third_party\/abseil-cpp\/absl\/types\/optional.h\"" "<optional>"

git status
echo "Formatting"

echo "IncludeBlocks: Regroup" >> ".clang-format"
echo "IncludeIsMainRegex: \"(_(android|apple|chromeos|freebsd|fuchsia|fuzzer|ios|linux|mac|nacl|openbsd|posix|stubs?|win))?(_(unit|browser|perf)?tests?)?$\"" >> ".clang-format"
git cl format
git restore ".clang-format"
```

Cleanup: rename
Bug: chromium:1500249
Change-Id: I7cb6bc208109aa9b0a2d70e6cd5a7253c3cc4c0e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5321725
Commit-Queue: danakj <danakj@chromium.org>
Owners-Override: danakj <danakj@chromium.org>
Reviewed-by: danakj <danakj@chromium.org>
Auto-Submit: Arthur Sonzogni <arthursonzogni@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1265221}
This commit is contained in:
Arthur Sonzogni
2024-02-26 15:49:43 +00:00
committed by Chromium LUCI CQ
parent 3e1f8946b9
commit 405944145a
22 changed files with 120 additions and 118 deletions

@ -5,11 +5,11 @@
#ifndef COMPONENTS_POWER_METRICS_ENERGY_IMPACT_MAC_H_
#define COMPONENTS_POWER_METRICS_ENERGY_IMPACT_MAC_H_
#include <optional>
#include <string>
#include "base/files/file_path.h"
#include "base/time/time.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
struct coalition_resource_usage;
@ -47,7 +47,7 @@ struct EnergyImpactCoefficients {
// Reads the Energy Impact coefficients for the current machine from disk, or
// default coefficients if coefficients are not available for the current
// machine.
absl::optional<EnergyImpactCoefficients>
std::optional<EnergyImpactCoefficients>
ReadCoefficientsForCurrentMachineOrDefault();
// Computes the Energy Impact score for the resource consumption data in
@ -70,7 +70,7 @@ namespace internal {
// Reads the coefficients from the "energy_constants" sub-dictionary of the
// plist file at |plist_file|. This is exposed for testing, production code
// should use ReadCoefficientsForCurrentMachineOrDefault().
absl::optional<EnergyImpactCoefficients> ReadCoefficientsFromPath(
std::optional<EnergyImpactCoefficients> ReadCoefficientsFromPath(
const base::FilePath& plist_file);
// Given a |directory| and a |board_id|, read the plist file for the board id
@ -78,7 +78,7 @@ absl::optional<EnergyImpactCoefficients> ReadCoefficientsFromPath(
// Returns true if either file can be loaded, false otherwise. This is exposed
// for testing, production code should use
// ReadCoefficientsForCurrentMachineOrDefault().
absl::optional<EnergyImpactCoefficients> ReadCoefficientsForBoardIdOrDefault(
std::optional<EnergyImpactCoefficients> ReadCoefficientsForBoardIdOrDefault(
const base::FilePath& directory,
const std::string& board_id);
@ -86,7 +86,7 @@ absl::optional<EnergyImpactCoefficients> ReadCoefficientsForBoardIdOrDefault(
// the current machine. This appears to work for Intel Macs only. This is
// exposed for testing, production code should use
// ReadCoefficientsForCurrentMachineOrDefault().
absl::optional<std::string> GetBoardIdForThisMachine();
std::optional<std::string> GetBoardIdForThisMachine();
} // namespace internal

@ -33,11 +33,11 @@ double GetNamedCoefficientOrZero(NSDictionary* dict, NSString* key) {
} // namespace
absl::optional<EnergyImpactCoefficients>
std::optional<EnergyImpactCoefficients>
ReadCoefficientsForCurrentMachineOrDefault() {
absl::optional<std::string> board_id = internal::GetBoardIdForThisMachine();
std::optional<std::string> board_id = internal::GetBoardIdForThisMachine();
if (!board_id.has_value())
return absl::nullopt;
return std::nullopt;
return internal::ReadCoefficientsForBoardIdOrDefault(
base::FilePath(FILE_PATH_LITERAL("/usr/share/pmenergy")),
@ -108,17 +108,17 @@ double ComputeEnergyImpactForResourceUsage(
namespace internal {
absl::optional<EnergyImpactCoefficients> ReadCoefficientsFromPath(
std::optional<EnergyImpactCoefficients> ReadCoefficientsFromPath(
const base::FilePath& plist_file) {
@autoreleasepool {
NSDictionary* dict = MaybeGetDictionaryFromPath(plist_file);
if (!dict) {
return absl::nullopt;
return std::nullopt;
}
NSDictionary* energy_constants = dict[@"energy_constants"];
if (!energy_constants) {
return absl::nullopt;
return std::nullopt;
}
EnergyImpactCoefficients coefficients{};
@ -161,7 +161,7 @@ absl::optional<EnergyImpactCoefficients> ReadCoefficientsFromPath(
}
}
absl::optional<EnergyImpactCoefficients> ReadCoefficientsForBoardIdOrDefault(
std::optional<EnergyImpactCoefficients> ReadCoefficientsForBoardIdOrDefault(
const base::FilePath& directory,
const std::string& board_id) {
auto coefficients = ReadCoefficientsFromPath(
@ -173,12 +173,12 @@ absl::optional<EnergyImpactCoefficients> ReadCoefficientsForBoardIdOrDefault(
directory.Append(FILE_PATH_LITERAL("default.plist")));
}
absl::optional<std::string> GetBoardIdForThisMachine() {
std::optional<std::string> GetBoardIdForThisMachine() {
base::mac::ScopedIOObject<io_service_t> platform_expert(
IOServiceGetMatchingService(kIOMasterPortDefault,
IOServiceMatching("IOPlatformExpertDevice")));
if (!platform_expert)
return absl::nullopt;
return std::nullopt;
// This is what libpmenergy is observed to do in order to retrieve the correct
// coefficients file for the local computer.
@ -187,7 +187,7 @@ absl::optional<std::string> GetBoardIdForThisMachine() {
platform_expert.get(), CFSTR("board-id"), kCFAllocatorDefault, 0)));
if (!board_id_data)
return absl::nullopt;
return std::nullopt;
return reinterpret_cast<const char*>(CFDataGetBytePtr(board_id_data.get()));
}

@ -7,8 +7,7 @@
#include <cstdint>
#include <memory>
#include "third_party/abseil-cpp/absl/types/optional.h"
#include <optional>
namespace power_metrics {
@ -45,7 +44,7 @@ class EnergyMetricsProvider {
// Used to capture energy consumption metrics. It returns nullopt if not
// available on the current platform.
virtual absl::optional<EnergyMetrics> CaptureMetrics() = 0;
virtual std::optional<EnergyMetrics> CaptureMetrics() = 0;
protected:
// The constructor is intentionally only exposed to subclasses. Production

@ -107,10 +107,10 @@ EnergyMetricsProviderLinux::Create() {
return base::WrapUnique(new EnergyMetricsProviderLinux());
}
absl::optional<EnergyMetricsProvider::EnergyMetrics>
std::optional<EnergyMetricsProvider::EnergyMetrics>
EnergyMetricsProviderLinux::CaptureMetrics() {
if (!Initialize()) {
return absl::nullopt;
return std::nullopt;
}
EnergyMetrics energy_metrics = {0};

@ -44,7 +44,7 @@ class EnergyMetricsProviderLinux : public EnergyMetricsProvider {
~EnergyMetricsProviderLinux() override;
// EnergyMetricsProvider implementation.
absl::optional<EnergyMetrics> CaptureMetrics() override;
std::optional<EnergyMetrics> CaptureMetrics() override;
private:
EnergyMetricsProviderLinux();

@ -65,11 +65,11 @@ std::unique_ptr<EnergyMetricsProviderWin> EnergyMetricsProviderWin::Create() {
return base::WrapUnique(new EnergyMetricsProviderWin());
}
absl::optional<EnergyMetricsProvider::EnergyMetrics>
std::optional<EnergyMetricsProvider::EnergyMetrics>
EnergyMetricsProviderWin::CaptureMetrics() {
if (!Initialize()) {
handle_.Close();
return absl::nullopt;
return std::nullopt;
}
base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
@ -85,7 +85,7 @@ EnergyMetricsProviderWin::CaptureMetrics() {
measurement_data.data(), measurement_data_size_bytes,
&bytes_returned, nullptr)) {
PLOG(ERROR) << "IOCTL_EMI_GET_MEASUREMENT failed";
return absl::nullopt;
return std::nullopt;
}
CHECK_EQ(bytes_returned, measurement_data_size_bytes);

@ -26,7 +26,7 @@ class EnergyMetricsProviderWin : public EnergyMetricsProvider {
~EnergyMetricsProviderWin() override;
// EnergyMetricsProvider implementation.
absl::optional<EnergyMetrics> CaptureMetrics() override;
std::optional<EnergyMetrics> CaptureMetrics() override;
private:
EnergyMetricsProviderWin();

@ -11,9 +11,9 @@
#include <IOKit/hidsystem/IOHIDEventSystemClient.h>
#include <memory>
#include <optional>
#include "base/apple/scoped_cftyperef.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
namespace power_metrics {
@ -24,8 +24,8 @@ class M1SensorsReader {
TemperaturesCelsius(const TemperaturesCelsius&) noexcept;
~TemperaturesCelsius();
absl::optional<double> p_cores;
absl::optional<double> e_cores;
std::optional<double> p_cores;
std::optional<double> e_cores;
};
virtual ~M1SensorsReader();

@ -32,12 +32,12 @@ namespace power_metrics {
namespace {
absl::optional<double> GetEventFloatValue(IOHIDServiceClientRef service,
int64_t event_type) {
std::optional<double> GetEventFloatValue(IOHIDServiceClientRef service,
int64_t event_type) {
base::apple::ScopedCFTypeRef<CFTypeRef> event(
IOHIDServiceClientCopyEvent(service, event_type, 0, 0));
if (!event)
return absl::nullopt;
return std::nullopt;
return IOHIDEventGetFloatValue(event.get(), IOHIDEventFieldBase(event_type));
}
@ -92,7 +92,7 @@ M1SensorsReader::TemperaturesCelsius M1SensorsReader::ReadTemperatures() {
}
if (CFStringHasPrefix(product.get(), CFSTR("pACC MTR Temp Sensor"))) {
absl::optional<double> temp =
std::optional<double> temp =
GetEventFloatValue(service, kIOHIDEventTypeTemperature);
if (temp.has_value()) {
num_p_core_temp += 1;
@ -101,7 +101,7 @@ M1SensorsReader::TemperaturesCelsius M1SensorsReader::ReadTemperatures() {
}
if (CFStringHasPrefix(product.get(), CFSTR("eACC MTR Temp Sensor"))) {
absl::optional<double> temp =
std::optional<double> temp =
GetEventFloatValue(service, kIOHIDEventTypeTemperature);
if (temp.has_value()) {
num_e_core_temp += 1;

@ -11,15 +11,15 @@
#ifndef COMPONENTS_POWER_METRICS_RESOURCE_COALITION_MAC_H_
#define COMPONENTS_POWER_METRICS_RESOURCE_COALITION_MAC_H_
#include <stdint.h>
#include <memory>
#include <mach/mach_time.h>
#include <stdint.h>
#include <memory>
#include <optional>
#include "base/process/process_handle.h"
#include "base/time/time.h"
#include "components/power_metrics/resource_coalition_internal_types_mac.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
namespace power_metrics {
@ -27,7 +27,7 @@ struct EnergyImpactCoefficients;
// Returns the coalition id for the process identified by |pid| or nullopt if
// not available.
absl::optional<uint64_t> GetProcessCoalitionId(base::ProcessId pid);
std::optional<uint64_t> GetProcessCoalitionId(base::ProcessId pid);
// Returns resource usage data for the coalition identified by |coalition_id|,
// or nullptr if not available (e.g. if `coalition_id` is invalid or if the
@ -50,7 +50,7 @@ struct CoalitionResourceUsageRate {
double byteswritten_per_second;
double gpu_time_per_second;
// Only makes sense on Intel macs, not computed on M1 macs.
absl::optional<double> energy_impact_per_second;
std::optional<double> energy_impact_per_second;
// Only available on M1 macs as of September 2021.
double power_nw;
@ -59,12 +59,12 @@ struct CoalitionResourceUsageRate {
// Returns rate of resource usage for a coalition, given the usage at
// the beginning and end of an interval and the duration of the interval.
absl::optional<CoalitionResourceUsageRate> GetCoalitionResourceUsageRate(
std::optional<CoalitionResourceUsageRate> GetCoalitionResourceUsageRate(
const coalition_resource_usage& begin,
const coalition_resource_usage& end,
base::TimeDelta interval_duration,
mach_timebase_info_data_t timebase,
absl::optional<EnergyImpactCoefficients> energy_impact_coefficients);
std::optional<EnergyImpactCoefficients> energy_impact_coefficients);
} // namespace power_metrics

@ -17,13 +17,13 @@ extern "C" int coalition_info_resource_usage(
namespace power_metrics {
absl::optional<uint64_t> GetProcessCoalitionId(base::ProcessId pid) {
std::optional<uint64_t> GetProcessCoalitionId(base::ProcessId pid) {
proc_pidcoalitioninfo coalition_info = {};
int res = proc_pidinfo(pid, PROC_PIDCOALITIONINFO, 0, &coalition_info,
sizeof(coalition_info));
if (res != sizeof(coalition_info))
return absl::nullopt;
return std::nullopt;
return coalition_info.coalition_id[COALITION_TYPE_RESOURCE];
}
@ -140,12 +140,12 @@ coalition_resource_usage GetCoalitionResourceUsageDifference(
return ret;
}
absl::optional<CoalitionResourceUsageRate> GetCoalitionResourceUsageRate(
std::optional<CoalitionResourceUsageRate> GetCoalitionResourceUsageRate(
const coalition_resource_usage& begin,
const coalition_resource_usage& end,
base::TimeDelta interval_duration,
mach_timebase_info_data_t timebase,
absl::optional<EnergyImpactCoefficients> energy_impact_coefficients) {
std::optional<EnergyImpactCoefficients> energy_impact_coefficients) {
// Validate that |end| >= |begin|.
bool end_greater_or_equal_begin =
std::tie(end.cpu_time, end.interrupt_wakeups, end.platform_idle_wakeups,
@ -158,7 +158,7 @@ absl::optional<CoalitionResourceUsageRate> GetCoalitionResourceUsageRate(
end_greater_or_equal_begin = false;
}
if (!end_greater_or_equal_begin)
return absl::nullopt;
return std::nullopt;
auto get_rate_per_second = [&interval_duration](uint64_t begin,
uint64_t end) -> double {

@ -4,10 +4,11 @@
#include "components/power_metrics/resource_coalition_mac.h"
#include <optional>
#include "base/rand_util.h"
#include "components/power_metrics/energy_impact_mac.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
namespace power_metrics {
@ -83,7 +84,7 @@ void BurnCPU() {
} // namespace
TEST(ResourceCoalitionMacTest, Busy) {
absl::optional<uint64_t> coalition_id =
std::optional<uint64_t> coalition_id =
GetProcessCoalitionId(base::GetCurrentProcId());
ASSERT_TRUE(coalition_id.has_value());
@ -213,7 +214,7 @@ TEST(ResourceCoalitionMacTest, GetDataRate_NoEnergyImpact_Intel) {
GetCoalitionResourceUsageRateTestData(kIntelTimebase);
auto rate = GetCoalitionResourceUsageRate(
*t0_data, *t1_data, kIntervalDuration, kIntelTimebase, absl::nullopt);
*t0_data, *t1_data, kIntervalDuration, kIntelTimebase, std::nullopt);
ASSERT_TRUE(rate);
EXPECT_EQ(kExpectedCPUUsagePerSecondPercent, rate->cpu_time_per_second);
EXPECT_EQ(kExpectedInterruptWakeUpPerSecond,
@ -240,7 +241,7 @@ TEST(ResourceCoalitionMacTest, GetDataRate_NoEnergyImpact_M1) {
GetCoalitionResourceUsageRateTestData(kM1Timebase);
auto rate = GetCoalitionResourceUsageRate(
*t0_data, *t1_data, kIntervalDuration, kM1Timebase, absl::nullopt);
*t0_data, *t1_data, kIntervalDuration, kM1Timebase, std::nullopt);
ASSERT_TRUE(rate);
EXPECT_DOUBLE_EQ(kExpectedCPUUsagePerSecondPercent,
rate->cpu_time_per_second);
@ -330,7 +331,7 @@ bool DataOverflowInvalidatesDiffImpl(
*field_to_overflow = 0;
t1->cpu_time_eqos_len = COALITION_NUM_THREAD_QOS_TYPES;
return !GetCoalitionResourceUsageRate(*t0, *t1, kIntervalDuration,
kIntelTimebase, absl::nullopt)
kIntelTimebase, std::nullopt)
.has_value();
}

@ -12,11 +12,11 @@
#import <Foundation/Foundation.h>
#include <memory>
#include <optional>
#include "base/containers/flat_map.h"
#include "base/mac/scoped_ioobject.h"
#include "components/power_metrics/smc_internal_types_mac.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
namespace power_metrics {
@ -29,7 +29,7 @@ class SMCReader {
// Returns the value of a key, or nullopt if not available.
// Virtual for testing.
virtual absl::optional<double> ReadKey(SMCKeyIdentifier identifier);
virtual std::optional<double> ReadKey(SMCKeyIdentifier identifier);
protected:
explicit SMCReader(base::mac::ScopedIOObject<io_object_t> connect);
@ -44,7 +44,7 @@ class SMCReader {
~SMCKey();
bool Exists() const;
absl::optional<double> Read();
std::optional<double> Read();
private:
bool CallSMCFunction(uint8_t function, SMCParamStruct* out);

@ -35,7 +35,7 @@ std::unique_ptr<SMCReader> SMCReader::Create() {
SMCReader::~SMCReader() = default;
absl::optional<double> SMCReader::ReadKey(SMCKeyIdentifier identifier) {
std::optional<double> SMCReader::ReadKey(SMCKeyIdentifier identifier) {
auto it = keys_.find(identifier);
if (it == keys_.end()) {
auto result = keys_.emplace(identifier, SMCKey(connect_, identifier));
@ -63,13 +63,13 @@ bool SMCReader::SMCKey::Exists() const {
return key_info_.dataSize > 0;
}
absl::optional<double> SMCReader::SMCKey::Read() {
std::optional<double> SMCReader::SMCKey::Read() {
if (!Exists())
return absl::nullopt;
return std::nullopt;
SMCParamStruct out{};
if (!CallSMCFunction(kSMCReadKey, &out))
return absl::nullopt;
return std::nullopt;
switch (key_info_.dataType) {
case SMCDataType::flt:
return *reinterpret_cast<float*>(out.bytes);
@ -80,7 +80,7 @@ absl::optional<double> SMCReader::SMCKey::Read() {
case SMCDataType::spa5:
return FromSMCFixedPoint(out.bytes, 5);
default:
return absl::nullopt;
return std::nullopt;
}
}

@ -15,10 +15,10 @@ class FakeProvider : public EnergyMetricsProvider {
public:
void set_metrics(EnergyMetrics metrics) { metrics_ = metrics; }
absl::optional<EnergyMetrics> CaptureMetrics() override { return metrics_; }
std::optional<EnergyMetrics> CaptureMetrics() override { return metrics_; }
private:
absl::optional<EnergyMetrics> metrics_;
std::optional<EnergyMetrics> metrics_;
};
class FakeDelegate : public SystemPowerMonitorDelegate {
@ -140,7 +140,7 @@ TEST_F(SystemPowerMonitorHelperTest, MonitorHelperStartStop) {
TEST_F(SystemPowerMonitorHelperTest, TimerStartFailed_InvalidSample) {
// We haven't set metrics for provider, so monitor gets an
// absl::nullopt sample at the beginning and it will not start.
// std::nullopt sample at the beginning and it will not start.
helper()->Start();
ASSERT_FALSE(helper()->IsTimerRunningForTesting());
}