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