2012-07-13 12:12:09 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2008-07-03 15:10:15 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#ifndef V8_COUNTERS_H_
|
|
|
|
#define V8_COUNTERS_H_
|
|
|
|
|
2010-12-17 08:58:04 +00:00
|
|
|
#include "../include/v8.h"
|
|
|
|
#include "allocation.h"
|
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// StatsCounters is an interface for plugging into external
|
|
|
|
// counters for monitoring. Counters can be looked up and
|
|
|
|
// manipulated by name.
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
class StatsTable {
|
2008-07-03 15:10:15 +00:00
|
|
|
public:
|
|
|
|
// Register an application-defined function where
|
|
|
|
// counters can be looked up.
|
2011-03-18 20:35:07 +00:00
|
|
|
void SetCounterFunction(CounterLookupCallback f) {
|
2008-07-03 15:10:15 +00:00
|
|
|
lookup_function_ = f;
|
|
|
|
}
|
|
|
|
|
2009-03-13 16:06:31 +00:00
|
|
|
// Register an application-defined function to create
|
|
|
|
// a histogram for passing to the AddHistogramSample function
|
2011-03-18 20:35:07 +00:00
|
|
|
void SetCreateHistogramFunction(CreateHistogramCallback f) {
|
2009-03-13 16:06:31 +00:00
|
|
|
create_histogram_function_ = f;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register an application-defined function to add a sample
|
|
|
|
// to a histogram created with CreateHistogram function
|
2011-03-18 20:35:07 +00:00
|
|
|
void SetAddHistogramSampleFunction(AddHistogramSampleCallback f) {
|
2009-03-13 16:06:31 +00:00
|
|
|
add_histogram_sample_function_ = f;
|
|
|
|
}
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
bool HasCounterFunction() const {
|
2008-07-03 15:10:15 +00:00
|
|
|
return lookup_function_ != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup the location of a counter by name. If the lookup
|
|
|
|
// is successful, returns a non-NULL pointer for writing the
|
|
|
|
// value of the counter. Each thread calling this function
|
|
|
|
// may receive a different location to store it's counter.
|
|
|
|
// The return value must not be cached and re-used across
|
|
|
|
// threads, although a single thread is free to cache it.
|
2011-03-18 20:35:07 +00:00
|
|
|
int* FindLocation(const char* name) {
|
2008-07-03 15:10:15 +00:00
|
|
|
if (!lookup_function_) return NULL;
|
|
|
|
return lookup_function_(name);
|
|
|
|
}
|
|
|
|
|
2009-03-13 16:06:31 +00:00
|
|
|
// Create a histogram by name. If the create is successful,
|
|
|
|
// returns a non-NULL pointer for use with AddHistogramSample
|
|
|
|
// function. min and max define the expected minimum and maximum
|
|
|
|
// sample values. buckets is the maximum number of buckets
|
|
|
|
// that the samples will be grouped into.
|
2011-03-18 20:35:07 +00:00
|
|
|
void* CreateHistogram(const char* name,
|
|
|
|
int min,
|
|
|
|
int max,
|
|
|
|
size_t buckets) {
|
2009-03-13 16:06:31 +00:00
|
|
|
if (!create_histogram_function_) return NULL;
|
|
|
|
return create_histogram_function_(name, min, max, buckets);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a sample to a histogram created with the CreateHistogram
|
|
|
|
// function.
|
2011-03-18 20:35:07 +00:00
|
|
|
void AddHistogramSample(void* histogram, int sample) {
|
2009-03-13 16:06:31 +00:00
|
|
|
if (!add_histogram_sample_function_) return;
|
|
|
|
return add_histogram_sample_function_(histogram, sample);
|
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
private:
|
2011-03-18 20:35:07 +00:00
|
|
|
StatsTable();
|
|
|
|
|
|
|
|
CounterLookupCallback lookup_function_;
|
|
|
|
CreateHistogramCallback create_histogram_function_;
|
|
|
|
AddHistogramSampleCallback add_histogram_sample_function_;
|
|
|
|
|
|
|
|
friend class Isolate;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(StatsTable);
|
2008-07-03 15:10:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// StatsCounters are dynamically created values which can be tracked in
|
|
|
|
// the StatsTable. They are designed to be lightweight to create and
|
|
|
|
// easy to use.
|
|
|
|
//
|
|
|
|
// 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.
|
2013-04-24 13:52:26 +00:00
|
|
|
class StatsCounter {
|
|
|
|
public:
|
|
|
|
StatsCounter() { }
|
2013-09-03 06:57:16 +00:00
|
|
|
explicit StatsCounter(Isolate* isolate, const char* name)
|
|
|
|
: isolate_(isolate), name_(name), ptr_(NULL), lookup_done_(false) { }
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Sets the counter to a specific value.
|
|
|
|
void Set(int value) {
|
|
|
|
int* loc = GetPtr();
|
|
|
|
if (loc) *loc = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Increments the counter.
|
|
|
|
void Increment() {
|
|
|
|
int* loc = GetPtr();
|
|
|
|
if (loc) (*loc)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Increment(int value) {
|
|
|
|
int* loc = GetPtr();
|
|
|
|
if (loc)
|
|
|
|
(*loc) += value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decrements the counter.
|
|
|
|
void Decrement() {
|
|
|
|
int* loc = GetPtr();
|
|
|
|
if (loc) (*loc)--;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Decrement(int value) {
|
|
|
|
int* loc = GetPtr();
|
|
|
|
if (loc) (*loc) -= value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is this counter enabled?
|
|
|
|
// Returns false if table is full.
|
|
|
|
bool Enabled() {
|
|
|
|
return GetPtr() != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the internal pointer to the counter. This is used
|
|
|
|
// by the code generator to emit code that manipulates a
|
|
|
|
// given counter without calling the runtime system.
|
|
|
|
int* GetInternalPointer() {
|
|
|
|
int* loc = GetPtr();
|
|
|
|
ASSERT(loc != NULL);
|
|
|
|
return loc;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// Returns the cached address of this counter location.
|
|
|
|
int* GetPtr() {
|
2012-07-13 12:12:09 +00:00
|
|
|
if (lookup_done_) return ptr_;
|
2008-07-03 15:10:15 +00:00
|
|
|
lookup_done_ = true;
|
2011-03-18 20:35:07 +00:00
|
|
|
ptr_ = FindLocationInStatsTable();
|
2008-07-03 15:10:15 +00:00
|
|
|
return ptr_;
|
|
|
|
}
|
2011-03-18 20:35:07 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
int* FindLocationInStatsTable() const;
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2013-09-03 06:57:16 +00:00
|
|
|
Isolate* isolate_;
|
2013-04-24 13:52:26 +00:00
|
|
|
const char* name_;
|
|
|
|
int* ptr_;
|
|
|
|
bool lookup_done_;
|
2008-07-03 15:10:15 +00:00
|
|
|
};
|
|
|
|
|
2012-07-13 12:12:09 +00:00
|
|
|
// A Histogram represents a dynamically created histogram in the StatsTable.
|
2013-04-24 13:52:26 +00:00
|
|
|
// It will be registered with the histogram system on first use.
|
|
|
|
class Histogram {
|
|
|
|
public:
|
|
|
|
Histogram() { }
|
|
|
|
Histogram(const char* name,
|
|
|
|
int min,
|
|
|
|
int max,
|
|
|
|
int num_buckets,
|
|
|
|
Isolate* isolate)
|
|
|
|
: name_(name),
|
|
|
|
min_(min),
|
|
|
|
max_(max),
|
|
|
|
num_buckets_(num_buckets),
|
|
|
|
histogram_(NULL),
|
|
|
|
lookup_done_(false),
|
|
|
|
isolate_(isolate) { }
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2012-07-13 12:12:09 +00:00
|
|
|
// Add a single sample to this histogram.
|
|
|
|
void AddSample(int sample);
|
2009-03-13 16:06:31 +00:00
|
|
|
|
2012-07-13 12:12:09 +00:00
|
|
|
// Returns true if this histogram is enabled.
|
|
|
|
bool Enabled() {
|
|
|
|
return GetHistogram() != NULL;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2012-08-01 12:51:24 +00:00
|
|
|
// Reset the cached internal pointer.
|
|
|
|
void Reset() {
|
|
|
|
lookup_done_ = false;
|
|
|
|
}
|
|
|
|
|
2009-03-13 16:06:31 +00:00
|
|
|
protected:
|
|
|
|
// Returns the handle to the histogram.
|
|
|
|
void* GetHistogram() {
|
|
|
|
if (!lookup_done_) {
|
|
|
|
lookup_done_ = true;
|
2011-03-18 20:35:07 +00:00
|
|
|
histogram_ = CreateHistogram();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2009-03-13 16:06:31 +00:00
|
|
|
return histogram_;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2011-03-18 20:35:07 +00:00
|
|
|
|
2013-04-24 13:52:26 +00:00
|
|
|
const char* name() { return name_; }
|
|
|
|
Isolate* isolate() const { return isolate_; }
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
private:
|
|
|
|
void* CreateHistogram() const;
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2013-04-24 13:52:26 +00:00
|
|
|
const char* name_;
|
|
|
|
int min_;
|
|
|
|
int max_;
|
|
|
|
int num_buckets_;
|
|
|
|
void* histogram_;
|
|
|
|
bool lookup_done_;
|
|
|
|
Isolate* isolate_;
|
|
|
|
};
|
2012-07-13 12:12:09 +00:00
|
|
|
|
2013-04-24 13:52:26 +00:00
|
|
|
// A HistogramTimer allows distributions of results to be created.
|
|
|
|
class HistogramTimer : public Histogram {
|
|
|
|
public:
|
|
|
|
HistogramTimer() { }
|
|
|
|
HistogramTimer(const char* name,
|
|
|
|
int min,
|
|
|
|
int max,
|
|
|
|
int num_buckets,
|
|
|
|
Isolate* isolate)
|
2013-08-29 09:15:13 +00:00
|
|
|
: Histogram(name, min, max, num_buckets, isolate) {}
|
2012-07-13 12:12:09 +00:00
|
|
|
|
|
|
|
// Start the timer.
|
|
|
|
void Start();
|
|
|
|
|
|
|
|
// Stop the timer and record the results.
|
|
|
|
void Stop();
|
|
|
|
|
|
|
|
// Returns true if the timer is running.
|
|
|
|
bool Running() {
|
2013-08-29 09:15:13 +00:00
|
|
|
return Enabled() && timer_.IsStarted();
|
2012-07-13 12:12:09 +00:00
|
|
|
}
|
2012-08-01 12:51:24 +00:00
|
|
|
|
2013-04-24 13:52:26 +00:00
|
|
|
private:
|
2013-08-29 09:15:13 +00:00
|
|
|
ElapsedTimer timer_;
|
2012-07-13 12:12:09 +00:00
|
|
|
};
|
|
|
|
|
2009-03-13 16:06:31 +00:00
|
|
|
// Helper class for scoping a HistogramTimer.
|
|
|
|
class HistogramTimerScope BASE_EMBEDDED {
|
2008-07-03 15:10:15 +00:00
|
|
|
public:
|
2009-03-13 16:06:31 +00:00
|
|
|
explicit HistogramTimerScope(HistogramTimer* timer) :
|
|
|
|
timer_(timer) {
|
|
|
|
timer_->Start();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2009-03-13 16:06:31 +00:00
|
|
|
~HistogramTimerScope() {
|
|
|
|
timer_->Stop();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
private:
|
2009-03-13 16:06:31 +00:00
|
|
|
HistogramTimer* timer_;
|
2008-07-03 15:10:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} } // namespace v8::internal
|
|
|
|
|
|
|
|
#endif // V8_COUNTERS_H_
|