Author: [email protected]
Date: Fri Mar 13 09:06:31 2009
New Revision: 1510

Modified:
    branches/bleeding_edge/include/v8.h
    branches/bleeding_edge/src/api.cc
    branches/bleeding_edge/src/compiler.cc
    branches/bleeding_edge/src/counters.cc
    branches/bleeding_edge/src/counters.h
    branches/bleeding_edge/src/heap.cc
    branches/bleeding_edge/src/parser.cc
    branches/bleeding_edge/src/v8-counters.cc
    branches/bleeding_edge/src/v8-counters.h

Log:
- Added ability to call histograms from within v8
- Changed the StatsRates to use the new HistogramTimers

Review URL: http://codereview.chromium.org/42020

Modified: branches/bleeding_edge/include/v8.h
==============================================================================
--- branches/bleeding_edge/include/v8.h (original)
+++ branches/bleeding_edge/include/v8.h Fri Mar 13 09:06:31 2009
@@ -839,7 +839,7 @@
     * external string resource.
     */
    static Local<String> NewExternal(ExternalStringResource* resource);
-
+
    /**
     * Associate an external string resource with this string by  
transforming it
     * in place so that existing references to this string in the JavaScript  
heap
@@ -859,7 +859,7 @@
     * external string resource.
     */
    static Local<String> NewExternal(ExternalAsciiStringResource* resource);
-
+
    /**
     * Associate an external string resource with this string by  
transforming it
     * in place so that existing references to this string in the JavaScript  
heap
@@ -1816,6 +1816,13 @@

  typedef int* (*CounterLookupCallback)(const char* name);

+typedef void* (*CreateHistogramCallback)(const char* name,
+                                         int min,
+                                         int max,
+                                         size_t buckets);
+
+typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
+
  // --- F a i l e d A c c e s s C h e c k C a l l b a c k ---
  typedef void (*FailedAccessCheckCallback)(Local<Object> target,
                                            AccessType type,
@@ -1904,6 +1911,15 @@
     * statistics counters.
     */
    static void SetCounterFunction(CounterLookupCallback);
