Revision: 4382
Author: [email protected]
Date: Mon Apr 12 00:23:43 2010
Log: Allow new CPU profiling subsystem to coexist nicely with the old one.

This is to make possible enabling usage of the new profiling subsystem
in Chromium without much hassle. The idea is pretty simple: unless the
new profiling API is used, all works as usual, as soon as Chromium
starts to use the new API, it will work too.

Review URL: http://codereview.chromium.org/1635005
http://code.google.com/p/v8/source/detail?r=4382

Modified:
 /branches/bleeding_edge/SConstruct
 /branches/bleeding_edge/src/cpu-profiler-inl.h
 /branches/bleeding_edge/src/cpu-profiler.cc
 /branches/bleeding_edge/src/cpu-profiler.h
 /branches/bleeding_edge/src/flag-definitions.h
 /branches/bleeding_edge/src/globals.h
 /branches/bleeding_edge/src/log.cc
 /branches/bleeding_edge/src/log.h
 /branches/bleeding_edge/src/platform-linux.cc
 /branches/bleeding_edge/src/platform-macos.cc
 /branches/bleeding_edge/src/platform-win32.cc
 /branches/bleeding_edge/src/v8.cc
 /branches/bleeding_edge/test/cctest/test-cpu-profiler.cc
 /branches/bleeding_edge/test/cctest/test-profile-generator.cc
 /branches/bleeding_edge/tools/gyp/v8.gyp

