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 std::vector
<std::string
> ps_output_lines
;
40 base::SplitString(ps_output
, '\n', &ps_output_lines
);
41 std::vector
<std::string
>::const_iterator line_iter
;
42 for (line_iter
= ps_output_lines
.begin();
43 line_iter
!= ps_output_lines
.end();
45 std::string
line(base::CollapseWhitespaceASCII(*line_iter
, false));
46 std::vector
<std::string
> values
;
47 base::SplitString(line
, ' ', &values
);
48 if (values
.size() == 3) {
49 MacChromeProcessInfo proc_info
;
51 base::StringToInt(values
[0], &pid
);
53 base::StringToInt(values
[1], &proc_info
.rsz_in_kb
);
54 base::StringToInt(values
[2], &proc_info
.vsz_in_kb
);
55 if (proc_info
.pid
&& proc_info
.rsz_in_kb
&& proc_info
.vsz_in_kb
)
56 result
.push_back(proc_info
);
63 // Common interface for fetching memory values from parsed ps output.
64 // We fill in both values we may get called for, even though our
65 // callers typically only care about one, just to keep the code
66 // simple and because this is a test.
67 static bool GetMemoryValuesHack(uint32 process_id
,
69 size_t* working_set_size
) {
70 DCHECK(virtual_size
&& working_set_size
);
72 std::vector
<base::ProcessId
> processes
;
73 processes
.push_back(process_id
);
75 MacChromeProcessInfoList process_info
= GetRunningMacProcessInfo(processes
);
76 if (process_info
.empty())
79 bool found_process
= false;
81 *working_set_size
= 0;
83 MacChromeProcessInfoList::iterator it
= process_info
.begin();
84 for (; it
!= process_info
.end(); ++it
) {
85 if (it
->pid
!= static_cast<base::ProcessId
>(process_id
))
88 *virtual_size
= it
->vsz_in_kb
* 1024;
89 *working_set_size
= it
->rsz_in_kb
* 1024;
96 size_t ChromeTestProcessMetrics::GetPagefileUsage() {
98 size_t working_set_size
;
99 GetMemoryValuesHack(process_handle_
, &virtual_size
, &working_set_size
);
103 size_t ChromeTestProcessMetrics::GetWorkingSetSize() {
105 size_t working_set_size
;
106 GetMemoryValuesHack(process_handle_
, &virtual_size
, &working_set_size
);
107 return working_set_size
;