Reviewers: Jakob, Yang, loislo,

Description:
Test that profiler is stopped when isolate is being disposed

The only way to get v8::CpuProfiler instance in the V8 public API is to call
v8::Iolate::GetCpuProfiler(). The method will return NULL if the isolate has not
been initialized yet or has been torn down already. It is the client's
reponsibility to make sure that CPU profiling has been stopped before disposing
of the isolate.

This CL adds a test for this and several ASSRTS enforcing that assumptions. This
allowed to be sure that heap is always setup when CPU profiling is being
started. Based on that the number of places where already compiled functions are
reported to the profiler event processor boils down to the single place
(CpuProfiler::StartProcessorIfNotStarted). I'm going to rely on this assumption
in further changes.

BUG=None

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

SVN Base: https://v8.googlecode.com/svn/branches/bleeding_edge

Affected files:
  M include/v8.h
  M src/cpu-profiler.cc
  M test/cctest/test-cpu-profiler.cc


Index: include/v8.h
diff --git a/include/v8.h b/include/v8.h
index cf01684729d9f4a433fbd1008d728752de6919a5..c9ad81f50150d3ccc8b62e31efd36bc35c524079 100644
--- a/include/v8.h
+++ b/include/v8.h
@@ -3993,8 +3993,9 @@ class V8EXPORT Isolate {
   HeapProfiler* GetHeapProfiler();

   /**
- * Returns CPU profiler for this isolate. Will return NULL until the isolate
-   * is initialized.
+ * Returns CPU profiler for this isolate. Will return NULL unless the isolate
+   * is initialized. It is the embedder's responsibility to stop all CPU
+   * profiling activities if it has started any.
    */
   CpuProfiler* GetCpuProfiler();

Index: src/cpu-profiler.cc
diff --git a/src/cpu-profiler.cc b/src/cpu-profiler.cc
index 109ddd5d976f0a36ccdb1512e02b2a78467238d6..51dc353f854f54044ad289f16769c7402d502673 100644
--- a/src/cpu-profiler.cc
+++ b/src/cpu-profiler.cc
@@ -425,6 +425,7 @@ CpuProfiler::CpuProfiler(Isolate* isolate)


 CpuProfiler::~CpuProfiler() {
+  ASSERT(!is_profiling_);
   delete token_enumerator_;
   delete profiles_;
 }
@@ -450,23 +451,23 @@ void CpuProfiler::StartProfiling(String* title, bool record_samples) {

 void CpuProfiler::StartProcessorIfNotStarted() {
   if (processor_ == NULL) {
+    Logger* logger = isolate_->logger();
     // Disable logging when using the new implementation.
-    saved_logging_nesting_ = isolate_->logger()->logging_nesting_;
-    isolate_->logger()->logging_nesting_ = 0;
+    saved_logging_nesting_ = logger->logging_nesting_;
+    logger->logging_nesting_ = 0;
     generator_ = new ProfileGenerator(profiles_);
     processor_ = new ProfilerEventsProcessor(generator_, profiles_);
     is_profiling_ = true;
     processor_->StartSynchronously();
     // Enumerate stuff we already have in the heap.
-    if (isolate_->heap()->HasBeenSetUp()) {
-      if (!FLAG_prof_browser_mode) {
-        isolate_->logger()->LogCodeObjects();
-      }
-      isolate_->logger()->LogCompiledFunctions();
-      isolate_->logger()->LogAccessorCallbacks();
+    ASSERT(isolate_->heap()->HasBeenSetUp());
+    if (!FLAG_prof_browser_mode) {
+      logger->LogCodeObjects();
     }
+    logger->LogCompiledFunctions();
+    logger->LogAccessorCallbacks();
     // Enable stack sampling.
-    Sampler* sampler = isolate_->logger()->sampler();
+    Sampler* sampler = logger->sampler();
     sampler->IncreaseProfilingDepth();
     if (!sampler->IsActive()) {
       sampler->Start();
Index: test/cctest/test-cpu-profiler.cc
diff --git a/test/cctest/test-cpu-profiler.cc b/test/cctest/test-cpu-profiler.cc index 9a2496a72a96a77154be1003aab35b150300be8a..35f4bb4cbb5f0cd3b2a4bbe7048c997b4647433f 100644
--- a/test/cctest/test-cpu-profiler.cc
+++ b/test/cctest/test-cpu-profiler.cc
@@ -412,6 +412,26 @@ TEST(DeleteCpuProfileDifferentTokens) {
 }


+TEST(GetProfilerWhenIsolateIsNotInitialized) {
+  v8::Isolate* isolate = v8::Isolate::GetCurrent();
+  CHECK(i::Isolate::Current()->IsDefaultIsolate());
+  CHECK(!i::Isolate::Current()->IsInitialized());
+  CHECK_EQ(NULL, isolate->GetCpuProfiler());
+  {
+    v8::Isolate::Scope isolateScope(isolate);
+    LocalContext env;
+    v8::HandleScope scope(isolate);
+    CHECK_NE(NULL, isolate->GetCpuProfiler());
+    isolate->GetCpuProfiler()->StartCpuProfiling(v8::String::New("Test"));
+    isolate->GetCpuProfiler()->StopCpuProfiling(v8::String::New("Test"));
+  }
+  CHECK(i::Isolate::Current()->IsInitialized());
+  CHECK_NE(NULL, isolate->GetCpuProfiler());
+  isolate->Dispose();
+  CHECK_EQ(NULL, isolate->GetCpuProfiler());
+}
+
+
 static bool ContainsString(v8::Handle<v8::String> string,
                            const Vector<v8::Handle<v8::String> >& vector) {
   for (int i = 0; i < vector.length(); i++) {


--
--
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/groups/opt_out.


Reply via email to