Reviewers: Jakob,

Message:
Please take a look. I was trying to use counters to debug a performance issue and discovered that some counters do not work as callback is installed too late
when stubs are already generated.

Description:
Make counter and histogram related callbacks part of the Isolate::CreateParams.

Some native counters (e.g. KeyedLoadGenericSlow) are referenced from stubs that
are generated very early in the Isolate lifecycle before v8::Isolate::New
returns. Thus counter lookup callback also needs to be installed early prior to v8::internal::Isolate::Init call. Otherwise assembler will just assume that the
counter is not enabled and produce no code from IncrementCounter - because
address of the counter is not yet available.

Histogram related callbacks are moved for consistency to make them able to
collect samples which occur at isolate initialization time.

BUG=

Please review this at https://codereview.chromium.org/1010233002/

Base URL: https://chromium.googlesource.com/v8/v8.git@master

Affected files (+41, -7 lines):
  M include/v8.h
  M src/api.cc
  M src/d8.cc


Index: include/v8.h
diff --git a/include/v8.h b/include/v8.h
index 755a87fdba14f78c70e9ec8bd05a6852a65a49ce..4c30b1f1d581b23fb6f45812310483772562d86d 100644
--- a/include/v8.h
+++ b/include/v8.h
@@ -4903,7 +4903,12 @@ class V8_EXPORT Isolate {
    */
   struct CreateParams {
     CreateParams()
- : entry_hook(NULL), code_event_handler(NULL), snapshot_blob(NULL) {}
+        : entry_hook(NULL),
+          code_event_handler(NULL),
+          snapshot_blob(NULL),
+          counter_lookup_callback(NULL),
+          create_histogram_callback(NULL),
+          add_histogram_sample_callback(NULL) {}

     /**
      * The optional entry_hook allows the host application to provide the
@@ -4929,6 +4934,22 @@ class V8_EXPORT Isolate {
* Explicitly specify a startup snapshot blob. The embedder owns the blob.
      */
     StartupData* snapshot_blob;
+
+
+    /**
+     * Enables the host application to provide a mechanism for recording
+     * statistics counters.
+     */
+    CounterLookupCallback counter_lookup_callback;
+
+    /**
+     * 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.
+     */
+    CreateHistogramCallback create_histogram_callback;
+    AddHistogramSampleCallback add_histogram_sample_callback;
   };


Index: src/api.cc
diff --git a/src/api.cc b/src/api.cc
index fd17edfc568cc16e1f20595c0f96f8d2ba32bd90..31707d999d4cff41b70648b7d4168a22b8fc3420 100644
--- a/src/api.cc
+++ b/src/api.cc
@@ -6803,6 +6803,18 @@ Isolate* Isolate::New(const Isolate::CreateParams& params) {
     isolate->logger()->SetCodeEventHandler(kJitCodeEventDefault,
                                            params.code_event_handler);
   }
+  if (params.counter_lookup_callback) {
+    v8_isolate->SetCounterFunction(params.counter_lookup_callback);
+  }
+
+  if (params.create_histogram_callback) {
+ v8_isolate->SetCreateHistogramFunction(params.create_histogram_callback);
+  }
+
+  if (params.add_histogram_sample_callback) {
+    v8_isolate->SetAddHistogramSampleFunction(
+        params.add_histogram_sample_callback);
+  }
   SetResourceConstraints(isolate, params.constraints);
// TODO(jochen): Once we got rid of Isolate::Current(), we can remove this.
   Isolate::Scope isolate_scope(v8_isolate);
Index: src/d8.cc
diff --git a/src/d8.cc b/src/d8.cc
index 7fa6f8c42e68a2cfd9de7f955da93229b94d1283..f28ff39ef354eb8a9436d9f3b4b31a48e7c467c3 100644
--- a/src/d8.cc
+++ b/src/d8.cc
@@ -963,15 +963,9 @@ Handle<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) {

 void Shell::Initialize(Isolate* isolate) {
 #ifndef V8_SHARED
-  Shell::counter_map_ = new CounterMap();
   // Set up counters
   if (i::StrLength(i::FLAG_map_counters) != 0)
     MapCounters(isolate, i::FLAG_map_counters);
-  if (i::FLAG_dump_counters || i::FLAG_track_gc_object_stats) {
-    isolate->SetCounterFunction(LookupCounter);
-    isolate->SetCreateHistogramFunction(CreateHistogram);
-    isolate->SetAddHistogramSampleFunction(AddHistogramSample);
-  }
 #endif  // !V8_SHARED
 }

@@ -1639,6 +1633,13 @@ int Shell::Main(int argc, char* argv[]) {
       base::SysInfo::AmountOfPhysicalMemory(),
       base::SysInfo::AmountOfVirtualMemory(),
       base::SysInfo::NumberOfProcessors());
+
+  Shell::counter_map_ = new CounterMap();
+  if (i::FLAG_dump_counters || i::FLAG_track_gc_object_stats) {
+    create_params.counter_lookup_callback = LookupCounter;
+    create_params.create_histogram_callback = CreateHistogram;
+    create_params.add_histogram_sample_callback = AddHistogramSample;
+  }
 #endif
   Isolate* isolate = Isolate::New(create_params);
   DumbLineEditor dumb_line_editor(isolate);


--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to