Expose counters in javascript.
authoryangguo@chromium.org <yangguo@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Wed, 18 Jul 2012 12:27:29 +0000 (12:27 +0000)
committeryangguo@chromium.org <yangguo@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Wed, 18 Jul 2012 12:27:29 +0000 (12:27 +0000)
R=rossberg
BUG=
TEST=

Review URL: https://chromiumcodereview.appspot.com/10803008

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@12124 ce2b1a6d-e550-0410-aec6-3dcde31c8c00

src/SConscript
src/bootstrapper.cc
src/d8.cc
src/extensions/statistics-extension.cc [new file with mode: 0644]
src/extensions/statistics-extension.h [new file with mode: 0644]
tools/gyp/v8.gyp

index cd359109830ae8c76d6847ffcf08d9860ce8ec3f..c1bb1f28506c2c85eedff865a42eeb9ac9c8f5a8 100755 (executable)
@@ -72,6 +72,7 @@ SOURCES = {
     execution.cc
     extensions/externalize-string-extension.cc
     extensions/gc-extension.cc
+    extensions/statistics-extension.cc
     factory.cc
     fast-dtoa.cc
     fixed-dtoa.cc
index 726670aae54a01cffe1890fd36a9dbadb988d66d..dd1c51164b2dd5ab39fa38d7e8bd1acf04f60323 100644 (file)
@@ -42,6 +42,7 @@
 #include "snapshot.h"
 #include "extensions/externalize-string-extension.h"
 #include "extensions/gc-extension.h"
+#include "extensions/statistics-extension.h"
 
 namespace v8 {
 namespace internal {
@@ -95,6 +96,7 @@ void Bootstrapper::Initialize(bool create_heap_objects) {
   extensions_cache_.Initialize(create_heap_objects);
   GCExtension::Register();
   ExternalizeStringExtension::Register();
+  StatisticsExtension::Register();
 }
 
 
@@ -1997,6 +1999,9 @@ bool Genesis::InstallExtensions(Handle<Context> global_context,
   if (FLAG_expose_externalize_string) {
     InstallExtension("v8/externalize", &extension_states);
   }
+  if (FLAG_track_gc_object_stats) {
+    InstallExtension("v8/statistics", &extension_states);
+  }
 
   if (extensions == NULL) return true;
   // Install required extensions
index 804b76ceeb5dcdb25030c1c1112de4215223b346..c716c1ccd341cd15698b1ad938ed8021fb302941 100644 (file)
--- a/src/d8.cc
+++ b/src/d8.cc
@@ -1211,7 +1211,7 @@ void Shell::Initialize() {
   // Set up counters
   if (i::StrLength(i::FLAG_map_counters) != 0)
     MapCounters(i::FLAG_map_counters);
-  if (i::FLAG_dump_counters) {
+  if (i::FLAG_dump_counters || i::FLAG_track_gc_object_stats) {
     V8::SetCounterFunction(LookupCounter);
     V8::SetCreateHistogramFunction(CreateHistogram);
     V8::SetAddHistogramSampleFunction(AddHistogramSample);
diff --git a/src/extensions/statistics-extension.cc b/src/extensions/statistics-extension.cc
new file mode 100644 (file)
index 0000000..ec804bc
--- /dev/null
@@ -0,0 +1,100 @@
+// Copyright 2010 the V8 project authors. All rights reserved.
+// 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.
+
+#include "statistics-extension.h"
+
+namespace v8 {
+namespace internal {
+
+const char* const StatisticsExtension::kSource =
+    "native function getV8Statistics();";
+
+
+v8::Handle<v8::FunctionTemplate> StatisticsExtension::GetNativeFunction(
+    v8::Handle<v8::String> str) {
+  ASSERT(strcmp(*v8::String::AsciiValue(str), "getV8Statistics") == 0);
+  return v8::FunctionTemplate::New(StatisticsExtension::GetCounters);
+}
+
+
+v8::Handle<v8::Value> StatisticsExtension::GetCounters(
+    const v8::Arguments& args) {
+  Isolate* isolate = Isolate::Current();
+  Heap* heap = isolate->heap();
+  if (args.Length() > 0) {  // GC if first argument evaluates to true.
+    if (args[0]->IsBoolean() && args[0]->ToBoolean()->Value()) {
+      heap->CollectAllGarbage(Heap::kNoGCFlags, "counters extension");
+    }
+  }
+
+  Counters* counters = isolate->counters();
+  v8::Local<v8::Object> result = v8::Object::New();
+
+  StatsCounter* counter = NULL;
+
+#define ADD_COUNTER(name, caption)                                             \
+  counter = counters->name();                                                  \
+  if (counter->Enabled())                                                      \
+    result->Set(v8::String::New(#name),                                        \
+        v8::Number::New(*counter->GetInternalPointer()));
+
+  STATS_COUNTER_LIST_1(ADD_COUNTER)
+  STATS_COUNTER_LIST_2(ADD_COUNTER)
+#undef ADD_COUNTER
+#define ADD_COUNTER(name)                                                      \
+  counter = counters->count_of_##name();                                       \
+  if (counter->Enabled())                                                      \
+    result->Set(v8::String::New("count_of_" #name),                            \
+        v8::Number::New(*counter->GetInternalPointer()));                      \
+  counter = counters->size_of_##name();                                        \
+  if (counter->Enabled())                                                      \
+    result->Set(v8::String::New("size_of_" #name),                             \
+        v8::Number::New(*counter->GetInternalPointer()));
+
+  INSTANCE_TYPE_LIST(ADD_COUNTER)
+#undef ADD_COUNTER
+#define ADD_COUNTER(name)                                                      \
+  result->Set(v8::String::New("count_of_CODE_TYPE_" #name),                    \
+      v8::Number::New(                                                         \
+          *counters->count_of_CODE_TYPE_##name()->GetInternalPointer()));      \
+  result->Set(v8::String::New("size_of_CODE_TYPE_" #name),                     \
+        v8::Number::New(                                                       \
+            *counters->size_of_CODE_TYPE_##name()->GetInternalPointer()));
+
+  CODE_KIND_LIST(ADD_COUNTER)
+#undef ADD_COUNTER
+
+  return result;
+}
+
+
+void StatisticsExtension::Register() {
+  static StatisticsExtension statistics_extension;
+  static v8::DeclareExtension declaration(&statistics_extension);
+}
+
+} }  // namespace v8::internal
diff --git a/src/extensions/statistics-extension.h b/src/extensions/statistics-extension.h
new file mode 100644 (file)
index 0000000..433c4cf
--- /dev/null
@@ -0,0 +1,49 @@
+// Copyright 2010 the V8 project authors. All rights reserved.
+// 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_EXTENSIONS_STATISTICS_EXTENSION_H_
+#define V8_EXTENSIONS_STATISTICS_EXTENSION_H_
+
+#include "v8.h"
+
+namespace v8 {
+namespace internal {
+
+class StatisticsExtension : public v8::Extension {
+ public:
+  StatisticsExtension() : v8::Extension("v8/statistics", kSource) {}
+  virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction(
+      v8::Handle<v8::String> name);
+  static v8::Handle<v8::Value> GetCounters(const v8::Arguments& args);
+  static void Register();
+ private:
+  static const char* const kSource;
+};
+
+} }  // namespace v8::internal
+
+#endif  // V8_EXTENSIONS_STATISTICS_EXTENSION_H_
index 8b5d1158cd2d9d271bdef6ab5eb51e464d41bf84..598bb3d91b0b050425f7732465586d42837d203a 100644 (file)
             '../../src/extensions/externalize-string-extension.h',
             '../../src/extensions/gc-extension.cc',
             '../../src/extensions/gc-extension.h',
+            '../../src/extensions/statistics-extension.cc',
+            '../../src/extensions/statistics-extension.h',
             '../../src/factory.cc',
             '../../src/factory.h',
             '../../src/fast-dtoa.cc',