summaryrefslogtreecommitdiffstats
path: root/chrome/browser/process_info_snapshot_mac_unittest.cc
diff options
context:
space:
mode:
authorviettrungluu@chromium.org <viettrungluu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-11-05 23:37:40 +0000
committerviettrungluu@chromium.org <viettrungluu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-11-05 23:37:40 +0000
commitf164cea41472f9e9ec21579233e2c42a7b9d5184 (patch)
tree523c6aa180be86ace3ac7797e4c9c75c369dff57 /chrome/browser/process_info_snapshot_mac_unittest.cc
parent422c0f17466cddf30fd6815f9d3519a3815264c6 (diff)
downloadchromium_src-f164cea41472f9e9ec21579233e2c42a7b9d5184.zip
chromium_src-f164cea41472f9e9ec21579233e2c42a7b9d5184.tar.gz
chromium_src-f164cea41472f9e9ec21579233e2c42a7b9d5184.tar.bz2
Mac: Implement about:memory.
This implements about:memory on Mac. It calls /bin/ps to obtain information about processes (this is Apple's officially supported "API"). Unfortunately, ps provides fairly minimal information (rss and vsize); top is better, but not a stable API -- it has changed greatly between Mac OS 10.5 and 10.6, and moreover the 10.6 version is more limited in its output formatting. BUG=9653 TEST=Go to about:memory under a variety of conditions (with a variety of browsers loaded). Review URL: http://codereview.chromium.org/333008 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@31168 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/process_info_snapshot_mac_unittest.cc')
-rw-r--r--chrome/browser/process_info_snapshot_mac_unittest.cc85
1 files changed, 85 insertions, 0 deletions
diff --git a/chrome/browser/process_info_snapshot_mac_unittest.cc b/chrome/browser/process_info_snapshot_mac_unittest.cc
new file mode 100644
index 0000000..babdf27
--- /dev/null
+++ b/chrome/browser/process_info_snapshot_mac_unittest.cc
@@ -0,0 +1,85 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/process_info_snapshot.h"
+
+#include <sys/types.h> // For |uid_t| (and |pid_t|).
+#include <unistd.h> // For |getpid()|, |getuid()|, etc.
+
+#include <vector>
+
+#include "testing/gtest/include/gtest/gtest.h"
+
+typedef testing::Test ProcessInfoSnapshotMacTest;
+
+TEST_F(ProcessInfoSnapshotMacTest, FindPidOneTest) {
+ // Sample process with PID 1, which should exist and presumably belong to
+ // root.
+ std::vector<base::ProcessId> pid_list;
+ pid_list.push_back(1);
+ ProcessInfoSnapshot snapshot;
+ ASSERT_TRUE(snapshot.Sample(pid_list));
+
+ ProcessInfoSnapshot::ProcInfoEntry proc_info;
+ ASSERT_TRUE(snapshot.GetProcInfo(1, &proc_info));
+ EXPECT_EQ(1, static_cast<int64>(proc_info.pid));
+ EXPECT_EQ(0, static_cast<int64>(proc_info.ppid));
+ EXPECT_EQ(0, static_cast<int64>(proc_info.uid));
+ EXPECT_EQ(0, static_cast<int64>(proc_info.euid));
+ EXPECT_GE(proc_info.rss, 0u);
+ EXPECT_GT(proc_info.vsize, 0u);
+
+ // Try out the |Get...OfPID()|, but don't examine the results, since they
+ // depend on how we map |ProcInfoEntry| to |...KBytes|.
+ base::CommittedKBytes usage;
+ EXPECT_TRUE(snapshot.GetCommittedKBytesOfPID(1, &usage));
+ base::WorkingSetKBytes ws_usage;
+ EXPECT_TRUE(snapshot.GetWorkingSetKBytesOfPID(1, &ws_usage));
+
+ // Make sure it hasn't picked up some other PID (say, 2).
+ EXPECT_FALSE(snapshot.GetProcInfo(2, &proc_info));
+
+ // Make sure PID 2 still isn't there (in case I mess up my use of std::map).
+ EXPECT_FALSE(snapshot.GetProcInfo(2, &proc_info));
+
+ // Test |Reset()|.
+ snapshot.Reset();
+ EXPECT_FALSE(snapshot.GetProcInfo(1, &proc_info));
+}
+
+TEST_F(ProcessInfoSnapshotMacTest, FindPidSelfTest) {
+ // Sample this process and its parent.
+ base::ProcessId pid = static_cast<base::ProcessId>(getpid());
+ base::ProcessId ppid = static_cast<base::ProcessId>(getppid());
+ uid_t uid = getuid();
+ uid_t euid = geteuid();
+ EXPECT_NE(static_cast<int64>(ppid), 0);
+
+ std::vector<base::ProcessId> pid_list;
+ pid_list.push_back(pid);
+ pid_list.push_back(ppid);
+ ProcessInfoSnapshot snapshot;
+ ASSERT_TRUE(snapshot.Sample(pid_list));
+
+ // Find our process.
+ ProcessInfoSnapshot::ProcInfoEntry proc_info;
+ ASSERT_TRUE(snapshot.GetProcInfo(pid, &proc_info));
+ EXPECT_EQ(pid, proc_info.pid);
+ EXPECT_EQ(ppid, proc_info.ppid);
+ EXPECT_EQ(uid, proc_info.uid);
+ EXPECT_EQ(euid, proc_info.euid);
+ EXPECT_GE(proc_info.rss, 100u); // Sanity check: we're running, so we
+ // should occupy at least 100 kilobytes.
+ EXPECT_GE(proc_info.vsize, 1024u); // Sanity check: our |vsize| is presumably
+ // at least a megabyte.
+
+ // Find our parent.
+ ASSERT_TRUE(snapshot.GetProcInfo(ppid, &proc_info));
+ EXPECT_EQ(ppid, proc_info.pid);
+ EXPECT_NE(static_cast<int64>(proc_info.ppid), 0);
+ EXPECT_EQ(uid, proc_info.uid); // This (and the following) should be true
+ EXPECT_EQ(euid, proc_info.euid); // under reasonable circumstances.
+ // Can't say anything definite about its |rss|.
+ EXPECT_GT(proc_info.vsize, 0u); // Its |vsize| should be nonzero though.
+}