Commit d1accc64 authored by Bailey Berro's avatar Bailey Berro Committed by Commit Bot

Add CpuTemperature data to SystemDataProvider's CpuUsage information

This change adds the average CPU Temperature to the CpuUsage struct.

Bug: 1128204
Change-Id: I62e335f163df94b0d53027692f3ae4960e9c48f7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2527825
Auto-Submit: Bailey Berro <baileyberro@chromium.org>
Commit-Queue: Dominick Ng <dominickn@chromium.org>
Reviewed-by: default avatarDominick Ng <dominickn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#826506}
parent 0d701581
...@@ -173,6 +173,18 @@ void PopulateCpuUsagePercentages(const CpuUsageData& new_usage, ...@@ -173,6 +173,18 @@ void PopulateCpuUsagePercentages(const CpuUsageData& new_usage,
out_cpu_usage.percent_usage_free = 100 * delta.GetIdleTime() / total_delta; out_cpu_usage.percent_usage_free = 100 * delta.GetIdleTime() / total_delta;
} }
void PopulateAverageCpuTemperature(const healthd::CpuInfo& cpu_info,
mojom::CpuUsage& out_cpu_usage) {
uint32_t cumulative_total = 0;
for (const auto& temp_channel_ptr : cpu_info.temperature_channels) {
cumulative_total += temp_channel_ptr->temperature_celsius;
}
// Integer divison.
out_cpu_usage.average_cpu_temp_celsius =
cumulative_total / cpu_info.temperature_channels.size();
}
} // namespace } // namespace
SystemDataProvider::SystemDataProvider() { SystemDataProvider::SystemDataProvider() {
...@@ -511,9 +523,11 @@ void SystemDataProvider::OnCpuUsageUpdated(healthd::TelemetryInfoPtr info_ptr) { ...@@ -511,9 +523,11 @@ void SystemDataProvider::OnCpuUsageUpdated(healthd::TelemetryInfoPtr info_ptr) {
} }
ComputeAndPopulateCpuUsage(*cpu_info, *cpu_usage.get()); ComputeAndPopulateCpuUsage(*cpu_info, *cpu_usage.get());
PopulateAverageCpuTemperature(*cpu_info, *cpu_usage.get());
NotifyCpuUsageObservers(cpu_usage); NotifyCpuUsageObservers(cpu_usage);
} }
void SystemDataProvider::ComputeAndPopulateCpuUsage( void SystemDataProvider::ComputeAndPopulateCpuUsage(
const healthd::CpuInfo& cpu_info, const healthd::CpuInfo& cpu_info,
mojom::CpuUsage& out_cpu_usage) { mojom::CpuUsage& out_cpu_usage) {
......
...@@ -221,10 +221,8 @@ void SetCrosHealthdMemoryUsageResponse(uint32_t total_memory_kib, ...@@ -221,10 +221,8 @@ void SetCrosHealthdMemoryUsageResponse(uint32_t total_memory_kib,
/*system_info=*/nullptr); /*system_info=*/nullptr);
} }
// Sets the CpuUsage response on cros_healthd. |usage_data| should contain one void SetCrosHealthdCpuResponse(const std::vector<CpuUsageData>& usage_data,
// entry for each logical cpu. const std::vector<int32_t>& cpu_temps) {
void SetCrosHealthdCpuUsageResponse(
const std::vector<CpuUsageData>& usage_data) {
auto cpu_info_ptr = cros_healthd::mojom::CpuInfo::New(); auto cpu_info_ptr = cros_healthd::mojom::CpuInfo::New();
auto physical_cpu_info_ptr = cros_healthd::mojom::PhysicalCpuInfo::New(); auto physical_cpu_info_ptr = cros_healthd::mojom::PhysicalCpuInfo::New();
...@@ -239,7 +237,14 @@ void SetCrosHealthdCpuUsageResponse( ...@@ -239,7 +237,14 @@ void SetCrosHealthdCpuUsageResponse(
std::move(logical_cpu_info_ptr)); std::move(logical_cpu_info_ptr));
} }
cpu_info_ptr->physical_cpus.emplace_back(std::move(physical_cpu_info_ptr)); cpu_info_ptr->physical_cpus.push_back(std::move(physical_cpu_info_ptr));
for (const auto& cpu_temp : cpu_temps) {
auto cpu_temp_channel_ptr =
cros_healthd::mojom::CpuTemperatureChannel::New();
cpu_temp_channel_ptr->temperature_celsius = cpu_temp;
cpu_info_ptr->temperature_channels.emplace_back(
std::move(cpu_temp_channel_ptr));
}
SetProbeTelemetryInfoResponse(/*battery_info=*/nullptr, SetProbeTelemetryInfoResponse(/*battery_info=*/nullptr,
std::move(cpu_info_ptr), std::move(cpu_info_ptr),
...@@ -247,6 +252,20 @@ void SetCrosHealthdCpuUsageResponse( ...@@ -247,6 +252,20 @@ void SetCrosHealthdCpuUsageResponse(
/*system_info=*/nullptr); /*system_info=*/nullptr);
} }
// Sets the CpuUsage response on cros_healthd. |usage_data| should contain one
// entry for each logical cpu.
void SetCrosHealthdCpuUsageResponse(
const std::vector<CpuUsageData>& usage_data) {
// Use fake temp data since none was supplied.
SetCrosHealthdCpuResponse(usage_data, {50});
}
void SetCrosHealthdCpuTemperatureResponse(
const std::vector<int32_t>& cpu_temps) {
// Use fake usage_data data since none was supplied.
SetCrosHealthdCpuResponse({CpuUsageData(1000, 1000, 1000)}, cpu_temps);
}
bool AreValidPowerTimes(int64_t time_to_full, int64_t time_to_empty) { bool AreValidPowerTimes(int64_t time_to_full, int64_t time_to_empty) {
// Exactly one of |time_to_full| or |time_to_empty| must be zero. The other // Exactly one of |time_to_full| or |time_to_empty| must be zero. The other
// can be a positive integer to represent the time to charge/discharge or -1 // can be a positive integer to represent the time to charge/discharge or -1
...@@ -374,6 +393,11 @@ void VerifyCpuUsageResult(const mojom::CpuUsagePtr& update, ...@@ -374,6 +393,11 @@ void VerifyCpuUsageResult(const mojom::CpuUsagePtr& update,
EXPECT_EQ(expected_percent_free, update->percent_usage_free); EXPECT_EQ(expected_percent_free, update->percent_usage_free);
} }
void VerifyCpuTempResult(const mojom::CpuUsagePtr& update,
uint32_t expected_average_temp) {
EXPECT_EQ(expected_average_temp, update->average_cpu_temp_celsius);
}
} // namespace } // namespace
struct FakeBatteryChargeStatusObserver struct FakeBatteryChargeStatusObserver
...@@ -782,5 +806,56 @@ TEST_F(SystemDataProviderTest, CpuUsageObserverTwoProcessor) { ...@@ -782,5 +806,56 @@ TEST_F(SystemDataProviderTest, CpuUsageObserverTwoProcessor) {
expected_percent_system, expected_percent_free); expected_percent_system, expected_percent_free);
} }
TEST_F(SystemDataProviderTest, CpuUsageObserverTemp) {
// Setup Timer
auto timer = std::make_unique<base::MockRepeatingTimer>();
auto* timer_ptr = timer.get();
system_data_provider_->SetCpuUsageTimerForTesting(std::move(timer));
// Setup initial data
uint32_t temp_1 = 40;
uint32_t temp_2 = 50;
uint32_t temp_3 = 15;
SetCrosHealthdCpuTemperatureResponse({temp_1, temp_2, temp_3});
// Registering as an observer should trigger one update.
FakeCpuUsageObserver cpu_usage_observer;
system_data_provider_->ObserveCpuUsage(
cpu_usage_observer.receiver.BindNewPipeAndPassRemote());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1u, cpu_usage_observer.updates.size());
VerifyCpuTempResult(cpu_usage_observer.updates[0],
/*expected_average_temp=*/35);
temp_1 = 20;
temp_2 = 25;
temp_3 = 45;
SetCrosHealthdCpuTemperatureResponse({temp_1, temp_2, temp_3});
timer_ptr->Fire();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(2u, cpu_usage_observer.updates.size());
VerifyCpuTempResult(cpu_usage_observer.updates[1],
/*expected_average_temp=*/30);
temp_1 = 20;
temp_2 = 26;
temp_3 = 46;
SetCrosHealthdCpuTemperatureResponse({temp_1, temp_2, temp_3});
timer_ptr->Fire();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(3u, cpu_usage_observer.updates.size());
// Integer division so `expected_average_temp` should still be 30.
VerifyCpuTempResult(cpu_usage_observer.updates[2],
/*expected_average_temp=*/30);
}
} // namespace diagnostics } // namespace diagnostics
} // namespace chromeos } // namespace chromeos
...@@ -75,6 +75,7 @@ struct CpuUsage { ...@@ -75,6 +75,7 @@ struct CpuUsage {
uint8 percent_usage_user; uint8 percent_usage_user;
uint8 percent_usage_system; uint8 percent_usage_system;
uint8 percent_usage_free; uint8 percent_usage_free;
uint16 average_cpu_temp_celsius;
}; };
// Implemented by clients that wish to be updated periodically about changes to // Implemented by clients that wish to be updated periodically about changes to
......
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