+
+  /**
+   * Enables the host application to provide a mechanism for recording
+   * histograms. The CreateHistogram function returns a
+   * histogram which will later be passed to the AddHistogramSample
+   * function.
+   */
+  static void SetCreateHistogramFunction(CreateHistogramCallback);
+  static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);

    /**
     * Enables the computation of a sliding window of states. The sliding

Modified: branches/bleeding_edge/src/api.cc
==============================================================================
--- branches/bleeding_edge/src/api.cc   (original)
+++ branches/bleeding_edge/src/api.cc   Fri Mar 13 09:06:31 2009
@@ -2700,6 +2700,15 @@
    i::StatsTable::SetCounterFunction(callback);
  }

+void V8::SetCreateHistogramFunction(CreateHistogramCallback callback) {
+  if (IsDeadCheck("v8::V8::SetCreateHistogramFunction()")) return;
+  i::StatsTable::SetCreateHistogramFunction(callback);
+}
+
+void V8::SetAddHistogramSampleFunction(AddHistogramSampleCallback  
callback) {
+  if (IsDeadCheck("v8::V8::SetAddHistogramSampleFunction()")) return;
+  i::StatsTable::SetAddHistogramSampleFunction(callback);
+}

  void V8::EnableSlidingStateWindow() {
    if (IsDeadCheck("v8::V8::EnableSlidingStateWindow()")) return;

Modified: branches/bleeding_edge/src/compiler.cc
==============================================================================
--- branches/bleeding_edge/src/compiler.cc      (original)
+++ branches/bleeding_edge/src/compiler.cc      Fri Mar 13 09:06:31 2009
@@ -110,10 +110,10 @@
    // Measure how long it takes to do the compilation; only take the
    // rest of the function into account to avoid overlap with the
    // parsing statistics.
-  StatsRate* rate = is_eval
+  HistogramTimer* rate = is_eval
        ? &Counters::compile_eval
        : &Counters::compile;
-  StatsRateScope timer(rate);
+  HistogramTimerScope timer(rate);

    // Compile the code.
    Handle<Code> code = MakeCode(lit, script, context, is_eval);
@@ -300,7 +300,7 @@
    // Measure how long it takes to do the lazy compilation; only take
    // the rest of the function into account to avoid overlap with the
    // lazy parsing statistics.
-  StatsRateScope timer(&Counters::compile_lazy);
+  HistogramTimerScope timer(&Counters::compile_lazy);

    // Compile the code.
    Handle<Code> code = MakeCode(lit, script, Handle<Context>::null(),  
false);

Modified: branches/bleeding_edge/src/counters.cc
==============================================================================
--- branches/bleeding_edge/src/counters.cc      (original)
+++ branches/bleeding_edge/src/counters.cc      Fri Mar 13 09:06:31 2009
@@ -33,6 +33,8 @@
  namespace v8 { namespace internal {

  CounterLookupCallback StatsTable::lookup_function_ = NULL;
+CreateHistogramCallback StatsTable::create_histogram_function_ = NULL;
+AddHistogramSampleCallback StatsTable::add_histogram_sample_function_ =  
NULL;

  // Start the timer.
  void StatsCounterTimer::Start() {
@@ -51,6 +53,25 @@
    // Compute the delta between start and stop, in milliseconds.
    int milliseconds = static_cast<int>(stop_time_ - start_time_) / 1000;
    counter_.Increment(milliseconds);
+}
+
+// Start the timer.
+void HistogramTimer::Start() {
+  if (GetHistogram() != NULL) {
+    stop_time_ = 0;
+    start_time_ = OS::Ticks();
+  }
+}
+
+// Stop the timer and record the results.
+void HistogramTimer::Stop() {
+  if (histogram_ != NULL) {
+    stop_time_ = OS::Ticks();
+
+    // Compute the delta between start and stop, in milliseconds.
+    int milliseconds = static_cast<int>(stop_time_ - start_time_) / 1000;
+    StatsTable::AddHistogramSample(histogram_, milliseconds);
+  }
  }

  } }  // namespace v8::internal

Modified: branches/bleeding_edge/src/counters.h
==============================================================================
--- branches/bleeding_edge/src/counters.h       (original)
+++ branches/bleeding_edge/src/counters.h       Fri Mar 13 09:06:31 2009
@@ -42,6 +42,18 @@
      lookup_function_ = f;
    }

+  // Register an application-defined function to create
+  // a histogram for passing to the AddHistogramSample function
+  static void SetCreateHistogramFunction(CreateHistogramCallback f) {
+    create_histogram_function_ = f;
+  }
+
+  // Register an application-defined function to add a sample
+  // to a histogram created with CreateHistogram function
+  static void SetAddHistogramSampleFunction(AddHistogramSampleCallback f) {
+    add_histogram_sample_function_ = f;
+  }
+
    static bool HasCounterFunction() {
      return lookup_function_ != NULL;
    }
@@ -57,8 +69,30 @@
      return lookup_function_(name);
    }

+  // 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.
+  static void *CreateHistogram(const char* name,
+                               int min,
+                               int max,
+                               size_t buckets) {
+    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.
+  static void AddHistogramSample(void* histogram, int sample) {
+    if (!add_histogram_sample_function_) return;
+    return add_histogram_sample_function_(histogram, sample);
+  }
+
   private:
    static CounterLookupCallback lookup_function_;
+  static CreateHistogramCallback create_histogram_function_;
+  static AddHistogramSampleCallback add_histogram_sample_function_;
  };

  // StatsCounters are dynamically created values which can be tracked in
@@ -152,44 +186,50 @@
    }
  };

-// A StatsRate is a combination of both a timer and a counter so that
-// several statistics can be produced:
-//    min, max, avg, count, total
-//
-// For example:
-//   StatsCounter c = { { { L"t:myrate", NULL, false }, 0, 0 },
-//                      { L"c:myrate", NULL, false } };
-struct StatsRate {
-  StatsCounterTimer timer_;
-  StatsCounter counter_;
+// A HistogramTimer allows distributions of results to be created
+// HistogramTimer t = { L"foo", NULL, false, 0, 0 };
+struct HistogramTimer {
+  const char* name_;
+  void* histogram_;
+  bool lookup_done_;

-  // Starts the rate timer.
-  void Start() {
-    timer_.Start();
+  int64_t start_time_;
+  int64_t stop_time_;
+
+  // Start the timer.
+  void Start();
+
+  // Stop the timer and record the results.
+  void Stop();
+
+  // Returns true if the timer is running.
+  bool Running() {
+    return (histogram_ != NULL) && (start_time_ != 0) && (stop_time_ == 0);
    }

-  // Stops the rate and records the time.
-  void Stop() {
-    if (timer_.Running()) {
-      timer_.Stop();
-      counter_.Increment();
+ protected:
+  // Returns the handle to the histogram.
+  void* GetHistogram() {
+    if (!lookup_done_) {
+      lookup_done_ = true;
+      histogram_ = StatsTable::CreateHistogram(name_, 0, 10000, 50);
      }
+    return histogram_;
    }
  };

-
-// Helper class for scoping a rate.
-class StatsRateScope BASE_EMBEDDED {
+// Helper class for scoping a HistogramTimer.
+class HistogramTimerScope BASE_EMBEDDED {
   public:
-  explicit StatsRateScope(StatsRate* rate) :
-      rate_(rate) {
-    rate_->Start();
+  explicit HistogramTimerScope(HistogramTimer* timer) :
+  timer_(timer) {
+    timer_->Start();
    }
-  ~StatsRateScope() {
-    rate_->Stop();
+  ~HistogramTimerScope() {
+    timer_->Stop();
    }
   private:
-  StatsRate* rate_;
+  HistogramTimer* timer_;
  };



Modified: branches/bleeding_edge/src/heap.cc
==============================================================================
--- branches/bleeding_edge/src/heap.cc  (original)
+++ branches/bleeding_edge/src/heap.cc  Fri Mar 13 09:06:31 2009
@@ -310,7 +310,7 @@
    // contexts are disposed and leave it to the embedder to make
    // informed decisions about when to force a collection.
    if (!FLAG_expose_gc && context_disposed_pending_) {
-    StatsRateScope scope(&Counters::gc_context);
+    HistogramTimerScope scope(&Counters::gc_context);
      CollectAllGarbage();
    }
    context_disposed_pending_ = false;
@@ -345,7 +345,7 @@
      // Tell the tracer which collector we've selected.
      tracer.set_collector(collector);

-    StatsRate* rate = (collector == SCAVENGER)
+    HistogramTimer* rate = (collector == SCAVENGER)
          ? &Counters::gc_scavenger
          : &Counters::gc_compactor;
      rate->Start();

Modified: branches/bleeding_edge/src/parser.cc
==============================================================================
--- branches/bleeding_edge/src/parser.cc        (original)
+++ branches/bleeding_edge/src/parser.cc        Fri Mar 13 09:06:31 2009
@@ -1075,7 +1075,7 @@


  bool Parser::PreParseProgram(unibrow::CharacterStream* stream) {
-  StatsRateScope timer(&Counters::pre_parse);
+  HistogramTimerScope timer(&Counters::pre_parse);
    StackGuard guard;
    AssertNoZoneAllocation assert_no_zone_allocation;
    AssertNoAllocation assert_no_allocation;
@@ -1098,7 +1098,7 @@
                                        bool in_global_context) {
    ZoneScope zone_scope(DONT_DELETE_ON_EXIT);

-  StatsRateScope timer(&Counters::parse);
+  HistogramTimerScope timer(&Counters::parse);
    StringShape shape(*source);
    Counters::total_parse_size.Increment(source->length(shape));

@@ -1151,7 +1151,7 @@
                                     int start_position,
                                     bool is_expression) {
    ZoneScope zone_scope(DONT_DELETE_ON_EXIT);
-  StatsRateScope timer(&Counters::parse_lazy);
+  HistogramTimerScope timer(&Counters::parse_lazy);
    source->TryFlattenIfNotFlat(StringShape(*source));
    StringShape shape(*source);
    Counters::total_parse_size.Increment(source->length(shape));

Modified: branches/bleeding_edge/src/v8-counters.cc
==============================================================================
--- branches/bleeding_edge/src/v8-counters.cc   (original)
+++ branches/bleeding_edge/src/v8-counters.cc   Fri Mar 13 09:06:31 2009
@@ -31,12 +31,10 @@

  namespace v8 { namespace internal {

-#define SR(name, caption) \
-  StatsRate Counters::name = { \
-  { { "t:" #caption, NULL, false }, 0, 0 }, \
-  { "c:" #caption, NULL, false } };
+#define HT(name, caption) \
+  HistogramTimer Counters::name = { #caption, NULL, false, 0, 0 }; \

-  STATS_RATE_LIST(SR)
+  HISTOGRAM_TIMER_LIST(HT)
  #undef SR

  #define SC(name, caption) \

Modified: branches/bleeding_edge/src/v8-counters.h
==============================================================================
--- branches/bleeding_edge/src/v8-counters.h    (original)
+++ branches/bleeding_edge/src/v8-counters.h    Fri Mar 13 09:06:31 2009
@@ -32,16 +32,16 @@

  namespace v8 { namespace internal {

-#define STATS_RATE_LIST(SR)                                             \
-  SR(gc_compactor, V8.GCCompactor) /* GC Compactor time */              \
-  SR(gc_scavenger, V8.GCScavenger) /* GC Scavenger time */              \
-  SR(gc_context, V8.GCContext)     /* GC context cleanup time */        \
-  SR(compile, V8.Compile)          /* Compile time*/                    \
-  SR(compile_eval, V8.CompileEval) /* Eval compile time */              \
-  SR(compile_lazy, V8.CompileLazy) /* Lazy compile time */              \
-  SR(parse, V8.Parse)              /* Parse time */                     \
-  SR(parse_lazy, V8.ParseLazy)     /* Lazy parse time */                \
-  SR(pre_parse, V8.PreParse)       /* Pre-parse time */
+#define HISTOGRAM_TIMER_LIST(HT)                                 \
+  HT(gc_compactor, V8.GCCompactor) /* GC Compactor time */       \
+  HT(gc_scavenger, V8.GCScavenger) /* GC Scavenger time */       \
+  HT(gc_context, V8.GCContext)     /* GC context cleanup time */ \
+  HT(compile, V8.Compile)          /* Compile time*/             \
+  HT(compile_eval, V8.CompileEval) /* Eval compile time */       \
+  HT(compile_lazy, V8.CompileLazy) /* Lazy compile time */       \
+  HT(parse, V8.Parse)              /* Parse time */              \
+  HT(parse_lazy, V8.ParseLazy)     /* Lazy parse time */         \
+  HT(pre_parse, V8.PreParse)       /* Pre-parse time */

  // WARNING: STATS_COUNTER_LIST_* is a very large macro that is causing MSVC
  // Intellisense to crash.  It was broken into two macros (each of length 40
@@ -129,10 +129,10 @@
  // This file contains all the v8 counters that are in use.
  class Counters : AllStatic {
   public:
-#define SR(name, caption) \
-  static StatsRate name;
-  STATS_RATE_LIST(SR)
-#undef SR
+#define HT(name, caption) \
+  static HistogramTimer name;
+  HISTOGRAM_TIMER_LIST(HT)
+#undef HT

  #define SC(name, caption) \
    static StatsCounter name;
@@ -142,7 +142,7 @@

    enum Id {
  #define RATE_ID(name, caption) k_##name,
-    STATS_RATE_LIST(RATE_ID)
+    HISTOGRAM_TIMER_LIST(RATE_ID)
  #undef RATE_ID
  #define COUNTER_ID(name, caption) k_##name,
    STATS_COUNTER_LIST_1(COUNTER_ID)

--~--~---------~--~----~------------~-------~--~----~
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
-~----------~----~----~----~------~----~------~--~---

Reply via email to