fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1 | // Copyright 2006-2011 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 Cygwin goes here. For the POSIX comaptible parts |
| 29 | // the implementation is in platform-posix.cc. |
| 30 | |
| 31 | #include <errno.h> |
| 32 | #include <pthread.h> |
| 33 | #include <semaphore.h> |
| 34 | #include <stdarg.h> |
| 35 | #include <strings.h> // index |
| 36 | #include <sys/time.h> |
| 37 | #include <sys/mman.h> // mmap & munmap |
| 38 | #include <unistd.h> // sysconf |
| 39 | |
| 40 | #undef MAP_TYPE |
| 41 | |
| 42 | #include "v8.h" |
| 43 | |
| 44 | #include "platform.h" |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 45 | #include "v8threads.h" |
| 46 | #include "vm-state-inl.h" |
| 47 | #include "win32-headers.h" |
| 48 | |
| 49 | namespace v8 { |
| 50 | namespace internal { |
| 51 | |
| 52 | // 0 is never a valid thread id |
| 53 | static const pthread_t kNoThread = (pthread_t) 0; |
| 54 | |
| 55 | |
| 56 | double ceiling(double x) { |
| 57 | return ceil(x); |
| 58 | } |
| 59 | |
| 60 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 61 | static Mutex* limit_mutex = NULL; |
| 62 | |
| 63 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 64 | void OS::Setup() { |
| 65 | // Seed the random number generator. |
| 66 | // Convert the current time to a 64-bit integer first, before converting it |
| 67 | // to an unsigned. Going directly can cause an overflow and the seed to be |
| 68 | // set to all ones. The seed will be identical for different instances that |
| 69 | // call this setup code within the same millisecond. |
| 70 | uint64_t seed = static_cast<uint64_t>(TimeCurrentMillis()); |
| 71 | srandom(static_cast<unsigned int>(seed)); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 72 | limit_mutex = CreateMutex(); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | |
| 76 | uint64_t OS::CpuFeaturesImpliedByPlatform() { |
| 77 | return 0; // Nothing special about Cygwin. |
| 78 | } |
| 79 | |
| 80 | |
| 81 | int OS::ActivationFrameAlignment() { |
| 82 | // With gcc 4.4 the tree vectorization optimizer can generate code |
| 83 | // that requires 16 byte alignment such as movdqa on x86. |
| 84 | return 16; |
| 85 | } |
| 86 | |
| 87 | |
| 88 | void OS::ReleaseStore(volatile AtomicWord* ptr, AtomicWord value) { |
| 89 | __asm__ __volatile__("" : : : "memory"); |
| 90 | // An x86 store acts as a release barrier. |
| 91 | *ptr = value; |
| 92 | } |
| 93 | |
| 94 | const char* OS::LocalTimezone(double time) { |
| 95 | if (isnan(time)) return ""; |
| 96 | time_t tv = static_cast<time_t>(floor(time/msPerSecond)); |
| 97 | struct tm* t = localtime(&tv); |
| 98 | if (NULL == t) return ""; |
| 99 | return tzname[0]; // The location of the timezone string on Cygwin. |
| 100 | } |
| 101 | |
| 102 | |
| 103 | double OS::LocalTimeOffset() { |
| 104 | // On Cygwin, struct tm does not contain a tm_gmtoff field. |
| 105 | time_t utc = time(NULL); |
| 106 | ASSERT(utc != -1); |
| 107 | struct tm* loc = localtime(&utc); |
| 108 | ASSERT(loc != NULL); |
| 109 | // time - localtime includes any daylight savings offset, so subtract it. |
| 110 | return static_cast<double>((mktime(loc) - utc) * msPerSecond - |
| 111 | (loc->tm_isdst > 0 ? 3600 * msPerSecond : 0)); |
| 112 | } |
| 113 | |
| 114 | |
| 115 | // We keep the lowest and highest addresses mapped as a quick way of |
| 116 | // determining that pointers are outside the heap (used mostly in assertions |
| 117 | // and verification). The estimate is conservative, ie, not all addresses in |
| 118 | // 'allocated' space are actually allocated to our heap. The range is |
| 119 | // [lowest, highest), inclusive on the low and and exclusive on the high end. |
| 120 | static void* lowest_ever_allocated = reinterpret_cast<void*>(-1); |
| 121 | static void* highest_ever_allocated = reinterpret_cast<void*>(0); |
| 122 | |
| 123 | |
| 124 | static void UpdateAllocatedSpaceLimits(void* address, int size) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 125 | ASSERT(limit_mutex != NULL); |
| 126 | ScopedLock lock(limit_mutex); |
| 127 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 128 | lowest_ever_allocated = Min(lowest_ever_allocated, address); |
| 129 | highest_ever_allocated = |
| 130 | Max(highest_ever_allocated, |
| 131 | reinterpret_cast<void*>(reinterpret_cast<char*>(address) + size)); |
| 132 | } |
| 133 | |
| 134 | |
| 135 | bool OS::IsOutsideAllocatedSpace(void* address) { |
| 136 | return address < lowest_ever_allocated || address >= highest_ever_allocated; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | size_t OS::AllocateAlignment() { |
| 141 | return sysconf(_SC_PAGESIZE); |
| 142 | } |
| 143 | |
| 144 | |
| 145 | void* OS::Allocate(const size_t requested, |
| 146 | size_t* allocated, |
| 147 | bool is_executable) { |
| 148 | const size_t msize = RoundUp(requested, sysconf(_SC_PAGESIZE)); |
| 149 | int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); |
| 150 | void* mbase = mmap(NULL, msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 151 | if (mbase == MAP_FAILED) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 152 | LOG(ISOLATE, StringEvent("OS::Allocate", "mmap failed")); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 153 | return NULL; |
| 154 | } |
| 155 | *allocated = msize; |
| 156 | UpdateAllocatedSpaceLimits(mbase, msize); |
| 157 | return mbase; |
| 158 | } |
| 159 | |
| 160 | |
| 161 | void OS::Free(void* address, const size_t size) { |
| 162 | // TODO(1240712): munmap has a return value which is ignored here. |
| 163 | int result = munmap(address, size); |
| 164 | USE(result); |
| 165 | ASSERT(result == 0); |
| 166 | } |
| 167 | |
| 168 | |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 169 | void OS::ProtectCode(void* address, const size_t size) { |
| 170 | DWORD old_protect; |
| 171 | VirtualProtect(address, size, PAGE_EXECUTE_READ, &old_protect); |
| 172 | } |
| 173 | |
| 174 | |
| 175 | void OS::Guard(void* address, const size_t size) { |
| 176 | DWORD oldprotect; |
| 177 | VirtualProtect(address, size, PAGE_READONLY | PAGE_GUARD, &oldprotect); |
| 178 | } |
| 179 | |
| 180 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 181 | void OS::Sleep(int milliseconds) { |
| 182 | unsigned int ms = static_cast<unsigned int>(milliseconds); |
| 183 | usleep(1000 * ms); |
| 184 | } |
| 185 | |
| 186 | |
| 187 | void OS::Abort() { |
| 188 | // Redirect to std abort to signal abnormal program termination. |
| 189 | abort(); |
| 190 | } |
| 191 | |
| 192 | |
| 193 | void OS::DebugBreak() { |
| 194 | asm("int $3"); |
| 195 | } |
| 196 | |
| 197 | |
| 198 | class PosixMemoryMappedFile : public OS::MemoryMappedFile { |
| 199 | public: |
| 200 | PosixMemoryMappedFile(FILE* file, void* memory, int size) |
| 201 | : file_(file), memory_(memory), size_(size) { } |
| 202 | virtual ~PosixMemoryMappedFile(); |
| 203 | virtual void* memory() { return memory_; } |
| 204 | virtual int size() { return size_; } |
| 205 | private: |
| 206 | FILE* file_; |
| 207 | void* memory_; |
| 208 | int size_; |
| 209 | }; |
| 210 | |
| 211 | |
| 212 | OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) { |
| 213 | FILE* file = fopen(name, "r+"); |
| 214 | if (file == NULL) return NULL; |
| 215 | |
| 216 | fseek(file, 0, SEEK_END); |
| 217 | int size = ftell(file); |
| 218 | |
| 219 | void* memory = |
| 220 | mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); |
| 221 | return new PosixMemoryMappedFile(file, memory, size); |
| 222 | } |
| 223 | |
| 224 | |
| 225 | OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size, |
| 226 | void* initial) { |
| 227 | FILE* file = fopen(name, "w+"); |
| 228 | if (file == NULL) return NULL; |
| 229 | int result = fwrite(initial, size, 1, file); |
| 230 | if (result < 1) { |
| 231 | fclose(file); |
| 232 | return NULL; |
| 233 | } |
| 234 | void* memory = |
| 235 | mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); |
| 236 | return new PosixMemoryMappedFile(file, memory, size); |
| 237 | } |
| 238 | |
| 239 | |
| 240 | PosixMemoryMappedFile::~PosixMemoryMappedFile() { |
| 241 | if (memory_) munmap(memory_, size_); |
| 242 | fclose(file_); |
| 243 | } |
| 244 | |
| 245 | |
| 246 | void OS::LogSharedLibraryAddresses() { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 247 | // This function assumes that the layout of the file is as follows: |
| 248 | // hex_start_addr-hex_end_addr rwxp <unused data> [binary_file_name] |
| 249 | // If we encounter an unexpected situation we abort scanning further entries. |
| 250 | FILE* fp = fopen("/proc/self/maps", "r"); |
| 251 | if (fp == NULL) return; |
| 252 | |
| 253 | // Allocate enough room to be able to store a full file name. |
| 254 | const int kLibNameLen = FILENAME_MAX + 1; |
| 255 | char* lib_name = reinterpret_cast<char*>(malloc(kLibNameLen)); |
| 256 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 257 | i::Isolate* isolate = ISOLATE; |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 258 | // This loop will terminate once the scanning hits an EOF. |
| 259 | while (true) { |
| 260 | uintptr_t start, end; |
| 261 | char attr_r, attr_w, attr_x, attr_p; |
| 262 | // Parse the addresses and permission bits at the beginning of the line. |
| 263 | if (fscanf(fp, "%" V8PRIxPTR "-%" V8PRIxPTR, &start, &end) != 2) break; |
| 264 | if (fscanf(fp, " %c%c%c%c", &attr_r, &attr_w, &attr_x, &attr_p) != 4) break; |
| 265 | |
| 266 | int c; |
| 267 | if (attr_r == 'r' && attr_w != 'w' && attr_x == 'x') { |
| 268 | // Found a read-only executable entry. Skip characters until we reach |
| 269 | // the beginning of the filename or the end of the line. |
| 270 | do { |
| 271 | c = getc(fp); |
| 272 | } while ((c != EOF) && (c != '\n') && (c != '/')); |
| 273 | if (c == EOF) break; // EOF: Was unexpected, just exit. |
| 274 | |
| 275 | // Process the filename if found. |
| 276 | if (c == '/') { |
| 277 | ungetc(c, fp); // Push the '/' back into the stream to be read below. |
| 278 | |
| 279 | // Read to the end of the line. Exit if the read fails. |
| 280 | if (fgets(lib_name, kLibNameLen, fp) == NULL) break; |
| 281 | |
| 282 | // Drop the newline character read by fgets. We do not need to check |
| 283 | // for a zero-length string because we know that we at least read the |
| 284 | // '/' character. |
| 285 | lib_name[strlen(lib_name) - 1] = '\0'; |
| 286 | } else { |
| 287 | // No library name found, just record the raw address range. |
| 288 | snprintf(lib_name, kLibNameLen, |
| 289 | "%08" V8PRIxPTR "-%08" V8PRIxPTR, start, end); |
| 290 | } |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 291 | LOG(isolate, SharedLibraryEvent(lib_name, start, end)); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 292 | } else { |
| 293 | // Entry not describing executable data. Skip to end of line to setup |
| 294 | // reading the next entry. |
| 295 | do { |
| 296 | c = getc(fp); |
| 297 | } while ((c != EOF) && (c != '\n')); |
| 298 | if (c == EOF) break; |
| 299 | } |
| 300 | } |
| 301 | free(lib_name); |
| 302 | fclose(fp); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | |
| 306 | void OS::SignalCodeMovingGC() { |
| 307 | // Nothing to do on Cygwin. |
| 308 | } |
| 309 | |
| 310 | |
| 311 | int OS::StackWalk(Vector<OS::StackFrame> frames) { |
| 312 | // Not supported on Cygwin. |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 317 | // The VirtualMemory implementation is taken from platform-win32.cc. |
| 318 | // The mmap-based virtual memory implementation as it is used on most posix |
| 319 | // platforms does not work well because Cygwin does not support MAP_FIXED. |
| 320 | // This causes VirtualMemory::Commit to not always commit the memory region |
| 321 | // specified. |
| 322 | |
| 323 | bool VirtualMemory::IsReserved() { |
| 324 | return address_ != NULL; |
| 325 | } |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 326 | |
| 327 | |
| 328 | VirtualMemory::VirtualMemory(size_t size) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 329 | address_ = VirtualAlloc(NULL, size, MEM_RESERVE, PAGE_NOACCESS); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 330 | size_ = size; |
| 331 | } |
| 332 | |
| 333 | |
| 334 | VirtualMemory::~VirtualMemory() { |
| 335 | if (IsReserved()) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 336 | if (0 == VirtualFree(address(), 0, MEM_RELEASE)) address_ = NULL; |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | |
| 340 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 341 | bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 342 | int prot = is_executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE; |
| 343 | if (NULL == VirtualAlloc(address, size, MEM_COMMIT, prot)) { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 344 | return false; |
| 345 | } |
| 346 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 347 | UpdateAllocatedSpaceLimits(address, static_cast<int>(size)); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 348 | return true; |
| 349 | } |
| 350 | |
| 351 | |
| 352 | bool VirtualMemory::Uncommit(void* address, size_t size) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 353 | ASSERT(IsReserved()); |
| 354 | return VirtualFree(address, size, MEM_DECOMMIT) != false; |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 358 | class Thread::PlatformData : public Malloced { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 359 | public: |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 360 | PlatformData() : thread_(kNoThread) {} |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 361 | pthread_t thread_; // Thread handle for pthread. |
| 362 | }; |
| 363 | |
| 364 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 365 | |
| 366 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 367 | Thread::Thread(const Options& options) |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 368 | : data_(new PlatformData), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 369 | stack_size_(options.stack_size) { |
| 370 | set_name(options.name); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 374 | Thread::Thread(const char* name) |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 375 | : data_(new PlatformData), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 376 | stack_size_(0) { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 377 | set_name(name); |
| 378 | } |
| 379 | |
| 380 | |
| 381 | Thread::~Thread() { |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 382 | delete data_; |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | |
| 386 | static void* ThreadEntry(void* arg) { |
| 387 | Thread* thread = reinterpret_cast<Thread*>(arg); |
| 388 | // This is also initialized by the first argument to pthread_create() but we |
| 389 | // don't know which thread will run first (the original thread or the new |
| 390 | // one) so we initialize it here too. |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 391 | thread->data()->thread_ = pthread_self(); |
| 392 | ASSERT(thread->data()->thread_ != kNoThread); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 393 | thread->Run(); |
| 394 | return NULL; |
| 395 | } |
| 396 | |
| 397 | |
| 398 | void Thread::set_name(const char* name) { |
| 399 | strncpy(name_, name, sizeof(name_)); |
| 400 | name_[sizeof(name_) - 1] = '\0'; |
| 401 | } |
| 402 | |
| 403 | |
| 404 | void Thread::Start() { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 405 | pthread_attr_t* attr_ptr = NULL; |
| 406 | pthread_attr_t attr; |
| 407 | if (stack_size_ > 0) { |
| 408 | pthread_attr_init(&attr); |
| 409 | pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_)); |
| 410 | attr_ptr = &attr; |
| 411 | } |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 412 | pthread_create(&data_->thread_, attr_ptr, ThreadEntry, this); |
| 413 | ASSERT(data_->thread_ != kNoThread); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | |
| 417 | void Thread::Join() { |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 418 | pthread_join(data_->thread_, NULL); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | |
| 422 | static inline Thread::LocalStorageKey PthreadKeyToLocalKey( |
| 423 | pthread_key_t pthread_key) { |
| 424 | // We need to cast pthread_key_t to Thread::LocalStorageKey in two steps |
| 425 | // because pthread_key_t is a pointer type on Cygwin. This will probably not |
| 426 | // work on 64-bit platforms, but Cygwin doesn't support 64-bit anyway. |
| 427 | STATIC_ASSERT(sizeof(Thread::LocalStorageKey) == sizeof(pthread_key_t)); |
| 428 | intptr_t ptr_key = reinterpret_cast<intptr_t>(pthread_key); |
| 429 | return static_cast<Thread::LocalStorageKey>(ptr_key); |
| 430 | } |
| 431 | |
| 432 | |
| 433 | static inline pthread_key_t LocalKeyToPthreadKey( |
| 434 | Thread::LocalStorageKey local_key) { |
| 435 | STATIC_ASSERT(sizeof(Thread::LocalStorageKey) == sizeof(pthread_key_t)); |
| 436 | intptr_t ptr_key = static_cast<intptr_t>(local_key); |
| 437 | return reinterpret_cast<pthread_key_t>(ptr_key); |
| 438 | } |
| 439 | |
| 440 | |
| 441 | Thread::LocalStorageKey Thread::CreateThreadLocalKey() { |
| 442 | pthread_key_t key; |
| 443 | int result = pthread_key_create(&key, NULL); |
| 444 | USE(result); |
| 445 | ASSERT(result == 0); |
| 446 | return PthreadKeyToLocalKey(key); |
| 447 | } |
| 448 | |
| 449 | |
| 450 | void Thread::DeleteThreadLocalKey(LocalStorageKey key) { |
| 451 | pthread_key_t pthread_key = LocalKeyToPthreadKey(key); |
| 452 | int result = pthread_key_delete(pthread_key); |
| 453 | USE(result); |
| 454 | ASSERT(result == 0); |
| 455 | } |
| 456 | |
| 457 | |
| 458 | void* Thread::GetThreadLocal(LocalStorageKey key) { |
| 459 | pthread_key_t pthread_key = LocalKeyToPthreadKey(key); |
| 460 | return pthread_getspecific(pthread_key); |
| 461 | } |
| 462 | |
| 463 | |
| 464 | void Thread::SetThreadLocal(LocalStorageKey key, void* value) { |
| 465 | pthread_key_t pthread_key = LocalKeyToPthreadKey(key); |
| 466 | pthread_setspecific(pthread_key, value); |
| 467 | } |
| 468 | |
| 469 | |
| 470 | void Thread::YieldCPU() { |
| 471 | sched_yield(); |
| 472 | } |
| 473 | |
| 474 | |
| 475 | class CygwinMutex : public Mutex { |
| 476 | public: |
| 477 | |
| 478 | CygwinMutex() { |
| 479 | pthread_mutexattr_t attrs; |
| 480 | memset(&attrs, 0, sizeof(attrs)); |
| 481 | |
| 482 | int result = pthread_mutexattr_init(&attrs); |
| 483 | ASSERT(result == 0); |
| 484 | result = pthread_mutexattr_settype(&attrs, PTHREAD_MUTEX_RECURSIVE); |
| 485 | ASSERT(result == 0); |
| 486 | result = pthread_mutex_init(&mutex_, &attrs); |
| 487 | ASSERT(result == 0); |
| 488 | } |
| 489 | |
| 490 | virtual ~CygwinMutex() { pthread_mutex_destroy(&mutex_); } |
| 491 | |
| 492 | virtual int Lock() { |
| 493 | int result = pthread_mutex_lock(&mutex_); |
| 494 | return result; |
| 495 | } |
| 496 | |
| 497 | virtual int Unlock() { |
| 498 | int result = pthread_mutex_unlock(&mutex_); |
| 499 | return result; |
| 500 | } |
| 501 | |
| 502 | virtual bool TryLock() { |
| 503 | int result = pthread_mutex_trylock(&mutex_); |
| 504 | // Return false if the lock is busy and locking failed. |
| 505 | if (result == EBUSY) { |
| 506 | return false; |
| 507 | } |
| 508 | ASSERT(result == 0); // Verify no other errors. |
| 509 | return true; |
| 510 | } |
| 511 | |
| 512 | private: |
| 513 | pthread_mutex_t mutex_; // Pthread mutex for POSIX platforms. |
| 514 | }; |
| 515 | |
| 516 | |
| 517 | Mutex* OS::CreateMutex() { |
| 518 | return new CygwinMutex(); |
| 519 | } |
| 520 | |
| 521 | |
| 522 | class CygwinSemaphore : public Semaphore { |
| 523 | public: |
| 524 | explicit CygwinSemaphore(int count) { sem_init(&sem_, 0, count); } |
| 525 | virtual ~CygwinSemaphore() { sem_destroy(&sem_); } |
| 526 | |
| 527 | virtual void Wait(); |
| 528 | virtual bool Wait(int timeout); |
| 529 | virtual void Signal() { sem_post(&sem_); } |
| 530 | private: |
| 531 | sem_t sem_; |
| 532 | }; |
| 533 | |
| 534 | |
| 535 | void CygwinSemaphore::Wait() { |
| 536 | while (true) { |
| 537 | int result = sem_wait(&sem_); |
| 538 | if (result == 0) return; // Successfully got semaphore. |
| 539 | CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup. |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | |
| 544 | #ifndef TIMEVAL_TO_TIMESPEC |
| 545 | #define TIMEVAL_TO_TIMESPEC(tv, ts) do { \ |
| 546 | (ts)->tv_sec = (tv)->tv_sec; \ |
| 547 | (ts)->tv_nsec = (tv)->tv_usec * 1000; \ |
| 548 | } while (false) |
| 549 | #endif |
| 550 | |
| 551 | |
| 552 | bool CygwinSemaphore::Wait(int timeout) { |
| 553 | const long kOneSecondMicros = 1000000; // NOLINT |
| 554 | |
| 555 | // Split timeout into second and nanosecond parts. |
| 556 | struct timeval delta; |
| 557 | delta.tv_usec = timeout % kOneSecondMicros; |
| 558 | delta.tv_sec = timeout / kOneSecondMicros; |
| 559 | |
| 560 | struct timeval current_time; |
| 561 | // Get the current time. |
| 562 | if (gettimeofday(¤t_time, NULL) == -1) { |
| 563 | return false; |
| 564 | } |
| 565 | |
| 566 | // Calculate time for end of timeout. |
| 567 | struct timeval end_time; |
| 568 | timeradd(¤t_time, &delta, &end_time); |
| 569 | |
| 570 | struct timespec ts; |
| 571 | TIMEVAL_TO_TIMESPEC(&end_time, &ts); |
| 572 | // Wait for semaphore signalled or timeout. |
| 573 | while (true) { |
| 574 | int result = sem_timedwait(&sem_, &ts); |
| 575 | if (result == 0) return true; // Successfully got semaphore. |
| 576 | if (result == -1 && errno == ETIMEDOUT) return false; // Timeout. |
| 577 | CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup. |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | |
| 582 | Semaphore* OS::CreateSemaphore(int count) { |
| 583 | return new CygwinSemaphore(count); |
| 584 | } |
| 585 | |
| 586 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 587 | // ---------------------------------------------------------------------------- |
| 588 | // Cygwin profiler support. |
| 589 | // |
| 590 | // On Cygwin we use the same sampler implementation as on win32. |
| 591 | |
| 592 | class Sampler::PlatformData : public Malloced { |
| 593 | public: |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 594 | // Get a handle to the calling thread. This is the thread that we are |
| 595 | // going to profile. We need to make a copy of the handle because we are |
| 596 | // going to use it in the sampler thread. Using GetThreadHandle() will |
| 597 | // not work in this case. We're using OpenThread because DuplicateHandle |
| 598 | // for some reason doesn't work in Chrome's sandbox. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 599 | PlatformData() : profiled_thread_(OpenThread(THREAD_GET_CONTEXT | |
| 600 | THREAD_SUSPEND_RESUME | |
| 601 | THREAD_QUERY_INFORMATION, |
| 602 | false, |
| 603 | GetCurrentThreadId())) {} |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 604 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 605 | ~PlatformData() { |
| 606 | if (profiled_thread_ != NULL) { |
| 607 | CloseHandle(profiled_thread_); |
| 608 | profiled_thread_ = NULL; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | HANDLE profiled_thread() { return profiled_thread_; } |
| 613 | |
| 614 | private: |
| 615 | HANDLE profiled_thread_; |
| 616 | }; |
| 617 | |
| 618 | |
| 619 | class SamplerThread : public Thread { |
| 620 | public: |
| 621 | explicit SamplerThread(int interval) |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 622 | : Thread("SamplerThread"), |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 623 | interval_(interval) {} |
| 624 | |
| 625 | static void AddActiveSampler(Sampler* sampler) { |
| 626 | ScopedLock lock(mutex_); |
| 627 | SamplerRegistry::AddActiveSampler(sampler); |
| 628 | if (instance_ == NULL) { |
| 629 | instance_ = new SamplerThread(sampler->interval()); |
| 630 | instance_->Start(); |
| 631 | } else { |
| 632 | ASSERT(instance_->interval_ == sampler->interval()); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | static void RemoveActiveSampler(Sampler* sampler) { |
| 637 | ScopedLock lock(mutex_); |
| 638 | SamplerRegistry::RemoveActiveSampler(sampler); |
| 639 | if (SamplerRegistry::GetState() == SamplerRegistry::HAS_NO_SAMPLERS) { |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 640 | RuntimeProfiler::StopRuntimeProfilerThreadBeforeShutdown(instance_); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 641 | delete instance_; |
| 642 | instance_ = NULL; |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | // Implement Thread::Run(). |
| 647 | virtual void Run() { |
| 648 | SamplerRegistry::State state; |
| 649 | while ((state = SamplerRegistry::GetState()) != |
| 650 | SamplerRegistry::HAS_NO_SAMPLERS) { |
| 651 | bool cpu_profiling_enabled = |
| 652 | (state == SamplerRegistry::HAS_CPU_PROFILING_SAMPLERS); |
| 653 | bool runtime_profiler_enabled = RuntimeProfiler::IsEnabled(); |
| 654 | // When CPU profiling is enabled both JavaScript and C++ code is |
| 655 | // profiled. We must not suspend. |
| 656 | if (!cpu_profiling_enabled) { |
| 657 | if (rate_limiter_.SuspendIfNecessary()) continue; |
| 658 | } |
| 659 | if (cpu_profiling_enabled) { |
| 660 | if (!SamplerRegistry::IterateActiveSamplers(&DoCpuProfile, this)) { |
| 661 | return; |
| 662 | } |
| 663 | } |
| 664 | if (runtime_profiler_enabled) { |
| 665 | if (!SamplerRegistry::IterateActiveSamplers(&DoRuntimeProfile, NULL)) { |
| 666 | return; |
| 667 | } |
| 668 | } |
| 669 | OS::Sleep(interval_); |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | static void DoCpuProfile(Sampler* sampler, void* raw_sampler_thread) { |
| 674 | if (!sampler->isolate()->IsInitialized()) return; |
| 675 | if (!sampler->IsProfiling()) return; |
| 676 | SamplerThread* sampler_thread = |
| 677 | reinterpret_cast<SamplerThread*>(raw_sampler_thread); |
| 678 | sampler_thread->SampleContext(sampler); |
| 679 | } |
| 680 | |
| 681 | static void DoRuntimeProfile(Sampler* sampler, void* ignored) { |
| 682 | if (!sampler->isolate()->IsInitialized()) return; |
| 683 | sampler->isolate()->runtime_profiler()->NotifyTick(); |
| 684 | } |
| 685 | |
| 686 | void SampleContext(Sampler* sampler) { |
| 687 | HANDLE profiled_thread = sampler->platform_data()->profiled_thread(); |
| 688 | if (profiled_thread == NULL) return; |
| 689 | |
| 690 | // Context used for sampling the register state of the profiled thread. |
| 691 | CONTEXT context; |
| 692 | memset(&context, 0, sizeof(context)); |
| 693 | |
| 694 | TickSample sample_obj; |
| 695 | TickSample* sample = CpuProfiler::TickSampleEvent(sampler->isolate()); |
| 696 | if (sample == NULL) sample = &sample_obj; |
| 697 | |
| 698 | static const DWORD kSuspendFailed = static_cast<DWORD>(-1); |
| 699 | if (SuspendThread(profiled_thread) == kSuspendFailed) return; |
| 700 | sample->state = sampler->isolate()->current_vm_state(); |
| 701 | |
| 702 | context.ContextFlags = CONTEXT_FULL; |
| 703 | if (GetThreadContext(profiled_thread, &context) != 0) { |
| 704 | #if V8_HOST_ARCH_X64 |
| 705 | sample->pc = reinterpret_cast<Address>(context.Rip); |
| 706 | sample->sp = reinterpret_cast<Address>(context.Rsp); |
| 707 | sample->fp = reinterpret_cast<Address>(context.Rbp); |
| 708 | #else |
| 709 | sample->pc = reinterpret_cast<Address>(context.Eip); |
| 710 | sample->sp = reinterpret_cast<Address>(context.Esp); |
| 711 | sample->fp = reinterpret_cast<Address>(context.Ebp); |
| 712 | #endif |
| 713 | sampler->SampleStack(sample); |
| 714 | sampler->Tick(sample); |
| 715 | } |
| 716 | ResumeThread(profiled_thread); |
| 717 | } |
| 718 | |
| 719 | const int interval_; |
| 720 | RuntimeProfilerRateLimiter rate_limiter_; |
| 721 | |
| 722 | // Protects the process wide state below. |
| 723 | static Mutex* mutex_; |
| 724 | static SamplerThread* instance_; |
| 725 | |
| 726 | DISALLOW_COPY_AND_ASSIGN(SamplerThread); |
| 727 | }; |
| 728 | |
| 729 | |
| 730 | Mutex* SamplerThread::mutex_ = OS::CreateMutex(); |
| 731 | SamplerThread* SamplerThread::instance_ = NULL; |
| 732 | |
| 733 | |
| 734 | Sampler::Sampler(Isolate* isolate, int interval) |
| 735 | : isolate_(isolate), |
| 736 | interval_(interval), |
| 737 | profiling_(false), |
| 738 | active_(false), |
| 739 | samples_taken_(0) { |
| 740 | data_ = new PlatformData; |
| 741 | } |
| 742 | |
| 743 | |
| 744 | Sampler::~Sampler() { |
| 745 | ASSERT(!IsActive()); |
| 746 | delete data_; |
| 747 | } |
| 748 | |
| 749 | |
| 750 | void Sampler::Start() { |
| 751 | ASSERT(!IsActive()); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 752 | SetActive(true); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 753 | SamplerThread::AddActiveSampler(this); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 757 | void Sampler::Stop() { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 758 | ASSERT(IsActive()); |
| 759 | SamplerThread::RemoveActiveSampler(this); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 760 | SetActive(false); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 761 | } |
| 762 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 763 | |
| 764 | } } // namespace v8::internal |