Test that profiler is stopped when isolate is being disposed
authoryurys@chromium.org <yurys@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Mon, 1 Jul 2013 12:32:52 +0000 (12:32 +0000)
committeryurys@chromium.org <yurys@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Mon, 1 Jul 2013 12:32:52 +0000 (12:32 +0000)
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
R=loislo@chromium.org, yangguo@chromium.org

Review URL: https://codereview.chromium.org/18336002

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

include/v8.h
src/cpu-profiler.cc
test/cctest/test-cpu-profiler.cc

index c4b22e3002e96e0d88c95c3e30dec6f44ac5478e..cefc87a13436d84de1a9396c5df9783f5555a9f2 100644 (file)
@@ -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 512f7d5123edb0ff8ba1a220322dd80a253997c0..b6cfbc5caee60e05abbfcd71954a8a852284dee6 100644 (file)
@@ -405,6 +405,7 @@ CpuProfiler::CpuProfiler(Isolate* isolate,
 
 
 CpuProfiler::~CpuProfiler() {
+  ASSERT(!is_profiling_);
   delete token_enumerator_;
   delete profiles_;
 }
@@ -430,23 +431,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_);
     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 fb0566d38ed3e42675352e21ee81ff07c361a819..251a4a529095eb260742d8fa613b679adc301656 100644 (file)
@@ -450,6 +450,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++) {