@@ -14,26 +14,26 @@ Profiler::Profiler()
1414{
1515}
1616
17- void Profiler::Init (const string& appName)
17+ void Profiler::Init (Isolate *isolate, const Local<Object>& globalObj, const string& appName, const string& outputDir )
1818{
19- s_appName = appName;
19+ m_appName = appName;
20+ m_outputDir = outputDir;
21+ auto extData = External::New (isolate, this );
22+ globalObj->Set (ConvertToV8String (" __startCPUProfiler" ), FunctionTemplate::New (isolate, Profiler::StartCPUProfilerCallback, extData)->GetFunction ());
23+ globalObj->Set (ConvertToV8String (" __stopCPUProfiler" ), FunctionTemplate::New (isolate, Profiler::StopCPUProfilerCallback, extData)->GetFunction ());
24+ globalObj->Set (ConvertToV8String (" __heapSnapshot" ), FunctionTemplate::New (isolate, Profiler::HeapSnapshotMethodCallback, extData)->GetFunction ());
25+ globalObj->Set (ConvertToV8String (" __startNDKProfiler" ), FunctionTemplate::New (isolate, Profiler::StartNDKProfilerCallback, extData)->GetFunction ());
26+ globalObj->Set (ConvertToV8String (" __stopNDKProfiler" ), FunctionTemplate::New (isolate, Profiler::StopNDKProfilerCallback, extData)->GetFunction ());
2027}
2128
2229void Profiler::StartCPUProfilerCallback (const v8::FunctionCallbackInfo<v8::Value>& args)
2330{
2431 try
2532 {
2633 auto isolate = args.GetIsolate ();
27- auto started = false ;
28- if ((args.Length () == 1 ) && (args[0 ]->IsString ()))
29- {
30- auto name = args[0 ]->ToString ();
31- StartCPUProfiler (isolate, name);
32- started = true ;
33- }
34-
35- args.GetReturnValue ().Set (Boolean::New (isolate, started));
36-
34+ auto extData = args.Data ().As <External>();
35+ auto thiz = static_cast <Profiler*>(extData->Value ());
36+ thiz->StartCPUProfilerCallbackImpl (args);
3737 }
3838 catch (NativeScriptException& e)
3939 {
@@ -51,18 +51,27 @@ void Profiler::StartCPUProfilerCallback(const v8::FunctionCallbackInfo<v8::Value
5151 }
5252}
5353
54+ void Profiler::StartCPUProfilerCallbackImpl (const v8::FunctionCallbackInfo<v8::Value>& args)
55+ {
56+ auto isolate = args.GetIsolate ();
57+ auto started = false ;
58+ if ((args.Length () == 1 ) && (args[0 ]->IsString ()))
59+ {
60+ auto name = args[0 ]->ToString ();
61+ StartCPUProfiler (isolate, name);
62+ started = true ;
63+ }
64+ args.GetReturnValue ().Set (started);
65+ }
66+
5467void Profiler::StopCPUProfilerCallback (const v8::FunctionCallbackInfo<v8::Value>& args)
5568{
5669 try
5770 {
5871 auto isolate = args.GetIsolate ();
59- auto stopped = false ;
60- if ((args.Length () == 1 ) && (args[0 ]->IsString ()))
61- {
62- auto name = args[0 ]->ToString ();
63- stopped = StopCPUProfiler (isolate, name);
64- }
65- args.GetReturnValue ().Set (Boolean::New (isolate, stopped));
72+ auto extData = args.Data ().As <External>();
73+ auto thiz = static_cast <Profiler*>(extData->Value ());
74+ thiz->StopCPUProfilerCallbackImpl (args);
6675 }
6776 catch (NativeScriptException& e)
6877 {
@@ -80,6 +89,18 @@ void Profiler::StopCPUProfilerCallback(const v8::FunctionCallbackInfo<v8::Value>
8089 }
8190}
8291
92+ void Profiler::StopCPUProfilerCallbackImpl (const v8::FunctionCallbackInfo<v8::Value>& args)
93+ {
94+ auto isolate = args.GetIsolate ();
95+ auto stopped = false ;
96+ if ((args.Length () == 1 ) && (args[0 ]->IsString ()))
97+ {
98+ auto name = args[0 ]->ToString ();
99+ stopped = StopCPUProfiler (isolate, name);
100+ }
101+ args.GetReturnValue ().Set (stopped);
102+ }
103+
83104void Profiler::StartCPUProfiler (Isolate *isolate, const Local<String>& name)
84105{
85106 auto v8prof = isolate->GetCpuProfiler ();
@@ -113,7 +134,7 @@ bool Profiler::Write(CpuProfile *cpuProfile)
113134
114135 char filename[256 ];
115136 auto profileName = ConvertToString (cpuProfile->GetTitle ());
116- snprintf (filename, sizeof (filename), " /sdcard/ %s-%s-%lu.%lu.cpuprofile" , s_appName .c_str (), profileName.c_str (), sec, usec);
137+ snprintf (filename, sizeof (filename), " %s/ %s-%s-%lu.%lu.cpuprofile" , m_outputDir. c_str (), m_appName .c_str (), profileName.c_str (), sec, usec);
117138
118139 auto fp = fopen (filename, " w" );
119140 if (nullptr == fp)
@@ -202,7 +223,10 @@ void Profiler::StartNDKProfilerCallback(const v8::FunctionCallbackInfo<v8::Value
202223{
203224 try
204225 {
205- StartNDKProfiler ();
226+ auto isolate = args.GetIsolate ();
227+ auto extData = args.Data ().As <External>();
228+ auto thiz = static_cast <Profiler*>(extData->Value ());
229+ thiz->StartNDKProfiler ();
206230 }
207231 catch (NativeScriptException& e)
208232 {
@@ -224,7 +248,10 @@ void Profiler::StopNDKProfilerCallback(const v8::FunctionCallbackInfo<v8::Value>
224248{
225249 try
226250 {
227- StopNDKProfiler ();
251+ auto isolate = args.GetIsolate ();
252+ auto extData = args.Data ().As <External>();
253+ auto thiz = static_cast <Profiler*>(extData->Value ());
254+ thiz->StopNDKProfiler ();
228255 }
229256 catch (NativeScriptException& e)
230257 {
@@ -292,30 +319,10 @@ void Profiler::HeapSnapshotMethodCallback(const v8::FunctionCallbackInfo<v8::Val
292319{
293320 try
294321 {
295- struct timespec nowt;
296- clock_gettime (CLOCK_MONOTONIC, &nowt);
297- uint64_t now = (int64_t ) nowt.tv_sec * 1000000000LL + nowt.tv_nsec ;
298-
299- unsigned long sec = static_cast <unsigned long >(now / 1000000 );
300- unsigned long usec = static_cast <unsigned long >(now % 1000000 );
301-
302- char filename[256 ];
303- snprintf (filename, sizeof (filename), " /sdcard/%s-heapdump-%lu.%lu.heapsnapshot" , s_appName.c_str (), sec, usec);
304-
305- FILE* fp = fopen (filename, " w" );
306- if (fp == nullptr )
307- {
308- return ;
309- }
310-
311322 auto isolate = args.GetIsolate ();
312-
313- const HeapSnapshot* snap = isolate->GetHeapProfiler ()->TakeHeapSnapshot ();
314-
315- FileOutputStream stream (fp);
316- snap->Serialize (&stream, HeapSnapshot::kJSON );
317- fclose (fp);
318- const_cast <HeapSnapshot*>(snap)->Delete ();
323+ auto extData = args.Data ().As <External>();
324+ auto thiz = static_cast <Profiler*>(extData->Value ());
325+ thiz->HeapSnapshotMethodCallbackImpl (args);
319326 }
320327 catch (NativeScriptException& e)
321328 {
@@ -333,4 +340,31 @@ void Profiler::HeapSnapshotMethodCallback(const v8::FunctionCallbackInfo<v8::Val
333340 }
334341}
335342
336- string Profiler::s_appName;
343+ void Profiler::HeapSnapshotMethodCallbackImpl (const v8::FunctionCallbackInfo<v8::Value>& args)
344+ {
345+ struct timespec nowt;
346+ clock_gettime (CLOCK_MONOTONIC, &nowt);
347+ uint64_t now = (int64_t ) nowt.tv_sec * 1000000000LL + nowt.tv_nsec ;
348+
349+ unsigned long sec = static_cast <unsigned long >(now / 1000000 );
350+ unsigned long usec = static_cast <unsigned long >(now % 1000000 );
351+
352+ char filename[256 ];
353+ snprintf (filename, sizeof (filename), " %s/%s-heapdump-%lu.%lu.heapsnapshot" , m_outputDir.c_str (), m_appName.c_str (), sec, usec);
354+
355+ FILE* fp = fopen (filename, " w" );
356+ if (fp == nullptr )
357+ {
358+ return ;
359+ }
360+
361+ auto isolate = args.GetIsolate ();
362+
363+ const HeapSnapshot* snap = isolate->GetHeapProfiler ()->TakeHeapSnapshot ();
364+
365+ FileOutputStream stream (fp);
366+ snap->Serialize (&stream, HeapSnapshot::kJSON );
367+ fclose (fp);
368+ const_cast <HeapSnapshot*>(snap)->Delete ();
369+ }
370+
0 commit comments