Isolatify CPU profiler public API
authoryurys@chromium.org <yurys@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Tue, 2 Apr 2013 08:16:53 +0000 (08:16 +0000)
committeryurys@chromium.org <yurys@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Tue, 2 Apr 2013 08:16:53 +0000 (08:16 +0000)
Relanding r14006 and r14009 that were reverted in r14031

TBR=danno
BUG=None

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

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

include/v8-profiler.h
include/v8.h
src/api.cc
src/log.h
test/cctest/test-cpu-profiler.cc
test/cctest/test-profile-generator.cc

index 34e2dbedd66b8ea2b461b7814bf52ecf5267180e..68f377c606b276d5459b367cb4b4ac074781f1e6 100644 (file)
@@ -135,7 +135,7 @@ class V8EXPORT CpuProfile {
 
   /**
     * Returns number of samples recorded. The samples are not recorded unless
-    * |record_samples| parameter of CpuProfiler::StartProfiling is true.
+    * |record_samples| parameter of CpuProfiler::StartCpuProfiling is true.
     */
   int GetSamplesCount() const;
 
@@ -158,7 +158,8 @@ class V8EXPORT CpuProfile {
 
 
 /**
- * Interface for controlling CPU profiling.
+ * Interface for controlling CPU profiling. Instance of the
+ * profiler can be retrieved using v8::Isolate::GetCpuProfiler.
  */
 class V8EXPORT CpuProfiler {
  public:
@@ -171,22 +172,34 @@ class V8EXPORT CpuProfiler {
    * obtaining profiling results.
    */
 
+  /** Deprecated. Use GetProfileCount instead. */
+  static int GetProfilesCount();
   /**
    * Returns the number of profiles collected (doesn't include
    * profiles that are being collected at the moment of call.)
    */
-  static int GetProfilesCount();
+  int GetProfileCount();
 
-  /** Returns a profile by index. */
+  /** Deprecated. Use GetCpuProfile instead. */
   static const CpuProfile* GetProfile(
       int index,
       Handle<Value> security_token = Handle<Value>());
+  /** Returns a profile by index. */
+  const CpuProfile* GetCpuProfile(
+      int index,
+      Handle<Value> security_token = Handle<Value>());
 
-  /** Returns a profile by uid. */
+  /** Deprecated. Use FindProfile instead. */
   static const CpuProfile* FindProfile(
       unsigned uid,
       Handle<Value> security_token = Handle<Value>());
+  /** Returns a profile by uid. */
+  const CpuProfile* FindCpuProfile(
+      unsigned uid,
+      Handle<Value> security_token = Handle<Value>());
 
+  /** Deprecated. Use StartCpuProfiling instead. */
+  static void StartProfiling(Handle<String> title, bool record_samples = false);
   /**
    * Starts collecting CPU profile. Title may be an empty string. It
    * is allowed to have several profiles being collected at
@@ -198,22 +211,34 @@ class V8EXPORT CpuProfiler {
    * |record_samples| parameter controls whether individual samples should
    * be recorded in addition to the aggregated tree.
    */
-  static void StartProfiling(Handle<String> title, bool record_samples = false);
+  void StartCpuProfiling(Handle<String> title, bool record_samples = false);
 
+  /** Deprecated. Use StopCpuProfiling instead. */
+  static const CpuProfile* StopProfiling(
+      Handle<String> title,
+      Handle<Value> security_token = Handle<Value>());
   /**
    * Stops collecting CPU profile with a given title and returns it.
    * If the title given is empty, finishes the last profile started.
    */
-  static const CpuProfile* StopProfiling(
+  const CpuProfile* StopCpuProfiling(
       Handle<String> title,
       Handle<Value> security_token = Handle<Value>());
 
+  /** Deprecated. Use DeleteAllCpuProfiles instead. */
+  static void DeleteAllProfiles();
   /**
    * Deletes all existing profiles, also cancelling all profiling
    * activity.  All previously returned pointers to profiles and their
    * contents become invalid after this call.
    */
-  static void DeleteAllProfiles();
+  void DeleteAllCpuProfiles();
+
+ private:
+  CpuProfiler();
+  ~CpuProfiler();
+  CpuProfiler(const CpuProfiler&);
+  CpuProfiler& operator=(const CpuProfiler&);
 };
 
 
index e65999fec6aacd87f4f891b193e3ef6db3e2b592..9adb1c041cf20bd2ed12d7a04dfe07c53cd1712b 100644 (file)
@@ -103,6 +103,7 @@ class Array;
 class Boolean;
 class BooleanObject;
 class Context;
+class CpuProfiler;
 class Data;
 class Date;
 class DeclaredAccessorDescriptor;
@@ -3029,6 +3030,12 @@ class V8EXPORT Isolate {
    */
   HeapProfiler* GetHeapProfiler();
 
+  /**
+   * Returns CPU profiler for this isolate. Will return NULL until the isolate
+   * is initialized.
+   */
+  CpuProfiler* GetCpuProfiler();
+
  private:
   Isolate();
   Isolate(const Isolate&);
index e378f3290e3d9050acf682cd912001e7b46ffa92..65663ba5c66c6dfcb1e210d31949fa59ed642d71 100644 (file)
@@ -5806,6 +5806,13 @@ HeapProfiler* Isolate::GetHeapProfiler() {
 }
 
 
+CpuProfiler* Isolate::GetCpuProfiler() {
+  i::CpuProfiler* cpu_profiler =
+      reinterpret_cast<i::Isolate*>(this)->cpu_profiler();
+  return reinterpret_cast<CpuProfiler*>(cpu_profiler);
+}
+
+
 void V8::SetGlobalGCPrologueCallback(GCCallback callback) {
   i::Isolate* isolate = i::Isolate::Current();
   if (IsDeadCheck(isolate, "v8::V8::SetGlobalGCPrologueCallback()")) return;
@@ -6539,6 +6546,11 @@ int CpuProfiler::GetProfilesCount() {
 }
 
 
+int CpuProfiler::GetProfileCount() {
+  return reinterpret_cast<i::CpuProfiler*>(this)->GetProfilesCount();
+}
+
+
 const CpuProfile* CpuProfiler::GetProfile(int index,
                                           Handle<Value> security_token) {
   i::Isolate* isolate = i::Isolate::Current();
@@ -6552,6 +6564,15 @@ const CpuProfile* CpuProfiler::GetProfile(int index,
 }
 
 
+const CpuProfile* CpuProfiler::GetCpuProfile(int index,
+                                             Handle<Value> security_token) {
+  return reinterpret_cast<const CpuProfile*>(
+      reinterpret_cast<i::CpuProfiler*>(this)->GetProfile(
+          security_token.IsEmpty() ? NULL : *Utils::OpenHandle(*security_token),
+          index));
+}
+
+
 const CpuProfile* CpuProfiler::FindProfile(unsigned uid,
                                            Handle<Value> security_token) {
   i::Isolate* isolate = i::Isolate::Current();
@@ -6565,6 +6586,15 @@ const CpuProfile* CpuProfiler::FindProfile(unsigned uid,
 }
 
 
+const CpuProfile* CpuProfiler::FindCpuProfile(unsigned uid,
+                                              Handle<Value> security_token) {
+  return reinterpret_cast<const CpuProfile*>(
+      reinterpret_cast<i::CpuProfiler*>(this)->FindProfile(
+          security_token.IsEmpty() ? NULL : *Utils::OpenHandle(*security_token),
+          uid));
+}
+
+
 void CpuProfiler::StartProfiling(Handle<String> title, bool record_samples) {
   i::Isolate* isolate = i::Isolate::Current();
   IsDeadCheck(isolate, "v8::CpuProfiler::StartProfiling");
@@ -6574,6 +6604,12 @@ void CpuProfiler::StartProfiling(Handle<String> title, bool record_samples) {
 }
 
 
+void CpuProfiler::StartCpuProfiling(Handle<String> title, bool record_samples) {
+  reinterpret_cast<i::CpuProfiler*>(this)->StartProfiling(
+      *Utils::OpenHandle(*title), record_samples);
+}
+
+
 const CpuProfile* CpuProfiler::StopProfiling(Handle<String> title,
                                              Handle<Value> security_token) {
   i::Isolate* isolate = i::Isolate::Current();
@@ -6587,6 +6623,15 @@ const CpuProfile* CpuProfiler::StopProfiling(Handle<String> title,
 }
 
 
+const CpuProfile* CpuProfiler::StopCpuProfiling(Handle<String> title,
+                                                Handle<Value> security_token) {
+  return reinterpret_cast<const CpuProfile*>(
+      reinterpret_cast<i::CpuProfiler*>(this)->StopProfiling(
+          security_token.IsEmpty() ? NULL : *Utils::OpenHandle(*security_token),
+          *Utils::OpenHandle(*title)));
+}
+
+
 void CpuProfiler::DeleteAllProfiles() {
   i::Isolate* isolate = i::Isolate::Current();
   IsDeadCheck(isolate, "v8::CpuProfiler::DeleteAllProfiles");
@@ -6596,6 +6641,11 @@ void CpuProfiler::DeleteAllProfiles() {
 }
 
 
+void CpuProfiler::DeleteAllCpuProfiles() {
+  reinterpret_cast<i::CpuProfiler*>(this)->DeleteAllProfiles();
+}
+
+
 static i::HeapGraphEdge* ToInternal(const HeapGraphEdge* edge) {
   return const_cast<i::HeapGraphEdge*>(
       reinterpret_cast<const i::HeapGraphEdge*>(edge));
index 5c121bc31683ab694701838d73875cc89afe6593..a5eddc7a34541e289dd8ec5dd8f8df4837add157 100644 (file)
--- a/src/log.h
+++ b/src/log.h
@@ -77,6 +77,7 @@ class Semaphore;
 class Ticker;
 class Isolate;
 class PositionsRecorder;
+class CpuProfiler;
 
 #undef LOG
 #define LOG(isolate, Call)                          \
index 04d7ecb4f496246b1cca3a3429194e0e2abe0132..0bf80003f86e0dbbc3fbb053965a38d953b89a5b 100644 (file)
@@ -300,100 +300,102 @@ TEST(DeleteAllCpuProfiles) {
 TEST(DeleteCpuProfile) {
   LocalContext env;
   v8::HandleScope scope(env->GetIsolate());
+  v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler();
 
-  CHECK_EQ(0, v8::CpuProfiler::GetProfilesCount());
+  CHECK_EQ(0, cpu_profiler->GetProfileCount());
   v8::Local<v8::String> name1 = v8::String::New("1");
-  v8::CpuProfiler::StartProfiling(name1);
-  const v8::CpuProfile* p1 = v8::CpuProfiler::StopProfiling(name1);
+  cpu_profiler->StartCpuProfiling(name1);
+  const v8::CpuProfile* p1 = cpu_profiler->StopCpuProfiling(name1);
   CHECK_NE(NULL, p1);
-  CHECK_EQ(1, v8::CpuProfiler::GetProfilesCount());
+  CHECK_EQ(1, cpu_profiler->GetProfileCount());
   unsigned uid1 = p1->GetUid();
-  CHECK_EQ(p1, v8::CpuProfiler::FindProfile(uid1));
+  CHECK_EQ(p1, cpu_profiler->FindCpuProfile(uid1));
   const_cast<v8::CpuProfile*>(p1)->Delete();
-  CHECK_EQ(0, v8::CpuProfiler::GetProfilesCount());
-  CHECK_EQ(NULL, v8::CpuProfiler::FindProfile(uid1));
+  CHECK_EQ(0, cpu_profiler->GetProfileCount());
+  CHECK_EQ(NULL, cpu_profiler->FindCpuProfile(uid1));
 
   v8::Local<v8::String> name2 = v8::String::New("2");
-  v8::CpuProfiler::StartProfiling(name2);
-  const v8::CpuProfile* p2 = v8::CpuProfiler::StopProfiling(name2);
+  cpu_profiler->StartCpuProfiling(name2);
+  const v8::CpuProfile* p2 = cpu_profiler->StopCpuProfiling(name2);
   CHECK_NE(NULL, p2);
-  CHECK_EQ(1, v8::CpuProfiler::GetProfilesCount());
+  CHECK_EQ(1, cpu_profiler->GetProfileCount());
   unsigned uid2 = p2->GetUid();
   CHECK_NE(static_cast<int>(uid1), static_cast<int>(uid2));
-  CHECK_EQ(p2, v8::CpuProfiler::FindProfile(uid2));
-  CHECK_EQ(NULL, v8::CpuProfiler::FindProfile(uid1));
+  CHECK_EQ(p2, cpu_profiler->FindCpuProfile(uid2));
+  CHECK_EQ(NULL, cpu_profiler->FindCpuProfile(uid1));
   v8::Local<v8::String> name3 = v8::String::New("3");
-  v8::CpuProfiler::StartProfiling(name3);
-  const v8::CpuProfile* p3 = v8::CpuProfiler::StopProfiling(name3);
+  cpu_profiler->StartCpuProfiling(name3);
+  const v8::CpuProfile* p3 = cpu_profiler->StopCpuProfiling(name3);
   CHECK_NE(NULL, p3);
-  CHECK_EQ(2, v8::CpuProfiler::GetProfilesCount());
+  CHECK_EQ(2, cpu_profiler->GetProfileCount());
   unsigned uid3 = p3->GetUid();
   CHECK_NE(static_cast<int>(uid1), static_cast<int>(uid3));
-  CHECK_EQ(p3, v8::CpuProfiler::FindProfile(uid3));
-  CHECK_EQ(NULL, v8::CpuProfiler::FindProfile(uid1));
+  CHECK_EQ(p3, cpu_profiler->FindCpuProfile(uid3));
+  CHECK_EQ(NULL, cpu_profiler->FindCpuProfile(uid1));
   const_cast<v8::CpuProfile*>(p2)->Delete();
-  CHECK_EQ(1, v8::CpuProfiler::GetProfilesCount());
-  CHECK_EQ(NULL, v8::CpuProfiler::FindProfile(uid2));
-  CHECK_EQ(p3, v8::CpuProfiler::FindProfile(uid3));
+  CHECK_EQ(1, cpu_profiler->GetProfileCount());
+  CHECK_EQ(NULL, cpu_profiler->FindCpuProfile(uid2));
+  CHECK_EQ(p3, cpu_profiler->FindCpuProfile(uid3));
   const_cast<v8::CpuProfile*>(p3)->Delete();
-  CHECK_EQ(0, v8::CpuProfiler::GetProfilesCount());
-  CHECK_EQ(NULL, v8::CpuProfiler::FindProfile(uid3));
-  CHECK_EQ(NULL, v8::CpuProfiler::FindProfile(uid2));
-  CHECK_EQ(NULL, v8::CpuProfiler::FindProfile(uid1));
+  CHECK_EQ(0, cpu_profiler->GetProfileCount());
+  CHECK_EQ(NULL, cpu_profiler->FindCpuProfile(uid3));
+  CHECK_EQ(NULL, cpu_profiler->FindCpuProfile(uid2));
+  CHECK_EQ(NULL, cpu_profiler->FindCpuProfile(uid1));
 }
 
 
 TEST(DeleteCpuProfileDifferentTokens) {
   LocalContext env;
   v8::HandleScope scope(env->GetIsolate());
+  v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler();
 
-  CHECK_EQ(0, v8::CpuProfiler::GetProfilesCount());
+  CHECK_EQ(0, cpu_profiler->GetProfileCount());
   v8::Local<v8::String> name1 = v8::String::New("1");
-  v8::CpuProfiler::StartProfiling(name1);
-  const v8::CpuProfile* p1 = v8::CpuProfiler::StopProfiling(name1);
+  cpu_profiler->StartCpuProfiling(name1);
+  const v8::CpuProfile* p1 = cpu_profiler->StopCpuProfiling(name1);
   CHECK_NE(NULL, p1);
-  CHECK_EQ(1, v8::CpuProfiler::GetProfilesCount());
+  CHECK_EQ(1, cpu_profiler->GetProfileCount());
   unsigned uid1 = p1->GetUid();
-  CHECK_EQ(p1, v8::CpuProfiler::FindProfile(uid1));
+  CHECK_EQ(p1, cpu_profiler->FindCpuProfile(uid1));
   v8::Local<v8::String> token1 = v8::String::New("token1");
-  const v8::CpuProfile* p1_t1 = v8::CpuProfiler::FindProfile(uid1, token1);
+  const v8::CpuProfile* p1_t1 = cpu_profiler->FindCpuProfile(uid1, token1);
   CHECK_NE(NULL, p1_t1);
   CHECK_NE(p1, p1_t1);
-  CHECK_EQ(1, v8::CpuProfiler::GetProfilesCount());
+  CHECK_EQ(1, cpu_profiler->GetProfileCount());
   const_cast<v8::CpuProfile*>(p1)->Delete();
-  CHECK_EQ(0, v8::CpuProfiler::GetProfilesCount());
-  CHECK_EQ(NULL, v8::CpuProfiler::FindProfile(uid1));
-  CHECK_EQ(NULL, v8::CpuProfiler::FindProfile(uid1, token1));
+  CHECK_EQ(0, cpu_profiler->GetProfileCount());
+  CHECK_EQ(NULL, cpu_profiler->FindCpuProfile(uid1));
+  CHECK_EQ(NULL, cpu_profiler->FindCpuProfile(uid1, token1));
   const_cast<v8::CpuProfile*>(p1_t1)->Delete();
-  CHECK_EQ(0, v8::CpuProfiler::GetProfilesCount());
+  CHECK_EQ(0, cpu_profiler->GetProfileCount());
 
   v8::Local<v8::String> name2 = v8::String::New("2");
-  v8::CpuProfiler::StartProfiling(name2);
+  cpu_profiler->StartCpuProfiling(name2);
   v8::Local<v8::String> token2 = v8::String::New("token2");
-  const v8::CpuProfile* p2_t2 = v8::CpuProfiler::StopProfiling(name2, token2);
+  const v8::CpuProfile* p2_t2 = cpu_profiler->StopCpuProfiling(name2, token2);
   CHECK_NE(NULL, p2_t2);
-  CHECK_EQ(1, v8::CpuProfiler::GetProfilesCount());
+  CHECK_EQ(1, cpu_profiler->GetProfileCount());
   unsigned uid2 = p2_t2->GetUid();
   CHECK_NE(static_cast<int>(uid1), static_cast<int>(uid2));
-  const v8::CpuProfile* p2 = v8::CpuProfiler::FindProfile(uid2);
+  const v8::CpuProfile* p2 = cpu_profiler->FindCpuProfile(uid2);
   CHECK_NE(p2_t2, p2);
   v8::Local<v8::String> name3 = v8::String::New("3");
-  v8::CpuProfiler::StartProfiling(name3);
-  const v8::CpuProfile* p3 = v8::CpuProfiler::StopProfiling(name3);
+  cpu_profiler->StartCpuProfiling(name3);
+  const v8::CpuProfile* p3 = cpu_profiler->StopCpuProfiling(name3);
   CHECK_NE(NULL, p3);
-  CHECK_EQ(2, v8::CpuProfiler::GetProfilesCount());
+  CHECK_EQ(2, cpu_profiler->GetProfileCount());
   unsigned uid3 = p3->GetUid();
   CHECK_NE(static_cast<int>(uid1), static_cast<int>(uid3));
-  CHECK_EQ(p3, v8::CpuProfiler::FindProfile(uid3));
+  CHECK_EQ(p3, cpu_profiler->FindCpuProfile(uid3));
   const_cast<v8::CpuProfile*>(p2_t2)->Delete();
-  CHECK_EQ(1, v8::CpuProfiler::GetProfilesCount());
-  CHECK_EQ(NULL, v8::CpuProfiler::FindProfile(uid2));
-  CHECK_EQ(p3, v8::CpuProfiler::FindProfile(uid3));
+  CHECK_EQ(1, cpu_profiler->GetProfileCount());
+  CHECK_EQ(NULL, cpu_profiler->FindCpuProfile(uid2));
+  CHECK_EQ(p3, cpu_profiler->FindCpuProfile(uid3));
   const_cast<v8::CpuProfile*>(p2)->Delete();
-  CHECK_EQ(1, v8::CpuProfiler::GetProfilesCount());
-  CHECK_EQ(NULL, v8::CpuProfiler::FindProfile(uid2));
-  CHECK_EQ(p3, v8::CpuProfiler::FindProfile(uid3));
+  CHECK_EQ(1, cpu_profiler->GetProfileCount());
+  CHECK_EQ(NULL, cpu_profiler->FindCpuProfile(uid2));
+  CHECK_EQ(p3, cpu_profiler->FindCpuProfile(uid3));
   const_cast<v8::CpuProfile*>(p3)->Delete();
-  CHECK_EQ(0, v8::CpuProfiler::GetProfilesCount());
-  CHECK_EQ(NULL, v8::CpuProfiler::FindProfile(uid3));
+  CHECK_EQ(0, cpu_profiler->GetProfileCount());
+  CHECK_EQ(NULL, cpu_profiler->FindCpuProfile(uid3));
 }
index 864229ee305e0c327b40209a39b059904f5719c9..56b1788a85132140bfeb0d7bea36479f0f0deb36 100644 (file)
@@ -830,20 +830,22 @@ v8::Handle<v8::FunctionTemplate> ProfilerExtension::GetNativeFunction(
 
 v8::Handle<v8::Value> ProfilerExtension::StartProfiling(
     const v8::Arguments& args) {
+  v8::CpuProfiler* cpu_profiler = args.GetIsolate()->GetCpuProfiler();
   if (args.Length() > 0)
-    v8::CpuProfiler::StartProfiling(args[0].As<v8::String>());
+    cpu_profiler->StartCpuProfiling(args[0].As<v8::String>());
   else
-    v8::CpuProfiler::StartProfiling(v8::String::New(""));
+    cpu_profiler->StartCpuProfiling(v8::String::New(""));
   return v8::Undefined();
 }
 
 
 v8::Handle<v8::Value> ProfilerExtension::StopProfiling(
     const v8::Arguments& args) {
+  v8::CpuProfiler* cpu_profiler = args.GetIsolate()->GetCpuProfiler();
   if (args.Length() > 0)
-    v8::CpuProfiler::StopProfiling(args[0].As<v8::String>());
+    cpu_profiler->StopCpuProfiling(args[0].As<v8::String>());
   else
-    v8::CpuProfiler::StopProfiling(v8::String::New(""));
+    cpu_profiler->StopCpuProfiling(v8::String::New(""));
   return v8::Undefined();
 }