ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 28 | // Platform specific code for Linux goes here. For the POSIX comaptible parts |
| 29 | // the implementation is in platform-posix.cc. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 30 | |
| 31 | #include <pthread.h> |
| 32 | #include <semaphore.h> |
| 33 | #include <signal.h> |
| 34 | #include <sys/time.h> |
| 35 | #include <sys/resource.h> |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 36 | #include <sys/types.h> |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 37 | #include <stdlib.h> |
| 38 | |
| 39 | // Ubuntu Dapper requires memory pages to be marked as |
| 40 | // executable. Otherwise, OS raises an exception when executing code |
| 41 | // in that page. |
| 42 | #include <sys/types.h> // mmap & munmap |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 43 | #include <sys/mman.h> // mmap & munmap |
| 44 | #include <sys/stat.h> // open |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 45 | #include <fcntl.h> // open |
| 46 | #include <unistd.h> // sysconf |
| 47 | #ifdef __GLIBC__ |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 48 | #include <execinfo.h> // backtrace, backtrace_symbols |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 49 | #endif // def __GLIBC__ |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 50 | #include <strings.h> // index |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 51 | #include <errno.h> |
| 52 | #include <stdarg.h> |
| 53 | |
| 54 | #undef MAP_TYPE |
| 55 | |
| 56 | #include "v8.h" |
| 57 | |
| 58 | #include "platform.h" |
| 59 | |
| 60 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 61 | namespace v8 { |
| 62 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 63 | |
| 64 | // 0 is never a valid thread id on Linux since tids and pids share a |
| 65 | // name space and pid 0 is reserved (see man 2 kill). |
| 66 | static const pthread_t kNoThread = (pthread_t) 0; |
| 67 | |
| 68 | |
| 69 | double ceiling(double x) { |
| 70 | return ceil(x); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | void OS::Setup() { |
| 75 | // Seed the random number generator. |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 76 | // Convert the current time to a 64-bit integer first, before converting it |
| 77 | // to an unsigned. Going directly can cause an overflow and the seed to be |
| 78 | // set to all ones. The seed will be identical for different instances that |
| 79 | // call this setup code within the same millisecond. |
| 80 | uint64_t seed = static_cast<uint64_t>(TimeCurrentMillis()); |
| 81 | srandom(static_cast<unsigned int>(seed)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 85 | double OS::nan_value() { |
| 86 | return NAN; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | int OS::ActivationFrameAlignment() { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 91 | #ifdef V8_TARGET_ARCH_ARM |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 92 | // On EABI ARM targets this is required for fp correctness in the |
| 93 | // runtime system. |
ager@chromium.org | 3a6061e | 2009-03-12 14:24:36 +0000 | [diff] [blame] | 94 | return 8; |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 95 | #else |
| 96 | // With gcc 4.4 the tree vectorization optimiser can generate code |
| 97 | // that requires 16 byte alignment such as movdqa on x86. |
| 98 | return 16; |
| 99 | #endif |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 100 | } |
| 101 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 102 | |
| 103 | // We keep the lowest and highest addresses mapped as a quick way of |
| 104 | // determining that pointers are outside the heap (used mostly in assertions |
| 105 | // and verification). The estimate is conservative, ie, not all addresses in |
| 106 | // 'allocated' space are actually allocated to our heap. The range is |
| 107 | // [lowest, highest), inclusive on the low and and exclusive on the high end. |
| 108 | static void* lowest_ever_allocated = reinterpret_cast<void*>(-1); |
| 109 | static void* highest_ever_allocated = reinterpret_cast<void*>(0); |
| 110 | |
| 111 | |
| 112 | static void UpdateAllocatedSpaceLimits(void* address, int size) { |
| 113 | lowest_ever_allocated = Min(lowest_ever_allocated, address); |
| 114 | highest_ever_allocated = |
| 115 | Max(highest_ever_allocated, |
| 116 | reinterpret_cast<void*>(reinterpret_cast<char*>(address) + size)); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | bool OS::IsOutsideAllocatedSpace(void* address) { |
| 121 | return address < lowest_ever_allocated || address >= highest_ever_allocated; |
| 122 | } |
| 123 | |
| 124 | |
| 125 | size_t OS::AllocateAlignment() { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 126 | return sysconf(_SC_PAGESIZE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 130 | void* OS::Allocate(const size_t requested, |
| 131 | size_t* allocated, |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 132 | bool is_executable) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 133 | const size_t msize = RoundUp(requested, sysconf(_SC_PAGESIZE)); |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 134 | int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 135 | void* mbase = mmap(NULL, msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 136 | if (mbase == MAP_FAILED) { |
| 137 | LOG(StringEvent("OS::Allocate", "mmap failed")); |
| 138 | return NULL; |
| 139 | } |
| 140 | *allocated = msize; |
| 141 | UpdateAllocatedSpaceLimits(mbase, msize); |
| 142 | return mbase; |
| 143 | } |
| 144 | |
| 145 | |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 146 | void OS::Free(void* address, const size_t size) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 147 | // TODO(1240712): munmap has a return value which is ignored here. |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 148 | munmap(address, size); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 152 | #ifdef ENABLE_HEAP_PROTECTION |
| 153 | |
| 154 | void OS::Protect(void* address, size_t size) { |
| 155 | // TODO(1240712): mprotect has a return value which is ignored here. |
| 156 | mprotect(address, size, PROT_READ); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | void OS::Unprotect(void* address, size_t size, bool is_executable) { |
| 161 | // TODO(1240712): mprotect has a return value which is ignored here. |
| 162 | int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); |
| 163 | mprotect(address, size, prot); |
| 164 | } |
| 165 | |
| 166 | #endif |
| 167 | |
| 168 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 169 | void OS::Sleep(int milliseconds) { |
| 170 | unsigned int ms = static_cast<unsigned int>(milliseconds); |
| 171 | usleep(1000 * ms); |
| 172 | } |
| 173 | |
| 174 | |
| 175 | void OS::Abort() { |
| 176 | // Redirect to std abort to signal abnormal program termination. |
| 177 | abort(); |
| 178 | } |
| 179 | |
| 180 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 181 | void OS::DebugBreak() { |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 182 | // TODO(lrn): Introduce processor define for runtime system (!= V8_ARCH_x, |
| 183 | // which is the architecture of generated code). |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 184 | #if defined(__arm__) || defined(__thumb__) |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 185 | asm("bkpt 0"); |
| 186 | #else |
| 187 | asm("int $3"); |
| 188 | #endif |
| 189 | } |
| 190 | |
| 191 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 192 | class PosixMemoryMappedFile : public OS::MemoryMappedFile { |
| 193 | public: |
| 194 | PosixMemoryMappedFile(FILE* file, void* memory, int size) |
| 195 | : file_(file), memory_(memory), size_(size) { } |
| 196 | virtual ~PosixMemoryMappedFile(); |
| 197 | virtual void* memory() { return memory_; } |
| 198 | private: |
| 199 | FILE* file_; |
| 200 | void* memory_; |
| 201 | int size_; |
| 202 | }; |
| 203 | |
| 204 | |
| 205 | OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size, |
| 206 | void* initial) { |
| 207 | FILE* file = fopen(name, "w+"); |
| 208 | if (file == NULL) return NULL; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 209 | int result = fwrite(initial, size, 1, file); |
| 210 | if (result < 1) { |
| 211 | fclose(file); |
| 212 | return NULL; |
| 213 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 214 | void* memory = |
| 215 | mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); |
| 216 | return new PosixMemoryMappedFile(file, memory, size); |
| 217 | } |
| 218 | |
| 219 | |
| 220 | PosixMemoryMappedFile::~PosixMemoryMappedFile() { |
| 221 | if (memory_) munmap(memory_, size_); |
| 222 | fclose(file_); |
| 223 | } |
| 224 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 225 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 226 | void OS::LogSharedLibraryAddresses() { |
| 227 | #ifdef ENABLE_LOGGING_AND_PROFILING |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame^] | 228 | FILE *fp; |
| 229 | fp = fopen("/proc/self/maps", "r"); |
| 230 | if (fp == NULL) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 231 | while (true) { |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame^] | 232 | uintptr_t start, end; |
| 233 | char attr_r, attr_w, attr_x, attr_p; |
| 234 | if (fscanf(fp, "%" V8PRIxPTR "-%" V8PRIxPTR, &start, &end) != 2) break; |
| 235 | if (fscanf(fp, " %c%c%c%c", &attr_r, &attr_w, &attr_x, &attr_p) != 4) break; |
| 236 | int c; |
| 237 | if (attr_r == 'r' && attr_x == 'x') { |
| 238 | while (c = getc(fp), (c != EOF) && (c != '\n') && (c != '/')); |
| 239 | char lib_name[1024]; |
| 240 | bool lib_has_name = false; |
| 241 | if (c == '/') { |
| 242 | ungetc(c, fp); |
| 243 | lib_has_name = fgets(lib_name, sizeof(lib_name), fp) != NULL; |
| 244 | } |
| 245 | if (lib_has_name && strlen(lib_name) > 0) { |
| 246 | lib_name[strlen(lib_name) - 1] = '\0'; |
| 247 | } else { |
| 248 | snprintf(lib_name, sizeof(lib_name), |
| 249 | "%08" V8PRIxPTR "-%08" V8PRIxPTR, start, end); |
| 250 | } |
| 251 | LOG(SharedLibraryEvent(lib_name, start, end)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 252 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame^] | 253 | while (c = getc(fp), (c != EOF) && (c != '\n')); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 254 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame^] | 255 | fclose(fp); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 256 | #endif |
| 257 | } |
| 258 | |
| 259 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 260 | int OS::StackWalk(Vector<OS::StackFrame> frames) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 261 | // backtrace is a glibc extension. |
| 262 | #ifdef __GLIBC__ |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 263 | int frames_size = frames.length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 264 | void** addresses = NewArray<void*>(frames_size); |
| 265 | |
| 266 | int frames_count = backtrace(addresses, frames_size); |
| 267 | |
| 268 | char** symbols; |
| 269 | symbols = backtrace_symbols(addresses, frames_count); |
| 270 | if (symbols == NULL) { |
| 271 | DeleteArray(addresses); |
| 272 | return kStackWalkError; |
| 273 | } |
| 274 | |
| 275 | for (int i = 0; i < frames_count; i++) { |
| 276 | frames[i].address = addresses[i]; |
| 277 | // Format a text representation of the frame based on the information |
| 278 | // available. |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 279 | SNPrintF(MutableCStrVector(frames[i].text, kStackWalkMaxTextLen), |
| 280 | "%s", |
| 281 | symbols[i]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 282 | // Make sure line termination is in place. |
| 283 | frames[i].text[kStackWalkMaxTextLen - 1] = '\0'; |
| 284 | } |
| 285 | |
| 286 | DeleteArray(addresses); |
| 287 | free(symbols); |
| 288 | |
| 289 | return frames_count; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 290 | #else // ndef __GLIBC__ |
| 291 | return 0; |
| 292 | #endif // ndef __GLIBC__ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | |
| 296 | // Constants used for mmap. |
| 297 | static const int kMmapFd = -1; |
| 298 | static const int kMmapFdOffset = 0; |
| 299 | |
| 300 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 301 | VirtualMemory::VirtualMemory(size_t size) { |
| 302 | address_ = mmap(NULL, size, PROT_NONE, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 303 | MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, |
| 304 | kMmapFd, kMmapFdOffset); |
| 305 | size_ = size; |
| 306 | } |
| 307 | |
| 308 | |
| 309 | VirtualMemory::~VirtualMemory() { |
| 310 | if (IsReserved()) { |
| 311 | if (0 == munmap(address(), size())) address_ = MAP_FAILED; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | |
| 316 | bool VirtualMemory::IsReserved() { |
| 317 | return address_ != MAP_FAILED; |
| 318 | } |
| 319 | |
| 320 | |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 321 | bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) { |
| 322 | int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 323 | if (MAP_FAILED == mmap(address, size, prot, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 324 | MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, |
| 325 | kMmapFd, kMmapFdOffset)) { |
| 326 | return false; |
| 327 | } |
| 328 | |
| 329 | UpdateAllocatedSpaceLimits(address, size); |
| 330 | return true; |
| 331 | } |
| 332 | |
| 333 | |
| 334 | bool VirtualMemory::Uncommit(void* address, size_t size) { |
| 335 | return mmap(address, size, PROT_NONE, |
| 336 | MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, |
| 337 | kMmapFd, kMmapFdOffset) != MAP_FAILED; |
| 338 | } |
| 339 | |
| 340 | |
| 341 | class ThreadHandle::PlatformData : public Malloced { |
| 342 | public: |
| 343 | explicit PlatformData(ThreadHandle::Kind kind) { |
| 344 | Initialize(kind); |
| 345 | } |
| 346 | |
| 347 | void Initialize(ThreadHandle::Kind kind) { |
| 348 | switch (kind) { |
| 349 | case ThreadHandle::SELF: thread_ = pthread_self(); break; |
| 350 | case ThreadHandle::INVALID: thread_ = kNoThread; break; |
| 351 | } |
| 352 | } |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 353 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 354 | pthread_t thread_; // Thread handle for pthread. |
| 355 | }; |
| 356 | |
| 357 | |
| 358 | ThreadHandle::ThreadHandle(Kind kind) { |
| 359 | data_ = new PlatformData(kind); |
| 360 | } |
| 361 | |
| 362 | |
| 363 | void ThreadHandle::Initialize(ThreadHandle::Kind kind) { |
| 364 | data_->Initialize(kind); |
| 365 | } |
| 366 | |
| 367 | |
| 368 | ThreadHandle::~ThreadHandle() { |
| 369 | delete data_; |
| 370 | } |
| 371 | |
| 372 | |
| 373 | bool ThreadHandle::IsSelf() const { |
| 374 | return pthread_equal(data_->thread_, pthread_self()); |
| 375 | } |
| 376 | |
| 377 | |
| 378 | bool ThreadHandle::IsValid() const { |
| 379 | return data_->thread_ != kNoThread; |
| 380 | } |
| 381 | |
| 382 | |
| 383 | Thread::Thread() : ThreadHandle(ThreadHandle::INVALID) { |
| 384 | } |
| 385 | |
| 386 | |
| 387 | Thread::~Thread() { |
| 388 | } |
| 389 | |
| 390 | |
| 391 | static void* ThreadEntry(void* arg) { |
| 392 | Thread* thread = reinterpret_cast<Thread*>(arg); |
| 393 | // This is also initialized by the first argument to pthread_create() but we |
| 394 | // don't know which thread will run first (the original thread or the new |
| 395 | // one) so we initialize it here too. |
| 396 | thread->thread_handle_data()->thread_ = pthread_self(); |
| 397 | ASSERT(thread->IsValid()); |
| 398 | thread->Run(); |
| 399 | return NULL; |
| 400 | } |
| 401 | |
| 402 | |
| 403 | void Thread::Start() { |
| 404 | pthread_create(&thread_handle_data()->thread_, NULL, ThreadEntry, this); |
| 405 | ASSERT(IsValid()); |
| 406 | } |
| 407 | |
| 408 | |
| 409 | void Thread::Join() { |
| 410 | pthread_join(thread_handle_data()->thread_, NULL); |
| 411 | } |
| 412 | |
| 413 | |
| 414 | Thread::LocalStorageKey Thread::CreateThreadLocalKey() { |
| 415 | pthread_key_t key; |
| 416 | int result = pthread_key_create(&key, NULL); |
| 417 | USE(result); |
| 418 | ASSERT(result == 0); |
| 419 | return static_cast<LocalStorageKey>(key); |
| 420 | } |
| 421 | |
| 422 | |
| 423 | void Thread::DeleteThreadLocalKey(LocalStorageKey key) { |
| 424 | pthread_key_t pthread_key = static_cast<pthread_key_t>(key); |
| 425 | int result = pthread_key_delete(pthread_key); |
| 426 | USE(result); |
| 427 | ASSERT(result == 0); |
| 428 | } |
| 429 | |
| 430 | |
| 431 | void* Thread::GetThreadLocal(LocalStorageKey key) { |
| 432 | pthread_key_t pthread_key = static_cast<pthread_key_t>(key); |
| 433 | return pthread_getspecific(pthread_key); |
| 434 | } |
| 435 | |
| 436 | |
| 437 | void Thread::SetThreadLocal(LocalStorageKey key, void* value) { |
| 438 | pthread_key_t pthread_key = static_cast<pthread_key_t>(key); |
| 439 | pthread_setspecific(pthread_key, value); |
| 440 | } |
| 441 | |
| 442 | |
| 443 | void Thread::YieldCPU() { |
| 444 | sched_yield(); |
| 445 | } |
| 446 | |
| 447 | |
| 448 | class LinuxMutex : public Mutex { |
| 449 | public: |
| 450 | |
| 451 | LinuxMutex() { |
| 452 | pthread_mutexattr_t attrs; |
| 453 | int result = pthread_mutexattr_init(&attrs); |
| 454 | ASSERT(result == 0); |
| 455 | result = pthread_mutexattr_settype(&attrs, PTHREAD_MUTEX_RECURSIVE); |
| 456 | ASSERT(result == 0); |
| 457 | result = pthread_mutex_init(&mutex_, &attrs); |
| 458 | ASSERT(result == 0); |
| 459 | } |
| 460 | |
| 461 | virtual ~LinuxMutex() { pthread_mutex_destroy(&mutex_); } |
| 462 | |
| 463 | virtual int Lock() { |
| 464 | int result = pthread_mutex_lock(&mutex_); |
| 465 | return result; |
| 466 | } |
| 467 | |
| 468 | virtual int Unlock() { |
| 469 | int result = pthread_mutex_unlock(&mutex_); |
| 470 | return result; |
| 471 | } |
| 472 | |
| 473 | private: |
| 474 | pthread_mutex_t mutex_; // Pthread mutex for POSIX platforms. |
| 475 | }; |
| 476 | |
| 477 | |
| 478 | Mutex* OS::CreateMutex() { |
| 479 | return new LinuxMutex(); |
| 480 | } |
| 481 | |
| 482 | |
| 483 | class LinuxSemaphore : public Semaphore { |
| 484 | public: |
| 485 | explicit LinuxSemaphore(int count) { sem_init(&sem_, 0, count); } |
| 486 | virtual ~LinuxSemaphore() { sem_destroy(&sem_); } |
| 487 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 488 | virtual void Wait(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 489 | virtual bool Wait(int timeout); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 490 | virtual void Signal() { sem_post(&sem_); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 491 | private: |
| 492 | sem_t sem_; |
| 493 | }; |
| 494 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 495 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 496 | void LinuxSemaphore::Wait() { |
| 497 | while (true) { |
| 498 | int result = sem_wait(&sem_); |
| 499 | if (result == 0) return; // Successfully got semaphore. |
| 500 | CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup. |
| 501 | } |
| 502 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 503 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 504 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 505 | #ifndef TIMEVAL_TO_TIMESPEC |
| 506 | #define TIMEVAL_TO_TIMESPEC(tv, ts) do { \ |
| 507 | (ts)->tv_sec = (tv)->tv_sec; \ |
| 508 | (ts)->tv_nsec = (tv)->tv_usec * 1000; \ |
| 509 | } while (false) |
| 510 | #endif |
| 511 | |
| 512 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 513 | bool LinuxSemaphore::Wait(int timeout) { |
| 514 | const long kOneSecondMicros = 1000000; // NOLINT |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 515 | |
| 516 | // Split timeout into second and nanosecond parts. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 517 | struct timeval delta; |
| 518 | delta.tv_usec = timeout % kOneSecondMicros; |
| 519 | delta.tv_sec = timeout / kOneSecondMicros; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 520 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 521 | struct timeval current_time; |
| 522 | // Get the current time. |
| 523 | if (gettimeofday(¤t_time, NULL) == -1) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 524 | return false; |
| 525 | } |
| 526 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 527 | // Calculate time for end of timeout. |
| 528 | struct timeval end_time; |
| 529 | timeradd(¤t_time, &delta, &end_time); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 530 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 531 | struct timespec ts; |
| 532 | TIMEVAL_TO_TIMESPEC(&end_time, &ts); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 533 | // Wait for semaphore signalled or timeout. |
| 534 | while (true) { |
| 535 | int result = sem_timedwait(&sem_, &ts); |
| 536 | if (result == 0) return true; // Successfully got semaphore. |
| 537 | if (result > 0) { |
| 538 | // For glibc prior to 2.3.4 sem_timedwait returns the error instead of -1. |
| 539 | errno = result; |
| 540 | result = -1; |
| 541 | } |
| 542 | if (result == -1 && errno == ETIMEDOUT) return false; // Timeout. |
| 543 | CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup. |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 548 | Semaphore* OS::CreateSemaphore(int count) { |
| 549 | return new LinuxSemaphore(count); |
| 550 | } |
| 551 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 552 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 553 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 554 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 555 | static Sampler* active_sampler_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 556 | |
kasperl@chromium.org | acae378 | 2009-04-11 09:17:08 +0000 | [diff] [blame] | 557 | |
| 558 | #if !defined(__GLIBC__) && (defined(__arm__) || defined(__thumb__)) |
| 559 | // Android runs a fairly new Linux kernel, so signal info is there, |
| 560 | // but the C library doesn't have the structs defined. |
| 561 | |
| 562 | struct sigcontext { |
| 563 | uint32_t trap_no; |
| 564 | uint32_t error_code; |
| 565 | uint32_t oldmask; |
| 566 | uint32_t gregs[16]; |
| 567 | uint32_t arm_cpsr; |
| 568 | uint32_t fault_address; |
| 569 | }; |
| 570 | typedef uint32_t __sigset_t; |
| 571 | typedef struct sigcontext mcontext_t; |
| 572 | typedef struct ucontext { |
| 573 | uint32_t uc_flags; |
| 574 | struct ucontext *uc_link; |
| 575 | stack_t uc_stack; |
| 576 | mcontext_t uc_mcontext; |
| 577 | __sigset_t uc_sigmask; |
| 578 | } ucontext_t; |
| 579 | enum ArmRegisters {R15 = 15, R13 = 13, R11 = 11}; |
| 580 | |
| 581 | #endif |
| 582 | |
| 583 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 584 | static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) { |
| 585 | USE(info); |
| 586 | if (signal != SIGPROF) return; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 587 | if (active_sampler_ == NULL) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 588 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 589 | TickSample sample; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 590 | |
| 591 | // If profiling, we extract the current pc and sp. |
| 592 | if (active_sampler_->IsProfiling()) { |
| 593 | // Extracting the sample from the context is extremely machine dependent. |
| 594 | ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context); |
| 595 | mcontext_t& mcontext = ucontext->uc_mcontext; |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 596 | #if V8_HOST_ARCH_IA32 |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 597 | sample.pc = mcontext.gregs[REG_EIP]; |
| 598 | sample.sp = mcontext.gregs[REG_ESP]; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 599 | sample.fp = mcontext.gregs[REG_EBP]; |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 600 | #elif V8_HOST_ARCH_X64 |
| 601 | sample.pc = mcontext.gregs[REG_RIP]; |
| 602 | sample.sp = mcontext.gregs[REG_RSP]; |
| 603 | sample.fp = mcontext.gregs[REG_RBP]; |
| 604 | #elif V8_HOST_ARCH_ARM |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 605 | // An undefined macro evaluates to 0, so this applies to Android's Bionic also. |
| 606 | #if (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3)) |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 607 | sample.pc = mcontext.gregs[R15]; |
| 608 | sample.sp = mcontext.gregs[R13]; |
| 609 | sample.fp = mcontext.gregs[R11]; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 610 | #else |
| 611 | sample.pc = mcontext.arm_pc; |
| 612 | sample.sp = mcontext.arm_sp; |
| 613 | sample.fp = mcontext.arm_fp; |
| 614 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 615 | #endif |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 616 | active_sampler_->SampleStack(&sample); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | // We always sample the VM state. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 620 | sample.state = Logger::state(); |
| 621 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 622 | active_sampler_->Tick(&sample); |
| 623 | } |
| 624 | |
| 625 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 626 | class Sampler::PlatformData : public Malloced { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 627 | public: |
| 628 | PlatformData() { |
| 629 | signal_handler_installed_ = false; |
| 630 | } |
| 631 | |
| 632 | bool signal_handler_installed_; |
| 633 | struct sigaction old_signal_handler_; |
| 634 | struct itimerval old_timer_value_; |
| 635 | }; |
| 636 | |
| 637 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 638 | Sampler::Sampler(int interval, bool profiling) |
| 639 | : interval_(interval), profiling_(profiling), active_(false) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 640 | data_ = new PlatformData(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 644 | Sampler::~Sampler() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 645 | delete data_; |
| 646 | } |
| 647 | |
| 648 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 649 | void Sampler::Start() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 650 | // There can only be one active sampler at the time on POSIX |
| 651 | // platforms. |
| 652 | if (active_sampler_ != NULL) return; |
| 653 | |
| 654 | // Request profiling signals. |
| 655 | struct sigaction sa; |
| 656 | sa.sa_sigaction = ProfilerSignalHandler; |
| 657 | sigemptyset(&sa.sa_mask); |
| 658 | sa.sa_flags = SA_SIGINFO; |
| 659 | if (sigaction(SIGPROF, &sa, &data_->old_signal_handler_) != 0) return; |
| 660 | data_->signal_handler_installed_ = true; |
| 661 | |
| 662 | // Set the itimer to generate a tick for each interval. |
| 663 | itimerval itimer; |
| 664 | itimer.it_interval.tv_sec = interval_ / 1000; |
| 665 | itimer.it_interval.tv_usec = (interval_ % 1000) * 1000; |
| 666 | itimer.it_value.tv_sec = itimer.it_interval.tv_sec; |
| 667 | itimer.it_value.tv_usec = itimer.it_interval.tv_usec; |
| 668 | setitimer(ITIMER_PROF, &itimer, &data_->old_timer_value_); |
| 669 | |
| 670 | // Set this sampler as the active sampler. |
| 671 | active_sampler_ = this; |
| 672 | active_ = true; |
| 673 | } |
| 674 | |
| 675 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 676 | void Sampler::Stop() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 677 | // Restore old signal handler |
| 678 | if (data_->signal_handler_installed_) { |
| 679 | setitimer(ITIMER_PROF, &data_->old_timer_value_, NULL); |
| 680 | sigaction(SIGPROF, &data_->old_signal_handler_, 0); |
| 681 | data_->signal_handler_installed_ = false; |
| 682 | } |
| 683 | |
| 684 | // This sampler is no longer the active sampler. |
| 685 | active_sampler_ = NULL; |
| 686 | active_ = false; |
| 687 | } |
| 688 | |
| 689 | #endif // ENABLE_LOGGING_AND_PROFILING |
| 690 | |
| 691 | } } // namespace v8::internal |