jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 1 | // Copyright 2014 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. |
| 4 | |
| 5 | #include "mojo/edk/system/handle_table.h" |
| 6 | |
Avi Drissman | 2e88ac37 | 2015-12-21 18:14:57 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 9 | #include <limits> |
| 10 | |
erikchen | ea7ac132 | 2017-06-03 02:40:57 | [diff] [blame] | 11 | #include "base/trace_event/memory_dump_manager.h" |
| 12 | |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 13 | namespace mojo { |
| 14 | namespace edk { |
| 15 | |
erikchen | ea7ac132 | 2017-06-03 02:40:57 | [diff] [blame] | 16 | namespace { |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 17 | |
erikchen | ea7ac132 | 2017-06-03 02:40:57 | [diff] [blame] | 18 | const char* GetNameForDispatcherType(Dispatcher::Type type) { |
| 19 | switch (type) { |
| 20 | case Dispatcher::Type::UNKNOWN: |
| 21 | return "unknown"; |
| 22 | case Dispatcher::Type::MESSAGE_PIPE: |
| 23 | return "message_pipe"; |
| 24 | case Dispatcher::Type::DATA_PIPE_PRODUCER: |
| 25 | return "data_pipe_producer"; |
| 26 | case Dispatcher::Type::DATA_PIPE_CONSUMER: |
| 27 | return "data_pipe_consumer"; |
| 28 | case Dispatcher::Type::SHARED_BUFFER: |
| 29 | return "shared_buffer"; |
| 30 | case Dispatcher::Type::WATCHER: |
| 31 | return "watcher"; |
| 32 | case Dispatcher::Type::PLATFORM_HANDLE: |
| 33 | return "platform_handle"; |
| 34 | } |
erikchen | 337e9da | 2017-06-09 17:49:45 | [diff] [blame] | 35 | NOTREACHED(); |
erikchen | ea7ac132 | 2017-06-03 02:40:57 | [diff] [blame] | 36 | return "unknown"; |
| 37 | } |
| 38 | |
erikchen | ea7ac132 | 2017-06-03 02:40:57 | [diff] [blame] | 39 | } // namespace |
| 40 | |
| 41 | HandleTable::HandleTable() { |
erikchen | ea7ac132 | 2017-06-03 02:40:57 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | HandleTable::~HandleTable() { |
erikchen | ea7ac132 | 2017-06-03 02:40:57 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | base::Lock& HandleTable::GetLock() { |
| 48 | return lock_; |
| 49 | } |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 50 | |
| 51 | MojoHandle HandleTable::AddDispatcher(scoped_refptr<Dispatcher> dispatcher) { |
| 52 | // Oops, we're out of handles. |
| 53 | if (next_available_handle_ == MOJO_HANDLE_INVALID) |
| 54 | return MOJO_HANDLE_INVALID; |
| 55 | |
| 56 | MojoHandle handle = next_available_handle_++; |
tzik | 1fb60dd | 2017-03-08 04:52:06 | [diff] [blame] | 57 | auto result = |
| 58 | handles_.insert(std::make_pair(handle, Entry(std::move(dispatcher)))); |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 59 | DCHECK(result.second); |
| 60 | |
| 61 | return handle; |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 62 | } |
| 63 | |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 64 | bool HandleTable::AddDispatchersFromTransit( |
| 65 | const std::vector<Dispatcher::DispatcherInTransit>& dispatchers, |
| 66 | MojoHandle* handles) { |
| 67 | // Oops, we're out of handles. |
| 68 | if (next_available_handle_ == MOJO_HANDLE_INVALID) |
| 69 | return false; |
| 70 | |
| 71 | DCHECK_LE(dispatchers.size(), std::numeric_limits<uint32_t>::max()); |
| 72 | // If this insertion would cause handle overflow, we're out of handles. |
| 73 | if (next_available_handle_ + dispatchers.size() < next_available_handle_) |
| 74 | return false; |
| 75 | |
| 76 | for (size_t i = 0; i < dispatchers.size(); ++i) { |
Ken Rockot | fdf158da | 2017-06-12 20:45:29 | [diff] [blame] | 77 | MojoHandle handle = MOJO_HANDLE_INVALID; |
| 78 | if (dispatchers[i].dispatcher) { |
| 79 | handle = next_available_handle_++; |
| 80 | auto result = handles_.insert( |
| 81 | std::make_pair(handle, Entry(dispatchers[i].dispatcher))); |
| 82 | DCHECK(result.second); |
| 83 | } |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 84 | handles[i] = handle; |
| 85 | } |
| 86 | |
| 87 | return true; |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 88 | } |
| 89 | |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 90 | scoped_refptr<Dispatcher> HandleTable::GetDispatcher(MojoHandle handle) const { |
| 91 | auto it = handles_.find(handle); |
| 92 | if (it == handles_.end()) |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 93 | return nullptr; |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 94 | return it->second.dispatcher; |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | MojoResult HandleTable::GetAndRemoveDispatcher( |
| 98 | MojoHandle handle, |
| 99 | scoped_refptr<Dispatcher>* dispatcher) { |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 100 | auto it = handles_.find(handle); |
| 101 | if (it == handles_.end()) |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 102 | return MOJO_RESULT_INVALID_ARGUMENT; |
| 103 | if (it->second.busy) |
| 104 | return MOJO_RESULT_BUSY; |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 105 | |
tzik | 1fb60dd | 2017-03-08 04:52:06 | [diff] [blame] | 106 | *dispatcher = std::move(it->second.dispatcher); |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 107 | handles_.erase(it); |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 108 | return MOJO_RESULT_OK; |
| 109 | } |
| 110 | |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 111 | MojoResult HandleTable::BeginTransit( |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 112 | const MojoHandle* handles, |
Ken Rockot | 15b2b0b | 2017-06-12 20:36:05 | [diff] [blame] | 113 | size_t num_handles, |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 114 | std::vector<Dispatcher::DispatcherInTransit>* dispatchers) { |
Ken Rockot | 766e766 | 2017-08-10 05:37:15 | [diff] [blame^] | 115 | dispatchers->reserve(dispatchers->size() + num_handles); |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 116 | for (size_t i = 0; i < num_handles; ++i) { |
| 117 | auto it = handles_.find(handles[i]); |
| 118 | if (it == handles_.end()) |
| 119 | return MOJO_RESULT_INVALID_ARGUMENT; |
| 120 | if (it->second.busy) |
| 121 | return MOJO_RESULT_BUSY; |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 122 | |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 123 | Dispatcher::DispatcherInTransit d; |
| 124 | d.local_handle = handles[i]; |
| 125 | d.dispatcher = it->second.dispatcher; |
| 126 | if (!d.dispatcher->BeginTransit()) |
| 127 | return MOJO_RESULT_BUSY; |
| 128 | it->second.busy = true; |
| 129 | dispatchers->push_back(d); |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 130 | } |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 131 | return MOJO_RESULT_OK; |
| 132 | } |
| 133 | |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 134 | void HandleTable::CompleteTransitAndClose( |
| 135 | const std::vector<Dispatcher::DispatcherInTransit>& dispatchers) { |
| 136 | for (const auto& dispatcher : dispatchers) { |
| 137 | auto it = handles_.find(dispatcher.local_handle); |
| 138 | DCHECK(it != handles_.end() && it->second.busy); |
| 139 | handles_.erase(it); |
| 140 | dispatcher.dispatcher->CompleteTransitAndClose(); |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 144 | void HandleTable::CancelTransit( |
| 145 | const std::vector<Dispatcher::DispatcherInTransit>& dispatchers) { |
| 146 | for (const auto& dispatcher : dispatchers) { |
| 147 | auto it = handles_.find(dispatcher.local_handle); |
| 148 | DCHECK(it != handles_.end() && it->second.busy); |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 149 | it->second.busy = false; |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 150 | dispatcher.dispatcher->CancelTransit(); |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 154 | void HandleTable::GetActiveHandlesForTest(std::vector<MojoHandle>* handles) { |
| 155 | handles->clear(); |
| 156 | for (const auto& entry : handles_) |
| 157 | handles->push_back(entry.first); |
| 158 | } |
| 159 | |
erikchen | ea7ac132 | 2017-06-03 02:40:57 | [diff] [blame] | 160 | // MemoryDumpProvider implementation. |
| 161 | bool HandleTable::OnMemoryDump(const base::trace_event::MemoryDumpArgs& args, |
| 162 | base::trace_event::ProcessMemoryDump* pmd) { |
| 163 | // Create entries for all relevant dispatcher types to ensure they are present |
| 164 | // in the final dump. |
| 165 | std::map<Dispatcher::Type, int> handle_count; |
| 166 | handle_count[Dispatcher::Type::MESSAGE_PIPE]; |
| 167 | handle_count[Dispatcher::Type::DATA_PIPE_PRODUCER]; |
| 168 | handle_count[Dispatcher::Type::DATA_PIPE_CONSUMER]; |
| 169 | handle_count[Dispatcher::Type::SHARED_BUFFER]; |
| 170 | handle_count[Dispatcher::Type::WATCHER]; |
| 171 | handle_count[Dispatcher::Type::PLATFORM_HANDLE]; |
| 172 | |
| 173 | // Count the number of each dispatcher type. |
| 174 | { |
| 175 | base::AutoLock lock(GetLock()); |
| 176 | for (const auto& entry : handles_) { |
| 177 | ++handle_count[entry.second.dispatcher->GetType()]; |
| 178 | } |
| 179 | } |
| 180 | |
erikchen | ea7ac132 | 2017-06-03 02:40:57 | [diff] [blame] | 181 | for (const auto& entry : handle_count) { |
erikchen | bf2cf91 | 2017-06-06 23:54:41 | [diff] [blame] | 182 | base::trace_event::MemoryAllocatorDump* inner_dump = |
| 183 | pmd->CreateAllocatorDump(std::string("mojo/") + |
| 184 | GetNameForDispatcherType(entry.first)); |
| 185 | inner_dump->AddScalar( |
| 186 | base::trace_event::MemoryAllocatorDump::kNameObjectCount, |
| 187 | base::trace_event::MemoryAllocatorDump::kUnitsObjects, entry.second); |
erikchen | ea7ac132 | 2017-06-03 02:40:57 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | return true; |
| 191 | } |
| 192 | |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 193 | HandleTable::Entry::Entry() {} |
| 194 | |
| 195 | HandleTable::Entry::Entry(scoped_refptr<Dispatcher> dispatcher) |
tzik | 1fb60dd | 2017-03-08 04:52:06 | [diff] [blame] | 196 | : dispatcher(std::move(dispatcher)) {} |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 197 | |
vmpstr | 11ea385 | 2016-02-29 20:33:57 | [diff] [blame] | 198 | HandleTable::Entry::Entry(const Entry& other) = default; |
| 199 | |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 200 | HandleTable::Entry::~Entry() {} |
| 201 | |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 202 | } // namespace edk |
| 203 | } // namespace mojo |