Commit c2ef94e8 authored by jwmak@chromium.org's avatar jwmak@chromium.org

Implement ToValue() for SystemMetrics, SystemMemoryInfoKB, DiskInfo, and...

Implement ToValue() for SystemMetrics, SystemMemoryInfoKB, DiskInfo, and SwapInfo. These are used to serialize the data inside these structs.

BUG=236763

Review URL: https://chromiumcodereview.appspot.com/23155002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@221696 0039d316-1c4b-4281-b951-d872f2087c98
parent 05cab6cb
......@@ -27,4 +27,19 @@ SystemMetrics SystemMetrics::Sample() {
return system_metrics;
}
scoped_ptr<Value> SystemMetrics::ToValue() const {
scoped_ptr<DictionaryValue> res(new DictionaryValue());
res->SetInteger("committed_memory", static_cast<int>(committed_memory_));
#if defined(OS_LINUX) || defined(OS_ANDROID)
res->Set("meminfo", memory_info_.ToValue().release());
res->Set("diskinfo", disk_info_.ToValue().release());
#endif
#if defined(OS_CHROMEOS)
res->Set("swapinfo", swap_info_.ToValue().release());
#endif
return res.PassAs<Value>();
}
} // namespace base
......@@ -15,6 +15,7 @@
#include "base/gtest_prod_util.h"
#include "base/process/process_handle.h"
#include "base/time/time.h"
#include "base/values.h"
#if defined(OS_MACOSX)
#include <mach/mach.h>
......@@ -243,6 +244,9 @@ BASE_EXPORT extern const char kProcSelfExe[];
struct BASE_EXPORT SystemMemoryInfoKB {
SystemMemoryInfoKB();
// Serializes the platform specific fields to value.
scoped_ptr<Value> ToValue() const;
int total;
int free;
int buffers;
......@@ -279,6 +283,9 @@ BASE_EXPORT bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo);
struct BASE_EXPORT SystemDiskInfo {
SystemDiskInfo();
// Serializes the platform specific fields to value.
scoped_ptr<Value> ToValue() const;
uint64 reads;
uint64 reads_merged;
uint64 sectors_read;
......@@ -313,6 +320,9 @@ struct BASE_EXPORT SwapInfo {
mem_used_total(0) {
}
// Serializes the platform specific fields to value.
scoped_ptr<Value> ToValue() const;
uint64 num_reads;
uint64 num_writes;
uint64 compr_data_size;
......@@ -334,6 +344,9 @@ class SystemMetrics {
static SystemMetrics Sample();
// Serializes the system metrics to value.
scoped_ptr<Value> ToValue() const;
private:
FRIEND_TEST_ALL_PREFIXES(SystemMetricsTest, SystemMetrics);
......
......@@ -510,6 +510,34 @@ SystemMemoryInfoKB::SystemMemoryInfoKB() {
#endif
}
scoped_ptr<Value> SystemMemoryInfoKB::ToValue() const {
scoped_ptr<DictionaryValue> res(new base::DictionaryValue());
res->SetInteger("total", total);
res->SetInteger("free", free);
res->SetInteger("buffers", buffers);
res->SetInteger("cached", cached);
res->SetInteger("active_anon", active_anon);
res->SetInteger("inactive_anon", inactive_anon);
res->SetInteger("active_file", active_file);
res->SetInteger("inactive_file", inactive_file);
res->SetInteger("swap_total", swap_total);
res->SetInteger("swap_free", swap_free);
res->SetInteger("swap_used", swap_total - swap_free);
res->SetInteger("dirty", dirty);
res->SetInteger("pswpin", pswpin);
res->SetInteger("pswpout", pswpout);
res->SetInteger("pgmajfault", pgmajfault);
#ifdef OS_CHROMEOS
res->SetInteger("shmem", shmem);
res->SetInteger("slab", slab);
res->SetInteger("gem_objects", gem_objects);
res->SetInteger("gem_size", gem_size);
#endif
return res.PassAs<Value>();
}
bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo) {
// Synchronously reading files in /proc is safe.
ThreadRestrictions::ScopedAllowIO allow_io;
......@@ -639,6 +667,26 @@ SystemDiskInfo::SystemDiskInfo() {
weighted_io_time = 0;
}
scoped_ptr<Value> SystemDiskInfo::ToValue() const {
scoped_ptr<DictionaryValue> res(new base::DictionaryValue());
// Write out uint64 variables as doubles.
// Note: this may discard some precision, but for JS there's no other option.
res->SetDouble("reads", static_cast<double>(reads));
res->SetDouble("reads_merged", static_cast<double>(reads_merged));
res->SetDouble("sectors_read", static_cast<double>(sectors_read));
res->SetDouble("read_time", static_cast<double>(read_time));
res->SetDouble("writes", static_cast<double>(writes));
res->SetDouble("writes_merged", static_cast<double>(writes_merged));
res->SetDouble("sectors_written", static_cast<double>(sectors_written));
res->SetDouble("write_time", static_cast<double>(write_time));
res->SetDouble("io", static_cast<double>(io));
res->SetDouble("io_time", static_cast<double>(io_time));
res->SetDouble("weighted_io_time", static_cast<double>(weighted_io_time));
return res.PassAs<Value>();
}
bool IsValidDiskName(const std::string& candidate) {
if (candidate.length() < 3)
return false;
......@@ -744,6 +792,25 @@ bool GetSystemDiskInfo(SystemDiskInfo* diskinfo) {
}
#if defined(OS_CHROMEOS)
scoped_ptr<Value> SwapInfo::ToValue() const {
scoped_ptr<DictionaryValue> res(new DictionaryValue());
// Write out uint64 variables as doubles.
// Note: this may discard some precision, but for JS there's no other option.
res->SetDouble("num_reads", static_cast<double>(num_reads));
res->SetDouble("num_writes", static_cast<double>(num_writes));
res->SetDouble("orig_data_size", static_cast<double>(orig_data_size));
res->SetDouble("compr_data_size", static_cast<double>(compr_data_size));
res->SetDouble("mem_used_total", static_cast<double>(mem_used_total));
if (compr_data_size > 0)
res->SetDouble("compression_ratio", static_cast<double>(orig_data_size) /
static_cast<double>(compr_data_size));
else
res->SetDouble("compression_ratio", 0);
return res.PassAs<Value>();
}
void GetSwapInfo(SwapInfo* swap_info) {
// Synchronously reading files in /sys/block/zram0 is safe.
ThreadRestrictions::ScopedAllowIO allow_io;
......
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