This source file includes following definitions.
- AlignOffset
- AlignedSize
- shared_memory
- table_header
- version
- size
- max_counters
- max_threads
- thread_name
- thread_tid
- thread_pid
- counter_name
- row
- data_table_
- New
- CreateSharedMemory
- InitializeTable
- ComputeMappedPointers
- tls_index_
- current
- set_current
- GetSlot
- RegisterThread
- CountThreadsRegistered
- FindCounter
- GetLocation
- GetRowName
- GetRowValue
- GetRowValue
- GetCounterValue
- GetCounterValue
- GetMaxCounters
- GetMaxThreads
- FindLocation
- UnregisterThread
- UnregisterThread
- SlotReturnFunction
- FindEmptyThread
- FindCounterOrEmptyRow
- AddCounter
- GetTLSData
- GetSharedMemoryHandle
#include "base/metrics/stats_table.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/shared_memory.h"
#include "base/process/process_handle.h"
#include "base/strings/string_piece.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/platform_thread.h"
#include "base/threading/thread_local_storage.h"
#if defined(OS_POSIX)
#include "base/posix/global_descriptors.h"
#include "errno.h"
#include "ipc/ipc_descriptors.h"
#endif
namespace base {
COMPILE_ASSERT(sizeof(int)==4, expect_4_byte_ints);
namespace {
const int kTableVersion = 0x13131313;
const char kUnknownName[] = "<unknown>";
inline int AlignOffset(int offset) {
return (sizeof(int) - (offset % sizeof(int))) % sizeof(int);
}
inline int AlignedSize(int size) {
return size + AlignOffset(size);
}
}
class StatsTable::Internal {
public:
struct TableHeader {
int version;
int size;
int max_counters;
int max_threads;
};
static Internal* New(const std::string& name,
int size,
int max_threads,
int max_counters);
SharedMemory* shared_memory() { return shared_memory_.get(); }
TableHeader* table_header() const { return table_header_; }
int version() const { return table_header_->version; }
int size() const { return table_header_->size; }
int max_counters() const { return table_header_->max_counters; }
int max_threads() const { return table_header_->max_threads; }
char* thread_name(int slot_id) const {
return &thread_names_table_[
(slot_id-1) * (StatsTable::kMaxThreadNameLength)];
}
PlatformThreadId* thread_tid(int slot_id) const {
return &(thread_tid_table_[slot_id-1]);
}
int* thread_pid(int slot_id) const {
return &(thread_pid_table_[slot_id-1]);
}
char* counter_name(int counter_id) const {
return &counter_names_table_[
(counter_id-1) * (StatsTable::kMaxCounterNameLength)];
}
int* row(int counter_id) const {
return &data_table_[(counter_id-1) * max_threads()];
}
private:
explicit Internal(SharedMemory* shared_memory)
: shared_memory_(shared_memory),
table_header_(NULL),
thread_names_table_(NULL),
thread_tid_table_(NULL),
thread_pid_table_(NULL),
counter_names_table_(NULL),
data_table_(NULL) {
}
static SharedMemory* CreateSharedMemory(const std::string& name,
int size);
void InitializeTable(void* memory, int size, int max_counters,
int max_threads);
void ComputeMappedPointers(void* memory);
scoped_ptr<SharedMemory> shared_memory_;
TableHeader* table_header_;
char* thread_names_table_;
PlatformThreadId* thread_tid_table_;
int* thread_pid_table_;
char* counter_names_table_;
int* data_table_;
DISALLOW_COPY_AND_ASSIGN(Internal);
};
StatsTable::Internal* StatsTable::Internal::New(const std::string& name,
int size,
int max_threads,
int max_counters) {
scoped_ptr<SharedMemory> shared_memory(CreateSharedMemory(name, size));
if (!shared_memory.get())
return NULL;
if (!shared_memory->Map(size))
return NULL;
void* memory = shared_memory->memory();
scoped_ptr<Internal> internal(new Internal(shared_memory.release()));
TableHeader* header = static_cast<TableHeader*>(memory);
if (header->version != kTableVersion)
internal->InitializeTable(memory, size, max_counters, max_threads);
internal->ComputeMappedPointers(memory);
return internal.release();
}
SharedMemory* StatsTable::Internal::CreateSharedMemory(const std::string& name,
int size) {
#if defined(OS_POSIX)
GlobalDescriptors* global_descriptors = GlobalDescriptors::GetInstance();
if (global_descriptors->MaybeGet(kStatsTableSharedMemFd) != -1) {
FileDescriptor file_descriptor(
global_descriptors->Get(kStatsTableSharedMemFd), false);
return new SharedMemory(file_descriptor, false);
}
scoped_ptr<SharedMemory> shared_memory(new SharedMemory());
if (!shared_memory->CreateAnonymous(size))
return NULL;
return shared_memory.release();
#elif defined(OS_WIN)
scoped_ptr<SharedMemory> shared_memory(new SharedMemory());
if (!shared_memory->CreateNamedDeprecated(name, true, size))
return NULL;
return shared_memory.release();
#endif
}
void StatsTable::Internal::InitializeTable(void* memory, int size,
int max_counters,
int max_threads) {
memset(memory, 0, size);
TableHeader* header = static_cast<TableHeader*>(memory);
header->version = kTableVersion;
header->size = size;
header->max_counters = max_counters;
header->max_threads = max_threads;
}
void StatsTable::Internal::ComputeMappedPointers(void* memory) {
char* data = static_cast<char*>(memory);
int offset = 0;
table_header_ = reinterpret_cast<TableHeader*>(data);
offset += sizeof(*table_header_);
offset += AlignOffset(offset);
DCHECK_EQ(table_header_->version, kTableVersion);
thread_names_table_ = reinterpret_cast<char*>(data + offset);
offset += sizeof(char) *
max_threads() * StatsTable::kMaxThreadNameLength;
offset += AlignOffset(offset);
thread_tid_table_ = reinterpret_cast<PlatformThreadId*>(data + offset);
offset += sizeof(int) * max_threads();
offset += AlignOffset(offset);
thread_pid_table_ = reinterpret_cast<int*>(data + offset);
offset += sizeof(int) * max_threads();
offset += AlignOffset(offset);
counter_names_table_ = reinterpret_cast<char*>(data + offset);
offset += sizeof(char) *
max_counters() * StatsTable::kMaxCounterNameLength;
offset += AlignOffset(offset);
data_table_ = reinterpret_cast<int*>(data + offset);
offset += sizeof(int) * max_threads() * max_counters();
DCHECK_EQ(offset, size());
}
struct StatsTable::TLSData {
StatsTable* table;
int slot;
};
StatsTable* global_table = NULL;
StatsTable::StatsTable(const std::string& name, int max_threads,
int max_counters)
: internal_(NULL),
tls_index_(SlotReturnFunction) {
int table_size =
AlignedSize(sizeof(Internal::TableHeader)) +
AlignedSize((max_counters * sizeof(char) * kMaxCounterNameLength)) +
AlignedSize((max_threads * sizeof(char) * kMaxThreadNameLength)) +
AlignedSize(max_threads * sizeof(int)) +
AlignedSize(max_threads * sizeof(int)) +
AlignedSize((sizeof(int) * (max_counters * max_threads)));
internal_ = Internal::New(name, table_size, max_threads, max_counters);
if (!internal_)
DPLOG(ERROR) << "StatsTable did not initialize";
}
StatsTable::~StatsTable() {
UnregisterThread();
tls_index_.Free();
delete internal_;
if (global_table == this)
global_table = NULL;
}
StatsTable* StatsTable::current() {
return global_table;
}
void StatsTable::set_current(StatsTable* value) {
global_table = value;
}
int StatsTable::GetSlot() const {
TLSData* data = GetTLSData();
if (!data)
return 0;
return data->slot;
}
int StatsTable::RegisterThread(const std::string& name) {
int slot = 0;
if (!internal_)
return 0;
{
SharedMemoryAutoLockDeprecated lock(internal_->shared_memory());
slot = FindEmptyThread();
if (!slot) {
return 0;
}
std::string thread_name = name;
if (name.empty())
thread_name = kUnknownName;
strlcpy(internal_->thread_name(slot), thread_name.c_str(),
kMaxThreadNameLength);
*(internal_->thread_tid(slot)) = PlatformThread::CurrentId();
*(internal_->thread_pid(slot)) = GetCurrentProcId();
}
TLSData* data = new TLSData;
data->table = this;
data->slot = slot;
tls_index_.Set(data);
return slot;
}
int StatsTable::CountThreadsRegistered() const {
if (!internal_)
return 0;
int count = 0;
for (int index = 1; index <= internal_->max_threads(); index++) {
char* name = internal_->thread_name(index);
if (*name != '\0')
count++;
}
return count;
}
int StatsTable::FindCounter(const std::string& name) {
if (!internal_)
return 0;
{
AutoLock scoped_lock(counters_lock_);
CountersMap::const_iterator iter;
iter = counters_.find(name);
if (iter != counters_.end())
return iter->second;
}
return AddCounter(name);
}
int* StatsTable::GetLocation(int counter_id, int slot_id) const {
if (!internal_)
return NULL;
if (slot_id > internal_->max_threads())
return NULL;
int* row = internal_->row(counter_id);
return &(row[slot_id-1]);
}
const char* StatsTable::GetRowName(int index) const {
if (!internal_)
return NULL;
return internal_->counter_name(index);
}
int StatsTable::GetRowValue(int index) const {
return GetRowValue(index, 0);
}
int StatsTable::GetRowValue(int index, int pid) const {
if (!internal_)
return 0;
int rv = 0;
int* row = internal_->row(index);
for (int slot_id = 1; slot_id <= internal_->max_threads(); slot_id++) {
if (pid == 0 || *internal_->thread_pid(slot_id) == pid)
rv += row[slot_id-1];
}
return rv;
}
int StatsTable::GetCounterValue(const std::string& name) {
return GetCounterValue(name, 0);
}
int StatsTable::GetCounterValue(const std::string& name, int pid) {
if (!internal_)
return 0;
int row = FindCounter(name);
if (!row)
return 0;
return GetRowValue(row, pid);
}
int StatsTable::GetMaxCounters() const {
if (!internal_)
return 0;
return internal_->max_counters();
}
int StatsTable::GetMaxThreads() const {
if (!internal_)
return 0;
return internal_->max_threads();
}
int* StatsTable::FindLocation(const char* name) {
StatsTable *table = StatsTable::current();
if (!table)
return NULL;
int slot = table->GetSlot();
if (!slot && !(slot = table->RegisterThread(std::string())))
return NULL;
std::string str_name(name);
int counter = table->FindCounter(str_name);
return table->GetLocation(counter, slot);
}
void StatsTable::UnregisterThread() {
UnregisterThread(GetTLSData());
}
void StatsTable::UnregisterThread(TLSData* data) {
if (!data)
return;
DCHECK(internal_);
char* name = internal_->thread_name(data->slot);
*name = '\0';
tls_index_.Set(NULL);
delete data;
}
void StatsTable::SlotReturnFunction(void* data) {
TLSData* tls_data = static_cast<TLSData*>(data);
if (tls_data) {
DCHECK(tls_data->table);
tls_data->table->UnregisterThread(tls_data);
}
}
int StatsTable::FindEmptyThread() const {
if (!internal_)
return 0;
int index = 1;
for (; index <= internal_->max_threads(); index++) {
char* name = internal_->thread_name(index);
if (!*name)
break;
}
if (index > internal_->max_threads())
return 0;
return index;
}
int StatsTable::FindCounterOrEmptyRow(const std::string& name) const {
if (!internal_)
return 0;
int free_slot = 0;
for (int index = 1; index <= internal_->max_counters(); index++) {
char* row_name = internal_->counter_name(index);
if (!*row_name && !free_slot)
free_slot = index;
else if (!strncmp(row_name, name.c_str(), kMaxCounterNameLength))
return index;
}
return free_slot;
}
int StatsTable::AddCounter(const std::string& name) {
if (!internal_)
return 0;
int counter_id = 0;
{
SharedMemoryAutoLockDeprecated lock(internal_->shared_memory());
counter_id = FindCounterOrEmptyRow(name);
if (!counter_id)
return 0;
std::string counter_name = name;
if (name.empty())
counter_name = kUnknownName;
strlcpy(internal_->counter_name(counter_id), counter_name.c_str(),
kMaxCounterNameLength);
}
{
AutoLock lock(counters_lock_);
counters_[name] = counter_id;
}
return counter_id;
}
StatsTable::TLSData* StatsTable::GetTLSData() const {
TLSData* data =
static_cast<TLSData*>(tls_index_.Get());
if (!data)
return NULL;
DCHECK(data->slot);
DCHECK_EQ(data->table, this);
return data;
}
#if defined(OS_POSIX)
SharedMemoryHandle StatsTable::GetSharedMemoryHandle() const {
if (!internal_)
return SharedMemory::NULLHandle();
return internal_->shared_memory()->handle();
}
#endif
}