=======================================
--- /branches/bleeding_edge/SConstruct  Thu Apr  8 06:37:39 2010
+++ /branches/bleeding_edge/SConstruct  Mon Apr 12 00:23:43 2010
@@ -695,7 +695,7 @@
   },
   'cppprofilesprocessor': {
     'values': ['on', 'off'],
-    'default': 'off',
+    'default': 'on',
     'help': 'enable C++ profiles processor'
   },
   'debuggersupport': {
=======================================
--- /branches/bleeding_edge/src/cpu-profiler-inl.h      Wed Apr  7 07:18:26 2010
+++ /branches/bleeding_edge/src/cpu-profiler-inl.h      Mon Apr 12 00:23:43 2010
@@ -39,28 +39,40 @@
 namespace internal {

 void CodeCreateEventRecord::UpdateCodeMap(CodeMap* code_map) {
-    code_map->AddCode(start, entry, size);
+  code_map->AddCode(start, entry, size);
 }


 void CodeMoveEventRecord::UpdateCodeMap(CodeMap* code_map) {
-    code_map->MoveCode(from, to);
+  code_map->MoveCode(from, to);
 }


 void CodeDeleteEventRecord::UpdateCodeMap(CodeMap* code_map) {
-    code_map->DeleteCode(start);
+  code_map->DeleteCode(start);
 }


 void CodeAliasEventRecord::UpdateCodeMap(CodeMap* code_map) {
-    code_map->AddAlias(alias, start);
+  code_map->AddAlias(alias, start);
+}
+
+
+TickSampleEventRecord* TickSampleEventRecord::init(void* value) {
+  TickSampleEventRecord* result =
+      reinterpret_cast<TickSampleEventRecord*>(value);
+  result->filler = 1;
+  ASSERT(result->filler != SamplingCircularQueue::kClear);
+  // Init the required fields only.
+  result->sample.pc = NULL;
+  result->sample.frames_count = 0;
+  return result;
 }


 TickSample* ProfilerEventsProcessor::TickSampleEvent() {
   TickSampleEventRecord* evt =
-      TickSampleEventRecord::cast(ticks_buffer_.Enqueue());
+      TickSampleEventRecord::init(ticks_buffer_.Enqueue());
   evt->order = enqueue_order_;  // No increment!
   return &evt->sample;
 }
=======================================
--- /branches/bleeding_edge/src/cpu-profiler.cc Wed Apr  7 07:18:26 2010
+++ /branches/bleeding_edge/src/cpu-profiler.cc Mon Apr 12 00:23:43 2010
@@ -283,8 +283,7 @@


 TickSample* CpuProfiler::TickSampleEvent() {
-  ASSERT(singleton_ != NULL);
-  if (singleton_->is_profiling()) {
+  if (CpuProfiler::is_profiling()) {
     return singleton_->processor_->TickSampleEvent();
   } else {
     return NULL;
@@ -417,6 +416,9 @@

 void CpuProfiler::StartProcessorIfNotStarted() {
   if (processor_ == NULL) {
+    // Disable logging when using the new implementation.
+    saved_logging_nesting_ = Logger::logging_nesting_;
+    Logger::logging_nesting_ = 0;
     generator_ = new ProfileGenerator(profiles_);
     processor_ = new ProfilerEventsProcessor(generator_);
     processor_->Start();
@@ -428,7 +430,7 @@
       Logger::LogAccessorCallbacks();
     }
     // Enable stack sampling.
-    Logger::ticker_->Start();
+    reinterpret_cast<Sampler*>(Logger::ticker_)->Start();
   }
 }

@@ -451,13 +453,14 @@

 void CpuProfiler::StopProcessorIfLastProfile() {
   if (profiles_->is_last_profile()) {
-    Logger::ticker_->Stop();
+    reinterpret_cast<Sampler*>(Logger::ticker_)->Stop();
     processor_->Stop();
     processor_->Join();
     delete processor_;
     delete generator_;
     processor_ = NULL;
     generator_ = NULL;
+    Logger::logging_nesting_ = saved_logging_nesting_;
   }
 }

=======================================
--- /branches/bleeding_edge/src/cpu-profiler.h  Wed Apr  7 07:18:26 2010
+++ /branches/bleeding_edge/src/cpu-profiler.h  Mon Apr 12 00:23:43 2010
@@ -103,17 +103,20 @@

 class TickSampleEventRecord BASE_EMBEDDED {
  public:
- // In memory, the first machine word of a TickSampleEventRecord will be the
-  // first entry of TickSample, that is -- the VM state field.
-  // TickSample is put first, because 'order' can become equal to
-  // SamplingCircularQueue::kClear, while VM state can't, see
-  // the definition of 'enum StateTag'.
-  TickSample sample;
+  // The first machine word of a TickSampleEventRecord must not ever
+  // become equal to SamplingCircularQueue::kClear.  As both order and
+  // TickSample's first field are not reliable in this sense (order
+  // can overflow, TickSample can have all fields reset), we are
+  // forced to use an artificial filler field.
+  int filler;
   unsigned order;
+  TickSample sample;

   static TickSampleEventRecord* cast(void* value) {
     return reinterpret_cast<TickSampleEventRecord*>(value);
   }
+
+  INLINE(static TickSampleEventRecord* init(void* value));

  private:
   DISALLOW_IMPLICIT_CONSTRUCTORS(TickSampleEventRecord);
@@ -256,6 +259,7 @@
   unsigned next_profile_uid_;
   ProfileGenerator* generator_;
   ProfilerEventsProcessor* processor_;
+  int saved_logging_nesting_;

   static CpuProfiler* singleton_;

=======================================
--- /branches/bleeding_edge/src/flag-definitions.h      Tue Apr  6 07:54:20 2010
+++ /branches/bleeding_edge/src/flag-definitions.h      Mon Apr 12 00:23:43 2010
@@ -391,7 +391,7 @@
 DEFINE_bool(prof_lazy, false,
             "Used with --prof, only does sampling and logging"
             " when profiler is active (implies --noprof_auto).")
-DEFINE_bool(prof_browser_mode, false,
+DEFINE_bool(prof_browser_mode, true,
"Used with --prof, turns on browser-compatible mode for profiling.")
 DEFINE_bool(log_regexp, false, "Log regular expression execution.")
 DEFINE_bool(sliding_state_window, false,
=======================================
--- /branches/bleeding_edge/src/globals.h       Wed Apr  7 07:18:26 2010
+++ /branches/bleeding_edge/src/globals.h       Mon Apr 12 00:23:43 2010
@@ -458,7 +458,7 @@

 // Logging and profiling.
 // A StateTag represents a possible state of the VM.  When compiled with
-// ENABLE_LOGGING_AND_PROFILING, the logger maintains a stack of these.
+// ENABLE_VMSTATE_TRACKING, the logger maintains a stack of these.
 // Creating a VMState object enters a state by pushing on the stack, and
 // destroying a VMState object leaves a state by popping the current state
 // from the stack.
@@ -471,11 +471,6 @@
   V(EXTERNAL)

 enum StateTag {
-#ifdef ENABLE_CPP_PROFILES_PROCESSOR
-  // This is to ensure that VM state field value of TickSample
-  // never gets equal to SamplingCircularQueue::kClear.
-  NULL_STATE = 0,
-#endif
 #define DEF_STATE_TAG(name) name,
   STATE_TAG_LIST(DEF_STATE_TAG)
 #undef DEF_STATE_TAG
=======================================
--- /branches/bleeding_edge/src/log.cc  Thu Apr  8 07:00:51 2010
+++ /branches/bleeding_edge/src/log.cc  Mon Apr 12 00:23:43 2010
@@ -181,8 +181,6 @@
 // Ticker used to provide ticks to the profiler and the sliding state
 // window.
 //
-#ifndef ENABLE_CPP_PROFILES_PROCESSOR
-
 class Ticker: public Sampler {
  public:
   explicit Ticker(int interval):
@@ -224,8 +222,6 @@
   Profiler* profiler_;
 };

-#endif  // ENABLE_CPP_PROFILES_PROCESSOR
-

 //
 // SlidingStateWindow implementation.
@@ -1507,11 +1503,6 @@
       profiler_->Engage();
     }
   }
-
-#ifdef ENABLE_CPP_PROFILES_PROCESSOR
-  // Disable old logging, as we are using the same '--prof' flag.
-  logging_nesting_ = 0;
-#endif

   LogMessageBuilder::set_write_failure_handler(StopLoggingAndProfiling);

=======================================
--- /branches/bleeding_edge/src/log.h   Thu Apr  8 06:37:39 2010
+++ /branches/bleeding_edge/src/log.h   Mon Apr 12 00:23:43 2010
@@ -87,7 +87,7 @@
 #define LOG(Call) ((void) 0)
 #endif

-#define LOG_EVENTS_AND_TAGS_LIST(V) \
+#define LOG_EVENTS_AND_TAGS_LIST_NO_NATIVES(V) \
   V(CODE_CREATION_EVENT,            "code-creation",          "cc")       \
   V(CODE_MOVE_EVENT,                "code-move",              "cm")       \
   V(CODE_DELETE_EVENT,              "code-delete",            "cd")       \
@@ -118,17 +118,24 @@
   V(STORE_IC_TAG,                   "StoreIC",                "sic")      \
   V(STUB_TAG,                       "Stub",                   "s")

+#ifdef ENABLE_CPP_PROFILES_PROCESSOR
+// Add 'NATIVE_' cases for functions and scripts, but map them to
+// original tags when writing to the log.
+#define LOG_EVENTS_AND_TAGS_LIST(V) \
+  LOG_EVENTS_AND_TAGS_LIST_NO_NATIVES(V)                                  \
+  V(NATIVE_FUNCTION_TAG,            "Function",               "f")        \
+  V(NATIVE_LAZY_COMPILE_TAG,        "LazyCompile",            "lc")       \
+  V(NATIVE_SCRIPT_TAG,              "Script",                 "sc")
+#else
+#define LOG_EVENTS_AND_TAGS_LIST(V) LOG_EVENTS_AND_TAGS_LIST_NO_NATIVES(V)
+#endif
+
 class Logger {
  public:
 #define DECLARE_ENUM(enum_item, ignore1, ignore2) enum_item,
   enum LogEventsAndTags {
     LOG_EVENTS_AND_TAGS_LIST(DECLARE_ENUM)
     NUMBER_OF_LOG_EVENTS
-#ifdef ENABLE_CPP_PROFILES_PROCESSOR
-    , NATIVE_FUNCTION_TAG
-    , NATIVE_LAZY_COMPILE_TAG
-    , NATIVE_SCRIPT_TAG
-#endif
   };
 #undef DECLARE_ENUM

@@ -365,26 +372,6 @@
   static void Trace(TickSample* sample);
 };

-
-#ifdef ENABLE_CPP_PROFILES_PROCESSOR
-
-class Ticker: public Sampler {
- public:
-  explicit Ticker(int interval):
-      Sampler(interval, FLAG_prof) {}
-
-  void SampleStack(TickSample* sample) {
-    StackTracer::Trace(sample);
-  }
-  void Tick(TickSample* sample) { }
-  void SetWindow(SlidingStateWindow* window) { }
-  void ClearWindow() { }
-  void SetProfiler(Profiler* profiler) { }
-  void ClearProfiler() { }
-};
-
-#endif  // ENABLE_CPP_PROFILES_PROCESSOR
-
 } }  // namespace v8::internal


=======================================
--- /branches/bleeding_edge/src/platform-linux.cc       Thu Apr  8 06:37:39 2010
+++ /branches/bleeding_edge/src/platform-linux.cc       Mon Apr 12 00:23:43 2010
@@ -727,15 +727,12 @@
   if (signal != SIGPROF) return;
   if (active_sampler_ == NULL) return;

-#ifdef ENABLE_CPP_PROFILES_PROCESSOR
-  TickSample* sample = CpuProfiler::TickSampleEvent();
-  if (sample == NULL) return;
-  sample->pc = NULL;  // Impossible value if sampling succeeds.
-  sample->frames_count = 0;
-#else
   TickSample sample_obj;
-  TickSample* sample = &sample_obj;
+  TickSample* sample = NULL;
+#ifdef ENABLE_CPP_PROFILES_PROCESSOR
+  sample = CpuProfiler::TickSampleEvent();
 #endif
+  if (sample == NULL) sample = &sample_obj;

   // We always sample the VM state.
   sample->state = VMState::current_state();
@@ -771,10 +768,9 @@
       active_sampler_->SampleStack(sample);
     }
   }
-#ifndef ENABLE_CPP_PROFILES_PROCESSOR
+
   active_sampler_->Tick(sample);
 #endif
-#endif
 }


=======================================
--- /branches/bleeding_edge/src/platform-macos.cc       Thu Apr  8 06:37:39 2010
+++ /branches/bleeding_edge/src/platform-macos.cc       Mon Apr 12 00:23:43 2010
@@ -546,15 +546,12 @@
   void Runner() {
// Loop until the sampler is disengaged, keeping the specified samling freq.
     for ( ; sampler_->IsActive(); OS::Sleep(sampler_->interval_)) {
-#ifdef ENABLE_CPP_PROFILES_PROCESSOR
-      TickSample* sample = CpuProfiler::TickSampleEvent();
-      if (sample == NULL) continue;
-      sample->pc = NULL;  // Impossible value if sampling succeeds.
-      sample->frames_count = 0;
-#else
       TickSample sample_obj;
-      TickSample* sample = &sample_obj;
-#endif  // ENABLE_CPP_PROFILES_PROCESSOR
+      TickSample* sample = NULL;
+#ifdef ENABLE_CPP_PROFILES_PROCESSOR
+      sample = CpuProfiler::TickSampleEvent();
+#endif
+      if (sample == NULL) sample = &sample_obj;

       // We always sample the VM state.
       sample->state = VMState::current_state();
@@ -595,10 +592,8 @@
         thread_resume(profiled_thread_);
       }

-#ifndef ENABLE_CPP_PROFILES_PROCESSOR
       // Invoke tick handler with program counter and stack pointer.
       sampler_->Tick(sample);
-#endif
     }
   }
 };
