Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #include "base/stats_table.h" |
michael@0 | 6 | |
michael@0 | 7 | #include "base/logging.h" |
michael@0 | 8 | #include "base/platform_thread.h" |
michael@0 | 9 | #include "base/process_util.h" |
michael@0 | 10 | #include "base/scoped_ptr.h" |
michael@0 | 11 | #include "base/shared_memory.h" |
michael@0 | 12 | #include "base/string_piece.h" |
michael@0 | 13 | #include "base/string_util.h" |
michael@0 | 14 | #include "base/sys_string_conversions.h" |
michael@0 | 15 | #include "base/thread_local_storage.h" |
michael@0 | 16 | |
michael@0 | 17 | #if defined(OS_POSIX) |
michael@0 | 18 | #include "errno.h" |
michael@0 | 19 | #endif |
michael@0 | 20 | |
michael@0 | 21 | // The StatsTable uses a shared memory segment that is laid out as follows |
michael@0 | 22 | // |
michael@0 | 23 | // +-------------------------------------------+ |
michael@0 | 24 | // | Version | Size | MaxCounters | MaxThreads | |
michael@0 | 25 | // +-------------------------------------------+ |
michael@0 | 26 | // | Thread names table | |
michael@0 | 27 | // +-------------------------------------------+ |
michael@0 | 28 | // | Thread TID table | |
michael@0 | 29 | // +-------------------------------------------+ |
michael@0 | 30 | // | Thread PID table | |
michael@0 | 31 | // +-------------------------------------------+ |
michael@0 | 32 | // | Counter names table | |
michael@0 | 33 | // +-------------------------------------------+ |
michael@0 | 34 | // | Data | |
michael@0 | 35 | // +-------------------------------------------+ |
michael@0 | 36 | // |
michael@0 | 37 | // The data layout is a grid, where the columns are the thread_ids and the |
michael@0 | 38 | // rows are the counter_ids. |
michael@0 | 39 | // |
michael@0 | 40 | // If the first character of the thread_name is '\0', then that column is |
michael@0 | 41 | // empty. |
michael@0 | 42 | // If the first character of the counter_name is '\0', then that row is |
michael@0 | 43 | // empty. |
michael@0 | 44 | // |
michael@0 | 45 | // About Locking: |
michael@0 | 46 | // This class is designed to be both multi-thread and multi-process safe. |
michael@0 | 47 | // Aside from initialization, this is done by partitioning the data which |
michael@0 | 48 | // each thread uses so that no locking is required. However, to allocate |
michael@0 | 49 | // the rows and columns of the table to particular threads, locking is |
michael@0 | 50 | // required. |
michael@0 | 51 | // |
michael@0 | 52 | // At the shared-memory level, we have a lock. This lock protects the |
michael@0 | 53 | // shared-memory table only, and is used when we create new counters (e.g. |
michael@0 | 54 | // use rows) or when we register new threads (e.g. use columns). Reading |
michael@0 | 55 | // data from the table does not require any locking at the shared memory |
michael@0 | 56 | // level. |
michael@0 | 57 | // |
michael@0 | 58 | // Each process which accesses the table will create a StatsTable object. |
michael@0 | 59 | // The StatsTable maintains a hash table of the existing counters in the |
michael@0 | 60 | // table for faster lookup. Since the hash table is process specific, |
michael@0 | 61 | // each process maintains its own cache. We avoid complexity here by never |
michael@0 | 62 | // de-allocating from the hash table. (Counters are dynamically added, |
michael@0 | 63 | // but not dynamically removed). |
michael@0 | 64 | |
michael@0 | 65 | // In order for external viewers to be able to read our shared memory, |
michael@0 | 66 | // we all need to use the same size ints. |
michael@0 | 67 | COMPILE_ASSERT(sizeof(int)==4, expect_4_byte_ints); |
michael@0 | 68 | |
michael@0 | 69 | namespace { |
michael@0 | 70 | |
michael@0 | 71 | // An internal version in case we ever change the format of this |
michael@0 | 72 | // file, and so that we can identify our table. |
michael@0 | 73 | const int kTableVersion = 0x13131313; |
michael@0 | 74 | |
michael@0 | 75 | // The name for un-named counters and threads in the table. |
michael@0 | 76 | const char kUnknownName[] = "<unknown>"; |
michael@0 | 77 | |
michael@0 | 78 | // Calculates delta to align an offset to the size of an int |
michael@0 | 79 | inline int AlignOffset(int offset) { |
michael@0 | 80 | return (sizeof(int) - (offset % sizeof(int))) % sizeof(int); |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | inline int AlignedSize(int size) { |
michael@0 | 84 | return size + AlignOffset(size); |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | // StatsTableTLSData carries the data stored in the TLS slots for the |
michael@0 | 88 | // StatsTable. This is used so that we can properly cleanup when the |
michael@0 | 89 | // thread exits and return the table slot. |
michael@0 | 90 | // |
michael@0 | 91 | // Each thread that calls RegisterThread in the StatsTable will have |
michael@0 | 92 | // a StatsTableTLSData stored in its TLS. |
michael@0 | 93 | struct StatsTableTLSData { |
michael@0 | 94 | StatsTable* table; |
michael@0 | 95 | int slot; |
michael@0 | 96 | }; |
michael@0 | 97 | |
michael@0 | 98 | } // namespace |
michael@0 | 99 | |
michael@0 | 100 | // The StatsTablePrivate maintains convenience pointers into the |
michael@0 | 101 | // shared memory segment. Use this class to keep the data structure |
michael@0 | 102 | // clean and accessible. |
michael@0 | 103 | class StatsTablePrivate { |
michael@0 | 104 | public: |
michael@0 | 105 | // Various header information contained in the memory mapped segment. |
michael@0 | 106 | struct TableHeader { |
michael@0 | 107 | int version; |
michael@0 | 108 | int size; |
michael@0 | 109 | int max_counters; |
michael@0 | 110 | int max_threads; |
michael@0 | 111 | }; |
michael@0 | 112 | |
michael@0 | 113 | // Construct a new StatsTablePrivate based on expected size parameters, or |
michael@0 | 114 | // return NULL on failure. |
michael@0 | 115 | static StatsTablePrivate* New(const std::string& name, int size, |
michael@0 | 116 | int max_threads, int max_counters); |
michael@0 | 117 | |
michael@0 | 118 | base::SharedMemory* shared_memory() { return &shared_memory_; } |
michael@0 | 119 | |
michael@0 | 120 | // Accessors for our header pointers |
michael@0 | 121 | TableHeader* table_header() const { return table_header_; } |
michael@0 | 122 | int version() const { return table_header_->version; } |
michael@0 | 123 | int size() const { return table_header_->size; } |
michael@0 | 124 | int max_counters() const { return table_header_->max_counters; } |
michael@0 | 125 | int max_threads() const { return table_header_->max_threads; } |
michael@0 | 126 | |
michael@0 | 127 | // Accessors for our tables |
michael@0 | 128 | char* thread_name(int slot_id) const { |
michael@0 | 129 | return &thread_names_table_[ |
michael@0 | 130 | (slot_id-1) * (StatsTable::kMaxThreadNameLength)]; |
michael@0 | 131 | } |
michael@0 | 132 | PlatformThreadId* thread_tid(int slot_id) const { |
michael@0 | 133 | return &(thread_tid_table_[slot_id-1]); |
michael@0 | 134 | } |
michael@0 | 135 | int* thread_pid(int slot_id) const { |
michael@0 | 136 | return &(thread_pid_table_[slot_id-1]); |
michael@0 | 137 | } |
michael@0 | 138 | char* counter_name(int counter_id) const { |
michael@0 | 139 | return &counter_names_table_[ |
michael@0 | 140 | (counter_id-1) * (StatsTable::kMaxCounterNameLength)]; |
michael@0 | 141 | } |
michael@0 | 142 | int* row(int counter_id) const { |
michael@0 | 143 | return &data_table_[(counter_id-1) * max_threads()]; |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | private: |
michael@0 | 147 | // Constructor is private because you should use New() instead. |
michael@0 | 148 | StatsTablePrivate() {} |
michael@0 | 149 | |
michael@0 | 150 | // Initializes the table on first access. Sets header values |
michael@0 | 151 | // appropriately and zeroes all counters. |
michael@0 | 152 | void InitializeTable(void* memory, int size, int max_counters, |
michael@0 | 153 | int max_threads); |
michael@0 | 154 | |
michael@0 | 155 | // Initializes our in-memory pointers into a pre-created StatsTable. |
michael@0 | 156 | void ComputeMappedPointers(void* memory); |
michael@0 | 157 | |
michael@0 | 158 | base::SharedMemory shared_memory_; |
michael@0 | 159 | TableHeader* table_header_; |
michael@0 | 160 | char* thread_names_table_; |
michael@0 | 161 | PlatformThreadId* thread_tid_table_; |
michael@0 | 162 | int* thread_pid_table_; |
michael@0 | 163 | char* counter_names_table_; |
michael@0 | 164 | int* data_table_; |
michael@0 | 165 | }; |
michael@0 | 166 | |
michael@0 | 167 | // static |
michael@0 | 168 | StatsTablePrivate* StatsTablePrivate::New(const std::string& name, |
michael@0 | 169 | int size, |
michael@0 | 170 | int max_threads, |
michael@0 | 171 | int max_counters) { |
michael@0 | 172 | scoped_ptr<StatsTablePrivate> priv(new StatsTablePrivate()); |
michael@0 | 173 | if (!priv->shared_memory_.Create(name, false, true, size)) |
michael@0 | 174 | return NULL; |
michael@0 | 175 | if (!priv->shared_memory_.Map(size)) |
michael@0 | 176 | return NULL; |
michael@0 | 177 | void* memory = priv->shared_memory_.memory(); |
michael@0 | 178 | |
michael@0 | 179 | TableHeader* header = static_cast<TableHeader*>(memory); |
michael@0 | 180 | |
michael@0 | 181 | // If the version does not match, then assume the table needs |
michael@0 | 182 | // to be initialized. |
michael@0 | 183 | if (header->version != kTableVersion) |
michael@0 | 184 | priv->InitializeTable(memory, size, max_counters, max_threads); |
michael@0 | 185 | |
michael@0 | 186 | // We have a valid table, so compute our pointers. |
michael@0 | 187 | priv->ComputeMappedPointers(memory); |
michael@0 | 188 | |
michael@0 | 189 | return priv.release(); |
michael@0 | 190 | } |
michael@0 | 191 | |
michael@0 | 192 | void StatsTablePrivate::InitializeTable(void* memory, int size, |
michael@0 | 193 | int max_counters, |
michael@0 | 194 | int max_threads) { |
michael@0 | 195 | // Zero everything. |
michael@0 | 196 | memset(memory, 0, size); |
michael@0 | 197 | |
michael@0 | 198 | // Initialize the header. |
michael@0 | 199 | TableHeader* header = static_cast<TableHeader*>(memory); |
michael@0 | 200 | header->version = kTableVersion; |
michael@0 | 201 | header->size = size; |
michael@0 | 202 | header->max_counters = max_counters; |
michael@0 | 203 | header->max_threads = max_threads; |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | void StatsTablePrivate::ComputeMappedPointers(void* memory) { |
michael@0 | 207 | char* data = static_cast<char*>(memory); |
michael@0 | 208 | int offset = 0; |
michael@0 | 209 | |
michael@0 | 210 | table_header_ = reinterpret_cast<TableHeader*>(data); |
michael@0 | 211 | offset += sizeof(*table_header_); |
michael@0 | 212 | offset += AlignOffset(offset); |
michael@0 | 213 | |
michael@0 | 214 | // Verify we're looking at a valid StatsTable. |
michael@0 | 215 | DCHECK_EQ(table_header_->version, kTableVersion); |
michael@0 | 216 | |
michael@0 | 217 | thread_names_table_ = reinterpret_cast<char*>(data + offset); |
michael@0 | 218 | offset += sizeof(char) * |
michael@0 | 219 | max_threads() * StatsTable::kMaxThreadNameLength; |
michael@0 | 220 | offset += AlignOffset(offset); |
michael@0 | 221 | |
michael@0 | 222 | thread_tid_table_ = reinterpret_cast<PlatformThreadId*>(data + offset); |
michael@0 | 223 | offset += sizeof(int) * max_threads(); |
michael@0 | 224 | offset += AlignOffset(offset); |
michael@0 | 225 | |
michael@0 | 226 | thread_pid_table_ = reinterpret_cast<int*>(data + offset); |
michael@0 | 227 | offset += sizeof(int) * max_threads(); |
michael@0 | 228 | offset += AlignOffset(offset); |
michael@0 | 229 | |
michael@0 | 230 | counter_names_table_ = reinterpret_cast<char*>(data + offset); |
michael@0 | 231 | offset += sizeof(char) * |
michael@0 | 232 | max_counters() * StatsTable::kMaxCounterNameLength; |
michael@0 | 233 | offset += AlignOffset(offset); |
michael@0 | 234 | |
michael@0 | 235 | data_table_ = reinterpret_cast<int*>(data + offset); |
michael@0 | 236 | offset += sizeof(int) * max_threads() * max_counters(); |
michael@0 | 237 | |
michael@0 | 238 | DCHECK_EQ(offset, size()); |
michael@0 | 239 | } |
michael@0 | 240 | |
michael@0 | 241 | |
michael@0 | 242 | |
michael@0 | 243 | // We keep a singleton table which can be easily accessed. |
michael@0 | 244 | StatsTable* StatsTable::global_table_ = NULL; |
michael@0 | 245 | |
michael@0 | 246 | StatsTable::StatsTable(const std::string& name, int max_threads, |
michael@0 | 247 | int max_counters) |
michael@0 | 248 | : impl_(NULL), |
michael@0 | 249 | tls_index_(SlotReturnFunction) { |
michael@0 | 250 | int table_size = |
michael@0 | 251 | AlignedSize(sizeof(StatsTablePrivate::TableHeader)) + |
michael@0 | 252 | AlignedSize((max_counters * sizeof(char) * kMaxCounterNameLength)) + |
michael@0 | 253 | AlignedSize((max_threads * sizeof(char) * kMaxThreadNameLength)) + |
michael@0 | 254 | AlignedSize(max_threads * sizeof(int)) + |
michael@0 | 255 | AlignedSize(max_threads * sizeof(int)) + |
michael@0 | 256 | AlignedSize((sizeof(int) * (max_counters * max_threads))); |
michael@0 | 257 | |
michael@0 | 258 | impl_ = StatsTablePrivate::New(name, table_size, max_threads, max_counters); |
michael@0 | 259 | |
michael@0 | 260 | // TODO(port): clean up this error reporting. |
michael@0 | 261 | #if defined(OS_WIN) |
michael@0 | 262 | if (!impl_) |
michael@0 | 263 | CHROMIUM_LOG(ERROR) << "StatsTable did not initialize:" << GetLastError(); |
michael@0 | 264 | #elif defined(OS_POSIX) |
michael@0 | 265 | if (!impl_) |
michael@0 | 266 | CHROMIUM_LOG(ERROR) << "StatsTable did not initialize:" << strerror(errno); |
michael@0 | 267 | #endif |
michael@0 | 268 | } |
michael@0 | 269 | |
michael@0 | 270 | StatsTable::~StatsTable() { |
michael@0 | 271 | // Before we tear down our copy of the table, be sure to |
michael@0 | 272 | // unregister our thread. |
michael@0 | 273 | UnregisterThread(); |
michael@0 | 274 | |
michael@0 | 275 | // Return ThreadLocalStorage. At this point, if any registered threads |
michael@0 | 276 | // still exist, they cannot Unregister. |
michael@0 | 277 | tls_index_.Free(); |
michael@0 | 278 | |
michael@0 | 279 | // Cleanup our shared memory. |
michael@0 | 280 | delete impl_; |
michael@0 | 281 | |
michael@0 | 282 | // If we are the global table, unregister ourselves. |
michael@0 | 283 | if (global_table_ == this) |
michael@0 | 284 | global_table_ = NULL; |
michael@0 | 285 | } |
michael@0 | 286 | |
michael@0 | 287 | int StatsTable::RegisterThread(const std::string& name) { |
michael@0 | 288 | int slot = 0; |
michael@0 | 289 | |
michael@0 | 290 | // Registering a thread requires that we lock the shared memory |
michael@0 | 291 | // so that two threads don't grab the same slot. Fortunately, |
michael@0 | 292 | // thread creation shouldn't happen in inner loops. |
michael@0 | 293 | { |
michael@0 | 294 | base::SharedMemoryAutoLock lock(impl_->shared_memory()); |
michael@0 | 295 | slot = FindEmptyThread(); |
michael@0 | 296 | if (!slot) { |
michael@0 | 297 | return 0; |
michael@0 | 298 | } |
michael@0 | 299 | |
michael@0 | 300 | DCHECK(impl_); |
michael@0 | 301 | |
michael@0 | 302 | // We have space, so consume a column in the table. |
michael@0 | 303 | std::string thread_name = name; |
michael@0 | 304 | if (name.empty()) |
michael@0 | 305 | thread_name = kUnknownName; |
michael@0 | 306 | base::strlcpy(impl_->thread_name(slot), thread_name.c_str(), |
michael@0 | 307 | kMaxThreadNameLength); |
michael@0 | 308 | *(impl_->thread_tid(slot)) = PlatformThread::CurrentId(); |
michael@0 | 309 | *(impl_->thread_pid(slot)) = base::GetCurrentProcId(); |
michael@0 | 310 | } |
michael@0 | 311 | |
michael@0 | 312 | // Set our thread local storage. |
michael@0 | 313 | StatsTableTLSData* data = new StatsTableTLSData; |
michael@0 | 314 | data->table = this; |
michael@0 | 315 | data->slot = slot; |
michael@0 | 316 | tls_index_.Set(data); |
michael@0 | 317 | return slot; |
michael@0 | 318 | } |
michael@0 | 319 | |
michael@0 | 320 | StatsTableTLSData* StatsTable::GetTLSData() const { |
michael@0 | 321 | StatsTableTLSData* data = |
michael@0 | 322 | static_cast<StatsTableTLSData*>(tls_index_.Get()); |
michael@0 | 323 | if (!data) |
michael@0 | 324 | return NULL; |
michael@0 | 325 | |
michael@0 | 326 | DCHECK(data->slot); |
michael@0 | 327 | DCHECK_EQ(data->table, this); |
michael@0 | 328 | return data; |
michael@0 | 329 | } |
michael@0 | 330 | |
michael@0 | 331 | void StatsTable::UnregisterThread() { |
michael@0 | 332 | UnregisterThread(GetTLSData()); |
michael@0 | 333 | } |
michael@0 | 334 | |
michael@0 | 335 | void StatsTable::UnregisterThread(StatsTableTLSData* data) { |
michael@0 | 336 | if (!data) |
michael@0 | 337 | return; |
michael@0 | 338 | DCHECK(impl_); |
michael@0 | 339 | |
michael@0 | 340 | // Mark the slot free by zeroing out the thread name. |
michael@0 | 341 | char* name = impl_->thread_name(data->slot); |
michael@0 | 342 | *name = '\0'; |
michael@0 | 343 | |
michael@0 | 344 | // Remove the calling thread's TLS so that it cannot use the slot. |
michael@0 | 345 | tls_index_.Set(NULL); |
michael@0 | 346 | delete data; |
michael@0 | 347 | } |
michael@0 | 348 | |
michael@0 | 349 | void StatsTable::SlotReturnFunction(void* data) { |
michael@0 | 350 | // This is called by the TLS destructor, which on some platforms has |
michael@0 | 351 | // already cleared the TLS info, so use the tls_data argument |
michael@0 | 352 | // rather than trying to fetch it ourselves. |
michael@0 | 353 | StatsTableTLSData* tls_data = static_cast<StatsTableTLSData*>(data); |
michael@0 | 354 | if (tls_data) { |
michael@0 | 355 | DCHECK(tls_data->table); |
michael@0 | 356 | tls_data->table->UnregisterThread(tls_data); |
michael@0 | 357 | } |
michael@0 | 358 | } |
michael@0 | 359 | |
michael@0 | 360 | int StatsTable::CountThreadsRegistered() const { |
michael@0 | 361 | if (!impl_) |
michael@0 | 362 | return 0; |
michael@0 | 363 | |
michael@0 | 364 | // Loop through the shared memory and count the threads that are active. |
michael@0 | 365 | // We intentionally do not lock the table during the operation. |
michael@0 | 366 | int count = 0; |
michael@0 | 367 | for (int index = 1; index <= impl_->max_threads(); index++) { |
michael@0 | 368 | char* name = impl_->thread_name(index); |
michael@0 | 369 | if (*name != '\0') |
michael@0 | 370 | count++; |
michael@0 | 371 | } |
michael@0 | 372 | return count; |
michael@0 | 373 | } |
michael@0 | 374 | |
michael@0 | 375 | int StatsTable::GetSlot() const { |
michael@0 | 376 | StatsTableTLSData* data = GetTLSData(); |
michael@0 | 377 | if (!data) |
michael@0 | 378 | return 0; |
michael@0 | 379 | return data->slot; |
michael@0 | 380 | } |
michael@0 | 381 | |
michael@0 | 382 | int StatsTable::FindEmptyThread() const { |
michael@0 | 383 | // Note: the API returns slots numbered from 1..N, although |
michael@0 | 384 | // internally, the array is 0..N-1. This is so that we can return |
michael@0 | 385 | // zero as "not found". |
michael@0 | 386 | // |
michael@0 | 387 | // The reason for doing this is because the thread 'slot' is stored |
michael@0 | 388 | // in TLS, which is always initialized to zero, not -1. If 0 were |
michael@0 | 389 | // returned as a valid slot number, it would be confused with the |
michael@0 | 390 | // uninitialized state. |
michael@0 | 391 | if (!impl_) |
michael@0 | 392 | return 0; |
michael@0 | 393 | |
michael@0 | 394 | int index = 1; |
michael@0 | 395 | for (; index <= impl_->max_threads(); index++) { |
michael@0 | 396 | char* name = impl_->thread_name(index); |
michael@0 | 397 | if (!*name) |
michael@0 | 398 | break; |
michael@0 | 399 | } |
michael@0 | 400 | if (index > impl_->max_threads()) |
michael@0 | 401 | return 0; // The table is full. |
michael@0 | 402 | return index; |
michael@0 | 403 | } |
michael@0 | 404 | |
michael@0 | 405 | int StatsTable::FindCounterOrEmptyRow(const std::string& name) const { |
michael@0 | 406 | // Note: the API returns slots numbered from 1..N, although |
michael@0 | 407 | // internally, the array is 0..N-1. This is so that we can return |
michael@0 | 408 | // zero as "not found". |
michael@0 | 409 | // |
michael@0 | 410 | // There isn't much reason for this other than to be consistent |
michael@0 | 411 | // with the way we track columns for thread slots. (See comments |
michael@0 | 412 | // in FindEmptyThread for why it is done this way). |
michael@0 | 413 | if (!impl_) |
michael@0 | 414 | return 0; |
michael@0 | 415 | |
michael@0 | 416 | int free_slot = 0; |
michael@0 | 417 | for (int index = 1; index <= impl_->max_counters(); index++) { |
michael@0 | 418 | char* row_name = impl_->counter_name(index); |
michael@0 | 419 | if (!*row_name && !free_slot) |
michael@0 | 420 | free_slot = index; // save that we found a free slot |
michael@0 | 421 | else if (!strncmp(row_name, name.c_str(), kMaxCounterNameLength)) |
michael@0 | 422 | return index; |
michael@0 | 423 | } |
michael@0 | 424 | return free_slot; |
michael@0 | 425 | } |
michael@0 | 426 | |
michael@0 | 427 | int StatsTable::FindCounter(const std::string& name) { |
michael@0 | 428 | // Note: the API returns counters numbered from 1..N, although |
michael@0 | 429 | // internally, the array is 0..N-1. This is so that we can return |
michael@0 | 430 | // zero as "not found". |
michael@0 | 431 | if (!impl_) |
michael@0 | 432 | return 0; |
michael@0 | 433 | |
michael@0 | 434 | // Create a scope for our auto-lock. |
michael@0 | 435 | { |
michael@0 | 436 | AutoLock scoped_lock(counters_lock_); |
michael@0 | 437 | |
michael@0 | 438 | // Attempt to find the counter. |
michael@0 | 439 | CountersMap::const_iterator iter; |
michael@0 | 440 | iter = counters_.find(name); |
michael@0 | 441 | if (iter != counters_.end()) |
michael@0 | 442 | return iter->second; |
michael@0 | 443 | } |
michael@0 | 444 | |
michael@0 | 445 | // Counter does not exist, so add it. |
michael@0 | 446 | return AddCounter(name); |
michael@0 | 447 | } |
michael@0 | 448 | |
michael@0 | 449 | int StatsTable::AddCounter(const std::string& name) { |
michael@0 | 450 | DCHECK(impl_); |
michael@0 | 451 | |
michael@0 | 452 | if (!impl_) |
michael@0 | 453 | return 0; |
michael@0 | 454 | |
michael@0 | 455 | int counter_id = 0; |
michael@0 | 456 | { |
michael@0 | 457 | // To add a counter to the shared memory, we need the |
michael@0 | 458 | // shared memory lock. |
michael@0 | 459 | base::SharedMemoryAutoLock lock(impl_->shared_memory()); |
michael@0 | 460 | |
michael@0 | 461 | // We have space, so create a new counter. |
michael@0 | 462 | counter_id = FindCounterOrEmptyRow(name); |
michael@0 | 463 | if (!counter_id) |
michael@0 | 464 | return 0; |
michael@0 | 465 | |
michael@0 | 466 | std::string counter_name = name; |
michael@0 | 467 | if (name.empty()) |
michael@0 | 468 | counter_name = kUnknownName; |
michael@0 | 469 | base::strlcpy(impl_->counter_name(counter_id), counter_name.c_str(), |
michael@0 | 470 | kMaxCounterNameLength); |
michael@0 | 471 | } |
michael@0 | 472 | |
michael@0 | 473 | // now add to our in-memory cache |
michael@0 | 474 | { |
michael@0 | 475 | AutoLock lock(counters_lock_); |
michael@0 | 476 | counters_[name] = counter_id; |
michael@0 | 477 | } |
michael@0 | 478 | return counter_id; |
michael@0 | 479 | } |
michael@0 | 480 | |
michael@0 | 481 | int* StatsTable::GetLocation(int counter_id, int slot_id) const { |
michael@0 | 482 | if (!impl_) |
michael@0 | 483 | return NULL; |
michael@0 | 484 | if (slot_id > impl_->max_threads()) |
michael@0 | 485 | return NULL; |
michael@0 | 486 | |
michael@0 | 487 | int* row = impl_->row(counter_id); |
michael@0 | 488 | return &(row[slot_id-1]); |
michael@0 | 489 | } |
michael@0 | 490 | |
michael@0 | 491 | const char* StatsTable::GetRowName(int index) const { |
michael@0 | 492 | if (!impl_) |
michael@0 | 493 | return NULL; |
michael@0 | 494 | |
michael@0 | 495 | return impl_->counter_name(index); |
michael@0 | 496 | } |
michael@0 | 497 | |
michael@0 | 498 | int StatsTable::GetRowValue(int index, int pid) const { |
michael@0 | 499 | if (!impl_) |
michael@0 | 500 | return 0; |
michael@0 | 501 | |
michael@0 | 502 | int rv = 0; |
michael@0 | 503 | int* row = impl_->row(index); |
michael@0 | 504 | for (int slot_id = 0; slot_id < impl_->max_threads(); slot_id++) { |
michael@0 | 505 | if (pid == 0 || *impl_->thread_pid(slot_id) == pid) |
michael@0 | 506 | rv += row[slot_id]; |
michael@0 | 507 | } |
michael@0 | 508 | return rv; |
michael@0 | 509 | } |
michael@0 | 510 | |
michael@0 | 511 | int StatsTable::GetRowValue(int index) const { |
michael@0 | 512 | return GetRowValue(index, 0); |
michael@0 | 513 | } |
michael@0 | 514 | |
michael@0 | 515 | int StatsTable::GetCounterValue(const std::string& name, int pid) { |
michael@0 | 516 | if (!impl_) |
michael@0 | 517 | return 0; |
michael@0 | 518 | |
michael@0 | 519 | int row = FindCounter(name); |
michael@0 | 520 | if (!row) |
michael@0 | 521 | return 0; |
michael@0 | 522 | return GetRowValue(row, pid); |
michael@0 | 523 | } |
michael@0 | 524 | |
michael@0 | 525 | int StatsTable::GetCounterValue(const std::string& name) { |
michael@0 | 526 | return GetCounterValue(name, 0); |
michael@0 | 527 | } |
michael@0 | 528 | |
michael@0 | 529 | int StatsTable::GetMaxCounters() const { |
michael@0 | 530 | if (!impl_) |
michael@0 | 531 | return 0; |
michael@0 | 532 | return impl_->max_counters(); |
michael@0 | 533 | } |
michael@0 | 534 | |
michael@0 | 535 | int StatsTable::GetMaxThreads() const { |
michael@0 | 536 | if (!impl_) |
michael@0 | 537 | return 0; |
michael@0 | 538 | return impl_->max_threads(); |
michael@0 | 539 | } |
michael@0 | 540 | |
michael@0 | 541 | int* StatsTable::FindLocation(const char* name) { |
michael@0 | 542 | // Get the static StatsTable |
michael@0 | 543 | StatsTable *table = StatsTable::current(); |
michael@0 | 544 | if (!table) |
michael@0 | 545 | return NULL; |
michael@0 | 546 | |
michael@0 | 547 | // Get the slot for this thread. Try to register |
michael@0 | 548 | // it if none exists. |
michael@0 | 549 | int slot = table->GetSlot(); |
michael@0 | 550 | if (!slot && !(slot = table->RegisterThread(""))) |
michael@0 | 551 | return NULL; |
michael@0 | 552 | |
michael@0 | 553 | // Find the counter id for the counter. |
michael@0 | 554 | std::string str_name(name); |
michael@0 | 555 | int counter = table->FindCounter(str_name); |
michael@0 | 556 | |
michael@0 | 557 | // Now we can find the location in the table. |
michael@0 | 558 | return table->GetLocation(counter, slot); |
michael@0 | 559 | } |