OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 the V8 project authors. All rights reserved. | |
Benedikt Meurer
2014/08/27 04:17:23
Nit: Use new short copyright header.
gholap
2014/08/27 21:48:09
Done.
| |
2 // Redistribution and use in source and binary forms, with or without | |
3 // modification, are permitted provided that the following conditions are | |
4 // met: | |
5 // | |
6 // * Redistributions of source code must retain the above copyright | |
7 // notice, this list of conditions and the following disclaimer. | |
8 // * Redistributions in binary form must reproduce the above | |
9 // copyright notice, this list of conditions and the following | |
10 // disclaimer in the documentation and/or other materials provided | |
11 // with the distribution. | |
12 // * Neither the name of Google Inc. nor the names of its | |
13 // contributors may be used to endorse or promote products derived | |
14 // from this software without specific prior written permission. | |
15 // | |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
27 // | |
28 // Tests the public sampling API in include/v8-sampler.h | |
29 | |
30 #include "include/v8.h" | |
31 #include "include/v8-sampler.h" | |
32 | |
33 #include "src/v8.h" | |
34 | |
35 #include "src/base/platform/platform.h" | |
36 | |
37 #include "test/cctest/cctest.h" | |
38 | |
39 using v8::Local; | |
40 using v8::Script; | |
41 | |
42 using v8::internal::Isolate; | |
43 | |
44 namespace SamplerTester { | |
Benedikt Meurer
2014/08/27 04:17:23
Use an anonymous namespace instead.
gholap
2014/08/27 21:48:09
Done.
| |
45 // sampler thread waits on this semaphore. | |
46 v8::base::Semaphore* SamplerSemaphore = new v8::base::Semaphore(0); | |
47 | |
48 // V8 thread (the JavaScript code) waits on this semaphore. | |
49 v8::base::Semaphore* V8Semaphore = new v8::base::Semaphore(0); | |
50 | |
51 // The JavaScript calls this function when an full stack depth. | |
52 void SignalAndWaitForSampler( | |
53 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
54 // Tell the sampler that it can take a sample now. | |
55 SamplerSemaphore->Signal(); | |
56 | |
57 // Wait for the sampler to finish collecting a sample. | |
58 V8Semaphore->Wait(); | |
59 } | |
60 } | |
61 | |
62 | |
63 // A thread which collects samples from v8. | |
64 class SamplerThread : public v8::base::Thread { | |
Benedikt Meurer
2014/08/27 04:17:23
Nit: Mark as V8_FINAL.
gholap
2014/08/27 21:48:09
Done.
| |
65 public: | |
66 explicit SamplerThread(v8::Isolate* isolate); | |
67 virtual ~SamplerThread() {} | |
68 int sampleStackSize; | |
69 | |
70 virtual void Run(); | |
71 void Stop(); | |
72 | |
73 private: | |
74 v8::Isolate* isolate_; | |
75 }; | |
76 | |
77 static const int kSamplerThreadStackSize = 64 * 1024; | |
Benedikt Meurer
2014/08/27 04:17:24
There should be no need to manually override speci
gholap
2014/08/27 21:48:09
Done.
| |
78 | |
79 SamplerThread::SamplerThread(v8::Isolate* isolate) | |
Benedikt Meurer
2014/08/27 04:17:23
Nit: Inline method definition into declaration abo
gholap
2014/08/27 21:48:09
Done.
| |
80 : Thread(v8::base::Thread::Options("sampler-api-tester", | |
81 kSamplerThreadStackSize)), | |
82 sampleStackSize(0), | |
83 isolate_(isolate) { | |
84 } | |
85 | |
86 | |
87 void SamplerThread::Stop() { | |
Benedikt Meurer
2014/08/27 04:17:23
Nit: Remove this method, there's no advantage over
gholap
2014/08/27 21:48:09
Done.
| |
88 Join(); | |
89 } | |
90 | |
91 | |
92 void SamplerThread::Run() { | |
Benedikt Meurer
2014/08/27 04:17:23
Nit: Inline method definition into declaration abo
gholap
2014/08/27 21:48:09
Done.
| |
93 v8::Sample sample; | |
94 | |
95 // Wait for JS to reach full stack depth. | |
96 SamplerTester::SamplerSemaphore->Wait(); | |
97 | |
98 v8::Sample* sample_ptr = &sample; | |
Benedikt Meurer
2014/08/27 04:17:23
Replace with something like:
v8::Sample sample;
C
gholap
2014/08/27 21:48:09
Changed the API so that sample.frames_count is zer
| |
99 sample_ptr = v8::Sampler::GetSample(isolate_, sample_ptr); | |
100 CHECK_NE(NULL, sample_ptr); | |
101 sampleStackSize = sample_ptr->frames_count; | |
102 | |
103 // Tell JS that sample collection is done. | |
104 SamplerTester::V8Semaphore->Signal(); | |
105 } | |
106 | |
107 | |
108 // A JavaScript program with stack depth 8. | |
109 // When at the bottom of the recursion, | |
110 // The JavaScript code calls into C++ test code, | |
111 // waiting for the sampler to take a sample. | |
112 static const char* sampler_api_test_source = "function func(count) {\n" | |
113 " if (count == 1) {\n" | |
114 " SignalAndWaitForSampler();\n" | |
115 " }\n" | |
116 " else return func(count - 1);\n" | |
117 "}\n" | |
118 "func(7);\n"; | |
119 | |
120 | |
121 // TODO(gholap): Right now, we are just checking whether GetSample | |
122 // gets samples and whether the stack depth is reasonable. | |
123 // After implementing code event listener API, add tests | |
124 // to further verify the correctness of collected samples. | |
125 TEST(StackDepthIsConsistent) { | |
126 v8::Isolate* isolate = CcTest::isolate(); | |
127 v8::HandleScope scope(isolate); | |
128 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate); | |
129 global->Set( | |
130 v8::String::NewFromUtf8(isolate, "SignalAndWaitForSampler"), | |
131 v8::FunctionTemplate::New(isolate, SamplerTester::SignalAndWaitForSampler)); | |
132 | |
133 LocalContext env(isolate, NULL, global); | |
134 Local<v8::Script> script = v8::Script::Compile( | |
135 v8::String::NewFromUtf8(isolate, sampler_api_test_source)); | |
136 SamplerThread* sampler = new SamplerThread(isolate); | |
Benedikt Meurer
2014/08/27 04:17:23
You leak the SamplerThread in this method. Just al
gholap
2014/08/27 21:48:09
Oops. Damn :(
Done.
| |
137 | |
138 sampler->Start(); | |
139 script->Run(); | |
140 sampler->Stop(); | |
141 | |
142 CHECK_EQ(8, sampler->sampleStackSize); | |
Benedikt Meurer
2014/08/27 04:17:23
Remove this CHECK_EQ here.
gholap
2014/08/27 21:48:09
Yes, removed all CHECK_* logic from the Run method
| |
143 } | |
OLD | NEW |