Durable Storage: Refactor browser test and test the basic "deny" flow.
[chromium-blink-merge.git] / chrome / test / base / chrome_process_util_mac.cc
blobcb19bc404fa441d067ba96f508e27d1ccc4a747c
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"
7 #include <string>
8 #include <vector>
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();
28 ++process_iter) {
29 cmdline.push_back("-p");
30 cmdline.push_back(base::IntToString(*process_iter));
33 // Invoke it
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;
46 int pid;
47 base::StringToInt(values[0], &pid);
48 proc_info.pid = 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);
56 return result;
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,
64 size_t* virtual_size,
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())
73 return false;
75 bool found_process = false;
76 *virtual_size = 0;
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))
82 continue;
83 found_process = true;
84 *virtual_size = it->vsz_in_kb * 1024;
85 *working_set_size = it->rsz_in_kb * 1024;
86 break;
89 return found_process;
92 size_t ChromeTestProcessMetrics::GetPagefileUsage() {
93 size_t virtual_size;
94 size_t working_set_size;
95 GetMemoryValuesHack(process_handle_, &virtual_size, &working_set_size);
96 return virtual_size;
99 size_t ChromeTestProcessMetrics::GetWorkingSetSize() {
100 size_t virtual_size;
101 size_t working_set_size;
102 GetMemoryValuesHack(process_handle_, &virtual_size, &working_set_size);
103 return working_set_size;