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 { ...@@ -34,19 +34,9 @@ namespace v8 { namespace internal {
CounterLookupCallback StatsTable::lookup_function_ = NULL; 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. // Start the timer.
void StatsCounterTimer::Start() { void StatsCounterTimer::Start() {
if (!Enabled()) if (!counter_.Enabled())
return; return;
stop_time_ = 0; stop_time_ = 0;
start_time_ = OS::Ticks(); start_time_ = OS::Ticks();
...@@ -54,10 +44,13 @@ void StatsCounterTimer::Start() { ...@@ -54,10 +44,13 @@ void StatsCounterTimer::Start() {
// Stop the timer and record the results. // Stop the timer and record the results.
void StatsCounterTimer::Stop() { void StatsCounterTimer::Stop() {
if (!Enabled()) if (!counter_.Enabled())
return; return;
stop_time_ = OS::Ticks(); 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 } } // namespace v8::internal
...@@ -67,38 +67,18 @@ class StatsTable : public AllStatic { ...@@ -67,38 +67,18 @@ class StatsTable : public AllStatic {
// the StatsTable. They are designed to be lightweight to create and // the StatsTable. They are designed to be lightweight to create and
// easy to use. // 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. // 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 // 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 // a name (stored in the table metadata). Since the storage location can be
// thread-specific, this class cannot be shared across threads. // thread-specific, this class cannot be shared across threads.
// //
// This class is designed to be POD initialized. It will be registered with
// StatsCounter represents a counter in the StatsTable class. // the counter system on first use. For example:
class StatsCounter BASE_EMBEDDED { // StatsCounter c = { L"c:myctr", NULL, false };
public: struct StatsCounter {
// Create a StatsCounter object. const wchar_t* name_;
explicit StatsCounter(const wchar_t* name, int id) : int* ptr_;
lookup_done_(false), bool lookup_done_;
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();
}
// Sets the counter to a specific value. // Sets the counter to a specific value.
void Set(int value) { void Set(int value) {
...@@ -144,38 +124,23 @@ class StatsCounter BASE_EMBEDDED { ...@@ -144,38 +124,23 @@ class StatsCounter BASE_EMBEDDED {
return loc; return loc;
} }
int Id() {
return id_;
}
protected: protected:
StatsCounter() :
lookup_done_(false),
ptr_(NULL) {
}
// Returns the cached address of this counter location. // Returns the cached address of this counter location.
int* GetPtr() { int* GetPtr() {
if (lookup_done_) if (lookup_done_)
return ptr_; return ptr_;
lookup_done_ = true; lookup_done_ = true;
ptr_ = StatsTable::FindLocation(name_.start()); ptr_ = StatsTable::FindLocation(name_);
return ptr_; return ptr_;
} }
Vector<wchar_t> name_;
bool lookup_done_;
int* ptr_;
int id_;
}; };
// A StatsCounterTimer is a StatsCounter which keeps a timer during // StatsCounterTimer t = { { L"t:foo", NULL, false }, 0, 0 };
// the scope of the StatsCounterTimer. On destruction, it will record struct StatsCounterTimer {
// its time measurement. StatsCounter counter_;
class StatsCounterTimer : StatsCounter {
public: int64_t start_time_;
// Constructs and starts the timer. int64_t stop_time_;
explicit StatsCounterTimer(const wchar_t* name);
// Start the timer. // Start the timer.
void Start(); void Start();
...@@ -185,31 +150,20 @@ class StatsCounterTimer : StatsCounter { ...@@ -185,31 +150,20 @@ class StatsCounterTimer : StatsCounter {
// Returns true if the timer is running. // Returns true if the timer is running.
bool 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 // A StatsRate is a combination of both a timer and a counter so that
// several statistics can be produced: // several statistics can be produced:
// min, max, avg, count, total // min, max, avg, count, total
class StatsRate BASE_EMBEDDED { //
public: // For example:
// Constructs and starts the timer. // StatsCounter c = { { { L"t:myrate", NULL, false }, 0, 0 },
explicit StatsRate(const wchar_t* name, int id) : // { L"c:myrate", NULL, false } };
timer_(name), struct StatsRate {
counter_(name, id) { StatsCounterTimer timer_;
} StatsCounter counter_;
// Starts the rate timer. // Starts the rate timer.
void Start() { void Start() {
...@@ -223,30 +177,9 @@ class StatsRate BASE_EMBEDDED { ...@@ -223,30 +177,9 @@ class StatsRate BASE_EMBEDDED {
counter_.Increment(); 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. // Helper class for scoping a rate.
class StatsRateScope BASE_EMBEDDED { class StatsRateScope BASE_EMBEDDED {
public: public:
......
...@@ -118,7 +118,8 @@ struct Flag { ...@@ -118,7 +118,8 @@ struct Flag {
if (str1 == NULL) return str2 == NULL; if (str1 == NULL) return str2 == NULL;
return strcmp(str1, str2) == 0; return strcmp(str1, str2) == 0;
} }
return true; // NOTREACHED UNREACHABLE();
return true;
} }
// Set a flag back to it's default value. // Set a flag back to it's default value.
......
...@@ -31,17 +31,24 @@ ...@@ -31,17 +31,24 @@
namespace v8 { namespace internal { 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) STATS_RATE_LIST(SR)
#undef 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_1(SC)
STATS_COUNTER_LIST_2(SC) STATS_COUNTER_LIST_2(SC)
#undef SC #undef SC
StatsCounter Counters::state_counters[state_tag_count] = { StatsCounter Counters::state_counters[] = {
#define COUNTER_NAME(name) StatsCounter(L"V8.State" L###name, k_##name), #define COUNTER_NAME(name) \
{ L"c:V8.State" L###name, NULL, false },
STATE_TAG_LIST(COUNTER_NAME) STATE_TAG_LIST(COUNTER_NAME)
#undef COUNTER_NAME #undef COUNTER_NAME
}; };
......
...@@ -122,11 +122,13 @@ namespace v8 { namespace internal { ...@@ -122,11 +122,13 @@ namespace v8 { namespace internal {
// This file contains all the v8 counters that are in use. // This file contains all the v8 counters that are in use.
class Counters : AllStatic { class Counters : AllStatic {
public: public:
#define SR(name, caption) static StatsRate name; #define SR(name, caption) \
static StatsRate name;
STATS_RATE_LIST(SR) STATS_RATE_LIST(SR)
#undef 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_1(SC)
STATS_COUNTER_LIST_2(SC) STATS_COUNTER_LIST_2(SC)
#undef 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