diff options
author | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-30 19:28:44 +0000 |
---|---|---|
committer | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-30 19:28:44 +0000 |
commit | 528c56de01bbbd38788ed6cf8d2eea4c56cbe19e (patch) | |
tree | ac4f7a001affd772c4ab89701d3d46109b5f9e19 /base/process_util_linux.cc | |
parent | 5c86ada8d84f6e67d17b027d347052ef451241c4 (diff) | |
download | chromium_src-528c56de01bbbd38788ed6cf8d2eea4c56cbe19e.zip chromium_src-528c56de01bbbd38788ed6cf8d2eea4c56cbe19e.tar.gz chromium_src-528c56de01bbbd38788ed6cf8d2eea4c56cbe19e.tar.bz2 |
Move the number conversions from string_util to a new file.
Use the base namespace in the new file. Update callers.
I removed all wstring variants and also the string->number ones that ignore the return value. That encourages people to write code and forget about error handling.
TEST=included unit tests
BUG=none
Review URL: http://codereview.chromium.org/3056029
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@54355 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/process_util_linux.cc')
-rw-r--r-- | base/process_util_linux.cc | 88 |
1 files changed, 56 insertions, 32 deletions
diff --git a/base/process_util_linux.cc b/base/process_util_linux.cc index d3d029e..8b8a0c7 100644 --- a/base/process_util_linux.cc +++ b/base/process_util_linux.cc @@ -17,6 +17,7 @@ #include "base/file_util.h" #include "base/logging.h" +#include "base/string_number_conversions.h" #include "base/string_tokenizer.h" #include "base/string_util.h" #include "base/sys_info.h" @@ -32,7 +33,7 @@ enum ParsingState { // spaces. void GetProcStats(pid_t pid, std::vector<std::string>* proc_stats) { FilePath stat_file("/proc"); - stat_file = stat_file.Append(IntToString(pid)); + stat_file = stat_file.Append(base::IntToString(pid)); stat_file = stat_file.Append("stat"); std::string mem_stats; if (!file_util::ReadFileToString(stat_file, &mem_stats)) @@ -46,7 +47,7 @@ namespace base { ProcessId GetParentProcessId(ProcessHandle process) { FilePath stat_file("/proc"); - stat_file = stat_file.Append(IntToString(process)); + stat_file = stat_file.Append(base::IntToString(process)); stat_file = stat_file.Append("status"); std::string status; if (!file_util::ReadFileToString(stat_file, &status)) @@ -64,7 +65,8 @@ ProcessId GetParentProcessId(ProcessHandle process) { case KEY_VALUE: DCHECK(!last_key_name.empty()); if (last_key_name == "PPid") { - pid_t ppid = StringToInt(tokenizer.token()); + int ppid; + base::StringToInt(tokenizer.token(), &ppid); return ppid; } state = KEY_NAME; @@ -77,7 +79,7 @@ ProcessId GetParentProcessId(ProcessHandle process) { FilePath GetProcessExecutablePath(ProcessHandle process) { FilePath stat_file("/proc"); - stat_file = stat_file.Append(IntToString(process)); + stat_file = stat_file.Append(base::IntToString(process)); stat_file = stat_file.Append("exe"); char exename[2048]; ssize_t len = readlink(stat_file.value().c_str(), exename, sizeof(exename)); @@ -205,8 +207,11 @@ size_t ProcessMetrics::GetPagefileUsage() const { std::vector<std::string> proc_stats; GetProcStats(process_, &proc_stats); const size_t kVmSize = 22; - if (proc_stats.size() > kVmSize) - return static_cast<size_t>(StringToInt(proc_stats[kVmSize])); + if (proc_stats.size() > kVmSize) { + int vm_size; + base::StringToInt(proc_stats[kVmSize], &vm_size); + return static_cast<size_t>(vm_size); + } return 0; } @@ -215,8 +220,11 @@ size_t ProcessMetrics::GetPeakPagefileUsage() const { std::vector<std::string> proc_stats; GetProcStats(process_, &proc_stats); const size_t kVmPeak = 21; - if (proc_stats.size() > kVmPeak) - return static_cast<size_t>(StringToInt(proc_stats[kVmPeak])); + if (proc_stats.size() > kVmPeak) { + int vm_peak; + if (base::StringToInt(proc_stats[kVmPeak], &vm_peak)) + return vm_peak; + } return 0; } @@ -226,8 +234,9 @@ size_t ProcessMetrics::GetWorkingSetSize() const { GetProcStats(process_, &proc_stats); const size_t kVmRss = 23; if (proc_stats.size() > kVmRss) { - size_t num_pages = static_cast<size_t>(StringToInt(proc_stats[kVmRss])); - return num_pages * getpagesize(); + int num_pages; + if (base::StringToInt(proc_stats[kVmRss], &num_pages)) + return static_cast<size_t>(num_pages) * getpagesize(); } return 0; } @@ -238,8 +247,9 @@ size_t ProcessMetrics::GetPeakWorkingSetSize() const { GetProcStats(process_, &proc_stats); const size_t kVmHwm = 23; if (proc_stats.size() > kVmHwm) { - size_t num_pages = static_cast<size_t>(StringToInt(proc_stats[kVmHwm])); - return num_pages * getpagesize(); + int num_pages; + base::StringToInt(proc_stats[kVmHwm], &num_pages); + return static_cast<size_t>(num_pages) * getpagesize(); } return 0; } @@ -265,7 +275,7 @@ bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes, // See http://www.pixelbeat.org/scripts/ps_mem.py bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { FilePath stat_file = - FilePath("/proc").Append(IntToString(process_)).Append("smaps"); + FilePath("/proc").Append(base::IntToString(process_)).Append("smaps"); std::string smaps; int private_kb = 0; int pss_kb = 0; @@ -288,10 +298,14 @@ bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { return false; } if (last_key_name.starts_with(private_prefix)) { - private_kb += StringToInt(tokenizer.token()); + int cur; + base::StringToInt(tokenizer.token(), &cur); + private_kb += cur; } else if (last_key_name.starts_with(pss_prefix)) { have_pss = true; - pss_kb += StringToInt(tokenizer.token()); + int cur; + base::StringToInt(tokenizer.token(), &cur); + pss_kb += cur; } state = KEY_NAME; break; @@ -305,7 +319,7 @@ bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { return false; stat_file = - FilePath("/proc").Append(IntToString(process_)).Append("statm"); + FilePath("/proc").Append(base::IntToString(process_)).Append("statm"); std::string statm; if (!file_util::ReadFileToString(stat_file, &statm) || statm.length() == 0) return false; @@ -314,8 +328,11 @@ bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { SplitString(statm, ' ', &statm_vec); if (statm_vec.size() != 7) return false; // Not the format we expect. - private_kb = StringToInt(statm_vec[1]) - StringToInt(statm_vec[2]); - private_kb *= page_size_kb; + + int statm1, statm2; + base::StringToInt(statm_vec[1], &statm1); + base::StringToInt(statm_vec[2], &statm2); + private_kb = (statm1 - statm2) * page_size_kb; } ws_usage->priv = private_kb; // Sharable is not calculated, as it does not provide interesting data. @@ -332,7 +349,7 @@ bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const { std::string proc_io_contents; FilePath io_file("/proc"); - io_file = io_file.Append(IntToString(process_)); + io_file = io_file.Append(base::IntToString(process_)); io_file = io_file.Append("io"); if (!file_util::ReadFileToString(io_file, &proc_io_contents)) return false; @@ -352,13 +369,17 @@ bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const { case KEY_VALUE: DCHECK(!last_key_name.empty()); if (last_key_name == "syscr") { - (*io_counters).ReadOperationCount = StringToInt64(tokenizer.token()); + base::StringToInt64(tokenizer.token(), + reinterpret_cast<int64*>(&(*io_counters).ReadOperationCount)); } else if (last_key_name == "syscw") { - (*io_counters).WriteOperationCount = StringToInt64(tokenizer.token()); + base::StringToInt64(tokenizer.token(), + reinterpret_cast<int64*>(&(*io_counters).WriteOperationCount)); } else if (last_key_name == "rchar") { - (*io_counters).ReadTransferCount = StringToInt64(tokenizer.token()); + base::StringToInt64(tokenizer.token(), + reinterpret_cast<int64*>(&(*io_counters).ReadTransferCount)); } else if (last_key_name == "wchar") { - (*io_counters).WriteTransferCount = StringToInt64(tokenizer.token()); + base::StringToInt64(tokenizer.token(), + reinterpret_cast<int64*>(&(*io_counters).WriteTransferCount)); } state = KEY_NAME; break; @@ -384,7 +405,10 @@ int ParseProcStatCPU(const std::string& input) { if (fields.size() < 13) return -1; // Output not in the format we expect. - return StringToInt(fields[11]) + StringToInt(fields[12]); + int fields11, fields12; + base::StringToInt(fields[11], &fields11); + base::StringToInt(fields[12], &fields12); + return fields11 + fields12; } // Get the total CPU of a single process. Return value is number of jiffies @@ -498,13 +522,13 @@ size_t GetSystemCommitCharge() { DCHECK_EQ(meminfo_fields[kMemBuffersIndex-1], "Buffers:"); DCHECK_EQ(meminfo_fields[kMemCacheIndex-1], "Cached:"); - size_t result_in_kb; - result_in_kb = StringToInt(meminfo_fields[kMemTotalIndex]); - result_in_kb -= StringToInt(meminfo_fields[kMemFreeIndex]); - result_in_kb -= StringToInt(meminfo_fields[kMemBuffersIndex]); - result_in_kb -= StringToInt(meminfo_fields[kMemCacheIndex]); + int mem_total, mem_free, mem_buffers, mem_cache; + base::StringToInt(meminfo_fields[kMemTotalIndex], &mem_total); + base::StringToInt(meminfo_fields[kMemFreeIndex], &mem_free); + base::StringToInt(meminfo_fields[kMemBuffersIndex], &mem_buffers); + base::StringToInt(meminfo_fields[kMemCacheIndex], &mem_cache); - return result_in_kb; + return mem_total - mem_free - mem_buffers - mem_cache; } namespace { @@ -613,13 +637,13 @@ bool AdjustOOMScore(ProcessId process, int score) { return false; FilePath oom_adj("/proc"); - oom_adj = oom_adj.Append(Int64ToString(process)); + oom_adj = oom_adj.Append(base::Int64ToString(process)); oom_adj = oom_adj.AppendASCII("oom_adj"); if (!file_util::PathExists(oom_adj)) return false; - std::string score_str = IntToString(score); + std::string score_str = base::IntToString(score); return (static_cast<int>(score_str.length()) == file_util::WriteFile(oom_adj, score_str.c_str(), score_str.length())); } |