blob: be6e1572dcdffe73453e92b85f325c1d7fb0927e [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// 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
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000028// Platform specific code for MacOS goes here. For the POSIX comaptible parts
29// the implementation is in platform-posix.cc.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000030
lrn@chromium.org5d00b602011-01-05 09:51:43 +000031#include <dlfcn.h>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000032#include <unistd.h>
33#include <sys/mman.h>
34#include <mach/mach_init.h>
kasperl@chromium.orge959c182009-07-27 08:59:04 +000035#include <mach-o/dyld.h>
36#include <mach-o/getsect.h>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000037
38#include <AvailabilityMacros.h>
39
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040#include <pthread.h>
41#include <semaphore.h>
42#include <signal.h>
kmillikin@chromium.org9155e252010-05-26 13:27:57 +000043#include <libkern/OSAtomic.h>
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000044#include <mach/mach.h>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000045#include <mach/semaphore.h>
46#include <mach/task.h>
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +000047#include <mach/vm_statistics.h>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000048#include <sys/time.h>
49#include <sys/resource.h>
ager@chromium.org381abbb2009-02-25 13:23:22 +000050#include <sys/types.h>
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +000051#include <sys/sysctl.h>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000052#include <stdarg.h>
53#include <stdlib.h>
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +000054#include <string.h>
ager@chromium.org381abbb2009-02-25 13:23:22 +000055#include <errno.h>
56
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000057#undef MAP_TYPE
58
59#include "v8.h"
60
61#include "platform.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000062#include "vm-state-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000063
ager@chromium.org5aa501c2009-06-23 07:57:28 +000064// Manually define these here as weak imports, rather than including execinfo.h.
65// This lets us launch on 10.4 which does not have these calls.
66extern "C" {
67 extern int backtrace(void**, int) __attribute__((weak_import));
68 extern char** backtrace_symbols(void* const*, int)
69 __attribute__((weak_import));
70 extern void backtrace_symbols_fd(void* const*, int, int)
71 __attribute__((weak_import));
72}
73
74
kasperl@chromium.org71affb52009-05-26 05:44:31 +000075namespace v8 {
76namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000077
78// 0 is never a valid thread id on MacOSX since a ptread_t is
79// a pointer.
80static const pthread_t kNoThread = (pthread_t) 0;
81
82
83double ceiling(double x) {
84 // Correct Mac OS X Leopard 'ceil' behavior.
85 if (-1.0 < x && x < 0.0) {
86 return -0.0;
87 } else {
88 return ceil(x);
89 }
90}
91
92
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000093static Mutex* limit_mutex = NULL;
94
95
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000096void OS::Setup() {
97 // Seed the random number generator.
ager@chromium.org9258b6b2008-09-11 09:11:10 +000098 // Convert the current time to a 64-bit integer first, before converting it
99 // to an unsigned. Going directly will cause an overflow and the seed to be
100 // set to all ones. The seed will be identical for different instances that
101 // call this setup code within the same millisecond.
102 uint64_t seed = static_cast<uint64_t>(TimeCurrentMillis());
103 srandom(static_cast<unsigned int>(seed));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000104 limit_mutex = CreateMutex();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000105}
106
107
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000108// We keep the lowest and highest addresses mapped as a quick way of
109// determining that pointers are outside the heap (used mostly in assertions
110// and verification). The estimate is conservative, ie, not all addresses in
111// 'allocated' space are actually allocated to our heap. The range is
112// [lowest, highest), inclusive on the low and and exclusive on the high end.
113static void* lowest_ever_allocated = reinterpret_cast<void*>(-1);
114static void* highest_ever_allocated = reinterpret_cast<void*>(0);
115
116
117static void UpdateAllocatedSpaceLimits(void* address, int size) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000118 ASSERT(limit_mutex != NULL);
119 ScopedLock lock(limit_mutex);
120
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000121 lowest_ever_allocated = Min(lowest_ever_allocated, address);
122 highest_ever_allocated =
123 Max(highest_ever_allocated,
124 reinterpret_cast<void*>(reinterpret_cast<char*>(address) + size));
125}
126
127
128bool OS::IsOutsideAllocatedSpace(void* address) {
129 return address < lowest_ever_allocated || address >= highest_ever_allocated;
130}
131
132
133size_t OS::AllocateAlignment() {
kasper.lund7276f142008-07-30 08:49:36 +0000134 return getpagesize();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000135}
136
137
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000138// Constants used for mmap.
139// kMmapFd is used to pass vm_alloc flags to tag the region with the user
140// defined tag 255 This helps identify V8-allocated regions in memory analysis
141// tools like vmmap(1).
142static const int kMmapFd = VM_MAKE_TAG(255);
143static const off_t kMmapFdOffset = 0;
144
145
kasper.lund7276f142008-07-30 08:49:36 +0000146void* OS::Allocate(const size_t requested,
147 size_t* allocated,
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000148 bool is_executable) {
kasper.lund7276f142008-07-30 08:49:36 +0000149 const size_t msize = RoundUp(requested, getpagesize());
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000150 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000151 void* mbase = mmap(NULL, msize, prot,
152 MAP_PRIVATE | MAP_ANON,
153 kMmapFd, kMmapFdOffset);
kasper.lund7276f142008-07-30 08:49:36 +0000154 if (mbase == MAP_FAILED) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000155 LOG(Isolate::Current(), StringEvent("OS::Allocate", "mmap failed"));
kasper.lund7276f142008-07-30 08:49:36 +0000156 return NULL;
157 }
158 *allocated = msize;
159 UpdateAllocatedSpaceLimits(mbase, msize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000160 return mbase;
161}
162
163
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000164void OS::Free(void* address, const size_t size) {
kasper.lund7276f142008-07-30 08:49:36 +0000165 // TODO(1240712): munmap has a return value which is ignored here.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000166 int result = munmap(address, size);
167 USE(result);
168 ASSERT(result == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000169}
170
171
ager@chromium.org32912102009-01-16 10:38:43 +0000172void OS::Sleep(int milliseconds) {
173 usleep(1000 * milliseconds);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000174}
175
176
177void OS::Abort() {
178 // Redirect to std abort to signal abnormal program termination
179 abort();
180}
181
182
kasper.lund7276f142008-07-30 08:49:36 +0000183void OS::DebugBreak() {
184 asm("int $3");
185}
186
187
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000188class PosixMemoryMappedFile : public OS::MemoryMappedFile {
189 public:
190 PosixMemoryMappedFile(FILE* file, void* memory, int size)
191 : file_(file), memory_(memory), size_(size) { }
192 virtual ~PosixMemoryMappedFile();
193 virtual void* memory() { return memory_; }
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000194 virtual int size() { return size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000195 private:
196 FILE* file_;
197 void* memory_;
198 int size_;
199};
200
201
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000202OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000203 FILE* file = fopen(name, "r+");
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000204 if (file == NULL) return NULL;
205
206 fseek(file, 0, SEEK_END);
207 int size = ftell(file);
208
209 void* memory =
210 mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
211 return new PosixMemoryMappedFile(file, memory, size);
212}
213
214
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000215OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size,
216 void* initial) {
217 FILE* file = fopen(name, "w+");
218 if (file == NULL) return NULL;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000219 int result = fwrite(initial, size, 1, file);
220 if (result < 1) {
221 fclose(file);
222 return NULL;
223 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000224 void* memory =
225 mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
226 return new PosixMemoryMappedFile(file, memory, size);
227}
228
229
230PosixMemoryMappedFile::~PosixMemoryMappedFile() {
231 if (memory_) munmap(memory_, size_);
232 fclose(file_);
233}
234
235
236void OS::LogSharedLibraryAddresses() {
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000237 unsigned int images_count = _dyld_image_count();
238 for (unsigned int i = 0; i < images_count; ++i) {
239 const mach_header* header = _dyld_get_image_header(i);
240 if (header == NULL) continue;
ager@chromium.orga1645e22009-09-09 19:27:10 +0000241#if V8_HOST_ARCH_X64
242 uint64_t size;
243 char* code_ptr = getsectdatafromheader_64(
244 reinterpret_cast<const mach_header_64*>(header),
245 SEG_TEXT,
246 SECT_TEXT,
247 &size);
248#else
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000249 unsigned int size;
250 char* code_ptr = getsectdatafromheader(header, SEG_TEXT, SECT_TEXT, &size);
ager@chromium.orga1645e22009-09-09 19:27:10 +0000251#endif
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000252 if (code_ptr == NULL) continue;
253 const uintptr_t slide = _dyld_get_image_vmaddr_slide(i);
254 const uintptr_t start = reinterpret_cast<uintptr_t>(code_ptr) + slide;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000255 LOG(Isolate::Current(),
256 SharedLibraryEvent(_dyld_get_image_name(i), start, start + size));
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000257 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000258}
259
260
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000261void OS::SignalCodeMovingGC() {
262}
263
264
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000265uint64_t OS::CpuFeaturesImpliedByPlatform() {
266 // MacOSX requires all these to install so we can assume they are present.
267 // These constants are defined by the CPUid instructions.
268 const uint64_t one = 1;
269 return (one << SSE2) | (one << CMOV) | (one << RDTSC) | (one << CPUID);
270}
271
272
ager@chromium.org236ad962008-09-25 09:45:57 +0000273int OS::ActivationFrameAlignment() {
274 // OS X activation frames must be 16 byte-aligned; see "Mac OS X ABI
275 // Function Call Guide".
276 return 16;
277}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000278
279
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000280void OS::ReleaseStore(volatile AtomicWord* ptr, AtomicWord value) {
281 OSMemoryBarrier();
282 *ptr = value;
283}
284
285
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000286const char* OS::LocalTimezone(double time) {
287 if (isnan(time)) return "";
288 time_t tv = static_cast<time_t>(floor(time/msPerSecond));
289 struct tm* t = localtime(&tv);
290 if (NULL == t) return "";
291 return t->tm_zone;
292}
293
294
295double OS::LocalTimeOffset() {
296 time_t tv = time(NULL);
297 struct tm* t = localtime(&tv);
298 // tm_gmtoff includes any daylight savings offset, so subtract it.
299 return static_cast<double>(t->tm_gmtoff * msPerSecond -
300 (t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
301}
302
303
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000304int OS::StackWalk(Vector<StackFrame> frames) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000305 // If weak link to execinfo lib has failed, ie because we are on 10.4, abort.
306 if (backtrace == NULL)
307 return 0;
308
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000309 int frames_size = frames.length();
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000310 ScopedVector<void*> addresses(frames_size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000311
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000312 int frames_count = backtrace(addresses.start(), frames_size);
313
314 char** symbols = backtrace_symbols(addresses.start(), frames_count);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000315 if (symbols == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000316 return kStackWalkError;
317 }
318
319 for (int i = 0; i < frames_count; i++) {
320 frames[i].address = addresses[i];
321 // Format a text representation of the frame based on the information
322 // available.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000323 SNPrintF(MutableCStrVector(frames[i].text,
324 kStackWalkMaxTextLen),
325 "%s",
326 symbols[i]);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000327 // Make sure line termination is in place.
328 frames[i].text[kStackWalkMaxTextLen - 1] = '\0';
329 }
330
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000331 free(symbols);
332
333 return frames_count;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000334}
335
336
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000337
338
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000339VirtualMemory::VirtualMemory(size_t size) {
340 address_ = mmap(NULL, size, PROT_NONE,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000341 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
342 kMmapFd, kMmapFdOffset);
343 size_ = size;
344}
345
346
347VirtualMemory::~VirtualMemory() {
348 if (IsReserved()) {
349 if (0 == munmap(address(), size())) address_ = MAP_FAILED;
350 }
351}
352
353
354bool VirtualMemory::IsReserved() {
355 return address_ != MAP_FAILED;
356}
357
358
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000359bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
360 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
kasper.lund7276f142008-07-30 08:49:36 +0000361 if (MAP_FAILED == mmap(address, size, prot,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000362 MAP_PRIVATE | MAP_ANON | MAP_FIXED,
363 kMmapFd, kMmapFdOffset)) {
364 return false;
365 }
366
367 UpdateAllocatedSpaceLimits(address, size);
368 return true;
369}
370
371
372bool VirtualMemory::Uncommit(void* address, size_t size) {
373 return mmap(address, size, PROT_NONE,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000374 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | MAP_FIXED,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000375 kMmapFd, kMmapFdOffset) != MAP_FAILED;
376}
377
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000378
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000379class Thread::PlatformData : public Malloced {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000380 public:
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000381 PlatformData() : thread_(kNoThread) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000382 pthread_t thread_; // Thread handle for pthread.
383};
384
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000385Thread::Thread(const Options& options)
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000386 : data_(new PlatformData),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000387 stack_size_(options.stack_size) {
388 set_name(options.name);
lrn@chromium.org5d00b602011-01-05 09:51:43 +0000389}
390
391
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000392Thread::Thread(const char* name)
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000393 : data_(new PlatformData),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000394 stack_size_(0) {
lrn@chromium.org5d00b602011-01-05 09:51:43 +0000395 set_name(name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000396}
397
398
399Thread::~Thread() {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000400 delete data_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000401}
402
403
lrn@chromium.org5d00b602011-01-05 09:51:43 +0000404static void SetThreadName(const char* name) {
405 // pthread_setname_np is only available in 10.6 or later, so test
406 // for it at runtime.
407 int (*dynamic_pthread_setname_np)(const char*);
408 *reinterpret_cast<void**>(&dynamic_pthread_setname_np) =
409 dlsym(RTLD_DEFAULT, "pthread_setname_np");
410 if (!dynamic_pthread_setname_np)
411 return;
412
413 // Mac OS X does not expose the length limit of the name, so hardcode it.
414 static const int kMaxNameLength = 63;
415 USE(kMaxNameLength);
416 ASSERT(Thread::kMaxThreadNameLength <= kMaxNameLength);
417 dynamic_pthread_setname_np(name);
418}
419
420
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000421static void* ThreadEntry(void* arg) {
422 Thread* thread = reinterpret_cast<Thread*>(arg);
423 // This is also initialized by the first argument to pthread_create() but we
424 // don't know which thread will run first (the original thread or the new
425 // one) so we initialize it here too.
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000426 thread->data()->thread_ = pthread_self();
lrn@chromium.org5d00b602011-01-05 09:51:43 +0000427 SetThreadName(thread->name());
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000428 ASSERT(thread->data()->thread_ != kNoThread);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000429 thread->Run();
430 return NULL;
431}
432
433
lrn@chromium.org5d00b602011-01-05 09:51:43 +0000434void Thread::set_name(const char* name) {
435 strncpy(name_, name, sizeof(name_));
436 name_[sizeof(name_) - 1] = '\0';
437}
438
439
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000440void Thread::Start() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000441 pthread_attr_t* attr_ptr = NULL;
442 pthread_attr_t attr;
443 if (stack_size_ > 0) {
444 pthread_attr_init(&attr);
445 pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_));
446 attr_ptr = &attr;
447 }
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000448 pthread_create(&data_->thread_, attr_ptr, ThreadEntry, this);
449 ASSERT(data_->thread_ != kNoThread);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000450}
451
452
453void Thread::Join() {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000454 pthread_join(data_->thread_, NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000455}
456
457
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000458#ifdef V8_FAST_TLS_SUPPORTED
459
460static Atomic32 tls_base_offset_initialized = 0;
461intptr_t kMacTlsBaseOffset = 0;
462
463// It's safe to do the initialization more that once, but it has to be
464// done at least once.
465static void InitializeTlsBaseOffset() {
466 const size_t kBufferSize = 128;
467 char buffer[kBufferSize];
468 size_t buffer_size = kBufferSize;
469 int ctl_name[] = { CTL_KERN , KERN_OSRELEASE };
470 if (sysctl(ctl_name, 2, buffer, &buffer_size, NULL, 0) != 0) {
471 V8_Fatal(__FILE__, __LINE__, "V8 failed to get kernel version");
472 }
473 // The buffer now contains a string of the form XX.YY.ZZ, where
474 // XX is the major kernel version component.
475 // Make sure the buffer is 0-terminated.
476 buffer[kBufferSize - 1] = '\0';
477 char* period_pos = strchr(buffer, '.');
478 *period_pos = '\0';
479 int kernel_version_major =
480 static_cast<int>(strtol(buffer, NULL, 10)); // NOLINT
481 // The constants below are taken from pthreads.s from the XNU kernel
482 // sources archive at www.opensource.apple.com.
483 if (kernel_version_major < 11) {
484 // 8.x.x (Tiger), 9.x.x (Leopard), 10.x.x (Snow Leopard) have the
485 // same offsets.
486#if defined(V8_HOST_ARCH_IA32)
487 kMacTlsBaseOffset = 0x48;
488#else
489 kMacTlsBaseOffset = 0x60;
490#endif
491 } else {
492 // 11.x.x (Lion) changed the offset.
493 kMacTlsBaseOffset = 0;
494 }
495
496 Release_Store(&tls_base_offset_initialized, 1);
497}
498
499static void CheckFastTls(Thread::LocalStorageKey key) {
500 void* expected = reinterpret_cast<void*>(0x1234CAFE);
501 Thread::SetThreadLocal(key, expected);
502 void* actual = Thread::GetExistingThreadLocal(key);
503 if (expected != actual) {
504 V8_Fatal(__FILE__, __LINE__,
505 "V8 failed to initialize fast TLS on current kernel");
506 }
507 Thread::SetThreadLocal(key, NULL);
508}
509
510#endif // V8_FAST_TLS_SUPPORTED
511
512
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000513Thread::LocalStorageKey Thread::CreateThreadLocalKey() {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000514#ifdef V8_FAST_TLS_SUPPORTED
515 bool check_fast_tls = false;
516 if (tls_base_offset_initialized == 0) {
517 check_fast_tls = true;
518 InitializeTlsBaseOffset();
519 }
520#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000521 pthread_key_t key;
522 int result = pthread_key_create(&key, NULL);
523 USE(result);
524 ASSERT(result == 0);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000525 LocalStorageKey typed_key = static_cast<LocalStorageKey>(key);
526#ifdef V8_FAST_TLS_SUPPORTED
527 // If we just initialized fast TLS support, make sure it works.
528 if (check_fast_tls) CheckFastTls(typed_key);
529#endif
530 return typed_key;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000531}
532
533
534void Thread::DeleteThreadLocalKey(LocalStorageKey key) {
535 pthread_key_t pthread_key = static_cast<pthread_key_t>(key);
536 int result = pthread_key_delete(pthread_key);
537 USE(result);
538 ASSERT(result == 0);
539}
540
541
542void* Thread::GetThreadLocal(LocalStorageKey key) {
543 pthread_key_t pthread_key = static_cast<pthread_key_t>(key);
544 return pthread_getspecific(pthread_key);
545}
546
547
548void Thread::SetThreadLocal(LocalStorageKey key, void* value) {
549 pthread_key_t pthread_key = static_cast<pthread_key_t>(key);
550 pthread_setspecific(pthread_key, value);
551}
552
553
554void Thread::YieldCPU() {
555 sched_yield();
556}
557
558
559class MacOSMutex : public Mutex {
560 public:
561
562 MacOSMutex() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000563 pthread_mutexattr_t attr;
564 pthread_mutexattr_init(&attr);
565 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000566 pthread_mutex_init(&mutex_, &attr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000567 }
568
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000569 virtual ~MacOSMutex() { pthread_mutex_destroy(&mutex_); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000570
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000571 virtual int Lock() { return pthread_mutex_lock(&mutex_); }
572 virtual int Unlock() { return pthread_mutex_unlock(&mutex_); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000573
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000574 virtual bool TryLock() {
575 int result = pthread_mutex_trylock(&mutex_);
576 // Return false if the lock is busy and locking failed.
577 if (result == EBUSY) {
578 return false;
579 }
580 ASSERT(result == 0); // Verify no other errors.
581 return true;
582 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000583
584 private:
585 pthread_mutex_t mutex_;
586};
587
588
589Mutex* OS::CreateMutex() {
590 return new MacOSMutex();
591}
592
593
594class MacOSSemaphore : public Semaphore {
595 public:
596 explicit MacOSSemaphore(int count) {
597 semaphore_create(mach_task_self(), &semaphore_, SYNC_POLICY_FIFO, count);
598 }
599
600 ~MacOSSemaphore() {
601 semaphore_destroy(mach_task_self(), semaphore_);
602 }
603
kasper.lund7276f142008-07-30 08:49:36 +0000604 // The MacOS mach semaphore documentation claims it does not have spurious
605 // wakeups, the way pthreads semaphores do. So the code from the linux
606 // platform is not needed here.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000607 void Wait() { semaphore_wait(semaphore_); }
608
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000609 bool Wait(int timeout);
610
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000611 void Signal() { semaphore_signal(semaphore_); }
612
613 private:
614 semaphore_t semaphore_;
615};
616
617
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000618bool MacOSSemaphore::Wait(int timeout) {
619 mach_timespec_t ts;
620 ts.tv_sec = timeout / 1000000;
621 ts.tv_nsec = (timeout % 1000000) * 1000;
622 return semaphore_timedwait(semaphore_, ts) != KERN_OPERATION_TIMED_OUT;
623}
624
625
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000626Semaphore* OS::CreateSemaphore(int count) {
627 return new MacOSSemaphore(count);
628}
629
ager@chromium.org381abbb2009-02-25 13:23:22 +0000630
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000631class Sampler::PlatformData : public Malloced {
632 public:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000633 PlatformData() : profiled_thread_(mach_thread_self()) {}
634
635 ~PlatformData() {
636 // Deallocate Mach port for thread.
637 mach_port_deallocate(mach_task_self(), profiled_thread_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000638 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000639
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000640 thread_act_t profiled_thread() { return profiled_thread_; }
641
642 private:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000643 // Note: for profiled_thread_ Mach primitives are used instead of PThread's
644 // because the latter doesn't provide thread manipulation primitives required.
645 // For details, consult "Mac OS X Internals" book, Section 7.3.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000646 thread_act_t profiled_thread_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000647};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000648
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000649class SamplerThread : public Thread {
650 public:
651 explicit SamplerThread(int interval)
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000652 : Thread("SamplerThread"),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000653 interval_(interval) {}
654
655 static void AddActiveSampler(Sampler* sampler) {
656 ScopedLock lock(mutex_);
657 SamplerRegistry::AddActiveSampler(sampler);
658 if (instance_ == NULL) {
659 instance_ = new SamplerThread(sampler->interval());
660 instance_->Start();
661 } else {
662 ASSERT(instance_->interval_ == sampler->interval());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000663 }
664 }
665
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000666 static void RemoveActiveSampler(Sampler* sampler) {
667 ScopedLock lock(mutex_);
668 SamplerRegistry::RemoveActiveSampler(sampler);
669 if (SamplerRegistry::GetState() == SamplerRegistry::HAS_NO_SAMPLERS) {
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +0000670 RuntimeProfiler::StopRuntimeProfilerThreadBeforeShutdown(instance_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000671 delete instance_;
672 instance_ = NULL;
673 }
674 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000675
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000676 // Implement Thread::Run().
677 virtual void Run() {
678 SamplerRegistry::State state;
679 while ((state = SamplerRegistry::GetState()) !=
680 SamplerRegistry::HAS_NO_SAMPLERS) {
681 bool cpu_profiling_enabled =
682 (state == SamplerRegistry::HAS_CPU_PROFILING_SAMPLERS);
683 bool runtime_profiler_enabled = RuntimeProfiler::IsEnabled();
684 // When CPU profiling is enabled both JavaScript and C++ code is
685 // profiled. We must not suspend.
686 if (!cpu_profiling_enabled) {
687 if (rate_limiter_.SuspendIfNecessary()) continue;
688 }
689 if (cpu_profiling_enabled) {
690 if (!SamplerRegistry::IterateActiveSamplers(&DoCpuProfile, this)) {
691 return;
692 }
693 }
694 if (runtime_profiler_enabled) {
695 if (!SamplerRegistry::IterateActiveSamplers(&DoRuntimeProfile, NULL)) {
696 return;
697 }
698 }
699 OS::Sleep(interval_);
700 }
701 }
702
703 static void DoCpuProfile(Sampler* sampler, void* raw_sampler_thread) {
704 if (!sampler->isolate()->IsInitialized()) return;
705 if (!sampler->IsProfiling()) return;
706 SamplerThread* sampler_thread =
707 reinterpret_cast<SamplerThread*>(raw_sampler_thread);
708 sampler_thread->SampleContext(sampler);
709 }
710
711 static void DoRuntimeProfile(Sampler* sampler, void* ignored) {
712 if (!sampler->isolate()->IsInitialized()) return;
713 sampler->isolate()->runtime_profiler()->NotifyTick();
714 }
715
716 void SampleContext(Sampler* sampler) {
717 thread_act_t profiled_thread = sampler->platform_data()->profiled_thread();
718 TickSample sample_obj;
719 TickSample* sample = CpuProfiler::TickSampleEvent(sampler->isolate());
720 if (sample == NULL) sample = &sample_obj;
721
722 if (KERN_SUCCESS != thread_suspend(profiled_thread)) return;
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000723
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000724#if V8_HOST_ARCH_X64
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000725 thread_state_flavor_t flavor = x86_THREAD_STATE64;
726 x86_thread_state64_t state;
727 mach_msg_type_number_t count = x86_THREAD_STATE64_COUNT;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000728#if __DARWIN_UNIX03
729#define REGISTER_FIELD(name) __r ## name
730#else
731#define REGISTER_FIELD(name) r ## name
732#endif // __DARWIN_UNIX03
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000733#elif V8_HOST_ARCH_IA32
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000734 thread_state_flavor_t flavor = i386_THREAD_STATE;
735 i386_thread_state_t state;
736 mach_msg_type_number_t count = i386_THREAD_STATE_COUNT;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000737#if __DARWIN_UNIX03
738#define REGISTER_FIELD(name) __e ## name
739#else
740#define REGISTER_FIELD(name) e ## name
741#endif // __DARWIN_UNIX03
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000742#else
743#error Unsupported Mac OS X host architecture.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000744#endif // V8_HOST_ARCH
745
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000746 if (thread_get_state(profiled_thread,
747 flavor,
748 reinterpret_cast<natural_t*>(&state),
749 &count) == KERN_SUCCESS) {
750 sample->state = sampler->isolate()->current_vm_state();
751 sample->pc = reinterpret_cast<Address>(state.REGISTER_FIELD(ip));
752 sample->sp = reinterpret_cast<Address>(state.REGISTER_FIELD(sp));
753 sample->fp = reinterpret_cast<Address>(state.REGISTER_FIELD(bp));
754 sampler->SampleStack(sample);
755 sampler->Tick(sample);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000756 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000757 thread_resume(profiled_thread);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000758 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000759
760 const int interval_;
761 RuntimeProfilerRateLimiter rate_limiter_;
762
763 // Protects the process wide state below.
764 static Mutex* mutex_;
765 static SamplerThread* instance_;
766
767 DISALLOW_COPY_AND_ASSIGN(SamplerThread);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000768};
769
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000770#undef REGISTER_FIELD
771
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000772
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000773Mutex* SamplerThread::mutex_ = OS::CreateMutex();
774SamplerThread* SamplerThread::instance_ = NULL;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000775
776
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000777Sampler::Sampler(Isolate* isolate, int interval)
778 : isolate_(isolate),
779 interval_(interval),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000780 profiling_(false),
ager@chromium.orgbeb25712010-11-29 08:02:25 +0000781 active_(false),
782 samples_taken_(0) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000783 data_ = new PlatformData;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000784}
785
786
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000787Sampler::~Sampler() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000788 ASSERT(!IsActive());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000789 delete data_;
790}
791
792
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000793void Sampler::Start() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000794 ASSERT(!IsActive());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000795 SetActive(true);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000796 SamplerThread::AddActiveSampler(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000797}
798
799
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000800void Sampler::Stop() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000801 ASSERT(IsActive());
802 SamplerThread::RemoveActiveSampler(this);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000803 SetActive(false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000804}
805
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000806
807} } // namespace v8::internal