diff options
author | thakis@chromium.org <thakis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-12-21 17:44:20 +0000 |
---|---|---|
committer | thakis@chromium.org <thakis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-12-21 17:44:20 +0000 |
commit | b2e8e08818f3542c9043aececfbc913499226183 (patch) | |
tree | 84f4bcf67581c8239424420ba831aa8031db382f /chrome | |
parent | 6b4a530327976d8717dd007cf1403ee453148856 (diff) | |
download | chromium_src-b2e8e08818f3542c9043aececfbc913499226183.zip chromium_src-b2e8e08818f3542c9043aececfbc913499226183.tar.gz chromium_src-b2e8e08818f3542c9043aececfbc913499226183.tar.bz2 |
Mac: Create a pid->task_t mapping in the browser process.
Since nothing writes to this map in the browser atm, this does not have any visible effect.
BUG=13156,25454
TEST=unittest
Review URL: http://codereview.chromium.org/501138
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@35092 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/browser/mach_broker_mac.cc | 35 | ||||
-rw-r--r-- | chrome/browser/mach_broker_mac.h | 73 | ||||
-rw-r--r-- | chrome/browser/mach_broker_mac_unittest.cc | 24 | ||||
-rw-r--r-- | chrome/browser/task_manager.cc | 9 | ||||
-rwxr-xr-x | chrome/chrome_browser.gypi | 2 | ||||
-rwxr-xr-x | chrome/chrome_tests.gypi | 1 |
6 files changed, 141 insertions, 3 deletions
diff --git a/chrome/browser/mach_broker_mac.cc b/chrome/browser/mach_broker_mac.cc new file mode 100644 index 0000000..59f7728 --- /dev/null +++ b/chrome/browser/mach_broker_mac.cc @@ -0,0 +1,35 @@ +// 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/mach_broker_mac.h" + +#include "base/logging.h" + +// Returns the global MachBroker. +MachBroker* MachBroker::instance() { + return Singleton<MachBroker>::get(); +} + +// Adds mach info for a given pid. +void MachBroker::RegisterPid( + base::ProcessHandle pid, const MachInfo& mach_info) { + AutoLock lock(lock_); + DCHECK_EQ(0u, mach_map_.count(pid)); + mach_map_[pid] = mach_info; +} + +// Removes all mappings belonging to |pid| from the broker. +void MachBroker::Invalidate(base::ProcessHandle pid) { + AutoLock lock(lock_); + mach_map_.erase(pid); +} + +// Returns the mach task belonging to |pid|. +mach_port_t MachBroker::TaskForPid(base::ProcessHandle pid) const { + AutoLock lock(lock_); + MachBroker::MachMap::const_iterator it = mach_map_.find(pid); + if (it == mach_map_.end()) + return MACH_PORT_NULL; + return it->second.mach_task_; +} diff --git a/chrome/browser/mach_broker_mac.h b/chrome/browser/mach_broker_mac.h new file mode 100644 index 0000000..67f4fec --- /dev/null +++ b/chrome/browser/mach_broker_mac.h @@ -0,0 +1,73 @@ +// 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. + +#ifndef CHROME_BROWSER_MACH_BROKER_H_ +#define CHROME_BROWSER_MACH_BROKER_H_ + +#include <map> + +#include <mach/mach.h> + +#include "base/lock.h" +#include "base/process.h" +#include "base/process_util.h" +#include "base/singleton.h" + +// On OS X, the mach_port_t of a process is required to collect metrics about +// the process. Running |task_for_pid()| is only allowed for privileged code. +// However, a process has port rights to all its subprocesses, so let the +// browser's child processes send their Mach port to the browser over IPC. +// This way, the brower can at least collect metrics of its child processes, +// which is what it's most interested in anyway. +// +// Mach ports can only be sent over Mach IPC, not over the |socketpair()| that +// the regular IPC system uses. Hence, the child processes open a Mach +// connection shortly after launching and ipc their mach data to the browser +// process. This data is kept in a global |MachBroker| object. +// +// Since this data arrives over a separate channel, it is not available +// immediately after a child process has been started. +class MachBroker : public base::ProcessMetrics::PortProvider { + public: + // Returns the global MachBroker. + static MachBroker* instance(); + + struct MachInfo { + MachInfo() : mach_task_(MACH_PORT_NULL) {} + + MachInfo& SetTask(mach_port_t task) { + mach_task_ = task; + return *this; + } + + mach_port_t mach_task_; + }; + + // Adds mach info for a given pid. + void RegisterPid(base::ProcessHandle pid, const MachInfo& mach_info); + + // Removes all mappings belonging to |pid| from the broker. + void Invalidate(base::ProcessHandle pid); + + // Implement |ProcessMetrics::PortProvider|. + virtual mach_port_t TaskForPid(base::ProcessHandle process) const; + + private: + // Private constructor. + MachBroker() {} + friend struct DefaultSingletonTraits<MachBroker>; + friend class MachBrokerTest; + + // Stores mach info for every process in the broker. + typedef std::map<base::ProcessHandle, MachInfo> MachMap; + MachMap mach_map_; + + // Mutex that guards |mach_map_|. + mutable Lock lock_; + + DISALLOW_COPY_AND_ASSIGN(MachBroker); +}; + +#endif // CHROME_BROWSER_MACH_BROKER_H_ + diff --git a/chrome/browser/mach_broker_mac_unittest.cc b/chrome/browser/mach_broker_mac_unittest.cc new file mode 100644 index 0000000..56cc2ae2 --- /dev/null +++ b/chrome/browser/mach_broker_mac_unittest.cc @@ -0,0 +1,24 @@ +// 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/mach_broker_mac.h" + +#include "testing/gtest/include/gtest/gtest.h" + +class MachBrokerTest : public testing::Test { + public: + MachBroker broker_; +}; + +TEST_F(MachBrokerTest, Setter) { + broker_.RegisterPid(1u, MachBroker::MachInfo().SetTask(2u)); + EXPECT_EQ(2u, broker_.TaskForPid(1)); + EXPECT_EQ(0u, broker_.TaskForPid(2)); +} + +TEST_F(MachBrokerTest, Invalidate) { + broker_.RegisterPid(1u, MachBroker::MachInfo().SetTask(2)); + broker_.Invalidate(1u); + EXPECT_EQ(0u, broker_.TaskForPid(1)); +} diff --git a/chrome/browser/task_manager.cc b/chrome/browser/task_manager.cc index edd7b37..0667f94 100644 --- a/chrome/browser/task_manager.cc +++ b/chrome/browser/task_manager.cc @@ -29,6 +29,10 @@ #include "net/url_request/url_request.h" #include "net/url_request/url_request_job.h" +#if defined(OS_MACOSX) +#include "chrome/browser/mach_broker_mac.h" +#endif + namespace { // The delay between updates of the information (in ms). @@ -536,9 +540,8 @@ void TaskManagerModel::AddResource(TaskManager::Resource* resource) { #if !defined(OS_MACOSX) base::ProcessMetrics::CreateProcessMetrics(process); #else - // TODO(thakis): Write a port provider that knows the task ports of - // child processes. - base::ProcessMetrics::CreateProcessMetrics(process, NULL); + base::ProcessMetrics::CreateProcessMetrics(process, + MachBroker::instance()); #endif metrics_map_[process] = pm; diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index fe0328a..fe69765 100755 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -1098,6 +1098,8 @@ 'browser/login_prompt_mac.h', 'browser/login_prompt_mac.mm', 'browser/login_prompt_win.cc', + 'browser/mach_broker_mac.cc', + 'browser/mach_broker_mac.h', 'browser/memory_details.cc', 'browser/memory_details_linux.cc', 'browser/memory_details_mac.cc', diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index 9c3cb9f..4f510d6 100755 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -724,6 +724,7 @@ 'browser/keychain_mock_mac.h', 'browser/login_prompt_unittest.cc', 'browser/meta_table_helper_unittest.cc', + 'browser/mach_broker_mac_unittest.cc', 'browser/metrics/metrics_log_unittest.cc', 'browser/metrics/metrics_response_unittest.cc', 'browser/metrics/metrics_service_unittest.cc', |