ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame^] | 1 | // Copyright 2006-2008 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 | // Platform specific code for FreeBSD goes here |
| 29 | |
| 30 | #include <pthread.h> |
| 31 | #include <semaphore.h> |
| 32 | #include <signal.h> |
| 33 | #include <sys/time.h> |
| 34 | #include <sys/resource.h> |
| 35 | #include <sys/ucontext.h> |
| 36 | #include <stdlib.h> |
| 37 | |
| 38 | #include <sys/types.h> // mmap & munmap |
| 39 | #include <sys/mman.h> // mmap & munmap |
| 40 | #include <sys/stat.h> // open |
| 41 | #include <sys/fcntl.h> // open |
| 42 | #include <unistd.h> // getpagesize |
| 43 | #include <execinfo.h> // backtrace, backtrace_symbols |
| 44 | #include <strings.h> // index |
| 45 | #include <errno.h> |
| 46 | #include <stdarg.h> |
| 47 | #include <limits.h> |
| 48 | |
| 49 | #undef MAP_TYPE |
| 50 | |
| 51 | #include "v8.h" |
| 52 | |
| 53 | #include "platform.h" |
| 54 | |
| 55 | |
| 56 | namespace v8 { namespace internal { |
| 57 | |
| 58 | // 0 is never a valid thread id on FreeBSD since tids and pids share a |
| 59 | // name space and pid 0 is used to kill the group (see man 2 kill). |
| 60 | static const pthread_t kNoThread = (pthread_t) 0; |
| 61 | |
| 62 | |
| 63 | double ceiling(double x) { |
| 64 | // Correct as on OS X |
| 65 | if (-1.0 < x && x < 0.0) { |
| 66 | return -0.0; |
| 67 | } else { |
| 68 | return ceil(x); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | |
| 73 | void OS::Setup() { |
| 74 | // Seed the random number generator. |
| 75 | // Convert the current time to a 64-bit integer first, before converting it |
| 76 | // to an unsigned. Going directly can cause an overflow and the seed to be |
| 77 | // set to all ones. The seed will be identical for different instances that |
| 78 | // call this setup code within the same millisecond. |
| 79 | uint64_t seed = static_cast<uint64_t>(TimeCurrentMillis()); |
| 80 | srandom(static_cast<unsigned int>(seed)); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | int OS::GetUserTime(uint32_t* secs, uint32_t* usecs) { |
| 85 | struct rusage usage; |
| 86 | |
| 87 | if (getrusage(RUSAGE_SELF, &usage) < 0) return -1; |
| 88 | *secs = usage.ru_utime.tv_sec; |
| 89 | *usecs = usage.ru_utime.tv_usec; |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | |
| 94 | double OS::TimeCurrentMillis() { |
| 95 | struct timeval tv; |
| 96 | if (gettimeofday(&tv, NULL) < 0) return 0.0; |
| 97 | return (static_cast<double>(tv.tv_sec) * 1000) + |
| 98 | (static_cast<double>(tv.tv_usec) / 1000); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | int64_t OS::Ticks() { |
| 103 | // FreeBSD's gettimeofday has microsecond resolution. |
| 104 | struct timeval tv; |
| 105 | if (gettimeofday(&tv, NULL) < 0) |
| 106 | return 0; |
| 107 | return (static_cast<int64_t>(tv.tv_sec) * 1000000) + tv.tv_usec; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | char* OS::LocalTimezone(double time) { |
| 112 | time_t tv = static_cast<time_t>(floor(time/msPerSecond)); |
| 113 | struct tm* t = localtime(&tv); |
| 114 | return const_cast<char*>(t->tm_zone); |
| 115 | } |
| 116 | |
| 117 | |
| 118 | double OS::DaylightSavingsOffset(double time) { |
| 119 | time_t tv = static_cast<time_t>(floor(time/msPerSecond)); |
| 120 | struct tm* t = localtime(&tv); |
| 121 | return t->tm_isdst > 0 ? 3600 * msPerSecond : 0; |
| 122 | } |
| 123 | |
| 124 | |
| 125 | double OS::LocalTimeOffset() { |
| 126 | time_t tv = time(NULL); |
| 127 | struct tm* t = localtime(&tv); |
| 128 | // tm_gmtoff includes any daylight savings offset, so subtract it. |
| 129 | return static_cast<double>(t->tm_gmtoff * msPerSecond - |
| 130 | (t->tm_isdst > 0 ? 3600 * msPerSecond : 0)); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | FILE* OS::FOpen(const char* path, const char* mode) { |
| 135 | return fopen(path, mode); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | void OS::Print(const char* format, ...) { |
| 140 | va_list args; |
| 141 | va_start(args, format); |
| 142 | VPrint(format, args); |
| 143 | va_end(args); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | void OS::VPrint(const char* format, va_list args) { |
| 148 | vprintf(format, args); |
| 149 | } |
| 150 | |
| 151 | |
| 152 | void OS::PrintError(const char* format, ...) { |
| 153 | va_list args; |
| 154 | va_start(args, format); |
| 155 | VPrintError(format, args); |
| 156 | va_end(args); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | void OS::VPrintError(const char* format, va_list args) { |
| 161 | vfprintf(stderr, format, args); |
| 162 | } |
| 163 | |
| 164 | |
| 165 | int OS::SNPrintF(Vector<char> str, const char* format, ...) { |
| 166 | va_list args; |
| 167 | va_start(args, format); |
| 168 | int result = VSNPrintF(str, format, args); |
| 169 | va_end(args); |
| 170 | return result; |
| 171 | } |
| 172 | |
| 173 | |
| 174 | int OS::VSNPrintF(Vector<char> str, |
| 175 | const char* format, |
| 176 | va_list args) { |
| 177 | int n = vsnprintf(str.start(), str.length(), format, args); |
| 178 | if (n < 0 || n >= str.length()) { |
| 179 | str[str.length() - 1] = '\0'; |
| 180 | return -1; |
| 181 | } else { |
| 182 | return n; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | |
| 187 | void OS::StrNCpy(Vector<char> dest, const char* src, size_t n) { |
| 188 | strncpy(dest.start(), src, n); |
| 189 | } |
| 190 | |
| 191 | |
| 192 | char *OS::StrDup(const char* str) { |
| 193 | return strdup(str); |
| 194 | } |
| 195 | |
| 196 | |
| 197 | double OS::nan_value() { |
| 198 | return NAN; |
| 199 | } |
| 200 | |
| 201 | |
| 202 | int OS::ActivationFrameAlignment() { |
| 203 | // 16 byte alignment on FreeBSD |
| 204 | return 16; |
| 205 | } |
| 206 | |
| 207 | |
| 208 | // We keep the lowest and highest addresses mapped as a quick way of |
| 209 | // determining that pointers are outside the heap (used mostly in assertions |
| 210 | // and verification). The estimate is conservative, ie, not all addresses in |
| 211 | // 'allocated' space are actually allocated to our heap. The range is |
| 212 | // [lowest, highest), inclusive on the low and and exclusive on the high end. |
| 213 | static void* lowest_ever_allocated = reinterpret_cast<void*>(-1); |
| 214 | static void* highest_ever_allocated = reinterpret_cast<void*>(0); |
| 215 | |
| 216 | |
| 217 | static void UpdateAllocatedSpaceLimits(void* address, int size) { |
| 218 | lowest_ever_allocated = Min(lowest_ever_allocated, address); |
| 219 | highest_ever_allocated = |
| 220 | Max(highest_ever_allocated, |
| 221 | reinterpret_cast<void*>(reinterpret_cast<char*>(address) + size)); |
| 222 | } |
| 223 | |
| 224 | |
| 225 | bool OS::IsOutsideAllocatedSpace(void* address) { |
| 226 | return address < lowest_ever_allocated || address >= highest_ever_allocated; |
| 227 | } |
| 228 | |
| 229 | |
| 230 | size_t OS::AllocateAlignment() { |
| 231 | return getpagesize(); |
| 232 | } |
| 233 | |
| 234 | |
| 235 | void* OS::Allocate(const size_t requested, |
| 236 | size_t* allocated, |
| 237 | bool executable) { |
| 238 | const size_t msize = RoundUp(requested, getpagesize()); |
| 239 | int prot = PROT_READ | PROT_WRITE | (executable ? PROT_EXEC : 0); |
| 240 | void* mbase = mmap(NULL, msize, prot, MAP_PRIVATE | MAP_ANON, -1, 0); |
| 241 | |
| 242 | if (mbase == MAP_FAILED) { |
| 243 | LOG(StringEvent("OS::Allocate", "mmap failed")); |
| 244 | return NULL; |
| 245 | } |
| 246 | *allocated = msize; |
| 247 | UpdateAllocatedSpaceLimits(mbase, msize); |
| 248 | return mbase; |
| 249 | } |
| 250 | |
| 251 | |
| 252 | void OS::Free(void* buf, const size_t length) { |
| 253 | // TODO(1240712): munmap has a return value which is ignored here. |
| 254 | munmap(buf, length); |
| 255 | } |
| 256 | |
| 257 | |
| 258 | void OS::Sleep(int milliseconds) { |
| 259 | unsigned int ms = static_cast<unsigned int>(milliseconds); |
| 260 | usleep(1000 * ms); |
| 261 | } |
| 262 | |
| 263 | |
| 264 | void OS::Abort() { |
| 265 | // Redirect to std abort to signal abnormal program termination. |
| 266 | abort(); |
| 267 | } |
| 268 | |
| 269 | |
| 270 | void OS::DebugBreak() { |
| 271 | #if defined (__arm__) || defined(__thumb__) |
| 272 | asm("bkpt 0"); |
| 273 | #else |
| 274 | asm("int $3"); |
| 275 | #endif |
| 276 | } |
| 277 | |
| 278 | |
| 279 | class PosixMemoryMappedFile : public OS::MemoryMappedFile { |
| 280 | public: |
| 281 | PosixMemoryMappedFile(FILE* file, void* memory, int size) |
| 282 | : file_(file), memory_(memory), size_(size) { } |
| 283 | virtual ~PosixMemoryMappedFile(); |
| 284 | virtual void* memory() { return memory_; } |
| 285 | private: |
| 286 | FILE* file_; |
| 287 | void* memory_; |
| 288 | int size_; |
| 289 | }; |
| 290 | |
| 291 | |
| 292 | OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size, |
| 293 | void* initial) { |
| 294 | FILE* file = fopen(name, "w+"); |
| 295 | if (file == NULL) return NULL; |
| 296 | int result = fwrite(initial, size, 1, file); |
| 297 | if (result < 1) { |
| 298 | fclose(file); |
| 299 | return NULL; |
| 300 | } |
| 301 | void* memory = |
| 302 | mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); |
| 303 | return new PosixMemoryMappedFile(file, memory, size); |
| 304 | } |
| 305 | |
| 306 | |
| 307 | PosixMemoryMappedFile::~PosixMemoryMappedFile() { |
| 308 | if (memory_) munmap(memory_, size_); |
| 309 | fclose(file_); |
| 310 | } |
| 311 | |
| 312 | |
| 313 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 314 | static unsigned StringToLong(char* buffer) { |
| 315 | return static_cast<unsigned>(strtol(buffer, NULL, 16)); // NOLINT |
| 316 | } |
| 317 | #endif |
| 318 | |
| 319 | |
| 320 | void OS::LogSharedLibraryAddresses() { |
| 321 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 322 | static const int MAP_LENGTH = 1024; |
| 323 | int fd = open("/proc/self/maps", O_RDONLY); |
| 324 | if (fd < 0) return; |
| 325 | while (true) { |
| 326 | char addr_buffer[11]; |
| 327 | addr_buffer[0] = '0'; |
| 328 | addr_buffer[1] = 'x'; |
| 329 | addr_buffer[10] = 0; |
| 330 | int result = read(fd, addr_buffer + 2, 8); |
| 331 | if (result < 8) break; |
| 332 | unsigned start = StringToLong(addr_buffer); |
| 333 | result = read(fd, addr_buffer + 2, 1); |
| 334 | if (result < 1) break; |
| 335 | if (addr_buffer[2] != '-') break; |
| 336 | result = read(fd, addr_buffer + 2, 8); |
| 337 | if (result < 8) break; |
| 338 | unsigned end = StringToLong(addr_buffer); |
| 339 | char buffer[MAP_LENGTH]; |
| 340 | int bytes_read = -1; |
| 341 | do { |
| 342 | bytes_read++; |
| 343 | if (bytes_read >= MAP_LENGTH - 1) |
| 344 | break; |
| 345 | result = read(fd, buffer + bytes_read, 1); |
| 346 | if (result < 1) break; |
| 347 | } while (buffer[bytes_read] != '\n'); |
| 348 | buffer[bytes_read] = 0; |
| 349 | // Ignore mappings that are not executable. |
| 350 | if (buffer[3] != 'x') continue; |
| 351 | char* start_of_path = index(buffer, '/'); |
| 352 | // There may be no filename in this line. Skip to next. |
| 353 | if (start_of_path == NULL) continue; |
| 354 | buffer[bytes_read] = 0; |
| 355 | LOG(SharedLibraryEvent(start_of_path, start, end)); |
| 356 | } |
| 357 | close(fd); |
| 358 | #endif |
| 359 | } |
| 360 | |
| 361 | |
| 362 | int OS::StackWalk(OS::StackFrame* frames, int frames_size) { |
| 363 | void** addresses = NewArray<void*>(frames_size); |
| 364 | |
| 365 | int frames_count = backtrace(addresses, frames_size); |
| 366 | |
| 367 | char** symbols; |
| 368 | symbols = backtrace_symbols(addresses, frames_count); |
| 369 | if (symbols == NULL) { |
| 370 | DeleteArray(addresses); |
| 371 | return kStackWalkError; |
| 372 | } |
| 373 | |
| 374 | for (int i = 0; i < frames_count; i++) { |
| 375 | frames[i].address = addresses[i]; |
| 376 | // Format a text representation of the frame based on the information |
| 377 | // available. |
| 378 | SNPrintF(MutableCStrVector(frames[i].text, kStackWalkMaxTextLen), |
| 379 | "%s", |
| 380 | symbols[i]); |
| 381 | // Make sure line termination is in place. |
| 382 | frames[i].text[kStackWalkMaxTextLen - 1] = '\0'; |
| 383 | } |
| 384 | |
| 385 | DeleteArray(addresses); |
| 386 | free(symbols); |
| 387 | |
| 388 | return frames_count; |
| 389 | } |
| 390 | |
| 391 | |
| 392 | // Constants used for mmap. |
| 393 | static const int kMmapFd = -1; |
| 394 | static const int kMmapFdOffset = 0; |
| 395 | |
| 396 | |
| 397 | VirtualMemory::VirtualMemory(size_t size) { |
| 398 | address_ = mmap(NULL, size, PROT_NONE, |
| 399 | MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, |
| 400 | kMmapFd, kMmapFdOffset); |
| 401 | size_ = size; |
| 402 | } |
| 403 | |
| 404 | |
| 405 | VirtualMemory::~VirtualMemory() { |
| 406 | if (IsReserved()) { |
| 407 | if (0 == munmap(address(), size())) address_ = MAP_FAILED; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | |
| 412 | bool VirtualMemory::IsReserved() { |
| 413 | return address_ != MAP_FAILED; |
| 414 | } |
| 415 | |
| 416 | |
| 417 | bool VirtualMemory::Commit(void* address, size_t size, bool executable) { |
| 418 | int prot = PROT_READ | PROT_WRITE | (executable ? PROT_EXEC : 0); |
| 419 | if (MAP_FAILED == mmap(address, size, prot, |
| 420 | MAP_PRIVATE | MAP_ANON | MAP_FIXED, |
| 421 | kMmapFd, kMmapFdOffset)) { |
| 422 | return false; |
| 423 | } |
| 424 | |
| 425 | UpdateAllocatedSpaceLimits(address, size); |
| 426 | return true; |
| 427 | } |
| 428 | |
| 429 | |
| 430 | bool VirtualMemory::Uncommit(void* address, size_t size) { |
| 431 | return mmap(address, size, PROT_NONE, |
| 432 | MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, |
| 433 | kMmapFd, kMmapFdOffset) != MAP_FAILED; |
| 434 | } |
| 435 | |
| 436 | |
| 437 | class ThreadHandle::PlatformData : public Malloced { |
| 438 | public: |
| 439 | explicit PlatformData(ThreadHandle::Kind kind) { |
| 440 | Initialize(kind); |
| 441 | } |
| 442 | |
| 443 | void Initialize(ThreadHandle::Kind kind) { |
| 444 | switch (kind) { |
| 445 | case ThreadHandle::SELF: thread_ = pthread_self(); break; |
| 446 | case ThreadHandle::INVALID: thread_ = kNoThread; break; |
| 447 | } |
| 448 | } |
| 449 | pthread_t thread_; // Thread handle for pthread. |
| 450 | }; |
| 451 | |
| 452 | |
| 453 | ThreadHandle::ThreadHandle(Kind kind) { |
| 454 | data_ = new PlatformData(kind); |
| 455 | } |
| 456 | |
| 457 | |
| 458 | void ThreadHandle::Initialize(ThreadHandle::Kind kind) { |
| 459 | data_->Initialize(kind); |
| 460 | } |
| 461 | |
| 462 | |
| 463 | ThreadHandle::~ThreadHandle() { |
| 464 | delete data_; |
| 465 | } |
| 466 | |
| 467 | |
| 468 | bool ThreadHandle::IsSelf() const { |
| 469 | return pthread_equal(data_->thread_, pthread_self()); |
| 470 | } |
| 471 | |
| 472 | |
| 473 | bool ThreadHandle::IsValid() const { |
| 474 | return data_->thread_ != kNoThread; |
| 475 | } |
| 476 | |
| 477 | |
| 478 | Thread::Thread() : ThreadHandle(ThreadHandle::INVALID) { |
| 479 | } |
| 480 | |
| 481 | |
| 482 | Thread::~Thread() { |
| 483 | } |
| 484 | |
| 485 | |
| 486 | static void* ThreadEntry(void* arg) { |
| 487 | Thread* thread = reinterpret_cast<Thread*>(arg); |
| 488 | // This is also initialized by the first argument to pthread_create() but we |
| 489 | // don't know which thread will run first (the original thread or the new |
| 490 | // one) so we initialize it here too. |
| 491 | thread->thread_handle_data()->thread_ = pthread_self(); |
| 492 | ASSERT(thread->IsValid()); |
| 493 | thread->Run(); |
| 494 | return NULL; |
| 495 | } |
| 496 | |
| 497 | |
| 498 | void Thread::Start() { |
| 499 | pthread_create(&thread_handle_data()->thread_, NULL, ThreadEntry, this); |
| 500 | ASSERT(IsValid()); |
| 501 | } |
| 502 | |
| 503 | |
| 504 | void Thread::Join() { |
| 505 | pthread_join(thread_handle_data()->thread_, NULL); |
| 506 | } |
| 507 | |
| 508 | |
| 509 | Thread::LocalStorageKey Thread::CreateThreadLocalKey() { |
| 510 | pthread_key_t key; |
| 511 | int result = pthread_key_create(&key, NULL); |
| 512 | USE(result); |
| 513 | ASSERT(result == 0); |
| 514 | return static_cast<LocalStorageKey>(key); |
| 515 | } |
| 516 | |
| 517 | |
| 518 | void Thread::DeleteThreadLocalKey(LocalStorageKey key) { |
| 519 | pthread_key_t pthread_key = static_cast<pthread_key_t>(key); |
| 520 | int result = pthread_key_delete(pthread_key); |
| 521 | USE(result); |
| 522 | ASSERT(result == 0); |
| 523 | } |
| 524 | |
| 525 | |
| 526 | void* Thread::GetThreadLocal(LocalStorageKey key) { |
| 527 | pthread_key_t pthread_key = static_cast<pthread_key_t>(key); |
| 528 | return pthread_getspecific(pthread_key); |
| 529 | } |
| 530 | |
| 531 | |
| 532 | void Thread::SetThreadLocal(LocalStorageKey key, void* value) { |
| 533 | pthread_key_t pthread_key = static_cast<pthread_key_t>(key); |
| 534 | pthread_setspecific(pthread_key, value); |
| 535 | } |
| 536 | |
| 537 | |
| 538 | void Thread::YieldCPU() { |
| 539 | sched_yield(); |
| 540 | } |
| 541 | |
| 542 | |
| 543 | class FreeBSDMutex : public Mutex { |
| 544 | public: |
| 545 | |
| 546 | FreeBSDMutex() { |
| 547 | pthread_mutexattr_t attrs; |
| 548 | int result = pthread_mutexattr_init(&attrs); |
| 549 | ASSERT(result == 0); |
| 550 | result = pthread_mutexattr_settype(&attrs, PTHREAD_MUTEX_RECURSIVE); |
| 551 | ASSERT(result == 0); |
| 552 | result = pthread_mutex_init(&mutex_, &attrs); |
| 553 | ASSERT(result == 0); |
| 554 | } |
| 555 | |
| 556 | virtual ~FreeBSDMutex() { pthread_mutex_destroy(&mutex_); } |
| 557 | |
| 558 | virtual int Lock() { |
| 559 | int result = pthread_mutex_lock(&mutex_); |
| 560 | return result; |
| 561 | } |
| 562 | |
| 563 | virtual int Unlock() { |
| 564 | int result = pthread_mutex_unlock(&mutex_); |
| 565 | return result; |
| 566 | } |
| 567 | |
| 568 | private: |
| 569 | pthread_mutex_t mutex_; // Pthread mutex for POSIX platforms. |
| 570 | }; |
| 571 | |
| 572 | |
| 573 | Mutex* OS::CreateMutex() { |
| 574 | return new FreeBSDMutex(); |
| 575 | } |
| 576 | |
| 577 | |
| 578 | class FreeBSDSemaphore : public Semaphore { |
| 579 | public: |
| 580 | explicit FreeBSDSemaphore(int count) { sem_init(&sem_, 0, count); } |
| 581 | virtual ~FreeBSDSemaphore() { sem_destroy(&sem_); } |
| 582 | |
| 583 | virtual void Wait(); |
| 584 | virtual void Signal() { sem_post(&sem_); } |
| 585 | private: |
| 586 | sem_t sem_; |
| 587 | }; |
| 588 | |
| 589 | void FreeBSDSemaphore::Wait() { |
| 590 | while (true) { |
| 591 | int result = sem_wait(&sem_); |
| 592 | if (result == 0) return; // Successfully got semaphore. |
| 593 | CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup. |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | Semaphore* OS::CreateSemaphore(int count) { |
| 598 | return new FreeBSDSemaphore(count); |
| 599 | } |
| 600 | |
| 601 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 602 | |
| 603 | static Sampler* active_sampler_ = NULL; |
| 604 | |
| 605 | static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) { |
| 606 | USE(info); |
| 607 | if (signal != SIGPROF) return; |
| 608 | if (active_sampler_ == NULL) return; |
| 609 | |
| 610 | TickSample sample; |
| 611 | |
| 612 | // If profiling, we extract the current pc and sp. |
| 613 | if (active_sampler_->IsProfiling()) { |
| 614 | // Extracting the sample from the context is extremely machine dependent. |
| 615 | ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context); |
| 616 | mcontext_t& mcontext = ucontext->uc_mcontext; |
| 617 | #if defined (__arm__) || defined(__thumb__) |
| 618 | sample.pc = mcontext.mc_r15; |
| 619 | sample.sp = mcontext.mc_r13; |
| 620 | #else |
| 621 | sample.pc = mcontext.mc_eip; |
| 622 | sample.sp = mcontext.mc_esp; |
| 623 | #endif |
| 624 | } |
| 625 | |
| 626 | // We always sample the VM state. |
| 627 | sample.state = Logger::state(); |
| 628 | |
| 629 | active_sampler_->Tick(&sample); |
| 630 | } |
| 631 | |
| 632 | |
| 633 | class Sampler::PlatformData : public Malloced { |
| 634 | public: |
| 635 | PlatformData() { |
| 636 | signal_handler_installed_ = false; |
| 637 | } |
| 638 | |
| 639 | bool signal_handler_installed_; |
| 640 | struct sigaction old_signal_handler_; |
| 641 | struct itimerval old_timer_value_; |
| 642 | }; |
| 643 | |
| 644 | |
| 645 | Sampler::Sampler(int interval, bool profiling) |
| 646 | : interval_(interval), profiling_(profiling), active_(false) { |
| 647 | data_ = new PlatformData(); |
| 648 | } |
| 649 | |
| 650 | |
| 651 | Sampler::~Sampler() { |
| 652 | delete data_; |
| 653 | } |
| 654 | |
| 655 | |
| 656 | void Sampler::Start() { |
| 657 | // There can only be one active sampler at the time on POSIX |
| 658 | // platforms. |
| 659 | if (active_sampler_ != NULL) return; |
| 660 | |
| 661 | // Request profiling signals. |
| 662 | struct sigaction sa; |
| 663 | sa.sa_sigaction = ProfilerSignalHandler; |
| 664 | sigemptyset(&sa.sa_mask); |
| 665 | sa.sa_flags = SA_SIGINFO; |
| 666 | if (sigaction(SIGPROF, &sa, &data_->old_signal_handler_) != 0) return; |
| 667 | data_->signal_handler_installed_ = true; |
| 668 | |
| 669 | // Set the itimer to generate a tick for each interval. |
| 670 | itimerval itimer; |
| 671 | itimer.it_interval.tv_sec = interval_ / 1000; |
| 672 | itimer.it_interval.tv_usec = (interval_ % 1000) * 1000; |
| 673 | itimer.it_value.tv_sec = itimer.it_interval.tv_sec; |
| 674 | itimer.it_value.tv_usec = itimer.it_interval.tv_usec; |
| 675 | setitimer(ITIMER_PROF, &itimer, &data_->old_timer_value_); |
| 676 | |
| 677 | // Set this sampler as the active sampler. |
| 678 | active_sampler_ = this; |
| 679 | active_ = true; |
| 680 | } |
| 681 | |
| 682 | |
| 683 | void Sampler::Stop() { |
| 684 | // Restore old signal handler |
| 685 | if (data_->signal_handler_installed_) { |
| 686 | setitimer(ITIMER_PROF, &data_->old_timer_value_, NULL); |
| 687 | sigaction(SIGPROF, &data_->old_signal_handler_, 0); |
| 688 | data_->signal_handler_installed_ = false; |
| 689 | } |
| 690 | |
| 691 | // This sampler is no longer the active sampler. |
| 692 | active_sampler_ = NULL; |
| 693 | active_ = false; |
| 694 | } |
| 695 | |
| 696 | #endif // ENABLE_LOGGING_AND_PROFILING |
| 697 | |
| 698 | } } // namespace v8::internal |