=======================================
--- /branches/bleeding_edge/src/platform-win32.cc       Thu Apr  8 06:37:39 2010
+++ /branches/bleeding_edge/src/platform-win32.cc       Mon Apr 12 00:23:43 2010
@@ -1805,15 +1805,12 @@
     memset(&context, 0, sizeof(context));
// Loop until the sampler is disengaged, keeping the specified samling freq.
     for ( ; sampler_->IsActive(); Sleep(sampler_->interval_)) {
-#ifdef ENABLE_CPP_PROFILES_PROCESSOR
-      TickSample* sample = CpuProfiler::TickSampleEvent();
-      if (sample == NULL) continue;
-      sample->pc = NULL;  // Impossible value if sampling succeeds.
-      sample->frames_count = 0;
-#else
       TickSample sample_obj;
-      TickSample* sample = &sample_obj;
-#endif  // ENABLE_CPP_PROFILES_PROCESSOR
+      TickSample* sample = NULL;
+#ifdef ENABLE_CPP_PROFILES_PROCESSOR
+      sample = CpuProfiler::TickSampleEvent();
+#endif
+      if (sample == NULL) sample = &sample_obj;

       // We always sample the VM state.
       sample->state = VMState::current_state();
@@ -1836,10 +1833,8 @@
         ResumeThread(profiled_thread_);
       }

