Commit 9e939314 authored by Bailey Berro's avatar Bailey Berro Committed by Commit Bot

Introduce CpuUsageData class

This change introduces the CpuUsageData and accompanying tests.

Bug: 1128204
Change-Id: I401b4884be3cac434196da1ab35977c9e8866e3d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2522055
Commit-Queue: Bailey Berro <baileyberro@chromium.org>
Reviewed-by: default avatarZentaro Kavanagh <zentaro@chromium.org>
Auto-Submit: Bailey Berro <baileyberro@chromium.org>
Cr-Commit-Position: refs/heads/master@{#824939}
parent 53b2dae3
......@@ -6,6 +6,8 @@ assert(is_chromeos, "Non-ChromeOS builds cannot depend on //chromeos")
static_library("backend") {
sources = [
"cpu_usage_data.cc",
"cpu_usage_data.h",
"cros_healthd_helpers.cc",
"cros_healthd_helpers.h",
"diagnostics_manager.cc",
......@@ -32,6 +34,7 @@ source_set("unit_tests") {
testonly = true
sources = [
"cpu_usage_data_unittest.cc",
"power_manager_client_conversions_unittest.cc",
"system_data_provider_unittest.cc",
"system_routine_controller_unittest.cc",
......
// Copyright 2020 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 "chromeos/components/diagnostics_ui/backend/cpu_usage_data.h"
namespace chromeos {
namespace diagnostics {
CpuUsageData::CpuUsageData(uint64_t user_time,
uint64_t system_time,
uint64_t idle_time)
: user_time_(user_time), system_time_(system_time), idle_time_(idle_time) {}
bool CpuUsageData::IsInitialized() const {
return user_time_ != std::numeric_limits<uint64_t>::max() &&
system_time_ != std::numeric_limits<uint64_t>::max() &&
idle_time_ != std::numeric_limits<uint64_t>::max();
}
uint64_t CpuUsageData::GetTotalTime() const {
return user_time_ + system_time_ + idle_time_;
}
CpuUsageData CpuUsageData::operator+(const CpuUsageData& other) const {
return CpuUsageData(user_time_ + other.user_time_,
system_time_ + other.system_time_,
idle_time_ + other.idle_time_);
}
CpuUsageData& CpuUsageData::operator+=(const CpuUsageData& other) {
user_time_ += other.user_time_;
system_time_ += other.system_time_;
idle_time_ += other.idle_time_;
return *this;
}
CpuUsageData CpuUsageData::operator-(const CpuUsageData& other) const {
return CpuUsageData(user_time_ - other.user_time_,
system_time_ - other.system_time_,
idle_time_ - other.idle_time_);
}
CpuUsageData& CpuUsageData::operator-=(const CpuUsageData& other) {
user_time_ -= other.user_time_;
system_time_ -= other.system_time_;
idle_time_ -= other.idle_time_;
return *this;
}
} // namespace diagnostics
} // namespace chromeos
// Copyright 2020 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 CHROMEOS_COMPONENTS_DIAGNOSTICS_UI_BACKEND_CPU_USAGE_DATA_H_
#define CHROMEOS_COMPONENTS_DIAGNOSTICS_UI_BACKEND_CPU_USAGE_DATA_H_
#include <cstdint>
#include <limits>
namespace chromeos {
namespace diagnostics {
class CpuUsageData {
public:
CpuUsageData() = default;
CpuUsageData(uint64_t user_time, uint64_t system_time, uint64_t idle_time);
~CpuUsageData() = default;
CpuUsageData(const CpuUsageData& other) = default;
CpuUsageData& operator=(const CpuUsageData& other) = default;
bool IsInitialized() const;
uint64_t GetUserTime() const { return user_time_; }
uint64_t GetSystemTime() const { return system_time_; }
uint64_t GetIdleTime() const { return idle_time_; }
uint64_t GetTotalTime() const;
CpuUsageData operator+(const CpuUsageData& other) const;
CpuUsageData& operator+=(const CpuUsageData& other);
CpuUsageData operator-(const CpuUsageData& other) const;
CpuUsageData& operator-=(const CpuUsageData& other);
private:
uint64_t user_time_ = std::numeric_limits<uint64_t>::max();
uint64_t system_time_ = std::numeric_limits<uint64_t>::max();
uint64_t idle_time_ = std::numeric_limits<uint64_t>::max();
};
} // namespace diagnostics
} // namespace chromeos
#endif // CHROMEOS_COMPONENTS_DIAGNOSTICS_UI_BACKEND_CPU_USAGE_DATA_H_
// Copyright 2020 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 "chromeos/components/diagnostics_ui/backend/cpu_usage_data.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
namespace diagnostics {
class CpuUsageDataTest : public testing::Test {
public:
CpuUsageDataTest() = default;
~CpuUsageDataTest() override = default;
};
TEST_F(CpuUsageDataTest, Initialized) {
CpuUsageData data;
EXPECT_FALSE(data.IsInitialized());
data = CpuUsageData(1, 2, 3);
EXPECT_TRUE(data.IsInitialized());
}
TEST_F(CpuUsageDataTest, Total) {
CpuUsageData data(1, 2, 3);
EXPECT_EQ(6u, data.GetTotalTime());
}
TEST_F(CpuUsageDataTest, Addition) {
CpuUsageData data_1(1, 2, 3);
CpuUsageData data_2(4, 5, 6);
CpuUsageData data_3 = data_1 + data_2;
EXPECT_EQ(5u, data_3.GetUserTime());
EXPECT_EQ(7u, data_3.GetSystemTime());
EXPECT_EQ(9u, data_3.GetIdleTime());
}
TEST_F(CpuUsageDataTest, CompoundAddition) {
CpuUsageData data_1(1, 2, 3);
CpuUsageData data_2(4, 5, 6);
data_1 += data_2;
EXPECT_EQ(5u, data_1.GetUserTime());
EXPECT_EQ(7u, data_1.GetSystemTime());
EXPECT_EQ(9u, data_1.GetIdleTime());
}
TEST_F(CpuUsageDataTest, Subtraction) {
CpuUsageData data_1(1, 2, 3);
CpuUsageData data_2(4, 5, 6);
CpuUsageData data_3 = data_2 - data_1;
EXPECT_EQ(3u, data_3.GetUserTime());
EXPECT_EQ(3u, data_3.GetSystemTime());
EXPECT_EQ(3u, data_3.GetIdleTime());
}
TEST_F(CpuUsageDataTest, CompoundSubtraction) {
CpuUsageData data_1(1, 2, 3);
CpuUsageData data_2(4, 5, 6);
data_2 -= data_1;
EXPECT_EQ(3u, data_2.GetUserTime());
EXPECT_EQ(3u, data_2.GetSystemTime());
EXPECT_EQ(3u, data_2.GetIdleTime());
}
} // namespace diagnostics
} // namespace chromeos
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