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