diff options
author | davemoore@chromium.org <davemoore@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-02 16:58:44 +0000 |
---|---|---|
committer | davemoore@chromium.org <davemoore@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-02 16:58:44 +0000 |
commit | 70baebcd5056f9f0be25e83df3e43f537d18aaeb (patch) | |
tree | 2e7ff71a59e1318e512b5ec799f499110a39dd37 /base | |
parent | 66d73ba41434dc2396bcd87b20b0e53b1be7e9d1 (diff) | |
download | chromium_src-70baebcd5056f9f0be25e83df3e43f537d18aaeb.zip chromium_src-70baebcd5056f9f0be25e83df3e43f537d18aaeb.tar.gz chromium_src-70baebcd5056f9f0be25e83df3e43f537d18aaeb.tar.bz2 |
Refactor parsing of /proc files into separate functions
This refactors out the non-trivial parsing functions from
GetSystemMemoryInfo in preparation for fixing them and adding unit-tests.
BUG=309930
TEST=about:memory still works
R=brettw@chromium.org, davemoore@chromium.org
Review URL: https://codereview.chromium.org/32253007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@232627 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/process/process_metrics.h | 10 | ||||
-rw-r--r-- | base/process/process_metrics_linux.cc | 66 |
2 files changed, 54 insertions, 22 deletions
diff --git a/base/process/process_metrics.h b/base/process/process_metrics.h index 81fd445..560490a 100644 --- a/base/process/process_metrics.h +++ b/base/process/process_metrics.h @@ -278,6 +278,16 @@ struct BASE_EXPORT SystemMemoryInfoKB { #endif }; +// Parses a string containing the contents of /proc/meminfo +// returns true on success or false for a parsing error +BASE_EXPORT bool ParseProcMeminfo(const std::string& input, + SystemMemoryInfoKB* meminfo); + +// Parses a string containing the contents of /proc/vmstat +// returns true on success or false for a parsing error +BASE_EXPORT bool ParseProcVmstat(const std::string& input, + SystemMemoryInfoKB* meminfo); + // Retrieves data from /proc/meminfo and /proc/vmstat // about system-wide memory consumption. // Fills in the provided |meminfo| structure. Returns true on success. diff --git a/base/process/process_metrics_linux.cc b/base/process/process_metrics_linux.cc index 7f46639..e2a97c3 100644 --- a/base/process/process_metrics_linux.cc +++ b/base/process/process_metrics_linux.cc @@ -538,23 +538,13 @@ scoped_ptr<Value> SystemMemoryInfoKB::ToValue() const { return res.PassAs<Value>(); } -bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo) { - // Synchronously reading files in /proc is safe. - ThreadRestrictions::ScopedAllowIO allow_io; - - // Used memory is: total - free - buffers - caches - FilePath meminfo_file("/proc/meminfo"); - std::string meminfo_data; - if (!ReadFileToString(meminfo_file, &meminfo_data)) { - DLOG(WARNING) << "Failed to open " << meminfo_file.value(); - return false; - } +// exposed for testing +bool ParseProcMeminfo(const std::string& meminfo_data, + SystemMemoryInfoKB* meminfo) { std::vector<std::string> meminfo_fields; SplitStringAlongWhitespace(meminfo_data, &meminfo_fields); if (meminfo_fields.size() < kMemCachedIndex) { - DLOG(WARNING) << "Failed to parse " << meminfo_file.value() - << ". Only found " << meminfo_fields.size() << " fields."; return false; } @@ -597,7 +587,44 @@ bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo) { if (meminfo_fields[i] == "Slab:") StringToInt(meminfo_fields[i+1], &meminfo->slab); } +#endif + + return true; +} + +// exposed for testing +bool ParseProcVmstat(const std::string& vmstat_data, + SystemMemoryInfoKB* meminfo) { + std::vector<std::string> vmstat_fields; + SplitStringAlongWhitespace(vmstat_data, &vmstat_fields); + if (vmstat_fields[kVMPagesSwappedIn-1] == "pswpin") + StringToInt(vmstat_fields[kVMPagesSwappedIn], &meminfo->pswpin); + if (vmstat_fields[kVMPagesSwappedOut-1] == "pswpout") + StringToInt(vmstat_fields[kVMPagesSwappedOut], &meminfo->pswpout); + if (vmstat_fields[kVMPageMajorFaults-1] == "pgmajfault") + StringToInt(vmstat_fields[kVMPageMajorFaults], &meminfo->pgmajfault); + + return true; +} + +bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo) { + // Synchronously reading files in /proc is safe. + ThreadRestrictions::ScopedAllowIO allow_io; + + // Used memory is: total - free - buffers - caches + FilePath meminfo_file("/proc/meminfo"); + std::string meminfo_data; + if (!ReadFileToString(meminfo_file, &meminfo_data)) { + DLOG(WARNING) << "Failed to open " << meminfo_file.value(); + return false; + } + if (!ParseProcMeminfo(meminfo_data, meminfo)) { + DLOG(WARNING) << "Failed to parse " << meminfo_file.value(); + return false; + } + +#if defined(OS_CHROMEOS) // Report on Chrome OS GEM object graphics memory. /var/run/debugfs_gpu is a // bind mount into /sys/kernel/debug and synchronously reading the in-memory // files in /sys is fast. @@ -640,15 +667,10 @@ bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo) { DLOG(WARNING) << "Failed to open " << vmstat_file.value(); return false; } - - std::vector<std::string> vmstat_fields; - SplitStringAlongWhitespace(vmstat_data, &vmstat_fields); - if (vmstat_fields[kVMPagesSwappedIn-1] == "pswpin") - StringToInt(vmstat_fields[kVMPagesSwappedIn], &meminfo->pswpin); - if (vmstat_fields[kVMPagesSwappedOut-1] == "pswpout") - StringToInt(vmstat_fields[kVMPagesSwappedOut], &meminfo->pswpout); - if (vmstat_fields[kVMPageMajorFaults-1] == "pgmajfault") - StringToInt(vmstat_fields[kVMPageMajorFaults], &meminfo->pgmajfault); + if (!ParseProcVmstat(vmstat_data, meminfo)) { + DLOG(WARNING) << "Failed to parse " << vmstat_file.value(); + return false; + } return true; } |