ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 28 | // Platform specific code for MacOS goes here. For the POSIX comaptible parts |
| 29 | // the implementation is in platform-posix.cc. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 30 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 31 | #include <unistd.h> |
| 32 | #include <sys/mman.h> |
| 33 | #include <mach/mach_init.h> |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 34 | #include <mach-o/dyld.h> |
| 35 | #include <mach-o/getsect.h> |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 36 | |
| 37 | #include <AvailabilityMacros.h> |
| 38 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 39 | #include <pthread.h> |
| 40 | #include <semaphore.h> |
| 41 | #include <signal.h> |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 42 | #include <libkern/OSAtomic.h> |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 43 | #include <mach/mach.h> |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 44 | #include <mach/semaphore.h> |
| 45 | #include <mach/task.h> |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 46 | #include <mach/vm_statistics.h> |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 47 | #include <sys/time.h> |
| 48 | #include <sys/resource.h> |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 49 | #include <sys/types.h> |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 50 | #include <stdarg.h> |
| 51 | #include <stdlib.h> |
| 52 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 53 | #include <errno.h> |
| 54 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 55 | #undef MAP_TYPE |
| 56 | |
| 57 | #include "v8.h" |
| 58 | |
| 59 | #include "platform.h" |
| 60 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 61 | // Manually define these here as weak imports, rather than including execinfo.h. |
| 62 | // This lets us launch on 10.4 which does not have these calls. |
| 63 | extern "C" { |
| 64 | extern int backtrace(void**, int) __attribute__((weak_import)); |
| 65 | extern char** backtrace_symbols(void* const*, int) |
| 66 | __attribute__((weak_import)); |
| 67 | extern void backtrace_symbols_fd(void* const*, int, int) |
| 68 | __attribute__((weak_import)); |
| 69 | } |
| 70 | |
| 71 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 72 | namespace v8 { |
| 73 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 74 | |
| 75 | // 0 is never a valid thread id on MacOSX since a ptread_t is |
| 76 | // a pointer. |
| 77 | static const pthread_t kNoThread = (pthread_t) 0; |
| 78 | |
| 79 | |
| 80 | double ceiling(double x) { |
| 81 | // Correct Mac OS X Leopard 'ceil' behavior. |
| 82 | if (-1.0 < x && x < 0.0) { |
| 83 | return -0.0; |
| 84 | } else { |
| 85 | return ceil(x); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | |
| 90 | void OS::Setup() { |
| 91 | // Seed the random number generator. |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 92 | // Convert the current time to a 64-bit integer first, before converting it |
| 93 | // to an unsigned. Going directly will cause an overflow and the seed to be |
| 94 | // set to all ones. The seed will be identical for different instances that |
| 95 | // call this setup code within the same millisecond. |
| 96 | uint64_t seed = static_cast<uint64_t>(TimeCurrentMillis()); |
| 97 | srandom(static_cast<unsigned int>(seed)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 101 | // We keep the lowest and highest addresses mapped as a quick way of |
| 102 | // determining that pointers are outside the heap (used mostly in assertions |
| 103 | // and verification). The estimate is conservative, ie, not all addresses in |
| 104 | // 'allocated' space are actually allocated to our heap. The range is |
| 105 | // [lowest, highest), inclusive on the low and and exclusive on the high end. |
| 106 | static void* lowest_ever_allocated = reinterpret_cast<void*>(-1); |
| 107 | static void* highest_ever_allocated = reinterpret_cast<void*>(0); |
| 108 | |
| 109 | |
| 110 | static void UpdateAllocatedSpaceLimits(void* address, int size) { |
| 111 | lowest_ever_allocated = Min(lowest_ever_allocated, address); |
| 112 | highest_ever_allocated = |
| 113 | Max(highest_ever_allocated, |
| 114 | reinterpret_cast<void*>(reinterpret_cast<char*>(address) + size)); |
| 115 | } |
| 116 | |
| 117 | |
| 118 | bool OS::IsOutsideAllocatedSpace(void* address) { |
| 119 | return address < lowest_ever_allocated || address >= highest_ever_allocated; |
| 120 | } |
| 121 | |
| 122 | |
| 123 | size_t OS::AllocateAlignment() { |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 124 | return getpagesize(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 128 | // Constants used for mmap. |
| 129 | // kMmapFd is used to pass vm_alloc flags to tag the region with the user |
| 130 | // defined tag 255 This helps identify V8-allocated regions in memory analysis |
| 131 | // tools like vmmap(1). |
| 132 | static const int kMmapFd = VM_MAKE_TAG(255); |
| 133 | static const off_t kMmapFdOffset = 0; |
| 134 | |
| 135 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 136 | void* OS::Allocate(const size_t requested, |
| 137 | size_t* allocated, |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 138 | bool is_executable) { |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 139 | const size_t msize = RoundUp(requested, getpagesize()); |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 140 | int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 141 | void* mbase = mmap(NULL, msize, prot, |
| 142 | MAP_PRIVATE | MAP_ANON, |
| 143 | kMmapFd, kMmapFdOffset); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 144 | if (mbase == MAP_FAILED) { |
| 145 | LOG(StringEvent("OS::Allocate", "mmap failed")); |
| 146 | return NULL; |
| 147 | } |
| 148 | *allocated = msize; |
| 149 | UpdateAllocatedSpaceLimits(mbase, msize); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 150 | return mbase; |
| 151 | } |
| 152 | |
| 153 | |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 154 | void OS::Free(void* address, const size_t size) { |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 155 | // TODO(1240712): munmap has a return value which is ignored here. |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 156 | int result = munmap(address, size); |
| 157 | USE(result); |
| 158 | ASSERT(result == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 162 | #ifdef ENABLE_HEAP_PROTECTION |
| 163 | |
| 164 | void OS::Protect(void* address, size_t size) { |
| 165 | UNIMPLEMENTED(); |
| 166 | } |
| 167 | |
| 168 | |
| 169 | void OS::Unprotect(void* address, size_t size, bool is_executable) { |
| 170 | UNIMPLEMENTED(); |
| 171 | } |
| 172 | |
| 173 | #endif |
| 174 | |
| 175 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 176 | void OS::Sleep(int milliseconds) { |
| 177 | usleep(1000 * milliseconds); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | |
| 181 | void OS::Abort() { |
| 182 | // Redirect to std abort to signal abnormal program termination |
| 183 | abort(); |
| 184 | } |
| 185 | |
| 186 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 187 | void OS::DebugBreak() { |
| 188 | asm("int $3"); |
| 189 | } |
| 190 | |
| 191 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 192 | class PosixMemoryMappedFile : public OS::MemoryMappedFile { |
| 193 | public: |
| 194 | PosixMemoryMappedFile(FILE* file, void* memory, int size) |
| 195 | : file_(file), memory_(memory), size_(size) { } |
| 196 | virtual ~PosixMemoryMappedFile(); |
| 197 | virtual void* memory() { return memory_; } |
| 198 | private: |
| 199 | FILE* file_; |
| 200 | void* memory_; |
| 201 | int size_; |
| 202 | }; |
| 203 | |
| 204 | |
| 205 | OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size, |
| 206 | void* initial) { |
| 207 | FILE* file = fopen(name, "w+"); |
| 208 | if (file == NULL) return NULL; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 209 | int result = fwrite(initial, size, 1, file); |
| 210 | if (result < 1) { |
| 211 | fclose(file); |
| 212 | return NULL; |
| 213 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 214 | void* memory = |
| 215 | mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); |
| 216 | return new PosixMemoryMappedFile(file, memory, size); |
| 217 | } |
| 218 | |
| 219 | |
| 220 | PosixMemoryMappedFile::~PosixMemoryMappedFile() { |
| 221 | if (memory_) munmap(memory_, size_); |
| 222 | fclose(file_); |
| 223 | } |
| 224 | |
| 225 | |
| 226 | void OS::LogSharedLibraryAddresses() { |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 227 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 228 | unsigned int images_count = _dyld_image_count(); |
| 229 | for (unsigned int i = 0; i < images_count; ++i) { |
| 230 | const mach_header* header = _dyld_get_image_header(i); |
| 231 | if (header == NULL) continue; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 232 | #if V8_HOST_ARCH_X64 |
| 233 | uint64_t size; |
| 234 | char* code_ptr = getsectdatafromheader_64( |
| 235 | reinterpret_cast<const mach_header_64*>(header), |
| 236 | SEG_TEXT, |
| 237 | SECT_TEXT, |
| 238 | &size); |
| 239 | #else |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 240 | unsigned int size; |
| 241 | char* code_ptr = getsectdatafromheader(header, SEG_TEXT, SECT_TEXT, &size); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 242 | #endif |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 243 | if (code_ptr == NULL) continue; |
| 244 | const uintptr_t slide = _dyld_get_image_vmaddr_slide(i); |
| 245 | const uintptr_t start = reinterpret_cast<uintptr_t>(code_ptr) + slide; |
| 246 | LOG(SharedLibraryEvent(_dyld_get_image_name(i), start, start + size)); |
| 247 | } |
| 248 | #endif // ENABLE_LOGGING_AND_PROFILING |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 252 | void OS::SignalCodeMovingGC() { |
| 253 | } |
| 254 | |
| 255 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 256 | uint64_t OS::CpuFeaturesImpliedByPlatform() { |
| 257 | // MacOSX requires all these to install so we can assume they are present. |
| 258 | // These constants are defined by the CPUid instructions. |
| 259 | const uint64_t one = 1; |
| 260 | return (one << SSE2) | (one << CMOV) | (one << RDTSC) | (one << CPUID); |
| 261 | } |
| 262 | |
| 263 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 264 | int OS::ActivationFrameAlignment() { |
| 265 | // OS X activation frames must be 16 byte-aligned; see "Mac OS X ABI |
| 266 | // Function Call Guide". |
| 267 | return 16; |
| 268 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 269 | |
| 270 | |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 271 | void OS::ReleaseStore(volatile AtomicWord* ptr, AtomicWord value) { |
| 272 | OSMemoryBarrier(); |
| 273 | *ptr = value; |
| 274 | } |
| 275 | |
| 276 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 277 | const char* OS::LocalTimezone(double time) { |
| 278 | if (isnan(time)) return ""; |
| 279 | time_t tv = static_cast<time_t>(floor(time/msPerSecond)); |
| 280 | struct tm* t = localtime(&tv); |
| 281 | if (NULL == t) return ""; |
| 282 | return t->tm_zone; |
| 283 | } |
| 284 | |
| 285 | |
| 286 | double OS::LocalTimeOffset() { |
| 287 | time_t tv = time(NULL); |
| 288 | struct tm* t = localtime(&tv); |
| 289 | // tm_gmtoff includes any daylight savings offset, so subtract it. |
| 290 | return static_cast<double>(t->tm_gmtoff * msPerSecond - |
| 291 | (t->tm_isdst > 0 ? 3600 * msPerSecond : 0)); |
| 292 | } |
| 293 | |
| 294 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 295 | int OS::StackWalk(Vector<StackFrame> frames) { |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 296 | // If weak link to execinfo lib has failed, ie because we are on 10.4, abort. |
| 297 | if (backtrace == NULL) |
| 298 | return 0; |
| 299 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 300 | int frames_size = frames.length(); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 301 | ScopedVector<void*> addresses(frames_size); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 302 | |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 303 | int frames_count = backtrace(addresses.start(), frames_size); |
| 304 | |
| 305 | char** symbols = backtrace_symbols(addresses.start(), frames_count); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 306 | if (symbols == NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 307 | return kStackWalkError; |
| 308 | } |
| 309 | |
| 310 | for (int i = 0; i < frames_count; i++) { |
| 311 | frames[i].address = addresses[i]; |
| 312 | // Format a text representation of the frame based on the information |
| 313 | // available. |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 314 | SNPrintF(MutableCStrVector(frames[i].text, |
| 315 | kStackWalkMaxTextLen), |
| 316 | "%s", |
| 317 | symbols[i]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 318 | // Make sure line termination is in place. |
| 319 | frames[i].text[kStackWalkMaxTextLen - 1] = '\0'; |
| 320 | } |
| 321 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 322 | free(symbols); |
| 323 | |
| 324 | return frames_count; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 328 | |
| 329 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 330 | VirtualMemory::VirtualMemory(size_t size) { |
| 331 | address_ = mmap(NULL, size, PROT_NONE, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 332 | MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, |
| 333 | kMmapFd, kMmapFdOffset); |
| 334 | size_ = size; |
| 335 | } |
| 336 | |
| 337 | |
| 338 | VirtualMemory::~VirtualMemory() { |
| 339 | if (IsReserved()) { |
| 340 | if (0 == munmap(address(), size())) address_ = MAP_FAILED; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | |
| 345 | bool VirtualMemory::IsReserved() { |
| 346 | return address_ != MAP_FAILED; |
| 347 | } |
| 348 | |
| 349 | |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 350 | bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) { |
| 351 | int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 352 | if (MAP_FAILED == mmap(address, size, prot, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 353 | MAP_PRIVATE | MAP_ANON | MAP_FIXED, |
| 354 | kMmapFd, kMmapFdOffset)) { |
| 355 | return false; |
| 356 | } |
| 357 | |
| 358 | UpdateAllocatedSpaceLimits(address, size); |
| 359 | return true; |
| 360 | } |
| 361 | |
| 362 | |
| 363 | bool VirtualMemory::Uncommit(void* address, size_t size) { |
| 364 | return mmap(address, size, PROT_NONE, |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 365 | MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | MAP_FIXED, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 366 | kMmapFd, kMmapFdOffset) != MAP_FAILED; |
| 367 | } |
| 368 | |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 369 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 370 | class ThreadHandle::PlatformData : public Malloced { |
| 371 | public: |
| 372 | explicit PlatformData(ThreadHandle::Kind kind) { |
| 373 | Initialize(kind); |
| 374 | } |
| 375 | |
| 376 | void Initialize(ThreadHandle::Kind kind) { |
| 377 | switch (kind) { |
| 378 | case ThreadHandle::SELF: thread_ = pthread_self(); break; |
| 379 | case ThreadHandle::INVALID: thread_ = kNoThread; break; |
| 380 | } |
| 381 | } |
| 382 | pthread_t thread_; // Thread handle for pthread. |
| 383 | }; |
| 384 | |
| 385 | |
| 386 | |
| 387 | ThreadHandle::ThreadHandle(Kind kind) { |
| 388 | data_ = new PlatformData(kind); |
| 389 | } |
| 390 | |
| 391 | |
| 392 | void ThreadHandle::Initialize(ThreadHandle::Kind kind) { |
| 393 | data_->Initialize(kind); |
| 394 | } |
| 395 | |
| 396 | |
| 397 | ThreadHandle::~ThreadHandle() { |
| 398 | delete data_; |
| 399 | } |
| 400 | |
| 401 | |
| 402 | bool ThreadHandle::IsSelf() const { |
| 403 | return pthread_equal(data_->thread_, pthread_self()); |
| 404 | } |
| 405 | |
| 406 | |
| 407 | bool ThreadHandle::IsValid() const { |
| 408 | return data_->thread_ != kNoThread; |
| 409 | } |
| 410 | |
| 411 | |
| 412 | Thread::Thread() : ThreadHandle(ThreadHandle::INVALID) { |
| 413 | } |
| 414 | |
| 415 | |
| 416 | Thread::~Thread() { |
| 417 | } |
| 418 | |
| 419 | |
| 420 | static void* ThreadEntry(void* arg) { |
| 421 | Thread* thread = reinterpret_cast<Thread*>(arg); |
| 422 | // This is also initialized by the first argument to pthread_create() but we |
| 423 | // don't know which thread will run first (the original thread or the new |
| 424 | // one) so we initialize it here too. |
| 425 | thread->thread_handle_data()->thread_ = pthread_self(); |
| 426 | ASSERT(thread->IsValid()); |
| 427 | thread->Run(); |
| 428 | return NULL; |
| 429 | } |
| 430 | |
| 431 | |
| 432 | void Thread::Start() { |
| 433 | pthread_create(&thread_handle_data()->thread_, NULL, ThreadEntry, this); |
| 434 | } |
| 435 | |
| 436 | |
| 437 | void Thread::Join() { |
| 438 | pthread_join(thread_handle_data()->thread_, NULL); |
| 439 | } |
| 440 | |
| 441 | |
| 442 | Thread::LocalStorageKey Thread::CreateThreadLocalKey() { |
| 443 | pthread_key_t key; |
| 444 | int result = pthread_key_create(&key, NULL); |
| 445 | USE(result); |
| 446 | ASSERT(result == 0); |
| 447 | return static_cast<LocalStorageKey>(key); |
| 448 | } |
| 449 | |
| 450 | |
| 451 | void Thread::DeleteThreadLocalKey(LocalStorageKey key) { |
| 452 | pthread_key_t pthread_key = static_cast<pthread_key_t>(key); |
| 453 | int result = pthread_key_delete(pthread_key); |
| 454 | USE(result); |
| 455 | ASSERT(result == 0); |
| 456 | } |
| 457 | |
| 458 | |
| 459 | void* Thread::GetThreadLocal(LocalStorageKey key) { |
| 460 | pthread_key_t pthread_key = static_cast<pthread_key_t>(key); |
| 461 | return pthread_getspecific(pthread_key); |
| 462 | } |
| 463 | |
| 464 | |
| 465 | void Thread::SetThreadLocal(LocalStorageKey key, void* value) { |
| 466 | pthread_key_t pthread_key = static_cast<pthread_key_t>(key); |
| 467 | pthread_setspecific(pthread_key, value); |
| 468 | } |
| 469 | |
| 470 | |
| 471 | void Thread::YieldCPU() { |
| 472 | sched_yield(); |
| 473 | } |
| 474 | |
| 475 | |
| 476 | class MacOSMutex : public Mutex { |
| 477 | public: |
| 478 | |
| 479 | MacOSMutex() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 480 | pthread_mutexattr_t attr; |
| 481 | pthread_mutexattr_init(&attr); |
| 482 | pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 483 | pthread_mutex_init(&mutex_, &attr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | ~MacOSMutex() { pthread_mutex_destroy(&mutex_); } |
| 487 | |
| 488 | int Lock() { return pthread_mutex_lock(&mutex_); } |
| 489 | |
| 490 | int Unlock() { return pthread_mutex_unlock(&mutex_); } |
| 491 | |
| 492 | private: |
| 493 | pthread_mutex_t mutex_; |
| 494 | }; |
| 495 | |
| 496 | |
| 497 | Mutex* OS::CreateMutex() { |
| 498 | return new MacOSMutex(); |
| 499 | } |
| 500 | |
| 501 | |
| 502 | class MacOSSemaphore : public Semaphore { |
| 503 | public: |
| 504 | explicit MacOSSemaphore(int count) { |
| 505 | semaphore_create(mach_task_self(), &semaphore_, SYNC_POLICY_FIFO, count); |
| 506 | } |
| 507 | |
| 508 | ~MacOSSemaphore() { |
| 509 | semaphore_destroy(mach_task_self(), semaphore_); |
| 510 | } |
| 511 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 512 | // The MacOS mach semaphore documentation claims it does not have spurious |
| 513 | // wakeups, the way pthreads semaphores do. So the code from the linux |
| 514 | // platform is not needed here. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 515 | void Wait() { semaphore_wait(semaphore_); } |
| 516 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 517 | bool Wait(int timeout); |
| 518 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 519 | void Signal() { semaphore_signal(semaphore_); } |
| 520 | |
| 521 | private: |
| 522 | semaphore_t semaphore_; |
| 523 | }; |
| 524 | |
| 525 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 526 | bool MacOSSemaphore::Wait(int timeout) { |
| 527 | mach_timespec_t ts; |
| 528 | ts.tv_sec = timeout / 1000000; |
| 529 | ts.tv_nsec = (timeout % 1000000) * 1000; |
| 530 | return semaphore_timedwait(semaphore_, ts) != KERN_OPERATION_TIMED_OUT; |
| 531 | } |
| 532 | |
| 533 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 534 | Semaphore* OS::CreateSemaphore(int count) { |
| 535 | return new MacOSSemaphore(count); |
| 536 | } |
| 537 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 538 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 539 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 540 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 541 | class Sampler::PlatformData : public Malloced { |
| 542 | public: |
| 543 | explicit PlatformData(Sampler* sampler) |
| 544 | : sampler_(sampler), |
| 545 | task_self_(mach_task_self()), |
| 546 | profiled_thread_(0), |
| 547 | sampler_thread_(0) { |
| 548 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 549 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 550 | Sampler* sampler_; |
| 551 | // Note: for profiled_thread_ Mach primitives are used instead of PThread's |
| 552 | // because the latter doesn't provide thread manipulation primitives required. |
| 553 | // For details, consult "Mac OS X Internals" book, Section 7.3. |
| 554 | mach_port_t task_self_; |
| 555 | thread_act_t profiled_thread_; |
| 556 | pthread_t sampler_thread_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 557 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 558 | // Sampler thread handler. |
| 559 | void Runner() { |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 560 | // Loop until the sampler is disengaged, keeping the specified |
| 561 | // sampling frequency. |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 562 | for ( ; sampler_->IsActive(); OS::Sleep(sampler_->interval_)) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 563 | TickSample sample_obj; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 564 | TickSample* sample = CpuProfiler::TickSampleEvent(); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 565 | if (sample == NULL) sample = &sample_obj; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 566 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 567 | // If the sampler runs in sync with the JS thread, we try to |
| 568 | // suspend it. If we fail, we skip the current sample. |
| 569 | if (sampler_->IsSynchronous()) { |
| 570 | if (KERN_SUCCESS != thread_suspend(profiled_thread_)) continue; |
| 571 | } |
| 572 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 573 | // We always sample the VM state. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 574 | sample->state = VMState::current_state(); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 575 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 576 | // If profiling, we record the pc and sp of the profiled thread. |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 577 | if (sampler_->IsProfiling()) { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 578 | #if V8_HOST_ARCH_X64 |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 579 | thread_state_flavor_t flavor = x86_THREAD_STATE64; |
| 580 | x86_thread_state64_t state; |
| 581 | mach_msg_type_number_t count = x86_THREAD_STATE64_COUNT; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 582 | #if __DARWIN_UNIX03 |
| 583 | #define REGISTER_FIELD(name) __r ## name |
| 584 | #else |
| 585 | #define REGISTER_FIELD(name) r ## name |
| 586 | #endif // __DARWIN_UNIX03 |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 587 | #elif V8_HOST_ARCH_IA32 |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 588 | thread_state_flavor_t flavor = i386_THREAD_STATE; |
| 589 | i386_thread_state_t state; |
| 590 | mach_msg_type_number_t count = i386_THREAD_STATE_COUNT; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 591 | #if __DARWIN_UNIX03 |
| 592 | #define REGISTER_FIELD(name) __e ## name |
| 593 | #else |
| 594 | #define REGISTER_FIELD(name) e ## name |
| 595 | #endif // __DARWIN_UNIX03 |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 596 | #else |
| 597 | #error Unsupported Mac OS X host architecture. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 598 | #endif // V8_HOST_ARCH |
| 599 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 600 | if (thread_get_state(profiled_thread_, |
| 601 | flavor, |
| 602 | reinterpret_cast<natural_t*>(&state), |
| 603 | &count) == KERN_SUCCESS) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 604 | sample->pc = reinterpret_cast<Address>(state.REGISTER_FIELD(ip)); |
| 605 | sample->sp = reinterpret_cast<Address>(state.REGISTER_FIELD(sp)); |
| 606 | sample->fp = reinterpret_cast<Address>(state.REGISTER_FIELD(bp)); |
| 607 | sampler_->SampleStack(sample); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 608 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 609 | } |
| 610 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 611 | // Invoke tick handler with program counter and stack pointer. |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 612 | sampler_->Tick(sample); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 613 | |
| 614 | // If the sampler runs in sync with the JS thread, we have to |
| 615 | // remember to resume it. |
| 616 | if (sampler_->IsSynchronous()) thread_resume(profiled_thread_); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 617 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 618 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 619 | }; |
| 620 | |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 621 | #undef REGISTER_FIELD |
| 622 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 623 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 624 | // Entry point for sampler thread. |
| 625 | static void* SamplerEntry(void* arg) { |
| 626 | Sampler::PlatformData* data = |
| 627 | reinterpret_cast<Sampler::PlatformData*>(arg); |
| 628 | data->Runner(); |
| 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 633 | Sampler::Sampler(int interval, bool profiling) |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 634 | : interval_(interval), |
| 635 | profiling_(profiling), |
| 636 | synchronous_(profiling), |
| 637 | active_(false) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 638 | data_ = new PlatformData(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 642 | Sampler::~Sampler() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 643 | delete data_; |
| 644 | } |
| 645 | |
| 646 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 647 | void Sampler::Start() { |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 648 | // If we are starting a synchronous sampler, we need to be able to |
| 649 | // access the calling thread. |
| 650 | if (IsSynchronous()) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 651 | data_->profiled_thread_ = mach_thread_self(); |
| 652 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 653 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 654 | // Create sampler thread with high priority. |
| 655 | // According to POSIX spec, when SCHED_FIFO policy is used, a thread |
| 656 | // runs until it exits or blocks. |
| 657 | pthread_attr_t sched_attr; |
| 658 | sched_param fifo_param; |
| 659 | pthread_attr_init(&sched_attr); |
| 660 | pthread_attr_setinheritsched(&sched_attr, PTHREAD_EXPLICIT_SCHED); |
| 661 | pthread_attr_setschedpolicy(&sched_attr, SCHED_FIFO); |
| 662 | fifo_param.sched_priority = sched_get_priority_max(SCHED_FIFO); |
| 663 | pthread_attr_setschedparam(&sched_attr, &fifo_param); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 664 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 665 | active_ = true; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 666 | pthread_create(&data_->sampler_thread_, &sched_attr, SamplerEntry, data_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 670 | void Sampler::Stop() { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 671 | // Seting active to false triggers termination of the sampler |
| 672 | // thread. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 673 | active_ = false; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 674 | |
| 675 | // Wait for sampler thread to terminate. |
| 676 | pthread_join(data_->sampler_thread_, NULL); |
| 677 | |
| 678 | // Deallocate Mach port for thread. |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 679 | if (IsSynchronous()) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 680 | mach_port_deallocate(data_->task_self_, data_->profiled_thread_); |
| 681 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | #endif // ENABLE_LOGGING_AND_PROFILING |
| 685 | |
| 686 | } } // namespace v8::internal |