-#ifndef ENABLE_CPP_PROFILES_PROCESSOR
       // Invoke tick handler with program counter and stack pointer.
       sampler_->Tick(sample);
-#endif
     }
   }
 };
=======================================
--- /branches/bleeding_edge/src/v8.cc   Wed Apr  7 01:18:51 2010
+++ /branches/bleeding_edge/src/v8.cc   Mon Apr 12 00:23:43 2010
@@ -62,12 +62,6 @@

   CpuProfiler::Setup();

-#ifdef ENABLE_CPP_PROFILES_PROCESSOR
-  if (FLAG_prof && FLAG_prof_auto) {
-    CpuProfiler::StartProfiling("internal.auto");
-  }
-#endif
-
   // Setup the platform OS support.
   OS::Setup();

@@ -143,12 +137,6 @@
 void V8::TearDown() {
   if (!has_been_setup_ || has_been_disposed_) return;

-#ifdef ENABLE_CPP_PROFILES_PROCESSOR
-  if (FLAG_prof && FLAG_prof_auto) {
-    CpuProfiler::StopProfiling("internal.auto");
-  }
-#endif
-
   OProfileAgent::TearDown();

   if (FLAG_preemption) {
=======================================
--- /branches/bleeding_edge/test/cctest/test-cpu-profiler.cc Wed Apr 7 07:18:26 2010 +++ /branches/bleeding_edge/test/cctest/test-cpu-profiler.cc Mon Apr 12 00:23:43 2010
@@ -47,7 +47,6 @@
                                    i::Address frame2 = NULL,
                                    i::Address frame3 = NULL) {
   i::TickSample* sample = proc->TickSampleEvent();
-  sample->state = i::OTHER;
   sample->pc = frame1;
   sample->function = frame1;
   sample->frames_count = 0;
@@ -61,8 +60,28 @@
   }
 }

