OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 the V8 project authors. All rights reserved. | |
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 #if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) \ | |
29 || defined(__NetBSD__) || defined(__sun) | |
Sven Panne
2013/04/15 07:08:40
In general, I'd like to move towards feature-based
yurys
2013/04/15 11:37:55
I'd first merge all implementations and have them
| |
30 | |
31 #include <errno.h> | |
32 #include <pthread.h> | |
33 #include <signal.h> | |
34 #include <sys/time.h> | |
35 #include <sys/syscall.h> | |
36 #include <ucontext.h> | |
37 #include <unistd.h> | |
38 | |
39 #endif | |
40 | |
41 // GLibc on ARM defines mcontext_t has a typedef for 'struct sigcontext'. | |
42 // Old versions of the C library <signal.h> didn't define the type. | |
43 #if defined(__ANDROID__) && !defined(__BIONIC_HAVE_UCONTEXT_T) && \ | |
44 defined(__arm__) && !defined(__BIONIC_HAVE_STRUCT_SIGCONTEXT) | |
45 #include <asm/sigcontext.h> | |
46 #endif | |
47 | |
48 #include "v8.h" | |
49 | |
50 #include "log.h" | |
51 #include "platform.h" | |
52 #include "simulator.h" | |
53 #include "v8threads.h" | |
54 | |
55 | |
56 namespace v8 { | |
57 namespace internal { | |
58 | |
59 | |
60 #if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) \ | |
61 || defined(__NetBSD__) || defined(__sun) | |
62 | |
63 #if defined(__ANDROID__) && !defined(__BIONIC_HAVE_UCONTEXT_T) | |
64 | |
65 // Not all versions of Android's C library provide ucontext_t. | |
66 // Detect this and provide custom but compatible definitions. Note that these | |
67 // follow the GLibc naming convention to access register values from | |
68 // mcontext_t. | |
69 // | |
70 // See http://code.google.com/p/android/issues/detail?id=34784 | |
71 | |
72 #if defined(__arm__) | |
73 | |
74 typedef struct sigcontext mcontext_t; | |
75 | |
76 typedef struct ucontext { | |
77 uint32_t uc_flags; | |
78 struct ucontext* uc_link; | |
79 stack_t uc_stack; | |
80 mcontext_t uc_mcontext; | |
81 // Other fields are not used by V8, don't define them here. | |
82 } ucontext_t; | |
83 | |
84 #elif defined(__mips__) | |
85 // MIPS version of sigcontext, for Android bionic. | |
86 typedef struct { | |
87 uint32_t regmask; | |
88 uint32_t status; | |
89 uint64_t pc; | |
90 uint64_t gregs[32]; | |
91 uint64_t fpregs[32]; | |
92 uint32_t acx; | |
93 uint32_t fpc_csr; | |
94 uint32_t fpc_eir; | |
95 uint32_t used_math; | |
96 uint32_t dsp; | |
97 uint64_t mdhi; | |
98 uint64_t mdlo; | |
99 uint32_t hi1; | |
100 uint32_t lo1; | |
101 uint32_t hi2; | |
102 uint32_t lo2; | |
103 uint32_t hi3; | |
104 uint32_t lo3; | |
105 } mcontext_t; | |
106 | |
107 typedef struct ucontext { | |
108 uint32_t uc_flags; | |
109 struct ucontext* uc_link; | |
110 stack_t uc_stack; | |
111 mcontext_t uc_mcontext; | |
112 // Other fields are not used by V8, don't define them here. | |
113 } ucontext_t; | |
114 | |
115 #elif defined(__i386__) | |
116 // x86 version for Android. | |
117 typedef struct { | |
118 uint32_t gregs[19]; | |
119 void* fpregs; | |
120 uint32_t oldmask; | |
121 uint32_t cr2; | |
122 } mcontext_t; | |
123 | |
124 typedef uint32_t kernel_sigset_t[2]; // x86 kernel uses 64-bit signal masks | |
125 typedef struct ucontext { | |
126 uint32_t uc_flags; | |
127 struct ucontext* uc_link; | |
128 stack_t uc_stack; | |
129 mcontext_t uc_mcontext; | |
130 // Other fields are not used by V8, don't define them here. | |
131 } ucontext_t; | |
132 enum { REG_EBP = 6, REG_ESP = 7, REG_EIP = 14 }; | |
133 #endif | |
134 | |
135 #endif // __ANDROID__ && !defined(__BIONIC_HAVE_UCONTEXT_T) | |
136 | |
137 | |
138 static pthread_t GetThreadID() { | |
139 #if defined(__ANDROID__) | |
140 // Android's C library provides gettid(2). | |
141 return gettid(); | |
142 #elif defined(__linux__) | |
143 // Glibc doesn't provide a wrapper for gettid(2). | |
144 return syscall(SYS_gettid); | |
145 #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \ | |
146 || defined(__sun) | |
147 return pthread_self(); | |
148 #endif | |
149 } | |
150 | |
151 | |
152 class Sampler::PlatformData : public Malloced { | |
153 public: | |
154 PlatformData() | |
155 : vm_tid_(GetThreadID()), | |
156 profiled_thread_id_(ThreadId::Current()) {} | |
157 | |
158 pthread_t vm_tid() const { return vm_tid_; } | |
159 ThreadId profiled_thread_id() { return profiled_thread_id_; } | |
160 | |
161 private: | |
162 pthread_t vm_tid_; | |
163 ThreadId profiled_thread_id_; | |
164 }; | |
165 | |
166 | |
167 static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) { | |
168 #if defined(__native_client__) | |
169 // As Native Client does not support signal handling, profiling | |
170 // is disabled. | |
171 return; | |
172 #else | |
173 USE(info); | |
174 if (signal != SIGPROF) return; | |
175 Isolate* isolate = Isolate::UncheckedCurrent(); | |
176 if (isolate == NULL || !isolate->IsInitialized() || !isolate->IsInUse()) { | |
177 // We require a fully initialized and entered isolate. | |
178 return; | |
179 } | |
180 if (v8::Locker::IsActive() && | |
181 !isolate->thread_manager()->IsLockedByCurrentThread()) { | |
182 return; | |
183 } | |
184 | |
185 Sampler* sampler = isolate->logger()->sampler(); | |
186 if (sampler == NULL || !sampler->IsActive()) return; | |
187 | |
188 #if defined(USE_SIMULATOR) | |
189 #if V8_TARGET_ARCH_ARM || V8_TARGET_ARCH_MIPS | |
190 ThreadId thread_id = sampler->platform_data()->profiled_thread_id(); | |
191 Isolate::PerIsolateThreadData* per_thread_data = isolate-> | |
192 FindPerThreadDataForThread(thread_id); | |
193 if (!per_thread_data) return; | |
194 Simulator* sim = per_thread_data->simulator(); | |
195 // Check if there is active simulator before allocating TickSample. | |
196 if (!sim) return; | |
197 #endif | |
198 #endif // USE_SIMULATOR | |
199 | |
200 TickSample sample_obj; | |
201 TickSample* sample = isolate->cpu_profiler()->TickSampleEvent(); | |
202 if (sample == NULL) sample = &sample_obj; | |
203 | |
204 #if defined(USE_SIMULATOR) | |
205 #if V8_TARGET_ARCH_ARM | |
206 sample->pc = reinterpret_cast<Address>(sim->get_register(Simulator::pc)); | |
207 sample->sp = reinterpret_cast<Address>(sim->get_register(Simulator::sp)); | |
208 sample->fp = reinterpret_cast<Address>(sim->get_register(Simulator::r11)); | |
209 #elif V8_TARGET_ARCH_MIPS | |
210 sample->pc = reinterpret_cast<Address>(sim->get_register(Simulator::pc)); | |
211 sample->sp = reinterpret_cast<Address>(sim->get_register(Simulator::sp)); | |
212 sample->fp = reinterpret_cast<Address>(sim->get_register(Simulator::fp)); | |
213 #endif // V8_TARGET_ARCH_* | |
214 #else | |
215 // Extracting the sample from the context is extremely machine dependent. | |
216 ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context); | |
217 mcontext_t& mcontext = ucontext->uc_mcontext; | |
218 sample->state = isolate->current_vm_state(); | |
219 #if defined(__linux__) | |
220 #if V8_HOST_ARCH_IA32 | |
221 sample->pc = reinterpret_cast<Address>(mcontext.gregs[REG_EIP]); | |
222 sample->sp = reinterpret_cast<Address>(mcontext.gregs[REG_ESP]); | |
223 sample->fp = reinterpret_cast<Address>(mcontext.gregs[REG_EBP]); | |
224 #elif V8_HOST_ARCH_X64 | |
225 sample->pc = reinterpret_cast<Address>(mcontext.gregs[REG_RIP]); | |
226 sample->sp = reinterpret_cast<Address>(mcontext.gregs[REG_RSP]); | |
227 sample->fp = reinterpret_cast<Address>(mcontext.gregs[REG_RBP]); | |
228 #elif V8_HOST_ARCH_ARM | |
229 #if defined(__GLIBC__) && !defined(__UCLIBC__) && \ | |
230 (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3)) | |
231 // Old GLibc ARM versions used a gregs[] array to access the register | |
232 // values from mcontext_t. | |
233 sample->pc = reinterpret_cast<Address>(mcontext.gregs[R15]); | |
234 sample->sp = reinterpret_cast<Address>(mcontext.gregs[R13]); | |
235 sample->fp = reinterpret_cast<Address>(mcontext.gregs[R11]); | |
236 #else | |
237 sample->pc = reinterpret_cast<Address>(mcontext.arm_pc); | |
238 sample->sp = reinterpret_cast<Address>(mcontext.arm_sp); | |
239 sample->fp = reinterpret_cast<Address>(mcontext.arm_fp); | |
240 #endif // defined(__GLIBC__) && !defined(__UCLIBC__) && | |
241 // (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3)) | |
242 #elif V8_HOST_ARCH_MIPS | |
243 sample->pc = reinterpret_cast<Address>(mcontext.pc); | |
244 sample->sp = reinterpret_cast<Address>(mcontext.gregs[29]); | |
245 sample->fp = reinterpret_cast<Address>(mcontext.gregs[30]); | |
246 #endif // V8_HOST_ARCH_* | |
247 #elif defined(__FreeBSD__) | |
248 #if V8_HOST_ARCH_IA32 | |
249 sample->pc = reinterpret_cast<Address>(mcontext.mc_eip); | |
250 sample->sp = reinterpret_cast<Address>(mcontext.mc_esp); | |
251 sample->fp = reinterpret_cast<Address>(mcontext.mc_ebp); | |
252 #elif V8_HOST_ARCH_X64 | |
253 sample->pc = reinterpret_cast<Address>(mcontext.mc_rip); | |
254 sample->sp = reinterpret_cast<Address>(mcontext.mc_rsp); | |
255 sample->fp = reinterpret_cast<Address>(mcontext.mc_rbp); | |
256 #elif V8_HOST_ARCH_ARM | |
257 sample->pc = reinterpret_cast<Address>(mcontext.mc_r15); | |
258 sample->sp = reinterpret_cast<Address>(mcontext.mc_r13); | |
259 sample->fp = reinterpret_cast<Address>(mcontext.mc_r11); | |
260 #endif // V8_HOST_ARCH_* | |
261 #elif defined(__NetBSD__) | |
262 #if V8_HOST_ARCH_IA32 | |
263 sample->pc = reinterpret_cast<Address>(mcontext.__gregs[_REG_EIP]); | |
264 sample->sp = reinterpret_cast<Address>(mcontext.__gregs[_REG_ESP]); | |
265 sample->fp = reinterpret_cast<Address>(mcontext.__gregs[_REG_EBP]); | |
266 #elif V8_HOST_ARCH_X64 | |
267 sample->pc = reinterpret_cast<Address>(mcontext.__gregs[_REG_RIP]); | |
268 sample->sp = reinterpret_cast<Address>(mcontext.__gregs[_REG_RSP]); | |
269 sample->fp = reinterpret_cast<Address>(mcontext.__gregs[_REG_RBP]); | |
270 #endif // V8_HOST_ARCH_* | |
271 #elif defined(__OpenBSD__) | |
272 USE(mcontext); | |
273 #if V8_HOST_ARCH_IA32 | |
274 sample->pc = reinterpret_cast<Address>(ucontext->sc_eip); | |
275 sample->sp = reinterpret_cast<Address>(ucontext->sc_esp); | |
276 sample->fp = reinterpret_cast<Address>(ucontext->sc_ebp); | |
277 #elif V8_HOST_ARCH_X64 | |
278 sample->pc = reinterpret_cast<Address>(ucontext->sc_rip); | |
279 sample->sp = reinterpret_cast<Address>(ucontext->sc_rsp); | |
280 sample->fp = reinterpret_cast<Address>(ucontext->sc_rbp); | |
281 #endif // V8_HOST_ARCH_* | |
282 #elif defined(__sun) | |
283 sample->pc = reinterpret_cast<Address>(mcontext.gregs[REG_PC]); | |
284 sample->sp = reinterpret_cast<Address>(mcontext.gregs[REG_SP]); | |
285 sample->fp = reinterpret_cast<Address>(mcontext.gregs[REG_FP]); | |
286 #endif // __sun | |
287 #endif // USE_SIMULATOR | |
288 | |
289 sampler->SampleStack(sample); | |
290 sampler->Tick(sample); | |
291 #endif // __native_client__ | |
292 } | |
293 | |
294 | |
295 class SignalSender : public Thread { | |
296 public: | |
297 static const int kSignalSenderStackSize = 64 * KB; | |
298 | |
299 explicit SignalSender(int interval) | |
300 : Thread(Thread::Options("SignalSender", kSignalSenderStackSize)), | |
301 vm_tgid_(getpid()), | |
302 interval_(interval) {} | |
303 | |
304 static void SetUp() { if (!mutex_) mutex_ = OS::CreateMutex(); } | |
305 static void TearDown() { delete mutex_; } | |
306 | |
307 static void InstallSignalHandler() { | |
308 struct sigaction sa; | |
309 sa.sa_sigaction = ProfilerSignalHandler; | |
310 sigemptyset(&sa.sa_mask); | |
311 sa.sa_flags = SA_RESTART | SA_SIGINFO; | |
312 signal_handler_installed_ = | |
313 (sigaction(SIGPROF, &sa, &old_signal_handler_) == 0); | |
314 } | |
315 | |
316 static void RestoreSignalHandler() { | |
317 if (signal_handler_installed_) { | |
318 sigaction(SIGPROF, &old_signal_handler_, 0); | |
319 signal_handler_installed_ = false; | |
320 } | |
321 } | |
322 | |
323 static void AddActiveSampler(Sampler* sampler) { | |
324 ScopedLock lock(mutex_); | |
325 SamplerRegistry::AddActiveSampler(sampler); | |
326 if (instance_ == NULL) { | |
327 // Start a thread that will send SIGPROF signal to VM threads, | |
328 // when CPU profiling will be enabled. | |
329 instance_ = new SignalSender(sampler->interval()); | |
330 instance_->StartSynchronously(); | |
331 } else { | |
332 ASSERT(instance_->interval_ == sampler->interval()); | |
333 } | |
334 } | |
335 | |
336 static void RemoveActiveSampler(Sampler* sampler) { | |
337 ScopedLock lock(mutex_); | |
338 SamplerRegistry::RemoveActiveSampler(sampler); | |
339 if (SamplerRegistry::GetState() == SamplerRegistry::HAS_NO_SAMPLERS) { | |
340 instance_->Join(); | |
341 delete instance_; | |
342 instance_ = NULL; | |
343 RestoreSignalHandler(); | |
344 } | |
345 } | |
346 | |
347 // Implement Thread::Run(). | |
348 virtual void Run() { | |
349 SamplerRegistry::State state; | |
350 while ((state = SamplerRegistry::GetState()) != | |
351 SamplerRegistry::HAS_NO_SAMPLERS) { | |
352 // When CPU profiling is enabled both JavaScript and C++ code is | |
353 // profiled. We must not suspend. | |
354 if (state == SamplerRegistry::HAS_CPU_PROFILING_SAMPLERS) { | |
355 if (!signal_handler_installed_) InstallSignalHandler(); | |
356 SamplerRegistry::IterateActiveSamplers(&DoCpuProfile, this); | |
357 } else { | |
358 if (signal_handler_installed_) RestoreSignalHandler(); | |
359 } | |
360 Sleep(); // TODO(svenpanne) Figure out if OS:Sleep(interval_) is enough. | |
361 } | |
362 } | |
363 | |
364 static void DoCpuProfile(Sampler* sampler, void* raw_sender) { | |
365 if (!sampler->IsProfiling()) return; | |
366 SignalSender* sender = reinterpret_cast<SignalSender*>(raw_sender); | |
367 sender->SendProfilingSignal(sampler->platform_data()->vm_tid()); | |
368 } | |
369 | |
370 void SendProfilingSignal(int tid) { | |
371 if (!signal_handler_installed_) return; | |
372 // Glibc doesn't provide a wrapper for tgkill(2). | |
373 #if defined(ANDROID) | |
Sven Panne
2013/04/15 07:08:40
Just another good example for feautre-based #ifdef
yurys
2013/04/15 11:37:55
Let's do this in a separate change?
| |
374 syscall(__NR_tgkill, vm_tgid_, tid, SIGPROF); | |
375 #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \ | |
376 || defined(__sun) | |
377 pthread_kill(tid, SIGPROF); | |
378 #else | |
379 int result = syscall(SYS_tgkill, vm_tgid_, tid, SIGPROF); | |
380 USE(result); | |
381 ASSERT(result == 0); | |
Sven Panne
2013/04/15 07:08:40
Do we really want to assert this here and ignore t
yurys
2013/04/15 11:37:55
I have no idea whether it was intentional or not b
| |
382 #endif | |
383 } | |
384 | |
385 void Sleep() { | |
386 // Convert ms to us and subtract 100 us to compensate delays | |
387 // occuring during signal delivery. | |
388 useconds_t interval = interval_ * 1000 - 100; | |
389 #if defined(ANDROID) | |
390 usleep(interval); | |
391 #else | |
392 int result = usleep(interval); | |
393 #ifdef DEBUG | |
394 if (result != 0 && errno != EINTR) { | |
395 fprintf(stderr, | |
396 "SignalSender usleep error; interval = %u, errno = %d\n", | |
397 interval, | |
398 errno); | |
399 ASSERT(result == 0 || errno == EINTR); | |
400 } | |
401 #endif // DEBUG | |
402 USE(result); | |
403 #endif // ANDROID | |
404 } | |
405 | |
406 const int vm_tgid_; | |
407 const int interval_; | |
408 | |
409 // Protects the process wide state below. | |
410 static Mutex* mutex_; | |
411 static SignalSender* instance_; | |
412 static bool signal_handler_installed_; | |
413 static struct sigaction old_signal_handler_; | |
414 | |
415 private: | |
416 DISALLOW_COPY_AND_ASSIGN(SignalSender); | |
417 }; | |
418 | |
419 | |
420 Mutex* SignalSender::mutex_ = NULL; | |
421 SignalSender* SignalSender::instance_ = NULL; | |
422 struct sigaction SignalSender::old_signal_handler_; | |
423 bool SignalSender::signal_handler_installed_ = false; | |
424 | |
425 | |
426 void Sampler::SetUp() { | |
427 SignalSender::SetUp(); | |
428 } | |
429 | |
430 | |
431 void Sampler::TearDown() { | |
432 SignalSender::TearDown(); | |
433 } | |
434 | |
435 | |
436 Sampler::Sampler(Isolate* isolate, int interval) | |
437 : isolate_(isolate), | |
438 interval_(interval), | |
439 profiling_(false), | |
440 active_(false), | |
441 samples_taken_(0) { | |
442 data_ = new PlatformData; | |
443 } | |
444 | |
445 | |
446 Sampler::~Sampler() { | |
447 ASSERT(!IsActive()); | |
448 delete data_; | |
449 } | |
450 | |
451 void Sampler::SampleStack(TickSample* sample) { | |
452 StackTracer::Trace(isolate_, sample); | |
453 if (++samples_taken_ < 0) samples_taken_ = 0; | |
454 } | |
455 | |
456 void Sampler::Start() { | |
457 ASSERT(!IsActive()); | |
458 SetActive(true); | |
459 SignalSender::AddActiveSampler(this); | |
460 } | |
461 | |
462 | |
463 void Sampler::Stop() { | |
464 ASSERT(IsActive()); | |
465 SignalSender::RemoveActiveSampler(this); | |
466 SetActive(false); | |
467 } | |
468 | |
469 #else | |
Sven Panne
2013/04/15 07:08:40
Just out of curiosity: Do we need this dummy secti
yurys
2013/04/15 11:37:55
I will remove this section when Mac and Win implem
| |
470 | |
471 void Sampler::SetUp() { | |
472 } | |
473 | |
474 | |
475 void Sampler::TearDown() { | |
476 } | |
477 | |
478 #endif // __linux__ || _*BSD__ || __sun | |
479 | |
480 } } // namespace v8::internal | |
OLD | NEW |