Skip to content

Commit

Permalink
Updated device reporting code to report multiple samples of free RAM
Browse files Browse the repository at this point in the history
DeviceStatusCollector used to report multiple CPU usage measurements, but only a
single free-RAM measurement. This CL changes the reporting code to report
multiple free-RAM measurements.

BUG=430908

Review URL: https://codereview.chromium.org/884063003

Cr-Commit-Position: refs/heads/master@{#313681}
  • Loading branch information
atwilson authored and Commit bot committed Jan 29, 2015
1 parent efbbcac commit 98a27f8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 24 deletions.
24 changes: 14 additions & 10 deletions chrome/browser/chromeos/policy/device_status_collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ void DeviceStatusCollector::AddActivePeriod(Time start, Time end) {

void DeviceStatusCollector::ClearCachedHardwareStatus() {
volume_info_.clear();
cpu_usage_percent_.clear();
resource_usage_.clear();
}

void DeviceStatusCollector::IdleStateCallback(ui::IdleState state) {
Expand Down Expand Up @@ -402,22 +402,26 @@ void DeviceStatusCollector::SampleHardwareStatus() {
base::Bind(&DeviceStatusCollector::ReceiveVolumeInfo,
weak_factory_.GetWeakPtr()));

SampleCPUUsage();
SampleResourceUsage();
}

void DeviceStatusCollector::SampleCPUUsage() {
void DeviceStatusCollector::SampleResourceUsage() {
// Walk the process list and measure CPU utilization.
double total_usage = 0;
std::vector<double> per_process_usage = GetPerProcessCPUUsage();
for (double cpu_usage : per_process_usage) {
total_usage += cpu_usage;
}
cpu_usage_percent_.push_back(total_usage);

ResourceUsage usage = { total_usage,
base::SysInfo::AmountOfAvailablePhysicalMemory() };

resource_usage_.push_back(usage);

// If our cache of samples is full, throw out old samples to make room for new
// sample.
if (cpu_usage_percent_.size() > kMaxCPUSamples)
cpu_usage_percent_.pop_front();
if (resource_usage_.size() > kMaxResourceUsageSamples)
resource_usage_.pop_front();
}

std::vector<double> DeviceStatusCollector::GetPerProcessCPUUsage() {
Expand Down Expand Up @@ -654,12 +658,12 @@ void DeviceStatusCollector::GetHardwareStatus(
*status->add_volume_info() = info;
}

status->set_system_ram_free(base::SysInfo::AmountOfAvailablePhysicalMemory());
status->set_system_ram_total(base::SysInfo::AmountOfPhysicalMemory());

status->clear_system_ram_free();
status->clear_cpu_utilization_pct();
for (const int cpu_usage : cpu_usage_percent_) {
status->add_cpu_utilization_pct(cpu_usage);
for (const ResourceUsage& usage : resource_usage_) {
status->add_cpu_utilization_pct(usage.cpu_usage_percent);
status->add_system_ram_free(usage.bytes_of_ram_free);
}
}

Expand Down
21 changes: 15 additions & 6 deletions chrome/browser/chromeos/policy/device_status_collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ class DeviceStatusCollector {
// How often, in seconds, to poll to see if the user is idle.
static const unsigned int kIdlePollIntervalSeconds = 30;

// The total number of CPU samples cached internally.
static const unsigned int kMaxCPUSamples = 10;
// The total number of hardware resource usage samples cached internally.
static const unsigned int kMaxResourceUsageSamples = 10;

protected:
// Check whether the user has been idle for a certain period of time.
Expand All @@ -99,8 +99,8 @@ class DeviceStatusCollector {
// Callback which receives the results of the idle state check.
void IdleStateCallback(ui::IdleState state);

// Samples the current CPU usage and updates our cache of samples.
void SampleCPUUsage();
// Samples the current CPU and RAM usage and updates our cache of samples.
void SampleResourceUsage();

// Returns the percentage of total CPU that each process uses. Virtual so it
// can be mocked.
Expand Down Expand Up @@ -195,9 +195,18 @@ class DeviceStatusCollector {
// Cached disk volume information.
std::vector<enterprise_management::VolumeInfo> volume_info_;

// Cached percentage-of-CPU-used data (contains multiple samples taken
struct ResourceUsage {
// Sample of percentage-of-CPU-used across all processes (0-100)
int cpu_usage_percent;

// Amount of free RAM (measures raw memory used by processes, not internal
// memory waiting to be reclaimed by GC).
int64 bytes_of_ram_free;
};

// Samples of resource usage (contains multiple samples taken
// periodically every kHardwareStatusSampleIntervalSeconds).
std::deque<int> cpu_usage_percent_;
std::deque<ResourceUsage> resource_usage_;

// Callback invoked to fetch information about the mounted disk volumes.
VolumeInfoFetcher volume_info_fetcher_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,14 @@ class TestingDeviceStatusCollector : public policy::DeviceStatusCollector {

mock_cpu_usage_ = usage;

// Refresh our samples.
for (int i = 0; i < static_cast<int>(kMaxCPUSamples); ++i)
SampleCPUUsage();
RefreshSampleResourceUsage();
}

void RefreshSampleResourceUsage() {
// Refresh our samples. Sample more than kMaxHardwareSamples times to
// make sure that the code correctly caps the number of cached samples.
for (int i = 0; i < static_cast<int>(kMaxResourceUsageSamples + 1); ++i)
SampleResourceUsage();
}

protected:
Expand Down Expand Up @@ -790,8 +795,10 @@ TEST_F(DeviceStatusCollectorTest, TestVolumeInfo) {
}

TEST_F(DeviceStatusCollectorTest, TestAvailableMemory) {
status_collector_->RefreshSampleResourceUsage();
GetStatus();
EXPECT_TRUE(status_.has_system_ram_free());
EXPECT_EQ(static_cast<int>(DeviceStatusCollector::kMaxResourceUsageSamples),
status_.system_ram_free().size());
EXPECT_TRUE(status_.has_system_ram_total());
// No good way to inject specific test values for available system RAM, so
// just make sure it's > 0.
Expand All @@ -803,7 +810,7 @@ TEST_F(DeviceStatusCollectorTest, TestCPUSamples) {
const int full_cpu_usage = 100;
status_collector_->set_mock_cpu_usage(full_cpu_usage, 2);
GetStatus();
EXPECT_EQ(static_cast<int>(DeviceStatusCollector::kMaxCPUSamples),
EXPECT_EQ(static_cast<int>(DeviceStatusCollector::kMaxResourceUsageSamples),
status_.cpu_utilization_pct().size());
for (const auto utilization : status_.cpu_utilization_pct())
EXPECT_EQ(full_cpu_usage, utilization);
Expand All @@ -812,7 +819,7 @@ TEST_F(DeviceStatusCollectorTest, TestCPUSamples) {
const int idle_cpu_usage = 0;
status_collector_->set_mock_cpu_usage(idle_cpu_usage, 2);
GetStatus();
EXPECT_EQ(static_cast<int>(DeviceStatusCollector::kMaxCPUSamples),
EXPECT_EQ(static_cast<int>(DeviceStatusCollector::kMaxResourceUsageSamples),
status_.cpu_utilization_pct().size());
for (const auto utilization : status_.cpu_utilization_pct())
EXPECT_EQ(idle_cpu_usage, utilization);
Expand Down
7 changes: 5 additions & 2 deletions components/policy/proto/device_management_backend.proto
Original file line number Diff line number Diff line change
Expand Up @@ -662,14 +662,17 @@ message DeviceStatusReportRequest {
// List of visible/configured networks
repeated NetworkState network_state = 11;

// Samples of CPU utilization (0-100), sampled once every 60 seconds.
// Samples of CPU utilization (0-100), sampled once every 120 seconds.
repeated int32 cpu_utilization_pct = 12;

// Free RAM (unreliable due to GC).
optional int64 system_ram_free = 13;
optional int64 deprecated_system_ram_free = 13 [deprecated = true];

// Total RAM on the device.
optional int64 system_ram_total = 14;

// Samples of free RAM [in bytes] (unreliable due to GC).
repeated int64 system_ram_free = 15;
}

// Provides status information for an installed app/extension.
Expand Down

0 comments on commit 98a27f8

Please sign in to comment.