+namespace {
+
+class TestSetup {
+ public:
+  TestSetup()
+      : old_flag_prof_browser_mode_(i::FLAG_prof_browser_mode) {
+    i::FLAG_prof_browser_mode = false;
+  }
+
+  ~TestSetup() {
+    i::FLAG_prof_browser_mode = old_flag_prof_browser_mode_;
+  }
+
+ private:
+  bool old_flag_prof_browser_mode_;
+};
+
+}  // namespace
+
 TEST(CodeEvents) {
   InitializeVM();
+  TestSetup test_setup;
   CpuProfilesCollection profiles;
   profiles.StartProfiling("", 1);
   ProfileGenerator generator(&profiles);
@@ -129,6 +148,7 @@
 }

 TEST(TickEvents) {
+  TestSetup test_setup;
   CpuProfilesCollection profiles;
   profiles.StartProfiling("", 1);
   ProfileGenerator generator(&profiles);
=======================================
--- /branches/bleeding_edge/test/cctest/test-profile-generator.cc Tue Apr 6 03:36:38 2010 +++ /branches/bleeding_edge/test/cctest/test-profile-generator.cc Mon Apr 12 00:23:43 2010
@@ -367,7 +367,27 @@
 }


+namespace {
+
+class TestSetup {
+ public:
+  TestSetup()
+      : old_flag_prof_browser_mode_(i::FLAG_prof_browser_mode) {
+    i::FLAG_prof_browser_mode = false;
+  }
+
+  ~TestSetup() {
+    i::FLAG_prof_browser_mode = old_flag_prof_browser_mode_;
+  }
+
+ private:
+  bool old_flag_prof_browser_mode_;
+};
+
+}  // namespace
+
 TEST(RecordTickSample) {
+  TestSetup test_setup;
   CpuProfilesCollection profiles;
   profiles.StartProfiling("", 1);
   ProfileGenerator generator(&profiles);
=======================================
--- /branches/bleeding_edge/tools/gyp/v8.gyp    Fri Apr  9 00:29:58 2010
+++ /branches/bleeding_edge/tools/gyp/v8.gyp    Mon Apr 12 00:23:43 2010
@@ -38,6 +38,7 @@
       'ENABLE_LOGGING_AND_PROFILING',
       'ENABLE_DEBUGGER_SUPPORT',
       'ENABLE_VMSTATE_TRACKING',
+      'ENABLE_CPP_PROFILES_PROCESSOR',
     ],
     'conditions': [
       ['target_arch=="arm"', {

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

To unsubscribe, reply using "remove me" as the subject.

Reply via email to