erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 1 | // Copyright 2006-2011 the V8 project authors. All rights reserved. |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +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 | |
| 28 | // Platform specific code for OpenBSD goes here. For the POSIX comaptible parts |
| 29 | // the implementation is in platform-posix.cc. |
| 30 | |
| 31 | #include <pthread.h> |
| 32 | #include <semaphore.h> |
| 33 | #include <signal.h> |
| 34 | #include <sys/time.h> |
| 35 | #include <sys/resource.h> |
| 36 | #include <sys/types.h> |
| 37 | #include <stdlib.h> |
| 38 | |
| 39 | #include <sys/types.h> // mmap & munmap |
| 40 | #include <sys/mman.h> // mmap & munmap |
| 41 | #include <sys/stat.h> // open |
| 42 | #include <sys/fcntl.h> // open |
| 43 | #include <unistd.h> // getpagesize |
| 44 | #include <execinfo.h> // backtrace, backtrace_symbols |
| 45 | #include <strings.h> // index |
| 46 | #include <errno.h> |
| 47 | #include <stdarg.h> |
| 48 | #include <limits.h> |
| 49 | |
| 50 | #undef MAP_TYPE |
| 51 | |
| 52 | #include "v8.h" |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 53 | #include "v8threads.h" |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 54 | |
| 55 | #include "platform.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 56 | #include "vm-state-inl.h" |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 57 | |
| 58 | |
| 59 | namespace v8 { |
| 60 | namespace internal { |
| 61 | |
| 62 | // 0 is never a valid thread id on OpenBSD since tids and pids share a |
| 63 | // name space and pid 0 is used to kill the group (see man 2 kill). |
| 64 | static const pthread_t kNoThread = (pthread_t) 0; |
| 65 | |
| 66 | |
| 67 | double ceiling(double x) { |
| 68 | // Correct as on OS X |
| 69 | if (-1.0 < x && x < 0.0) { |
| 70 | return -0.0; |
| 71 | } else { |
| 72 | return ceil(x); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 77 | static Mutex* limit_mutex = NULL; |
| 78 | |
| 79 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 80 | void OS::Setup() { |
| 81 | // Seed the random number generator. |
| 82 | // Convert the current time to a 64-bit integer first, before converting it |
| 83 | // to an unsigned. Going directly can cause an overflow and the seed to be |
| 84 | // set to all ones. The seed will be identical for different instances that |
| 85 | // call this setup code within the same millisecond. |
| 86 | uint64_t seed = static_cast<uint64_t>(TimeCurrentMillis()); |
| 87 | srandom(static_cast<unsigned int>(seed)); |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 88 | limit_mutex = CreateMutex(); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 92 | void OS::ReleaseStore(volatile AtomicWord* ptr, AtomicWord value) { |
| 93 | __asm__ __volatile__("" : : : "memory"); |
| 94 | *ptr = value; |
| 95 | } |
| 96 | |
| 97 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 98 | uint64_t OS::CpuFeaturesImpliedByPlatform() { |
| 99 | return 0; // OpenBSD runs on anything. |
| 100 | } |
| 101 | |
| 102 | |
| 103 | int OS::ActivationFrameAlignment() { |
| 104 | // 16 byte alignment on OpenBSD |
| 105 | return 16; |
| 106 | } |
| 107 | |
| 108 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 109 | const char* OS::LocalTimezone(double time) { |
| 110 | if (isnan(time)) return ""; |
| 111 | time_t tv = static_cast<time_t>(floor(time/msPerSecond)); |
| 112 | struct tm* t = localtime(&tv); |
| 113 | if (NULL == t) return ""; |
| 114 | return t->tm_zone; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | double OS::LocalTimeOffset() { |
| 119 | time_t tv = time(NULL); |
| 120 | struct tm* t = localtime(&tv); |
| 121 | // tm_gmtoff includes any daylight savings offset, so subtract it. |
| 122 | return static_cast<double>(t->tm_gmtoff * msPerSecond - |
| 123 | (t->tm_isdst > 0 ? 3600 * msPerSecond : 0)); |
| 124 | } |
| 125 | |
| 126 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 127 | // We keep the lowest and highest addresses mapped as a quick way of |
| 128 | // determining that pointers are outside the heap (used mostly in assertions |
| 129 | // and verification). The estimate is conservative, ie, not all addresses in |
| 130 | // 'allocated' space are actually allocated to our heap. The range is |
| 131 | // [lowest, highest), inclusive on the low and and exclusive on the high end. |
| 132 | static void* lowest_ever_allocated = reinterpret_cast<void*>(-1); |
| 133 | static void* highest_ever_allocated = reinterpret_cast<void*>(0); |
| 134 | |
| 135 | |
| 136 | static void UpdateAllocatedSpaceLimits(void* address, int size) { |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 137 | ASSERT(limit_mutex != NULL); |
| 138 | ScopedLock lock(limit_mutex); |
| 139 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 140 | lowest_ever_allocated = Min(lowest_ever_allocated, address); |
| 141 | highest_ever_allocated = |
| 142 | Max(highest_ever_allocated, |
| 143 | reinterpret_cast<void*>(reinterpret_cast<char*>(address) + size)); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | bool OS::IsOutsideAllocatedSpace(void* address) { |
| 148 | return address < lowest_ever_allocated || address >= highest_ever_allocated; |
| 149 | } |
| 150 | |
| 151 | |
| 152 | size_t OS::AllocateAlignment() { |
| 153 | return getpagesize(); |
| 154 | } |
| 155 | |
| 156 | |
| 157 | void* OS::Allocate(const size_t requested, |
| 158 | size_t* allocated, |
| 159 | bool executable) { |
| 160 | const size_t msize = RoundUp(requested, getpagesize()); |
| 161 | int prot = PROT_READ | PROT_WRITE | (executable ? PROT_EXEC : 0); |
| 162 | void* mbase = mmap(NULL, msize, prot, MAP_PRIVATE | MAP_ANON, -1, 0); |
| 163 | |
| 164 | if (mbase == MAP_FAILED) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 165 | LOG(ISOLATE, StringEvent("OS::Allocate", "mmap failed")); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 166 | return NULL; |
| 167 | } |
| 168 | *allocated = msize; |
| 169 | UpdateAllocatedSpaceLimits(mbase, msize); |
| 170 | return mbase; |
| 171 | } |
| 172 | |
| 173 | |
| 174 | void OS::Free(void* buf, const size_t length) { |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 175 | // TODO(1240712): munmap has a return value which is ignored here. |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 176 | int result = munmap(buf, length); |
| 177 | USE(result); |
| 178 | ASSERT(result == 0); |
| 179 | } |
| 180 | |
| 181 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 182 | void OS::Sleep(int milliseconds) { |
| 183 | unsigned int ms = static_cast<unsigned int>(milliseconds); |
| 184 | usleep(1000 * ms); |
| 185 | } |
| 186 | |
| 187 | |
| 188 | void OS::Abort() { |
| 189 | // Redirect to std abort to signal abnormal program termination. |
| 190 | abort(); |
| 191 | } |
| 192 | |
| 193 | |
| 194 | void OS::DebugBreak() { |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 195 | #if (defined(__arm__) || defined(__thumb__)) |
| 196 | # if defined(CAN_USE_ARMV5_INSTRUCTIONS) |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 197 | asm("bkpt 0"); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 198 | # endif |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 199 | #else |
| 200 | asm("int $3"); |
| 201 | #endif |
| 202 | } |
| 203 | |
| 204 | |
| 205 | class PosixMemoryMappedFile : public OS::MemoryMappedFile { |
| 206 | public: |
| 207 | PosixMemoryMappedFile(FILE* file, void* memory, int size) |
| 208 | : file_(file), memory_(memory), size_(size) { } |
| 209 | virtual ~PosixMemoryMappedFile(); |
| 210 | virtual void* memory() { return memory_; } |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 211 | virtual int size() { return size_; } |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 212 | private: |
| 213 | FILE* file_; |
| 214 | void* memory_; |
| 215 | int size_; |
| 216 | }; |
| 217 | |
| 218 | |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 219 | OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 220 | FILE* file = fopen(name, "r+"); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 221 | if (file == NULL) return NULL; |
| 222 | |
| 223 | fseek(file, 0, SEEK_END); |
| 224 | int size = ftell(file); |
| 225 | |
| 226 | void* memory = |
| 227 | mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); |
| 228 | return new PosixMemoryMappedFile(file, memory, size); |
| 229 | } |
| 230 | |
| 231 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 232 | OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size, |
| 233 | void* initial) { |
| 234 | FILE* file = fopen(name, "w+"); |
| 235 | if (file == NULL) return NULL; |
| 236 | int result = fwrite(initial, size, 1, file); |
| 237 | if (result < 1) { |
| 238 | fclose(file); |
| 239 | return NULL; |
| 240 | } |
| 241 | void* memory = |
| 242 | mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); |
| 243 | return new PosixMemoryMappedFile(file, memory, size); |
| 244 | } |
| 245 | |
| 246 | |
| 247 | PosixMemoryMappedFile::~PosixMemoryMappedFile() { |
| 248 | if (memory_) munmap(memory_, size_); |
| 249 | fclose(file_); |
| 250 | } |
| 251 | |
| 252 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 253 | static unsigned StringToLong(char* buffer) { |
| 254 | return static_cast<unsigned>(strtol(buffer, NULL, 16)); // NOLINT |
| 255 | } |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 256 | |
| 257 | |
| 258 | void OS::LogSharedLibraryAddresses() { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 259 | static const int MAP_LENGTH = 1024; |
| 260 | int fd = open("/proc/self/maps", O_RDONLY); |
| 261 | if (fd < 0) return; |
| 262 | while (true) { |
| 263 | char addr_buffer[11]; |
| 264 | addr_buffer[0] = '0'; |
| 265 | addr_buffer[1] = 'x'; |
| 266 | addr_buffer[10] = 0; |
| 267 | int result = read(fd, addr_buffer + 2, 8); |
| 268 | if (result < 8) break; |
| 269 | unsigned start = StringToLong(addr_buffer); |
| 270 | result = read(fd, addr_buffer + 2, 1); |
| 271 | if (result < 1) break; |
| 272 | if (addr_buffer[2] != '-') break; |
| 273 | result = read(fd, addr_buffer + 2, 8); |
| 274 | if (result < 8) break; |
| 275 | unsigned end = StringToLong(addr_buffer); |
| 276 | char buffer[MAP_LENGTH]; |
| 277 | int bytes_read = -1; |
| 278 | do { |
| 279 | bytes_read++; |
| 280 | if (bytes_read >= MAP_LENGTH - 1) |
| 281 | break; |
| 282 | result = read(fd, buffer + bytes_read, 1); |
| 283 | if (result < 1) break; |
| 284 | } while (buffer[bytes_read] != '\n'); |
| 285 | buffer[bytes_read] = 0; |
| 286 | // Ignore mappings that are not executable. |
| 287 | if (buffer[3] != 'x') continue; |
| 288 | char* start_of_path = index(buffer, '/'); |
| 289 | // There may be no filename in this line. Skip to next. |
| 290 | if (start_of_path == NULL) continue; |
| 291 | buffer[bytes_read] = 0; |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 292 | LOG(i::Isolate::Current(), SharedLibraryEvent(start_of_path, start, end)); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 293 | } |
| 294 | close(fd); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 298 | void OS::SignalCodeMovingGC() { |
| 299 | } |
| 300 | |
| 301 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 302 | int OS::StackWalk(Vector<OS::StackFrame> frames) { |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 303 | int frames_size = frames.length(); |
| 304 | ScopedVector<void*> addresses(frames_size); |
| 305 | |
| 306 | int frames_count = backtrace(addresses.start(), frames_size); |
| 307 | |
| 308 | char** symbols = backtrace_symbols(addresses.start(), frames_count); |
| 309 | if (symbols == NULL) { |
| 310 | return kStackWalkError; |
| 311 | } |
| 312 | |
| 313 | for (int i = 0; i < frames_count; i++) { |
| 314 | frames[i].address = addresses[i]; |
| 315 | // Format a text representation of the frame based on the information |
| 316 | // available. |
| 317 | SNPrintF(MutableCStrVector(frames[i].text, kStackWalkMaxTextLen), |
| 318 | "%s", |
| 319 | symbols[i]); |
| 320 | // Make sure line termination is in place. |
| 321 | frames[i].text[kStackWalkMaxTextLen - 1] = '\0'; |
| 322 | } |
| 323 | |
| 324 | free(symbols); |
| 325 | |
| 326 | return frames_count; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | |
| 330 | // Constants used for mmap. |
| 331 | static const int kMmapFd = -1; |
| 332 | static const int kMmapFdOffset = 0; |
| 333 | |
| 334 | |
| 335 | VirtualMemory::VirtualMemory(size_t size) { |
| 336 | address_ = mmap(NULL, size, PROT_NONE, |
| 337 | MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, |
| 338 | kMmapFd, kMmapFdOffset); |
| 339 | size_ = size; |
| 340 | } |
| 341 | |
| 342 | |
| 343 | VirtualMemory::~VirtualMemory() { |
| 344 | if (IsReserved()) { |
| 345 | if (0 == munmap(address(), size())) address_ = MAP_FAILED; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | |
| 350 | bool VirtualMemory::IsReserved() { |
| 351 | return address_ != MAP_FAILED; |
| 352 | } |
| 353 | |
| 354 | |
| 355 | bool VirtualMemory::Commit(void* address, size_t size, bool executable) { |
| 356 | int prot = PROT_READ | PROT_WRITE | (executable ? PROT_EXEC : 0); |
| 357 | if (MAP_FAILED == mmap(address, size, prot, |
| 358 | MAP_PRIVATE | MAP_ANON | MAP_FIXED, |
| 359 | kMmapFd, kMmapFdOffset)) { |
| 360 | return false; |
| 361 | } |
| 362 | |
| 363 | UpdateAllocatedSpaceLimits(address, size); |
| 364 | return true; |
| 365 | } |
| 366 | |
| 367 | |
| 368 | bool VirtualMemory::Uncommit(void* address, size_t size) { |
| 369 | return mmap(address, size, PROT_NONE, |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 370 | MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | MAP_FIXED, |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 371 | kMmapFd, kMmapFdOffset) != MAP_FAILED; |
| 372 | } |
| 373 | |
| 374 | |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 375 | class Thread::PlatformData : public Malloced { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 376 | public: |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 377 | pthread_t thread_; // Thread handle for pthread. |
| 378 | }; |
| 379 | |
| 380 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 381 | Thread::Thread(const Options& options) |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 382 | : data_(new PlatformData), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 383 | stack_size_(options.stack_size) { |
| 384 | set_name(options.name); |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 388 | Thread::Thread(const char* name) |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 389 | : data_(new PlatformData), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 390 | stack_size_(0) { |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 391 | set_name(name); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | |
| 395 | Thread::~Thread() { |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 396 | delete data_; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | |
| 400 | static void* ThreadEntry(void* arg) { |
| 401 | Thread* thread = reinterpret_cast<Thread*>(arg); |
| 402 | // This is also initialized by the first argument to pthread_create() but we |
| 403 | // don't know which thread will run first (the original thread or the new |
| 404 | // one) so we initialize it here too. |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 405 | thread->data()->thread_ = pthread_self(); |
| 406 | ASSERT(thread->data()->thread_ != kNoThread); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 407 | thread->Run(); |
| 408 | return NULL; |
| 409 | } |
| 410 | |
| 411 | |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 412 | void Thread::set_name(const char* name) { |
| 413 | strncpy(name_, name, sizeof(name_)); |
| 414 | name_[sizeof(name_) - 1] = '\0'; |
| 415 | } |
| 416 | |
| 417 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 418 | void Thread::Start() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 419 | pthread_attr_t* attr_ptr = NULL; |
| 420 | pthread_attr_t attr; |
| 421 | if (stack_size_ > 0) { |
| 422 | pthread_attr_init(&attr); |
| 423 | pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_)); |
| 424 | attr_ptr = &attr; |
| 425 | } |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 426 | pthread_create(&data_->thread_, attr_ptr, ThreadEntry, this); |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 427 | ASSERT(data_->thread_ != kNoThread); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | |
| 431 | void Thread::Join() { |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 432 | pthread_join(data_->thread_, NULL); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | |
| 436 | Thread::LocalStorageKey Thread::CreateThreadLocalKey() { |
| 437 | pthread_key_t key; |
| 438 | int result = pthread_key_create(&key, NULL); |
| 439 | USE(result); |
| 440 | ASSERT(result == 0); |
| 441 | return static_cast<LocalStorageKey>(key); |
| 442 | } |
| 443 | |
| 444 | |
| 445 | void Thread::DeleteThreadLocalKey(LocalStorageKey key) { |
| 446 | pthread_key_t pthread_key = static_cast<pthread_key_t>(key); |
| 447 | int result = pthread_key_delete(pthread_key); |
| 448 | USE(result); |
| 449 | ASSERT(result == 0); |
| 450 | } |
| 451 | |
| 452 | |
| 453 | void* Thread::GetThreadLocal(LocalStorageKey key) { |
| 454 | pthread_key_t pthread_key = static_cast<pthread_key_t>(key); |
| 455 | return pthread_getspecific(pthread_key); |
| 456 | } |
| 457 | |
| 458 | |
| 459 | void Thread::SetThreadLocal(LocalStorageKey key, void* value) { |
| 460 | pthread_key_t pthread_key = static_cast<pthread_key_t>(key); |
| 461 | pthread_setspecific(pthread_key, value); |
| 462 | } |
| 463 | |
| 464 | |
| 465 | void Thread::YieldCPU() { |
| 466 | sched_yield(); |
| 467 | } |
| 468 | |
| 469 | |
| 470 | class OpenBSDMutex : public Mutex { |
| 471 | public: |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 472 | OpenBSDMutex() { |
| 473 | pthread_mutexattr_t attrs; |
| 474 | int result = pthread_mutexattr_init(&attrs); |
| 475 | ASSERT(result == 0); |
| 476 | result = pthread_mutexattr_settype(&attrs, PTHREAD_MUTEX_RECURSIVE); |
| 477 | ASSERT(result == 0); |
| 478 | result = pthread_mutex_init(&mutex_, &attrs); |
| 479 | ASSERT(result == 0); |
| 480 | } |
| 481 | |
| 482 | virtual ~OpenBSDMutex() { pthread_mutex_destroy(&mutex_); } |
| 483 | |
| 484 | virtual int Lock() { |
| 485 | int result = pthread_mutex_lock(&mutex_); |
| 486 | return result; |
| 487 | } |
| 488 | |
| 489 | virtual int Unlock() { |
| 490 | int result = pthread_mutex_unlock(&mutex_); |
| 491 | return result; |
| 492 | } |
| 493 | |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 494 | virtual bool TryLock() { |
| 495 | int result = pthread_mutex_trylock(&mutex_); |
| 496 | // Return false if the lock is busy and locking failed. |
| 497 | if (result == EBUSY) { |
| 498 | return false; |
| 499 | } |
| 500 | ASSERT(result == 0); // Verify no other errors. |
| 501 | return true; |
| 502 | } |
| 503 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 504 | private: |
| 505 | pthread_mutex_t mutex_; // Pthread mutex for POSIX platforms. |
| 506 | }; |
| 507 | |
| 508 | |
| 509 | Mutex* OS::CreateMutex() { |
| 510 | return new OpenBSDMutex(); |
| 511 | } |
| 512 | |
| 513 | |
| 514 | class OpenBSDSemaphore : public Semaphore { |
| 515 | public: |
| 516 | explicit OpenBSDSemaphore(int count) { sem_init(&sem_, 0, count); } |
| 517 | virtual ~OpenBSDSemaphore() { sem_destroy(&sem_); } |
| 518 | |
| 519 | virtual void Wait(); |
| 520 | virtual bool Wait(int timeout); |
| 521 | virtual void Signal() { sem_post(&sem_); } |
| 522 | private: |
| 523 | sem_t sem_; |
| 524 | }; |
| 525 | |
| 526 | |
| 527 | void OpenBSDSemaphore::Wait() { |
| 528 | while (true) { |
| 529 | int result = sem_wait(&sem_); |
| 530 | if (result == 0) return; // Successfully got semaphore. |
| 531 | CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup. |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | |
| 536 | bool OpenBSDSemaphore::Wait(int timeout) { |
| 537 | const long kOneSecondMicros = 1000000; // NOLINT |
| 538 | |
| 539 | // Split timeout into second and nanosecond parts. |
| 540 | struct timeval delta; |
| 541 | delta.tv_usec = timeout % kOneSecondMicros; |
| 542 | delta.tv_sec = timeout / kOneSecondMicros; |
| 543 | |
| 544 | struct timeval current_time; |
| 545 | // Get the current time. |
| 546 | if (gettimeofday(¤t_time, NULL) == -1) { |
| 547 | return false; |
| 548 | } |
| 549 | |
| 550 | // Calculate time for end of timeout. |
| 551 | struct timeval end_time; |
| 552 | timeradd(¤t_time, &delta, &end_time); |
| 553 | |
| 554 | struct timespec ts; |
| 555 | TIMEVAL_TO_TIMESPEC(&end_time, &ts); |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 556 | |
| 557 | int to = ts.tv_sec; |
| 558 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 559 | while (true) { |
| 560 | int result = sem_trywait(&sem_); |
| 561 | if (result == 0) return true; // Successfully got semaphore. |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 562 | if (!to) return false; // Timeout. |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 563 | CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup. |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 564 | usleep(ts.tv_nsec / 1000); |
| 565 | to--; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | |
| 569 | |
| 570 | Semaphore* OS::CreateSemaphore(int count) { |
| 571 | return new OpenBSDSemaphore(count); |
| 572 | } |
| 573 | |
| 574 | |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 575 | static pthread_t GetThreadID() { |
| 576 | pthread_t thread_id = pthread_self(); |
| 577 | return thread_id; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | |
| 581 | class Sampler::PlatformData : public Malloced { |
| 582 | public: |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 583 | PlatformData() : vm_tid_(GetThreadID()) {} |
| 584 | |
| 585 | pthread_t vm_tid() const { return vm_tid_; } |
| 586 | |
| 587 | private: |
| 588 | pthread_t vm_tid_; |
| 589 | }; |
| 590 | |
| 591 | |
| 592 | static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) { |
| 593 | USE(info); |
| 594 | if (signal != SIGPROF) return; |
| 595 | Isolate* isolate = Isolate::UncheckedCurrent(); |
| 596 | if (isolate == NULL || !isolate->IsInitialized() || !isolate->IsInUse()) { |
| 597 | // We require a fully initialized and entered isolate. |
| 598 | return; |
| 599 | } |
| 600 | if (v8::Locker::IsActive() && |
| 601 | !isolate->thread_manager()->IsLockedByCurrentThread()) { |
| 602 | return; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 603 | } |
| 604 | |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 605 | Sampler* sampler = isolate->logger()->sampler(); |
| 606 | if (sampler == NULL || !sampler->IsActive()) return; |
| 607 | |
| 608 | TickSample sample_obj; |
| 609 | TickSample* sample = CpuProfiler::TickSampleEvent(isolate); |
| 610 | if (sample == NULL) sample = &sample_obj; |
| 611 | |
| 612 | // Extracting the sample from the context is extremely machine dependent. |
| 613 | ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context); |
| 614 | sample->state = isolate->current_vm_state(); |
| 615 | #if V8_HOST_ARCH_IA32 |
| 616 | sample->pc = reinterpret_cast<Address>(ucontext->sc_eip); |
| 617 | sample->sp = reinterpret_cast<Address>(ucontext->sc_esp); |
| 618 | sample->fp = reinterpret_cast<Address>(ucontext->sc_ebp); |
| 619 | #elif V8_HOST_ARCH_X64 |
| 620 | sample->pc = reinterpret_cast<Address>(ucontext->sc_rip); |
| 621 | sample->sp = reinterpret_cast<Address>(ucontext->sc_rsp); |
| 622 | sample->fp = reinterpret_cast<Address>(ucontext->sc_rbp); |
| 623 | #elif V8_HOST_ARCH_ARM |
| 624 | sample->pc = reinterpret_cast<Address>(ucontext->sc_r15); |
| 625 | sample->sp = reinterpret_cast<Address>(ucontext->sc_r13); |
| 626 | sample->fp = reinterpret_cast<Address>(ucontext->sc_r11); |
| 627 | #endif |
| 628 | sampler->SampleStack(sample); |
| 629 | sampler->Tick(sample); |
| 630 | } |
| 631 | |
| 632 | |
| 633 | class SignalSender : public Thread { |
| 634 | public: |
| 635 | enum SleepInterval { |
| 636 | HALF_INTERVAL, |
| 637 | FULL_INTERVAL |
| 638 | }; |
| 639 | |
| 640 | explicit SignalSender(int interval) |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 641 | : Thread("SignalSender"), |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 642 | interval_(interval) {} |
| 643 | |
| 644 | static void AddActiveSampler(Sampler* sampler) { |
| 645 | ScopedLock lock(mutex_); |
| 646 | SamplerRegistry::AddActiveSampler(sampler); |
| 647 | if (instance_ == NULL) { |
| 648 | // Install a signal handler. |
| 649 | struct sigaction sa; |
| 650 | sa.sa_sigaction = ProfilerSignalHandler; |
| 651 | sigemptyset(&sa.sa_mask); |
| 652 | sa.sa_flags = SA_RESTART | SA_SIGINFO; |
| 653 | signal_handler_installed_ = |
| 654 | (sigaction(SIGPROF, &sa, &old_signal_handler_) == 0); |
| 655 | |
| 656 | // Start a thread that sends SIGPROF signal to VM threads. |
| 657 | instance_ = new SignalSender(sampler->interval()); |
| 658 | instance_->Start(); |
| 659 | } else { |
| 660 | ASSERT(instance_->interval_ == sampler->interval()); |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | static void RemoveActiveSampler(Sampler* sampler) { |
| 665 | ScopedLock lock(mutex_); |
| 666 | SamplerRegistry::RemoveActiveSampler(sampler); |
| 667 | if (SamplerRegistry::GetState() == SamplerRegistry::HAS_NO_SAMPLERS) { |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 668 | RuntimeProfiler::StopRuntimeProfilerThreadBeforeShutdown(instance_); |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 669 | delete instance_; |
| 670 | instance_ = NULL; |
| 671 | |
| 672 | // Restore the old signal handler. |
| 673 | if (signal_handler_installed_) { |
| 674 | sigaction(SIGPROF, &old_signal_handler_, 0); |
| 675 | signal_handler_installed_ = false; |
| 676 | } |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | // Implement Thread::Run(). |
| 681 | virtual void Run() { |
| 682 | SamplerRegistry::State state; |
| 683 | while ((state = SamplerRegistry::GetState()) != |
| 684 | SamplerRegistry::HAS_NO_SAMPLERS) { |
| 685 | bool cpu_profiling_enabled = |
| 686 | (state == SamplerRegistry::HAS_CPU_PROFILING_SAMPLERS); |
| 687 | bool runtime_profiler_enabled = RuntimeProfiler::IsEnabled(); |
| 688 | // When CPU profiling is enabled both JavaScript and C++ code is |
| 689 | // profiled. We must not suspend. |
| 690 | if (!cpu_profiling_enabled) { |
| 691 | if (rate_limiter_.SuspendIfNecessary()) continue; |
| 692 | } |
| 693 | if (cpu_profiling_enabled && runtime_profiler_enabled) { |
| 694 | if (!SamplerRegistry::IterateActiveSamplers(&DoCpuProfile, this)) { |
| 695 | return; |
| 696 | } |
| 697 | Sleep(HALF_INTERVAL); |
| 698 | if (!SamplerRegistry::IterateActiveSamplers(&DoRuntimeProfile, NULL)) { |
| 699 | return; |
| 700 | } |
| 701 | Sleep(HALF_INTERVAL); |
| 702 | } else { |
| 703 | if (cpu_profiling_enabled) { |
| 704 | if (!SamplerRegistry::IterateActiveSamplers(&DoCpuProfile, |
| 705 | this)) { |
| 706 | return; |
| 707 | } |
| 708 | } |
| 709 | if (runtime_profiler_enabled) { |
| 710 | if (!SamplerRegistry::IterateActiveSamplers(&DoRuntimeProfile, |
| 711 | NULL)) { |
| 712 | return; |
| 713 | } |
| 714 | } |
| 715 | Sleep(FULL_INTERVAL); |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | static void DoCpuProfile(Sampler* sampler, void* raw_sender) { |
| 721 | if (!sampler->IsProfiling()) return; |
| 722 | SignalSender* sender = reinterpret_cast<SignalSender*>(raw_sender); |
| 723 | sender->SendProfilingSignal(sampler->platform_data()->vm_tid()); |
| 724 | } |
| 725 | |
| 726 | static void DoRuntimeProfile(Sampler* sampler, void* ignored) { |
| 727 | if (!sampler->isolate()->IsInitialized()) return; |
| 728 | sampler->isolate()->runtime_profiler()->NotifyTick(); |
| 729 | } |
| 730 | |
| 731 | void SendProfilingSignal(pthread_t tid) { |
| 732 | if (!signal_handler_installed_) return; |
| 733 | pthread_kill(tid, SIGPROF); |
| 734 | } |
| 735 | |
| 736 | void Sleep(SleepInterval full_or_half) { |
| 737 | // Convert ms to us and subtract 100 us to compensate delays |
| 738 | // occuring during signal delivery. |
| 739 | useconds_t interval = interval_ * 1000 - 100; |
| 740 | if (full_or_half == HALF_INTERVAL) interval /= 2; |
| 741 | int result = usleep(interval); |
| 742 | #ifdef DEBUG |
| 743 | if (result != 0 && errno != EINTR) { |
| 744 | fprintf(stderr, |
| 745 | "SignalSender usleep error; interval = %u, errno = %d\n", |
| 746 | interval, |
| 747 | errno); |
| 748 | ASSERT(result == 0 || errno == EINTR); |
| 749 | } |
| 750 | #endif |
| 751 | USE(result); |
| 752 | } |
| 753 | |
| 754 | const int interval_; |
| 755 | RuntimeProfilerRateLimiter rate_limiter_; |
| 756 | |
| 757 | // Protects the process wide state below. |
| 758 | static Mutex* mutex_; |
| 759 | static SignalSender* instance_; |
| 760 | static bool signal_handler_installed_; |
| 761 | static struct sigaction old_signal_handler_; |
| 762 | |
| 763 | DISALLOW_COPY_AND_ASSIGN(SignalSender); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 764 | }; |
| 765 | |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 766 | Mutex* SignalSender::mutex_ = OS::CreateMutex(); |
| 767 | SignalSender* SignalSender::instance_ = NULL; |
| 768 | struct sigaction SignalSender::old_signal_handler_; |
| 769 | bool SignalSender::signal_handler_installed_ = false; |
| 770 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 771 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 772 | Sampler::Sampler(Isolate* isolate, int interval) |
| 773 | : isolate_(isolate), |
| 774 | interval_(interval), |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 775 | profiling_(false), |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 776 | active_(false), |
| 777 | samples_taken_(0) { |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 778 | data_ = new PlatformData; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | |
| 782 | Sampler::~Sampler() { |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 783 | ASSERT(!IsActive()); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 784 | delete data_; |
| 785 | } |
| 786 | |
| 787 | |
| 788 | void Sampler::Start() { |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 789 | ASSERT(!IsActive()); |
| 790 | SetActive(true); |
| 791 | SignalSender::AddActiveSampler(this); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | |
| 795 | void Sampler::Stop() { |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 796 | ASSERT(IsActive()); |
| 797 | SignalSender::RemoveActiveSampler(this); |
| 798 | SetActive(false); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 799 | } |
| 800 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 801 | |
| 802 | } } // namespace v8::internal |