blob: c594e759a83fb832311e8a0b1e172e43da4e0989 [file] [log] [blame]
[email protected]f164cea2009-11-05 23:37:401// 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
3// found in the LICENSE file.
4
5#include "chrome/browser/process_info_snapshot.h"
6
7#include <iostream>
8#include <sstream>
9
10#include "base/string_util.h"
11#include "base/thread.h"
12
13// Implementation for the Mac; calls '/bin/ps' for information when
14// |Sample()| is called.
15
16// Default constructor.
17ProcessInfoSnapshot::ProcessInfoSnapshot() { }
18
19// Destructor: just call |Reset()| to release everything.
20ProcessInfoSnapshot::~ProcessInfoSnapshot() {
21 Reset();
22}
23
24// Capture the information by calling '/bin/ps'.
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).
27bool ProcessInfoSnapshot::Sample(std::vector<base::ProcessId> pid_list) {
28 Reset();
29
30 std::vector<std::string> argv;
31 argv.push_back("/bin/ps");
32 // Get PID, PPID, (real) UID, effective UID, resident set size, virtual memory
33 // size, and command.
34 argv.push_back("-o");
35 argv.push_back("pid=,ppid=,ruid=,uid=,rss=,vsz=,comm=");
36 // Only display the specified PIDs.
37 for(std::vector<base::ProcessId>::iterator it = pid_list.begin();
38 it != pid_list.end(); ++it) {
39 argv.push_back("-p");
40 argv.push_back(Int64ToString(static_cast<int64>(*it)));
41 }
42
43 std::string output;
44 CommandLine command_line(argv);
45 if (!base::GetAppOutputRestricted(command_line,
46 &output, (pid_list.size() + 10) * 100)) {
47 LOG(ERROR) << "Failure running /bin/ps to acquire data.";
48 return false;
49 }
50
51 std::istringstream in(output, std::istringstream::in);
52 std::string line;
53
54 // Process lines until done.
55 while (true) {
56 ProcInfoEntry proc_info;
57
58 // The format is as specified above to ps (see ps(1)):
59 // "-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 // the line.
62 in >> proc_info.pid;
63 if (in.eof())
64 break;
65 in >> proc_info.ppid;
66 in >> proc_info.uid;
67 in >> proc_info.euid;
68 in >> proc_info.rss;
69 in >> proc_info.vsize;
70 in.ignore(1, ' '); // Eat the space.
71 std::getline(in, proc_info.command); // Get the rest of the line.
72 if (!in.good()) {
73 LOG(ERROR) << "Error parsing output from /usr/bin/top.";
74 return false;
75 }
76
77 // 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 LOG(ERROR) << "Duplicate PID in output from /bin/ps.";
80 return false;
81 }
82
83 if (!proc_info.pid || ! proc_info.vsize) {
84 LOG(WARNING) << "Invalid data from /bin/ps.";
85 return false;
86 }
87
88 // Record the process information.
89 proc_info_entries_[proc_info.pid] = proc_info;
90 }
91
92 return true;
93}
94
95// Clear all the stored information.
96void ProcessInfoSnapshot::Reset() {
97 proc_info_entries_.clear();
98}
99
100bool ProcessInfoSnapshot::GetProcInfo(int pid,
101 ProcInfoEntry* proc_info) const {
102 std::map<int,ProcInfoEntry>::const_iterator it = proc_info_entries_.find(pid);
103 if (it == proc_info_entries_.end())
104 return false;
105
106 *proc_info = it->second;
107 return true;
108}
109
110bool ProcessInfoSnapshot::GetCommittedKBytesOfPID(
111 int pid,
112 base::CommittedKBytes* usage) const {
113 // Try to avoid crashing on a bug; stats aren't usually so crucial.
114 if (!usage) {
115 NOTREACHED();
116 return false;
117 }
118
119 // Failure of |GetProcInfo()| is "normal", due to racing.
120 ProcInfoEntry proc_info;
121 if (!GetProcInfo(pid, &proc_info)) {
122 usage->priv = 0;
123 usage->mapped = 0;
124 usage->image = 0;
125 return false;
126 }
127
128 usage->priv = proc_info.vsize;
129 usage->mapped = 0;
130 usage->image = 0;
131 return true;
132}
133
134bool ProcessInfoSnapshot::GetWorkingSetKBytesOfPID(
135 int pid,
136 base::WorkingSetKBytes* ws_usage) const {
137 // Try to avoid crashing on a bug; stats aren't usually so crucial.
138 if (!ws_usage) {
139 NOTREACHED();
140 return false;
141 }
142
143 // Failure of |GetProcInfo()| is "normal", due to racing.
144 ProcInfoEntry proc_info;
145 if (!GetProcInfo(pid, &proc_info)) {
146 ws_usage->priv = 0;
147 ws_usage->shareable = 0;
148 ws_usage->shared = 0;
149 return false;
150 }
151
152 ws_usage->priv = 0;
153 ws_usage->shareable = proc_info.rss;
154 ws_usage->shared = 0;
155 return true;
156}