| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/process_info_snapshot.h" | 5 #include "chrome/browser/process_info_snapshot.h" |
| 6 | 6 |
| 7 #include <iostream> | 7 #include <iostream> |
| 8 #include <sstream> | 8 #include <sstream> |
| 9 | 9 |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/thread.h" | 11 #include "base/thread.h" |
| 12 | 12 |
| 13 // Implementation for the Mac; calls '/bin/ps' for information when | 13 // Implementation for the Mac; calls '/bin/ps' for information when |
| 14 // |Sample()| is called. | 14 // |Sample()| is called. |
| 15 | 15 |
| 16 // Default constructor. | 16 // Default constructor. |
| 17 ProcessInfoSnapshot::ProcessInfoSnapshot() { } | 17 ProcessInfoSnapshot::ProcessInfoSnapshot() { } |
| 18 | 18 |
| 19 // Destructor: just call |Reset()| to release everything. | 19 // Destructor: just call |Reset()| to release everything. |
| 20 ProcessInfoSnapshot::~ProcessInfoSnapshot() { | 20 ProcessInfoSnapshot::~ProcessInfoSnapshot() { |
| 21 Reset(); | 21 Reset(); |
| 22 } | 22 } |
| 23 | 23 |
| 24 // Capture the information by calling '/bin/ps'. | 24 // Capture the information by calling '/bin/ps'. |
| 25 // Note: we ignore the "tsiz" (text size) display option of ps because it's | 25 // Note: we ignore the "tsiz" (text size) display option of ps because it's |
| 26 // always zero (tested on 10.5 and 10.6). | 26 // always zero (tested on 10.5 and 10.6). |
| 27 bool ProcessInfoSnapshot::Sample(std::vector<base::ProcessId> pid_list) { | 27 bool ProcessInfoSnapshot::Sample(std::vector<base::ProcessId> pid_list) { |
| 28 const char* kPsPathName = "/bin/ps"; |
| 28 Reset(); | 29 Reset(); |
| 29 | 30 |
| 30 std::vector<std::string> argv; | 31 std::vector<std::string> argv; |
| 31 argv.push_back("/bin/ps"); | 32 argv.push_back(kPsPathName); |
| 32 // Get PID, PPID, (real) UID, effective UID, resident set size, virtual memory | 33 // Get PID, PPID, (real) UID, effective UID, resident set size, virtual memory |
| 33 // size, and command. | 34 // size, and command. |
| 34 argv.push_back("-o"); | 35 argv.push_back("-o"); |
| 35 argv.push_back("pid=,ppid=,ruid=,uid=,rss=,vsz=,comm="); | 36 argv.push_back("pid=,ppid=,ruid=,uid=,rss=,vsz=,comm="); |
| 36 // Only display the specified PIDs. | 37 // Only display the specified PIDs. |
| 37 for (std::vector<base::ProcessId>::iterator it = pid_list.begin(); | 38 for (std::vector<base::ProcessId>::iterator it = pid_list.begin(); |
| 38 it != pid_list.end(); ++it) { | 39 it != pid_list.end(); ++it) { |
| 39 argv.push_back("-p"); | 40 argv.push_back("-p"); |
| 40 argv.push_back(Int64ToString(static_cast<int64>(*it))); | 41 argv.push_back(Int64ToString(static_cast<int64>(*it))); |
| 41 } | 42 } |
| 42 | 43 |
| 43 std::string output; | 44 std::string output; |
| 44 CommandLine command_line(argv); | 45 CommandLine command_line(argv); |
| 45 if (!base::GetAppOutputRestricted(command_line, | 46 if (!base::GetAppOutputRestricted(command_line, |
| 46 &output, (pid_list.size() + 10) * 100)) { | 47 &output, (pid_list.size() + 10) * 100)) { |
| 47 LOG(ERROR) << "Failure running /bin/ps to acquire data."; | 48 LOG(ERROR) << "Failure running " << kPsPathName << " to acquire data."; |
| 48 return false; | 49 return false; |
| 49 } | 50 } |
| 50 | 51 |
| 51 std::istringstream in(output, std::istringstream::in); | 52 std::istringstream in(output, std::istringstream::in); |
| 52 std::string line; | 53 std::string line; |
| 53 | 54 |
| 54 // Process lines until done. | 55 // Process lines until done. |
| 55 while (true) { | 56 while (true) { |
| 56 ProcInfoEntry proc_info; | 57 ProcInfoEntry proc_info; |
| 57 | 58 |
| 58 // The format is as specified above to ps (see ps(1)): | 59 // The format is as specified above to ps (see ps(1)): |
| 59 // "-o pid=,ppid=,ruid=,uid=,rss=,vsz=,comm=". | 60 // "-o pid=,ppid=,ruid=,uid=,rss=,vsz=,comm=". |
| 60 // Try to read the PID; if we get it, we should be able to get the rest of | 61 // Try to read the PID; if we get it, we should be able to get the rest of |
| 61 // the line. | 62 // the line. |
| 62 in >> proc_info.pid; | 63 in >> proc_info.pid; |
| 63 if (in.eof()) | 64 if (in.eof()) |
| 64 break; | 65 break; |
| 65 in >> proc_info.ppid; | 66 in >> proc_info.ppid; |
| 66 in >> proc_info.uid; | 67 in >> proc_info.uid; |
| 67 in >> proc_info.euid; | 68 in >> proc_info.euid; |
| 68 in >> proc_info.rss; | 69 in >> proc_info.rss; |
| 69 in >> proc_info.vsize; | 70 in >> proc_info.vsize; |
| 70 in.ignore(1, ' '); // Eat the space. | 71 in.ignore(1, ' '); // Eat the space. |
| 71 std::getline(in, proc_info.command); // Get the rest of the line. | 72 std::getline(in, proc_info.command); // Get the rest of the line. |
| 72 if (!in.good()) { | 73 if (!in.good()) { |
| 73 LOG(ERROR) << "Error parsing output from /usr/bin/top."; | 74 LOG(ERROR) << "Error parsing output from " << kPsPathName << "."; |
| 74 return false; | 75 return false; |
| 75 } | 76 } |
| 76 | 77 |
| 77 // Make sure the new PID isn't already in our list. | 78 // Make sure the new PID isn't already in our list. |
| 78 if (proc_info_entries_.find(proc_info.pid) != proc_info_entries_.end()) { | 79 if (proc_info_entries_.find(proc_info.pid) != proc_info_entries_.end()) { |
| 79 LOG(ERROR) << "Duplicate PID in output from /bin/ps."; | 80 LOG(ERROR) << "Duplicate PID in output from " << kPsPathName << "."; |
| 80 return false; | 81 return false; |
| 81 } | 82 } |
| 82 | 83 |
| 83 if (!proc_info.pid || ! proc_info.vsize) { | 84 if (!proc_info.pid || ! proc_info.vsize) { |
| 84 LOG(WARNING) << "Invalid data from /bin/ps."; | 85 LOG(WARNING) << "Invalid data from " << kPsPathName << "."; |
| 85 return false; | 86 return false; |
| 86 } | 87 } |
| 87 | 88 |
| 88 // Record the process information. | 89 // Record the process information. |
| 89 proc_info_entries_[proc_info.pid] = proc_info; | 90 proc_info_entries_[proc_info.pid] = proc_info; |
| 90 } | 91 } |
| 91 | 92 |
| 92 return true; | 93 return true; |
| 93 } | 94 } |
| 94 | 95 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 ws_usage->shareable = 0; | 148 ws_usage->shareable = 0; |
| 148 ws_usage->shared = 0; | 149 ws_usage->shared = 0; |
| 149 return false; | 150 return false; |
| 150 } | 151 } |
| 151 | 152 |
| 152 ws_usage->priv = 0; | 153 ws_usage->priv = 0; |
| 153 ws_usage->shareable = proc_info.rss; | 154 ws_usage->shareable = proc_info.rss; |
| 154 ws_usage->shared = 0; | 155 ws_usage->shared = 0; |
| 155 return true; | 156 return true; |
| 156 } | 157 } |
| OLD | NEW |