Make certificate viewer a tab-modal dialog.
[chromium-blink-merge.git] / base / metrics / stats_table.cc
blob9585863089e05f04579514481533b9ead4eb070f
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 "base/metrics/stats_table.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/process_util.h"
10 #include "base/shared_memory.h"
11 #include "base/string_util.h"
12 #include "base/strings/string_piece.h"
13 #include "base/threading/platform_thread.h"
14 #include "base/threading/thread_local_storage.h"
15 #include "base/utf_string_conversions.h"
17 #if defined(OS_POSIX)
18 #include "errno.h"
19 #endif
21 namespace base {
23 // The StatsTable uses a shared memory segment that is laid out as follows
25 // +-------------------------------------------+
26 // | Version | Size | MaxCounters | MaxThreads |
27 // +-------------------------------------------+
28 // | Thread names table |
29 // +-------------------------------------------+
30 // | Thread TID table |
31 // +-------------------------------------------+
32 // | Thread PID table |
33 // +-------------------------------------------+
34 // | Counter names table |
35 // +-------------------------------------------+
36 // | Data |
37 // +-------------------------------------------+
39 // The data layout is a grid, where the columns are the thread_ids and the
40 // rows are the counter_ids.
42 // If the first character of the thread_name is '\0', then that column is
43 // empty.
44 // If the first character of the counter_name is '\0', then that row is
45 // empty.
47 // About Locking:
48 // This class is designed to be both multi-thread and multi-process safe.
49 // Aside from initialization, this is done by partitioning the data which
50 // each thread uses so that no locking is required. However, to allocate
51 // the rows and columns of the table to particular threads, locking is
52 // required.
54 // At the shared-memory level, we have a lock. This lock protects the
55 // shared-memory table only, and is used when we create new counters (e.g.
56 // use rows) or when we register new threads (e.g. use columns). Reading
57 // data from the table does not require any locking at the shared memory
58 // level.
60 // Each process which accesses the table will create a StatsTable object.
61 // The StatsTable maintains a hash table of the existing counters in the
62 // table for faster lookup. Since the hash table is process specific,
63 // each process maintains its own cache. We avoid complexity here by never
64 // de-allocating from the hash table. (Counters are dynamically added,
65 // but not dynamically removed).
67 // In order for external viewers to be able to read our shared memory,
68 // we all need to use the same size ints.
69 COMPILE_ASSERT(sizeof(int)==4, expect_4_byte_ints);
71 namespace {
73 // An internal version in case we ever change the format of this
74 // file, and so that we can identify our table.
75 const int kTableVersion = 0x13131313;
77 // The name for un-named counters and threads in the table.
78 const char kUnknownName[] = "<unknown>";
80 // Calculates delta to align an offset to the size of an int
81 inline int AlignOffset(int offset) {
82 return (sizeof(int) - (offset % sizeof(int))) % sizeof(int);
85 inline int AlignedSize(int size) {
86 return size + AlignOffset(size);
89 } // namespace
91 // The StatsTable::Private maintains convenience pointers into the
92 // shared memory segment. Use this class to keep the data structure
93 // clean and accessible.
94 class StatsTable::Private {
95 public:
96 // Various header information contained in the memory mapped segment.
97 struct TableHeader {
98 int version;
99 int size;
100 int max_counters;
101 int max_threads;
104 // Construct a new Private based on expected size parameters, or
105 // return NULL on failure.
106 static Private* New(const std::string& name, int size,
107 int max_threads, int max_counters);
109 SharedMemory* shared_memory() { return &shared_memory_; }
111 // Accessors for our header pointers
112 TableHeader* table_header() const { return table_header_; }
113 int version() const { return table_header_->version; }
114 int size() const { return table_header_->size; }
115 int max_counters() const { return table_header_->max_counters; }
116 int max_threads() const { return table_header_->max_threads; }
118 // Accessors for our tables
119 char* thread_name(int slot_id) const {
120 return &thread_names_table_[
121 (slot_id-1) * (StatsTable::kMaxThreadNameLength)];
123 PlatformThreadId* thread_tid(int slot_id) const {
124 return &(thread_tid_table_[slot_id-1]);
126 int* thread_pid(int slot_id) const {
127 return &(thread_pid_table_[slot_id-1]);
129 char* counter_name(int counter_id) const {
130 return &counter_names_table_[
131 (counter_id-1) * (StatsTable::kMaxCounterNameLength)];
133 int* row(int counter_id) const {
134 return &data_table_[(counter_id-1) * max_threads()];
137 private:
138 // Constructor is private because you should use New() instead.
139 Private()
140 : table_header_(NULL),
141 thread_names_table_(NULL),
142 thread_tid_table_(NULL),
143 thread_pid_table_(NULL),
144 counter_names_table_(NULL),
145 data_table_(NULL) {
148 // Initializes the table on first access. Sets header values
149 // appropriately and zeroes all counters.
150 void InitializeTable(void* memory, int size, int max_counters,
151 int max_threads);
153 // Initializes our in-memory pointers into a pre-created StatsTable.
154 void ComputeMappedPointers(void* memory);
156 SharedMemory shared_memory_;
157 TableHeader* table_header_;
158 char* thread_names_table_;
159 PlatformThreadId* thread_tid_table_;
160 int* thread_pid_table_;
161 char* counter_names_table_;
162 int* data_table_;
165 // static
166 StatsTable::Private* StatsTable::Private::New(const std::string& name,
167 int size,
168 int max_threads,
169 int max_counters) {
170 scoped_ptr<Private> priv(new Private());
171 if (!priv->shared_memory_.CreateNamed(name, true, size))
172 return NULL;
173 if (!priv->shared_memory_.Map(size))
174 return NULL;
175 void* memory = priv->shared_memory_.memory();
177 TableHeader* header = static_cast<TableHeader*>(memory);
179 // If the version does not match, then assume the table needs
180 // to be initialized.
181 if (header->version != kTableVersion)
182 priv->InitializeTable(memory, size, max_counters, max_threads);
184 // We have a valid table, so compute our pointers.
185 priv->ComputeMappedPointers(memory);
187 return priv.release();
190 void StatsTable::Private::InitializeTable(void* memory, int size,
191 int max_counters,
192 int max_threads) {
193 // Zero everything.
194 memset(memory, 0, size);
196 // Initialize the header.
197 TableHeader* header = static_cast<TableHeader*>(memory);
198 header->version = kTableVersion;
199 header->size = size;
200 header->max_counters = max_counters;
201 header->max_threads = max_threads;
204 void StatsTable::Private::ComputeMappedPointers(void* memory) {
205 char* data = static_cast<char*>(memory);
206 int offset = 0;
208 table_header_ = reinterpret_cast<TableHeader*>(data);
209 offset += sizeof(*table_header_);
210 offset += AlignOffset(offset);
212 // Verify we're looking at a valid StatsTable.
213 DCHECK_EQ(table_header_->version, kTableVersion);
215 thread_names_table_ = reinterpret_cast<char*>(data + offset);
216 offset += sizeof(char) *
217 max_threads() * StatsTable::kMaxThreadNameLength;
218 offset += AlignOffset(offset);
220 thread_tid_table_ = reinterpret_cast<PlatformThreadId*>(data + offset);
221 offset += sizeof(int) * max_threads();
222 offset += AlignOffset(offset);
224 thread_pid_table_ = reinterpret_cast<int*>(data + offset);
225 offset += sizeof(int) * max_threads();
226 offset += AlignOffset(offset);
228 counter_names_table_ = reinterpret_cast<char*>(data + offset);
229 offset += sizeof(char) *
230 max_counters() * StatsTable::kMaxCounterNameLength;
231 offset += AlignOffset(offset);
233 data_table_ = reinterpret_cast<int*>(data + offset);
234 offset += sizeof(int) * max_threads() * max_counters();
236 DCHECK_EQ(offset, size());
239 // TLSData carries the data stored in the TLS slots for the
240 // StatsTable. This is used so that we can properly cleanup when the
241 // thread exits and return the table slot.
243 // Each thread that calls RegisterThread in the StatsTable will have
244 // a TLSData stored in its TLS.
245 struct StatsTable::TLSData {
246 StatsTable* table;
247 int slot;
250 // We keep a singleton table which can be easily accessed.
251 StatsTable* StatsTable::global_table_ = NULL;
253 StatsTable::StatsTable(const std::string& name, int max_threads,
254 int max_counters)
255 : impl_(NULL),
256 tls_index_(SlotReturnFunction) {
257 int table_size =
258 AlignedSize(sizeof(Private::TableHeader)) +
259 AlignedSize((max_counters * sizeof(char) * kMaxCounterNameLength)) +
260 AlignedSize((max_threads * sizeof(char) * kMaxThreadNameLength)) +
261 AlignedSize(max_threads * sizeof(int)) +
262 AlignedSize(max_threads * sizeof(int)) +
263 AlignedSize((sizeof(int) * (max_counters * max_threads)));
265 impl_ = Private::New(name, table_size, max_threads, max_counters);
267 if (!impl_)
268 DPLOG(ERROR) << "StatsTable did not initialize";
271 StatsTable::~StatsTable() {
272 // Before we tear down our copy of the table, be sure to
273 // unregister our thread.
274 UnregisterThread();
276 // Return ThreadLocalStorage. At this point, if any registered threads
277 // still exist, they cannot Unregister.
278 tls_index_.Free();
280 // Cleanup our shared memory.
281 delete impl_;
283 // If we are the global table, unregister ourselves.
284 if (global_table_ == this)
285 global_table_ = NULL;
288 int StatsTable::GetSlot() const {
289 TLSData* data = GetTLSData();
290 if (!data)
291 return 0;
292 return data->slot;
295 int StatsTable::RegisterThread(const std::string& name) {
296 int slot = 0;
297 if (!impl_)
298 return 0;
300 // Registering a thread requires that we lock the shared memory
301 // so that two threads don't grab the same slot. Fortunately,
302 // thread creation shouldn't happen in inner loops.
304 SharedMemoryAutoLock lock(impl_->shared_memory());
305 slot = FindEmptyThread();
306 if (!slot) {
307 return 0;
310 // We have space, so consume a column in the table.
311 std::string thread_name = name;
312 if (name.empty())
313 thread_name = kUnknownName;
314 strlcpy(impl_->thread_name(slot), thread_name.c_str(),
315 kMaxThreadNameLength);
316 *(impl_->thread_tid(slot)) = PlatformThread::CurrentId();
317 *(impl_->thread_pid(slot)) = GetCurrentProcId();
320 // Set our thread local storage.
321 TLSData* data = new TLSData;
322 data->table = this;
323 data->slot = slot;
324 tls_index_.Set(data);
325 return slot;
328 int StatsTable::CountThreadsRegistered() const {
329 if (!impl_)
330 return 0;
332 // Loop through the shared memory and count the threads that are active.
333 // We intentionally do not lock the table during the operation.
334 int count = 0;
335 for (int index = 1; index <= impl_->max_threads(); index++) {
336 char* name = impl_->thread_name(index);
337 if (*name != '\0')
338 count++;
340 return count;
343 int StatsTable::FindCounter(const std::string& name) {
344 // Note: the API returns counters numbered from 1..N, although
345 // internally, the array is 0..N-1. This is so that we can return
346 // zero as "not found".
347 if (!impl_)
348 return 0;
350 // Create a scope for our auto-lock.
352 AutoLock scoped_lock(counters_lock_);
354 // Attempt to find the counter.
355 CountersMap::const_iterator iter;
356 iter = counters_.find(name);
357 if (iter != counters_.end())
358 return iter->second;
361 // Counter does not exist, so add it.
362 return AddCounter(name);
365 int* StatsTable::GetLocation(int counter_id, int slot_id) const {
366 if (!impl_)
367 return NULL;
368 if (slot_id > impl_->max_threads())
369 return NULL;
371 int* row = impl_->row(counter_id);
372 return &(row[slot_id-1]);
375 const char* StatsTable::GetRowName(int index) const {
376 if (!impl_)
377 return NULL;
379 return impl_->counter_name(index);
382 int StatsTable::GetRowValue(int index) const {
383 return GetRowValue(index, 0);
386 int StatsTable::GetRowValue(int index, int pid) const {
387 if (!impl_)
388 return 0;
390 int rv = 0;
391 int* row = impl_->row(index);
392 for (int slot_id = 0; slot_id < impl_->max_threads(); slot_id++) {
393 if (pid == 0 || *impl_->thread_pid(slot_id) == pid)
394 rv += row[slot_id];
396 return rv;
399 int StatsTable::GetCounterValue(const std::string& name) {
400 return GetCounterValue(name, 0);
403 int StatsTable::GetCounterValue(const std::string& name, int pid) {
404 if (!impl_)
405 return 0;
407 int row = FindCounter(name);
408 if (!row)
409 return 0;
410 return GetRowValue(row, pid);
413 int StatsTable::GetMaxCounters() const {
414 if (!impl_)
415 return 0;
416 return impl_->max_counters();
419 int StatsTable::GetMaxThreads() const {
420 if (!impl_)
421 return 0;
422 return impl_->max_threads();
425 int* StatsTable::FindLocation(const char* name) {
426 // Get the static StatsTable
427 StatsTable *table = StatsTable::current();
428 if (!table)
429 return NULL;
431 // Get the slot for this thread. Try to register
432 // it if none exists.
433 int slot = table->GetSlot();
434 if (!slot && !(slot = table->RegisterThread(std::string())))
435 return NULL;
437 // Find the counter id for the counter.
438 std::string str_name(name);
439 int counter = table->FindCounter(str_name);
441 // Now we can find the location in the table.
442 return table->GetLocation(counter, slot);
445 void StatsTable::UnregisterThread() {
446 UnregisterThread(GetTLSData());
449 void StatsTable::UnregisterThread(TLSData* data) {
450 if (!data)
451 return;
452 DCHECK(impl_);
454 // Mark the slot free by zeroing out the thread name.
455 char* name = impl_->thread_name(data->slot);
456 *name = '\0';
458 // Remove the calling thread's TLS so that it cannot use the slot.
459 tls_index_.Set(NULL);
460 delete data;
463 void StatsTable::SlotReturnFunction(void* data) {
464 // This is called by the TLS destructor, which on some platforms has
465 // already cleared the TLS info, so use the tls_data argument
466 // rather than trying to fetch it ourselves.
467 TLSData* tls_data = static_cast<TLSData*>(data);
468 if (tls_data) {
469 DCHECK(tls_data->table);
470 tls_data->table->UnregisterThread(tls_data);
474 int StatsTable::FindEmptyThread() const {
475 // Note: the API returns slots numbered from 1..N, although
476 // internally, the array is 0..N-1. This is so that we can return
477 // zero as "not found".
479 // The reason for doing this is because the thread 'slot' is stored
480 // in TLS, which is always initialized to zero, not -1. If 0 were
481 // returned as a valid slot number, it would be confused with the
482 // uninitialized state.
483 if (!impl_)
484 return 0;
486 int index = 1;
487 for (; index <= impl_->max_threads(); index++) {
488 char* name = impl_->thread_name(index);
489 if (!*name)
490 break;
492 if (index > impl_->max_threads())
493 return 0; // The table is full.
494 return index;
497 int StatsTable::FindCounterOrEmptyRow(const std::string& name) const {
498 // Note: the API returns slots numbered from 1..N, although
499 // internally, the array is 0..N-1. This is so that we can return
500 // zero as "not found".
502 // There isn't much reason for this other than to be consistent
503 // with the way we track columns for thread slots. (See comments
504 // in FindEmptyThread for why it is done this way).
505 if (!impl_)
506 return 0;
508 int free_slot = 0;
509 for (int index = 1; index <= impl_->max_counters(); index++) {
510 char* row_name = impl_->counter_name(index);
511 if (!*row_name && !free_slot)
512 free_slot = index; // save that we found a free slot
513 else if (!strncmp(row_name, name.c_str(), kMaxCounterNameLength))
514 return index;
516 return free_slot;
519 int StatsTable::AddCounter(const std::string& name) {
520 if (!impl_)
521 return 0;
523 int counter_id = 0;
525 // To add a counter to the shared memory, we need the
526 // shared memory lock.
527 SharedMemoryAutoLock lock(impl_->shared_memory());
529 // We have space, so create a new counter.
530 counter_id = FindCounterOrEmptyRow(name);
531 if (!counter_id)
532 return 0;
534 std::string counter_name = name;
535 if (name.empty())
536 counter_name = kUnknownName;
537 strlcpy(impl_->counter_name(counter_id), counter_name.c_str(),
538 kMaxCounterNameLength);
541 // now add to our in-memory cache
543 AutoLock lock(counters_lock_);
544 counters_[name] = counter_id;
546 return counter_id;
549 StatsTable::TLSData* StatsTable::GetTLSData() const {
550 TLSData* data =
551 static_cast<TLSData*>(tls_index_.Get());
552 if (!data)
553 return NULL;
555 DCHECK(data->slot);
556 DCHECK_EQ(data->table, this);
557 return data;
560 } // namespace base