1 // Copyright (c) 2011 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.
5 #include "chrome/test/base/chrome_process_util.h"
10 #include "base/command_line.h"
11 #include "base/process/launch.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_split.h"
14 #include "base/strings/string_util.h"
16 MacChromeProcessInfoList
GetRunningMacProcessInfo(
17 const ChromeProcessList
& process_list
) {
18 MacChromeProcessInfoList result
;
20 // Build up the ps command line
21 std::vector
<std::string
> cmdline
;
22 cmdline
.push_back("ps");
23 cmdline
.push_back("-o");
24 cmdline
.push_back("pid=,rss=,vsz="); // fields we need, no headings
25 ChromeProcessList::const_iterator process_iter
;
26 for (process_iter
= process_list
.begin();
27 process_iter
!= process_list
.end();
29 cmdline
.push_back("-p");
30 cmdline
.push_back(base::IntToString(*process_iter
));
34 std::string ps_output
;
35 if (!base::GetAppOutput(base::CommandLine(cmdline
), &ps_output
))
36 return result
; // All the pids might have exited
38 // Process the results.
39 for (const std::string
& raw_line
: base::SplitString(
40 ps_output
, "\n", base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
)) {
41 std::string
line(base::CollapseWhitespaceASCII(raw_line
, false));
42 std::vector
<base::StringPiece
> values
= base::SplitStringPiece(
43 line
, " ", base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
44 if (values
.size() == 3) {
45 MacChromeProcessInfo proc_info
;
47 base::StringToInt(values
[0], &pid
);
49 base::StringToInt(values
[1], &proc_info
.rsz_in_kb
);
50 base::StringToInt(values
[2], &proc_info
.vsz_in_kb
);
51 if (proc_info
.pid
&& proc_info
.rsz_in_kb
&& proc_info
.vsz_in_kb
)
52 result
.push_back(proc_info
);
59 // Common interface for fetching memory values from parsed ps output.
60 // We fill in both values we may get called for, even though our
61 // callers typically only care about one, just to keep the code
62 // simple and because this is a test.
63 static bool GetMemoryValuesHack(uint32 process_id
,
65 size_t* working_set_size
) {
66 DCHECK(virtual_size
&& working_set_size
);
68 std::vector
<base::ProcessId
> processes
;
69 processes
.push_back(process_id
);
71 MacChromeProcessInfoList process_info
= GetRunningMacProcessInfo(processes
);
72 if (process_info
.empty())
75 bool found_process
= false;
77 *working_set_size
= 0;
79 MacChromeProcessInfoList::iterator it
= process_info
.begin();
80 for (; it
!= process_info
.end(); ++it
) {
81 if (it
->pid
!= static_cast<base::ProcessId
>(process_id
))
84 *virtual_size
= it
->vsz_in_kb
* 1024;
85 *working_set_size
= it
->rsz_in_kb
* 1024;
92 size_t ChromeTestProcessMetrics::GetPagefileUsage() {
94 size_t working_set_size
;
95 GetMemoryValuesHack(process_handle_
, &virtual_size
, &working_set_size
);
99 size_t ChromeTestProcessMetrics::GetWorkingSetSize() {
101 size_t working_set_size
;
102 GetMemoryValuesHack(process_handle_
, &virtual_size
, &working_set_size
);
103 return working_set_size
;