Commit 1a0ccbd9 authored by deanm@chromium.org's avatar deanm@chromium.org

Move the Counters to structures that can be POD initialized, avoiding the need...

Move the Counters to structures that can be POD initialized, avoiding the need for static constructors on program startup.  They were only default initializing and doing some unneeded string operations.

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@301 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 27e8ffe3
......@@ -34,19 +34,9 @@ namespace v8 { namespace internal {
CounterLookupCallback StatsTable::lookup_function_ = NULL;
StatsCounterTimer::StatsCounterTimer(const wchar_t* name)
: start_time_(0), // initialize to avoid compiler complaints
stop_time_(0) { // initialize to avoid compiler complaints
int len = wcslen(name);
// we prepend the name with 'c.' to indicate that it is a counter.
name_ = Vector<wchar_t>::New(len+3);
OS::WcsCpy(name_, L"t:");
OS::WcsCpy(name_ + 2, name);
}
// Start the timer.
void StatsCounterTimer::Start() {
if (!Enabled())
if (!counter_.Enabled())
return;
stop_time_ = 0;
start_time_ = OS::Ticks();
......@@ -54,10 +44,13 @@ void StatsCounterTimer::Start() {
// Stop the timer and record the results.
void StatsCounterTimer::Stop() {
if (!Enabled())
if (!counter_.Enabled())
return;
stop_time_ = OS::Ticks();
Record();
// Compute the delta between start and stop, in milliseconds.
int milliseconds = static_cast<int>(stop_time_ - start_time_) / 1000;
counter_.Increment(milliseconds);
}
} } // namespace v8::internal
......@@ -67,38 +67,18 @@ class StatsTable : public AllStatic {
// the StatsTable. They are designed to be lightweight to create and
// easy to use.
//
// The implementation of the StatsTable is external to this module.
//
// Example usage:
// {
// StatsCounter request_count("RequestCount");
// request_count.Increment();
// }
//
// Internally, a counter represents a value in a row of a StatsTable.
// The row has a 32bit value for each process/thread in the table and also
// a name (stored in the table metadata). Since the storage location can be
// thread-specific, this class cannot be shared across threads.
//
// StatsCounter represents a counter in the StatsTable class.
class StatsCounter BASE_EMBEDDED {
public:
// Create a StatsCounter object.
explicit StatsCounter(const wchar_t* name, int id) :
lookup_done_(false),
ptr_(NULL),
id_(id) {
int len = wcslen(name);
// we prepend the name with 'c:' to indicate that it is a counter.
name_ = Vector<wchar_t>::New(len+3);
OS::WcsCpy(name_, L"c:");
OS::WcsCpy(name_ + 2, name);
};
~StatsCounter() {
name_.Dispose();
}
// This class is designed to be POD initialized. It will be registered with
// the counter system on first use. For example:
// StatsCounter c = { L"c:myctr", NULL, false };
struct StatsCounter {
const wchar_t* name_;
int* ptr_;
bool lookup_done_;
// Sets the counter to a specific value.
void Set(int value) {
......@@ -144,38 +124,23 @@ class StatsCounter BASE_EMBEDDED {
return loc;
}
int Id() {
return id_;
}
protected:
StatsCounter() :
lookup_done_(false),
ptr_(NULL) {
}
// Returns the cached address of this counter location.
int* GetPtr() {
if (lookup_done_)
return ptr_;
lookup_done_ = true;
ptr_ = StatsTable::FindLocation(name_.start());
ptr_ = StatsTable::FindLocation(name_);
return ptr_;
}
Vector<wchar_t> name_;
bool lookup_done_;
int* ptr_;
int id_;
};
// A StatsCounterTimer is a StatsCounter which keeps a timer during
// the scope of the StatsCounterTimer. On destruction, it will record
// its time measurement.
class StatsCounterTimer : StatsCounter {
public:
// Constructs and starts the timer.
explicit StatsCounterTimer(const wchar_t* name);
// StatsCounterTimer t = { { L"t:foo", NULL, false }, 0, 0 };
struct StatsCounterTimer {
StatsCounter counter_;
int64_t start_time_;
int64_t stop_time_;
// Start the timer.
void Start();
......@@ -185,31 +150,20 @@ class StatsCounterTimer : StatsCounter {
// Returns true if the timer is running.
bool Running() {
return Enabled() && start_time_ != 0 && stop_time_ == 0;
return counter_.Enabled() && start_time_ != 0 && stop_time_ == 0;
}
private:
// Compute the delta between start and stop, in milliseconds.
void Record() {
int milliseconds = static_cast<int>(stop_time_ - start_time_) / 1000;
Increment(milliseconds);
}
int64_t start_time_;
int64_t stop_time_;
};
// A StatsRate is a combination of both a timer and a counter so that
// several statistics can be produced:
// min, max, avg, count, total
class StatsRate BASE_EMBEDDED {
public:
// Constructs and starts the timer.
explicit StatsRate(const wchar_t* name, int id) :
timer_(name),
counter_(name, id) {
}
//
// For example:
// StatsCounter c = { { { L"t:myrate", NULL, false }, 0, 0 },
// { L"c:myrate", NULL, false } };
struct StatsRate {
StatsCounterTimer timer_;
StatsCounter counter_;
// Starts the rate timer.
void Start() {
......@@ -223,30 +177,9 @@ class StatsRate BASE_EMBEDDED {
counter_.Increment();
}
}
// Access to the timer.
StatsCounterTimer& timer() { return timer_; }
private:
StatsCounterTimer timer_;
StatsCounter counter_;
};
// Helper class for scoping a timer.
class StatsTimerScope BASE_EMBEDDED {
public:
explicit StatsTimerScope(StatsCounterTimer* timer) :
timer_(timer) {
timer_->Start();
}
~StatsTimerScope() {
timer_->Stop();
}
private:
StatsCounterTimer* timer_;
};
// Helper class for scoping a rate.
class StatsRateScope BASE_EMBEDDED {
public:
......
......@@ -118,7 +118,8 @@ struct Flag {
if (str1 == NULL) return str2 == NULL;
return strcmp(str1, str2) == 0;
}
return true; // NOTREACHED
UNREACHABLE();
return true;
}
// Set a flag back to it's default value.
......
......@@ -31,17 +31,24 @@
namespace v8 { namespace internal {
#define SR(name, caption) StatsRate Counters::name(L###caption, k_##name);
#define SR(name, caption) \
StatsRate Counters::name = { \
{ { L"t:" L###caption, NULL, false }, 0, 0 }, \
{ L"c:" L###caption, NULL, false } };
STATS_RATE_LIST(SR)
#undef SR
#define SC(name, caption) StatsCounter Counters::name(L###caption, k_##name);
#define SC(name, caption) \
StatsCounter Counters::name = { L"c:" L###caption, NULL, false };
STATS_COUNTER_LIST_1(SC)
STATS_COUNTER_LIST_2(SC)
#undef SC
StatsCounter Counters::state_counters[state_tag_count] = {
#define COUNTER_NAME(name) StatsCounter(L"V8.State" L###name, k_##name),
StatsCounter Counters::state_counters[] = {
#define COUNTER_NAME(name) \
{ L"c:V8.State" L###name, NULL, false },
STATE_TAG_LIST(COUNTER_NAME)
#undef COUNTER_NAME
};
......
......@@ -122,11 +122,13 @@ namespace v8 { namespace internal {
// This file contains all the v8 counters that are in use.
class Counters : AllStatic {
public:
#define SR(name, caption) static StatsRate name;
#define SR(name, caption) \
static StatsRate name;
STATS_RATE_LIST(SR)
#undef SR
#define SC(name, caption) static StatsCounter name;
#define SC(name, caption) \
static StatsCounter name;
STATS_COUNTER_LIST_1(SC)
STATS_COUNTER_LIST_2(SC)
#undef SC
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment