sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 1 | // Copyright 2011 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> |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 34 | #include <sys/prctl.h> |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 35 | #include <sys/time.h> |
| 36 | #include <sys/resource.h> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 37 | #include <sys/syscall.h> |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 38 | #include <sys/types.h> |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 39 | #include <stdlib.h> |
| 40 | |
| 41 | // Ubuntu Dapper requires memory pages to be marked as |
| 42 | // executable. Otherwise, OS raises an exception when executing code |
| 43 | // in that page. |
| 44 | #include <sys/types.h> // mmap & munmap |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 45 | #include <sys/mman.h> // mmap & munmap |
| 46 | #include <sys/stat.h> // open |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 47 | #include <fcntl.h> // open |
| 48 | #include <unistd.h> // sysconf |
| 49 | #ifdef __GLIBC__ |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 50 | #include <execinfo.h> // backtrace, backtrace_symbols |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 51 | #endif // def __GLIBC__ |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 52 | #include <strings.h> // index |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 53 | #include <errno.h> |
| 54 | #include <stdarg.h> |
| 55 | |
| 56 | #undef MAP_TYPE |
| 57 | |
| 58 | #include "v8.h" |
| 59 | |
| 60 | #include "platform.h" |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 61 | #include "v8threads.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 62 | #include "vm-state-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 63 | |
| 64 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 65 | namespace v8 { |
| 66 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 67 | |
| 68 | // 0 is never a valid thread id on Linux since tids and pids share a |
| 69 | // name space and pid 0 is reserved (see man 2 kill). |
| 70 | static const pthread_t kNoThread = (pthread_t) 0; |
| 71 | |
| 72 | |
| 73 | double ceiling(double x) { |
| 74 | return ceil(x); |
| 75 | } |
| 76 | |
| 77 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 78 | static Mutex* limit_mutex = NULL; |
| 79 | |
| 80 | |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 81 | static void* GetRandomMmapAddr() { |
| 82 | Isolate* isolate = Isolate::UncheckedCurrent(); |
| 83 | // Note that the current isolate isn't set up in a call path via |
| 84 | // CpuFeatures::Probe. We don't care about randomization in this case because |
| 85 | // the code page is immediately freed. |
| 86 | if (isolate != NULL) { |
| 87 | #ifdef V8_TARGET_ARCH_X64 |
| 88 | uint64_t rnd1 = V8::RandomPrivate(isolate); |
| 89 | uint64_t rnd2 = V8::RandomPrivate(isolate); |
| 90 | uint64_t raw_addr = (rnd1 << 32) ^ rnd2; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame^] | 91 | // Currently available CPUs have 48 bits of virtual addressing. Truncate |
| 92 | // the hint address to 46 bits to give the kernel a fighting chance of |
| 93 | // fulfilling our placement request. |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 94 | raw_addr &= V8_UINT64_C(0x3ffffffff000); |
| 95 | #else |
| 96 | uint32_t raw_addr = V8::RandomPrivate(isolate); |
| 97 | // The range 0x20000000 - 0x60000000 is relatively unpopulated across a |
| 98 | // variety of ASLR modes (PAE kernel, NX compat mode, etc). |
| 99 | raw_addr &= 0x3ffff000; |
| 100 | raw_addr += 0x20000000; |
| 101 | #endif |
| 102 | return reinterpret_cast<void*>(raw_addr); |
| 103 | } |
| 104 | return NULL; |
| 105 | } |
| 106 | |
| 107 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 108 | void OS::Setup() { |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 109 | // Seed the random number generator. We preserve microsecond resolution. |
| 110 | uint64_t seed = Ticks() ^ (getpid() << 16); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 111 | srandom(static_cast<unsigned int>(seed)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 112 | limit_mutex = CreateMutex(); |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 113 | |
| 114 | #ifdef __arm__ |
| 115 | // When running on ARM hardware check that the EABI used by V8 and |
| 116 | // by the C code is the same. |
| 117 | bool hard_float = OS::ArmUsingHardFloat(); |
| 118 | if (hard_float) { |
| 119 | #if !USE_EABI_HARDFLOAT |
| 120 | PrintF("ERROR: Binary compiled with -mfloat-abi=hard but without " |
| 121 | "-DUSE_EABI_HARDFLOAT\n"); |
| 122 | exit(1); |
| 123 | #endif |
| 124 | } else { |
| 125 | #if USE_EABI_HARDFLOAT |
| 126 | PrintF("ERROR: Binary not compiled with -mfloat-abi=hard but with " |
| 127 | "-DUSE_EABI_HARDFLOAT\n"); |
| 128 | exit(1); |
| 129 | #endif |
| 130 | } |
| 131 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 135 | uint64_t OS::CpuFeaturesImpliedByPlatform() { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 136 | return 0; // Linux runs on anything. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 140 | #ifdef __arm__ |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 141 | static bool CPUInfoContainsString(const char * search_string) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 142 | const char* file_name = "/proc/cpuinfo"; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 143 | // This is written as a straight shot one pass parser |
| 144 | // and not using STL string and ifstream because, |
| 145 | // on Linux, it's reading from a (non-mmap-able) |
| 146 | // character special device. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 147 | FILE* f = NULL; |
| 148 | const char* what = search_string; |
| 149 | |
| 150 | if (NULL == (f = fopen(file_name, "r"))) |
| 151 | return false; |
| 152 | |
| 153 | int k; |
| 154 | while (EOF != (k = fgetc(f))) { |
| 155 | if (k == *what) { |
| 156 | ++what; |
| 157 | while ((*what != '\0') && (*what == fgetc(f))) { |
| 158 | ++what; |
| 159 | } |
| 160 | if (*what == '\0') { |
| 161 | fclose(f); |
| 162 | return true; |
| 163 | } else { |
| 164 | what = search_string; |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | fclose(f); |
| 169 | |
| 170 | // Did not find string in the proc file. |
| 171 | return false; |
| 172 | } |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 173 | |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 174 | |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 175 | bool OS::ArmCpuHasFeature(CpuFeature feature) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 176 | const char* search_string = NULL; |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 177 | // Simple detection of VFP at runtime for Linux. |
| 178 | // It is based on /proc/cpuinfo, which reveals hardware configuration |
| 179 | // to user-space applications. According to ARM (mid 2009), no similar |
| 180 | // facility is universally available on the ARM architectures, |
| 181 | // so it's up to individual OSes to provide such. |
| 182 | switch (feature) { |
| 183 | case VFP3: |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 184 | search_string = "vfpv3"; |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 185 | break; |
| 186 | case ARMv7: |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 187 | search_string = "ARMv7"; |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 188 | break; |
| 189 | default: |
| 190 | UNREACHABLE(); |
| 191 | } |
| 192 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 193 | if (CPUInfoContainsString(search_string)) { |
| 194 | return true; |
| 195 | } |
| 196 | |
| 197 | if (feature == VFP3) { |
| 198 | // Some old kernels will report vfp not vfpv3. Here we make a last attempt |
| 199 | // to detect vfpv3 by checking for vfp *and* neon, since neon is only |
| 200 | // available on architectures with vfpv3. |
| 201 | // Checking neon on its own is not enough as it is possible to have neon |
| 202 | // without vfp. |
| 203 | if (CPUInfoContainsString("vfp") && CPUInfoContainsString("neon")) { |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 204 | return true; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | return false; |
| 209 | } |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 210 | |
| 211 | |
| 212 | // Simple helper function to detect whether the C code is compiled with |
| 213 | // option -mfloat-abi=hard. The register d0 is loaded with 1.0 and the register |
| 214 | // pair r0, r1 is loaded with 0.0. If -mfloat-abi=hard is pased to GCC then |
| 215 | // calling this will return 1.0 and otherwise 0.0. |
| 216 | static void ArmUsingHardFloatHelper() { |
| 217 | asm("mov r0, #0"); |
| 218 | #if defined(__VFP_FP__) && !defined(__SOFTFP__) |
| 219 | // Load 0x3ff00000 into r1 using instructions available in both ARM |
| 220 | // and Thumb mode. |
| 221 | asm("mov r1, #3"); |
| 222 | asm("mov r2, #255"); |
| 223 | asm("lsl r1, r1, #8"); |
| 224 | asm("orr r1, r1, r2"); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 225 | asm("lsl r1, r1, #20"); |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 226 | // For vmov d0, r0, r1 use ARM mode. |
| 227 | #ifdef __thumb__ |
| 228 | asm volatile( |
| 229 | "@ Enter ARM Mode \n\t" |
| 230 | " adr r3, 1f \n\t" |
| 231 | " bx r3 \n\t" |
| 232 | " .ALIGN 4 \n\t" |
| 233 | " .ARM \n" |
| 234 | "1: vmov d0, r0, r1 \n\t" |
| 235 | "@ Enter THUMB Mode\n\t" |
| 236 | " adr r3, 2f+1 \n\t" |
| 237 | " bx r3 \n\t" |
| 238 | " .THUMB \n" |
| 239 | "2: \n\t"); |
| 240 | #else |
| 241 | asm("vmov d0, r0, r1"); |
| 242 | #endif // __thumb__ |
| 243 | #endif // defined(__VFP_FP__) && !defined(__SOFTFP__) |
| 244 | asm("mov r1, #0"); |
| 245 | } |
| 246 | |
| 247 | |
| 248 | bool OS::ArmUsingHardFloat() { |
| 249 | // Cast helper function from returning void to returning double. |
| 250 | typedef double (*F)(); |
| 251 | F f = FUNCTION_CAST<F>(FUNCTION_ADDR(ArmUsingHardFloatHelper)); |
| 252 | return f() == 1.0; |
| 253 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 254 | #endif // def __arm__ |
| 255 | |
| 256 | |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 257 | #ifdef __mips__ |
| 258 | bool OS::MipsCpuHasFeature(CpuFeature feature) { |
| 259 | const char* search_string = NULL; |
| 260 | const char* file_name = "/proc/cpuinfo"; |
| 261 | // Simple detection of FPU at runtime for Linux. |
| 262 | // It is based on /proc/cpuinfo, which reveals hardware configuration |
| 263 | // to user-space applications. According to MIPS (early 2010), no similar |
| 264 | // facility is universally available on the MIPS architectures, |
| 265 | // so it's up to individual OSes to provide such. |
| 266 | // |
| 267 | // This is written as a straight shot one pass parser |
| 268 | // and not using STL string and ifstream because, |
| 269 | // on Linux, it's reading from a (non-mmap-able) |
| 270 | // character special device. |
| 271 | |
| 272 | switch (feature) { |
| 273 | case FPU: |
| 274 | search_string = "FPU"; |
| 275 | break; |
| 276 | default: |
| 277 | UNREACHABLE(); |
| 278 | } |
| 279 | |
| 280 | FILE* f = NULL; |
| 281 | const char* what = search_string; |
| 282 | |
| 283 | if (NULL == (f = fopen(file_name, "r"))) |
| 284 | return false; |
| 285 | |
| 286 | int k; |
| 287 | while (EOF != (k = fgetc(f))) { |
| 288 | if (k == *what) { |
| 289 | ++what; |
| 290 | while ((*what != '\0') && (*what == fgetc(f))) { |
| 291 | ++what; |
| 292 | } |
| 293 | if (*what == '\0') { |
| 294 | fclose(f); |
| 295 | return true; |
| 296 | } else { |
| 297 | what = search_string; |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | fclose(f); |
| 302 | |
| 303 | // Did not find string in the proc file. |
| 304 | return false; |
| 305 | } |
| 306 | #endif // def __mips__ |
| 307 | |
| 308 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 309 | int OS::ActivationFrameAlignment() { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 310 | #ifdef V8_TARGET_ARCH_ARM |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 311 | // On EABI ARM targets this is required for fp correctness in the |
| 312 | // runtime system. |
ager@chromium.org | 3a6061e | 2009-03-12 14:24:36 +0000 | [diff] [blame] | 313 | return 8; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 314 | #elif V8_TARGET_ARCH_MIPS |
| 315 | return 8; |
| 316 | #endif |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 317 | // With gcc 4.4 the tree vectorization optimizer can generate code |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 318 | // that requires 16 byte alignment such as movdqa on x86. |
| 319 | return 16; |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 320 | } |
| 321 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 322 | |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 323 | void OS::ReleaseStore(volatile AtomicWord* ptr, AtomicWord value) { |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 324 | #if (defined(V8_TARGET_ARCH_ARM) && defined(__arm__)) || \ |
| 325 | (defined(V8_TARGET_ARCH_MIPS) && defined(__mips__)) |
| 326 | // Only use on ARM or MIPS hardware. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 327 | MemoryBarrier(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 328 | #else |
| 329 | __asm__ __volatile__("" : : : "memory"); |
| 330 | // An x86 store acts as a release barrier. |
| 331 | #endif |
| 332 | *ptr = value; |
| 333 | } |
| 334 | |
| 335 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 336 | const char* OS::LocalTimezone(double time) { |
| 337 | if (isnan(time)) return ""; |
| 338 | time_t tv = static_cast<time_t>(floor(time/msPerSecond)); |
| 339 | struct tm* t = localtime(&tv); |
| 340 | if (NULL == t) return ""; |
| 341 | return t->tm_zone; |
| 342 | } |
| 343 | |
| 344 | |
| 345 | double OS::LocalTimeOffset() { |
| 346 | time_t tv = time(NULL); |
| 347 | struct tm* t = localtime(&tv); |
| 348 | // tm_gmtoff includes any daylight savings offset, so subtract it. |
| 349 | return static_cast<double>(t->tm_gmtoff * msPerSecond - |
| 350 | (t->tm_isdst > 0 ? 3600 * msPerSecond : 0)); |
| 351 | } |
| 352 | |
| 353 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 354 | // We keep the lowest and highest addresses mapped as a quick way of |
| 355 | // determining that pointers are outside the heap (used mostly in assertions |
| 356 | // and verification). The estimate is conservative, ie, not all addresses in |
| 357 | // 'allocated' space are actually allocated to our heap. The range is |
| 358 | // [lowest, highest), inclusive on the low and and exclusive on the high end. |
| 359 | static void* lowest_ever_allocated = reinterpret_cast<void*>(-1); |
| 360 | static void* highest_ever_allocated = reinterpret_cast<void*>(0); |
| 361 | |
| 362 | |
| 363 | static void UpdateAllocatedSpaceLimits(void* address, int size) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 364 | ASSERT(limit_mutex != NULL); |
| 365 | ScopedLock lock(limit_mutex); |
| 366 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 367 | lowest_ever_allocated = Min(lowest_ever_allocated, address); |
| 368 | highest_ever_allocated = |
| 369 | Max(highest_ever_allocated, |
| 370 | reinterpret_cast<void*>(reinterpret_cast<char*>(address) + size)); |
| 371 | } |
| 372 | |
| 373 | |
| 374 | bool OS::IsOutsideAllocatedSpace(void* address) { |
| 375 | return address < lowest_ever_allocated || address >= highest_ever_allocated; |
| 376 | } |
| 377 | |
| 378 | |
| 379 | size_t OS::AllocateAlignment() { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 380 | return sysconf(_SC_PAGESIZE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 384 | void* OS::Allocate(const size_t requested, |
| 385 | size_t* allocated, |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 386 | bool is_executable) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame^] | 387 | const size_t msize = RoundUp(requested, AllocateAlignment()); |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 388 | int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 389 | void* addr = GetRandomMmapAddr(); |
| 390 | void* mbase = mmap(addr, msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 391 | if (mbase == MAP_FAILED) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 392 | LOG(i::Isolate::Current(), |
| 393 | StringEvent("OS::Allocate", "mmap failed")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 394 | return NULL; |
| 395 | } |
| 396 | *allocated = msize; |
| 397 | UpdateAllocatedSpaceLimits(mbase, msize); |
| 398 | return mbase; |
| 399 | } |
| 400 | |
| 401 | |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 402 | void OS::Free(void* address, const size_t size) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 403 | // TODO(1240712): munmap has a return value which is ignored here. |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 404 | int result = munmap(address, size); |
| 405 | USE(result); |
| 406 | ASSERT(result == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | |
| 410 | void OS::Sleep(int milliseconds) { |
| 411 | unsigned int ms = static_cast<unsigned int>(milliseconds); |
| 412 | usleep(1000 * ms); |
| 413 | } |
| 414 | |
| 415 | |
| 416 | void OS::Abort() { |
| 417 | // Redirect to std abort to signal abnormal program termination. |
| 418 | abort(); |
| 419 | } |
| 420 | |
| 421 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 422 | void OS::DebugBreak() { |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 423 | // TODO(lrn): Introduce processor define for runtime system (!= V8_ARCH_x, |
| 424 | // which is the architecture of generated code). |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 425 | #if (defined(__arm__) || defined(__thumb__)) |
| 426 | # if defined(CAN_USE_ARMV5_INSTRUCTIONS) |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 427 | asm("bkpt 0"); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 428 | # endif |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 429 | #elif defined(__mips__) |
| 430 | asm("break"); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 431 | #else |
| 432 | asm("int $3"); |
| 433 | #endif |
| 434 | } |
| 435 | |
| 436 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 437 | class PosixMemoryMappedFile : public OS::MemoryMappedFile { |
| 438 | public: |
| 439 | PosixMemoryMappedFile(FILE* file, void* memory, int size) |
| 440 | : file_(file), memory_(memory), size_(size) { } |
| 441 | virtual ~PosixMemoryMappedFile(); |
| 442 | virtual void* memory() { return memory_; } |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 443 | virtual int size() { return size_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 444 | private: |
| 445 | FILE* file_; |
| 446 | void* memory_; |
| 447 | int size_; |
| 448 | }; |
| 449 | |
| 450 | |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 451 | OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 452 | FILE* file = fopen(name, "r+"); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 453 | if (file == NULL) return NULL; |
| 454 | |
| 455 | fseek(file, 0, SEEK_END); |
| 456 | int size = ftell(file); |
| 457 | |
| 458 | void* memory = |
| 459 | mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); |
| 460 | return new PosixMemoryMappedFile(file, memory, size); |
| 461 | } |
| 462 | |
| 463 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 464 | OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size, |
| 465 | void* initial) { |
| 466 | FILE* file = fopen(name, "w+"); |
| 467 | if (file == NULL) return NULL; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 468 | int result = fwrite(initial, size, 1, file); |
| 469 | if (result < 1) { |
| 470 | fclose(file); |
| 471 | return NULL; |
| 472 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 473 | void* memory = |
| 474 | mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); |
| 475 | return new PosixMemoryMappedFile(file, memory, size); |
| 476 | } |
| 477 | |
| 478 | |
| 479 | PosixMemoryMappedFile::~PosixMemoryMappedFile() { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame^] | 480 | if (memory_) OS::Free(memory_, size_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 481 | fclose(file_); |
| 482 | } |
| 483 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 484 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 485 | void OS::LogSharedLibraryAddresses() { |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 486 | // This function assumes that the layout of the file is as follows: |
| 487 | // hex_start_addr-hex_end_addr rwxp <unused data> [binary_file_name] |
| 488 | // If we encounter an unexpected situation we abort scanning further entries. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 489 | FILE* fp = fopen("/proc/self/maps", "r"); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 490 | if (fp == NULL) return; |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 491 | |
| 492 | // Allocate enough room to be able to store a full file name. |
| 493 | const int kLibNameLen = FILENAME_MAX + 1; |
| 494 | char* lib_name = reinterpret_cast<char*>(malloc(kLibNameLen)); |
| 495 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 496 | i::Isolate* isolate = ISOLATE; |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 497 | // This loop will terminate once the scanning hits an EOF. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 498 | while (true) { |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 499 | uintptr_t start, end; |
| 500 | char attr_r, attr_w, attr_x, attr_p; |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 501 | // Parse the addresses and permission bits at the beginning of the line. |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 502 | if (fscanf(fp, "%" V8PRIxPTR "-%" V8PRIxPTR, &start, &end) != 2) break; |
| 503 | if (fscanf(fp, " %c%c%c%c", &attr_r, &attr_w, &attr_x, &attr_p) != 4) break; |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 504 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 505 | int c; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 506 | if (attr_r == 'r' && attr_w != 'w' && attr_x == 'x') { |
| 507 | // Found a read-only executable entry. Skip characters until we reach |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 508 | // the beginning of the filename or the end of the line. |
| 509 | do { |
| 510 | c = getc(fp); |
| 511 | } while ((c != EOF) && (c != '\n') && (c != '/')); |
| 512 | if (c == EOF) break; // EOF: Was unexpected, just exit. |
| 513 | |
| 514 | // Process the filename if found. |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 515 | if (c == '/') { |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 516 | ungetc(c, fp); // Push the '/' back into the stream to be read below. |
| 517 | |
| 518 | // Read to the end of the line. Exit if the read fails. |
| 519 | if (fgets(lib_name, kLibNameLen, fp) == NULL) break; |
| 520 | |
| 521 | // Drop the newline character read by fgets. We do not need to check |
| 522 | // for a zero-length string because we know that we at least read the |
| 523 | // '/' character. |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 524 | lib_name[strlen(lib_name) - 1] = '\0'; |
| 525 | } else { |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 526 | // No library name found, just record the raw address range. |
| 527 | snprintf(lib_name, kLibNameLen, |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 528 | "%08" V8PRIxPTR "-%08" V8PRIxPTR, start, end); |
| 529 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 530 | LOG(isolate, SharedLibraryEvent(lib_name, start, end)); |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 531 | } else { |
| 532 | // Entry not describing executable data. Skip to end of line to setup |
| 533 | // reading the next entry. |
| 534 | do { |
| 535 | c = getc(fp); |
| 536 | } while ((c != EOF) && (c != '\n')); |
| 537 | if (c == EOF) break; |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 538 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 539 | } |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 540 | free(lib_name); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 541 | fclose(fp); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 545 | static const char kGCFakeMmap[] = "/tmp/__v8_gc__"; |
| 546 | |
| 547 | |
| 548 | void OS::SignalCodeMovingGC() { |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 549 | // Support for ll_prof.py. |
| 550 | // |
| 551 | // The Linux profiler built into the kernel logs all mmap's with |
| 552 | // PROT_EXEC so that analysis tools can properly attribute ticks. We |
| 553 | // do a mmap with a name known by ll_prof.py and immediately munmap |
| 554 | // it. This injects a GC marker into the stream of events generated |
| 555 | // by the kernel and allows us to synchronize V8 code log and the |
| 556 | // kernel log. |
| 557 | int size = sysconf(_SC_PAGESIZE); |
| 558 | FILE* f = fopen(kGCFakeMmap, "w+"); |
| 559 | void* addr = mmap(NULL, size, PROT_READ | PROT_EXEC, MAP_PRIVATE, |
| 560 | fileno(f), 0); |
| 561 | ASSERT(addr != MAP_FAILED); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame^] | 562 | OS::Free(addr, size); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 563 | fclose(f); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 567 | int OS::StackWalk(Vector<OS::StackFrame> frames) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 568 | // backtrace is a glibc extension. |
| 569 | #ifdef __GLIBC__ |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 570 | int frames_size = frames.length(); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 571 | ScopedVector<void*> addresses(frames_size); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 572 | |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 573 | int frames_count = backtrace(addresses.start(), frames_size); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 574 | |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 575 | char** symbols = backtrace_symbols(addresses.start(), frames_count); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 576 | if (symbols == NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 577 | return kStackWalkError; |
| 578 | } |
| 579 | |
| 580 | for (int i = 0; i < frames_count; i++) { |
| 581 | frames[i].address = addresses[i]; |
| 582 | // Format a text representation of the frame based on the information |
| 583 | // available. |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 584 | SNPrintF(MutableCStrVector(frames[i].text, kStackWalkMaxTextLen), |
| 585 | "%s", |
| 586 | symbols[i]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 587 | // Make sure line termination is in place. |
| 588 | frames[i].text[kStackWalkMaxTextLen - 1] = '\0'; |
| 589 | } |
| 590 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 591 | free(symbols); |
| 592 | |
| 593 | return frames_count; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 594 | #else // ndef __GLIBC__ |
| 595 | return 0; |
| 596 | #endif // ndef __GLIBC__ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | |
| 600 | // Constants used for mmap. |
| 601 | static const int kMmapFd = -1; |
| 602 | static const int kMmapFdOffset = 0; |
| 603 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame^] | 604 | VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 605 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 606 | VirtualMemory::VirtualMemory(size_t size) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame^] | 607 | address_ = ReserveRegion(size); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 608 | size_ = size; |
| 609 | } |
| 610 | |
| 611 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame^] | 612 | VirtualMemory::VirtualMemory(size_t size, size_t alignment) |
| 613 | : address_(NULL), size_(0) { |
| 614 | ASSERT(IsAligned(alignment, static_cast<intptr_t>(OS::AllocateAlignment()))); |
| 615 | size_t request_size = RoundUp(size + alignment, |
| 616 | static_cast<intptr_t>(OS::AllocateAlignment())); |
| 617 | void* reservation = mmap(GetRandomMmapAddr(), |
| 618 | request_size, |
| 619 | PROT_NONE, |
| 620 | MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, |
| 621 | kMmapFd, |
| 622 | kMmapFdOffset); |
| 623 | if (reservation == MAP_FAILED) return; |
| 624 | |
| 625 | Address base = static_cast<Address>(reservation); |
| 626 | Address aligned_base = RoundUp(base, alignment); |
| 627 | ASSERT_LE(base, aligned_base); |
| 628 | |
| 629 | // Unmap extra memory reserved before and after the desired block. |
| 630 | if (aligned_base != base) { |
| 631 | size_t prefix_size = static_cast<size_t>(aligned_base - base); |
| 632 | OS::Free(base, prefix_size); |
| 633 | request_size -= prefix_size; |
| 634 | } |
| 635 | |
| 636 | size_t aligned_size = RoundUp(size, OS::AllocateAlignment()); |
| 637 | ASSERT_LE(aligned_size, request_size); |
| 638 | |
| 639 | if (aligned_size != request_size) { |
| 640 | size_t suffix_size = request_size - aligned_size; |
| 641 | OS::Free(aligned_base + aligned_size, suffix_size); |
| 642 | request_size -= suffix_size; |
| 643 | } |
| 644 | |
| 645 | ASSERT(aligned_size == request_size); |
| 646 | |
| 647 | address_ = static_cast<void*>(aligned_base); |
| 648 | size_ = aligned_size; |
| 649 | } |
| 650 | |
| 651 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 652 | VirtualMemory::~VirtualMemory() { |
| 653 | if (IsReserved()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame^] | 654 | bool result = ReleaseRegion(address(), size()); |
| 655 | ASSERT(result); |
| 656 | USE(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 657 | } |
| 658 | } |
| 659 | |
| 660 | |
| 661 | bool VirtualMemory::IsReserved() { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame^] | 662 | return address_ != NULL; |
| 663 | } |
| 664 | |
| 665 | |
| 666 | void VirtualMemory::Reset() { |
| 667 | address_ = NULL; |
| 668 | size_ = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 672 | bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame^] | 673 | return CommitRegion(address, size, is_executable); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | |
| 677 | bool VirtualMemory::Uncommit(void* address, size_t size) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame^] | 678 | return UncommitRegion(address, size); |
| 679 | } |
| 680 | |
| 681 | |
| 682 | void* VirtualMemory::ReserveRegion(size_t size) { |
| 683 | void* result = mmap(GetRandomMmapAddr(), |
| 684 | size, |
| 685 | PROT_NONE, |
| 686 | MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, |
| 687 | kMmapFd, |
| 688 | kMmapFdOffset); |
| 689 | |
| 690 | if (result == MAP_FAILED) return NULL; |
| 691 | |
| 692 | return result; |
| 693 | } |
| 694 | |
| 695 | |
| 696 | bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) { |
| 697 | int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); |
| 698 | if (MAP_FAILED == mmap(base, |
| 699 | size, |
| 700 | prot, |
| 701 | MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, |
| 702 | kMmapFd, |
| 703 | kMmapFdOffset)) { |
| 704 | return false; |
| 705 | } |
| 706 | |
| 707 | UpdateAllocatedSpaceLimits(base, size); |
| 708 | return true; |
| 709 | } |
| 710 | |
| 711 | |
| 712 | bool VirtualMemory::UncommitRegion(void* base, size_t size) { |
| 713 | return mmap(base, |
| 714 | size, |
| 715 | PROT_NONE, |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 716 | MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE | MAP_FIXED, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame^] | 717 | kMmapFd, |
| 718 | kMmapFdOffset) != MAP_FAILED; |
| 719 | } |
| 720 | |
| 721 | |
| 722 | bool VirtualMemory::ReleaseRegion(void* base, size_t size) { |
| 723 | return munmap(base, size) == 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 727 | class Thread::PlatformData : public Malloced { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 728 | public: |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 729 | PlatformData() : thread_(kNoThread) {} |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 730 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 731 | pthread_t thread_; // Thread handle for pthread. |
| 732 | }; |
| 733 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 734 | Thread::Thread(const Options& options) |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 735 | : data_(new PlatformData()), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 736 | stack_size_(options.stack_size) { |
| 737 | set_name(options.name); |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 741 | Thread::Thread(const char* name) |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 742 | : data_(new PlatformData()), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 743 | stack_size_(0) { |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 744 | set_name(name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | |
| 748 | Thread::~Thread() { |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 749 | delete data_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | |
| 753 | static void* ThreadEntry(void* arg) { |
| 754 | Thread* thread = reinterpret_cast<Thread*>(arg); |
| 755 | // This is also initialized by the first argument to pthread_create() but we |
| 756 | // don't know which thread will run first (the original thread or the new |
| 757 | // one) so we initialize it here too. |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 758 | #ifdef PR_SET_NAME |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 759 | prctl(PR_SET_NAME, |
| 760 | reinterpret_cast<unsigned long>(thread->name()), // NOLINT |
| 761 | 0, 0, 0); |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 762 | #endif |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 763 | thread->data()->thread_ = pthread_self(); |
| 764 | ASSERT(thread->data()->thread_ != kNoThread); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 765 | thread->Run(); |
| 766 | return NULL; |
| 767 | } |
| 768 | |
| 769 | |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 770 | void Thread::set_name(const char* name) { |
| 771 | strncpy(name_, name, sizeof(name_)); |
| 772 | name_[sizeof(name_) - 1] = '\0'; |
| 773 | } |
| 774 | |
| 775 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 776 | void Thread::Start() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 777 | pthread_attr_t* attr_ptr = NULL; |
| 778 | pthread_attr_t attr; |
| 779 | if (stack_size_ > 0) { |
| 780 | pthread_attr_init(&attr); |
| 781 | pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_)); |
| 782 | attr_ptr = &attr; |
| 783 | } |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 784 | pthread_create(&data_->thread_, attr_ptr, ThreadEntry, this); |
| 785 | ASSERT(data_->thread_ != kNoThread); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | |
| 789 | void Thread::Join() { |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 790 | pthread_join(data_->thread_, NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | |
| 794 | Thread::LocalStorageKey Thread::CreateThreadLocalKey() { |
| 795 | pthread_key_t key; |
| 796 | int result = pthread_key_create(&key, NULL); |
| 797 | USE(result); |
| 798 | ASSERT(result == 0); |
| 799 | return static_cast<LocalStorageKey>(key); |
| 800 | } |
| 801 | |
| 802 | |
| 803 | void Thread::DeleteThreadLocalKey(LocalStorageKey key) { |
| 804 | pthread_key_t pthread_key = static_cast<pthread_key_t>(key); |
| 805 | int result = pthread_key_delete(pthread_key); |
| 806 | USE(result); |
| 807 | ASSERT(result == 0); |
| 808 | } |
| 809 | |
| 810 | |
| 811 | void* Thread::GetThreadLocal(LocalStorageKey key) { |
| 812 | pthread_key_t pthread_key = static_cast<pthread_key_t>(key); |
| 813 | return pthread_getspecific(pthread_key); |
| 814 | } |
| 815 | |
| 816 | |
| 817 | void Thread::SetThreadLocal(LocalStorageKey key, void* value) { |
| 818 | pthread_key_t pthread_key = static_cast<pthread_key_t>(key); |
| 819 | pthread_setspecific(pthread_key, value); |
| 820 | } |
| 821 | |
| 822 | |
| 823 | void Thread::YieldCPU() { |
| 824 | sched_yield(); |
| 825 | } |
| 826 | |
| 827 | |
| 828 | class LinuxMutex : public Mutex { |
| 829 | public: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 830 | LinuxMutex() { |
| 831 | pthread_mutexattr_t attrs; |
| 832 | int result = pthread_mutexattr_init(&attrs); |
| 833 | ASSERT(result == 0); |
| 834 | result = pthread_mutexattr_settype(&attrs, PTHREAD_MUTEX_RECURSIVE); |
| 835 | ASSERT(result == 0); |
| 836 | result = pthread_mutex_init(&mutex_, &attrs); |
| 837 | ASSERT(result == 0); |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 838 | USE(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | virtual ~LinuxMutex() { pthread_mutex_destroy(&mutex_); } |
| 842 | |
| 843 | virtual int Lock() { |
| 844 | int result = pthread_mutex_lock(&mutex_); |
| 845 | return result; |
| 846 | } |
| 847 | |
| 848 | virtual int Unlock() { |
| 849 | int result = pthread_mutex_unlock(&mutex_); |
| 850 | return result; |
| 851 | } |
| 852 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 853 | virtual bool TryLock() { |
| 854 | int result = pthread_mutex_trylock(&mutex_); |
| 855 | // Return false if the lock is busy and locking failed. |
| 856 | if (result == EBUSY) { |
| 857 | return false; |
| 858 | } |
| 859 | ASSERT(result == 0); // Verify no other errors. |
| 860 | return true; |
| 861 | } |
| 862 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 863 | private: |
| 864 | pthread_mutex_t mutex_; // Pthread mutex for POSIX platforms. |
| 865 | }; |
| 866 | |
| 867 | |
| 868 | Mutex* OS::CreateMutex() { |
| 869 | return new LinuxMutex(); |
| 870 | } |
| 871 | |
| 872 | |
| 873 | class LinuxSemaphore : public Semaphore { |
| 874 | public: |
| 875 | explicit LinuxSemaphore(int count) { sem_init(&sem_, 0, count); } |
| 876 | virtual ~LinuxSemaphore() { sem_destroy(&sem_); } |
| 877 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 878 | virtual void Wait(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 879 | virtual bool Wait(int timeout); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 880 | virtual void Signal() { sem_post(&sem_); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 881 | private: |
| 882 | sem_t sem_; |
| 883 | }; |
| 884 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 885 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 886 | void LinuxSemaphore::Wait() { |
| 887 | while (true) { |
| 888 | int result = sem_wait(&sem_); |
| 889 | if (result == 0) return; // Successfully got semaphore. |
| 890 | CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup. |
| 891 | } |
| 892 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 893 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 894 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 895 | #ifndef TIMEVAL_TO_TIMESPEC |
| 896 | #define TIMEVAL_TO_TIMESPEC(tv, ts) do { \ |
| 897 | (ts)->tv_sec = (tv)->tv_sec; \ |
| 898 | (ts)->tv_nsec = (tv)->tv_usec * 1000; \ |
| 899 | } while (false) |
| 900 | #endif |
| 901 | |
| 902 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 903 | bool LinuxSemaphore::Wait(int timeout) { |
| 904 | const long kOneSecondMicros = 1000000; // NOLINT |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 905 | |
| 906 | // Split timeout into second and nanosecond parts. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 907 | struct timeval delta; |
| 908 | delta.tv_usec = timeout % kOneSecondMicros; |
| 909 | delta.tv_sec = timeout / kOneSecondMicros; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 910 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 911 | struct timeval current_time; |
| 912 | // Get the current time. |
| 913 | if (gettimeofday(¤t_time, NULL) == -1) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 914 | return false; |
| 915 | } |
| 916 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 917 | // Calculate time for end of timeout. |
| 918 | struct timeval end_time; |
| 919 | timeradd(¤t_time, &delta, &end_time); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 920 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 921 | struct timespec ts; |
| 922 | TIMEVAL_TO_TIMESPEC(&end_time, &ts); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 923 | // Wait for semaphore signalled or timeout. |
| 924 | while (true) { |
| 925 | int result = sem_timedwait(&sem_, &ts); |
| 926 | if (result == 0) return true; // Successfully got semaphore. |
| 927 | if (result > 0) { |
| 928 | // For glibc prior to 2.3.4 sem_timedwait returns the error instead of -1. |
| 929 | errno = result; |
| 930 | result = -1; |
| 931 | } |
| 932 | if (result == -1 && errno == ETIMEDOUT) return false; // Timeout. |
| 933 | CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup. |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 938 | Semaphore* OS::CreateSemaphore(int count) { |
| 939 | return new LinuxSemaphore(count); |
| 940 | } |
| 941 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 942 | |
kasperl@chromium.org | acae378 | 2009-04-11 09:17:08 +0000 | [diff] [blame] | 943 | #if !defined(__GLIBC__) && (defined(__arm__) || defined(__thumb__)) |
| 944 | // Android runs a fairly new Linux kernel, so signal info is there, |
| 945 | // but the C library doesn't have the structs defined. |
| 946 | |
| 947 | struct sigcontext { |
| 948 | uint32_t trap_no; |
| 949 | uint32_t error_code; |
| 950 | uint32_t oldmask; |
| 951 | uint32_t gregs[16]; |
| 952 | uint32_t arm_cpsr; |
| 953 | uint32_t fault_address; |
| 954 | }; |
| 955 | typedef uint32_t __sigset_t; |
| 956 | typedef struct sigcontext mcontext_t; |
| 957 | typedef struct ucontext { |
| 958 | uint32_t uc_flags; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 959 | struct ucontext* uc_link; |
kasperl@chromium.org | acae378 | 2009-04-11 09:17:08 +0000 | [diff] [blame] | 960 | stack_t uc_stack; |
| 961 | mcontext_t uc_mcontext; |
| 962 | __sigset_t uc_sigmask; |
| 963 | } ucontext_t; |
| 964 | enum ArmRegisters {R15 = 15, R13 = 13, R11 = 11}; |
| 965 | |
| 966 | #endif |
| 967 | |
| 968 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 969 | static int GetThreadID() { |
| 970 | // Glibc doesn't provide a wrapper for gettid(2). |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 971 | #if defined(ANDROID) |
| 972 | return syscall(__NR_gettid); |
| 973 | #else |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 974 | return syscall(SYS_gettid); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 975 | #endif |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 979 | static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 980 | #ifndef V8_HOST_ARCH_MIPS |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 981 | USE(info); |
| 982 | if (signal != SIGPROF) return; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 983 | Isolate* isolate = Isolate::UncheckedCurrent(); |
| 984 | if (isolate == NULL || !isolate->IsInitialized() || !isolate->IsInUse()) { |
| 985 | // We require a fully initialized and entered isolate. |
| 986 | return; |
| 987 | } |
vitalyr@chromium.org | 0ec56d6 | 2011-04-15 22:22:08 +0000 | [diff] [blame] | 988 | if (v8::Locker::IsActive() && |
| 989 | !isolate->thread_manager()->IsLockedByCurrentThread()) { |
| 990 | return; |
| 991 | } |
| 992 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 993 | Sampler* sampler = isolate->logger()->sampler(); |
| 994 | if (sampler == NULL || !sampler->IsActive()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 995 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 996 | TickSample sample_obj; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 997 | TickSample* sample = CpuProfiler::TickSampleEvent(isolate); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 998 | if (sample == NULL) sample = &sample_obj; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 999 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1000 | // Extracting the sample from the context is extremely machine dependent. |
| 1001 | ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context); |
| 1002 | mcontext_t& mcontext = ucontext->uc_mcontext; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1003 | sample->state = isolate->current_vm_state(); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 1004 | #if V8_HOST_ARCH_IA32 |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1005 | sample->pc = reinterpret_cast<Address>(mcontext.gregs[REG_EIP]); |
| 1006 | sample->sp = reinterpret_cast<Address>(mcontext.gregs[REG_ESP]); |
| 1007 | sample->fp = reinterpret_cast<Address>(mcontext.gregs[REG_EBP]); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 1008 | #elif V8_HOST_ARCH_X64 |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1009 | sample->pc = reinterpret_cast<Address>(mcontext.gregs[REG_RIP]); |
| 1010 | sample->sp = reinterpret_cast<Address>(mcontext.gregs[REG_RSP]); |
| 1011 | sample->fp = reinterpret_cast<Address>(mcontext.gregs[REG_RBP]); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 1012 | #elif V8_HOST_ARCH_ARM |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 1013 | // An undefined macro evaluates to 0, so this applies to Android's Bionic also. |
| 1014 | #if (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3)) |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1015 | sample->pc = reinterpret_cast<Address>(mcontext.gregs[R15]); |
| 1016 | sample->sp = reinterpret_cast<Address>(mcontext.gregs[R13]); |
| 1017 | sample->fp = reinterpret_cast<Address>(mcontext.gregs[R11]); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 1018 | #else |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1019 | sample->pc = reinterpret_cast<Address>(mcontext.arm_pc); |
| 1020 | sample->sp = reinterpret_cast<Address>(mcontext.arm_sp); |
| 1021 | sample->fp = reinterpret_cast<Address>(mcontext.arm_fp); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 1022 | #endif |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1023 | #elif V8_HOST_ARCH_MIPS |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1024 | sample.pc = reinterpret_cast<Address>(mcontext.pc); |
| 1025 | sample.sp = reinterpret_cast<Address>(mcontext.gregs[29]); |
| 1026 | sample.fp = reinterpret_cast<Address>(mcontext.gregs[30]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1027 | #endif |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1028 | sampler->SampleStack(sample); |
| 1029 | sampler->Tick(sample); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1030 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1034 | class Sampler::PlatformData : public Malloced { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1035 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1036 | PlatformData() : vm_tid_(GetThreadID()) {} |
| 1037 | |
| 1038 | int vm_tid() const { return vm_tid_; } |
| 1039 | |
| 1040 | private: |
| 1041 | const int vm_tid_; |
| 1042 | }; |
| 1043 | |
| 1044 | |
| 1045 | class SignalSender : public Thread { |
| 1046 | public: |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1047 | enum SleepInterval { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1048 | HALF_INTERVAL, |
| 1049 | FULL_INTERVAL |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1050 | }; |
| 1051 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1052 | explicit SignalSender(int interval) |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1053 | : Thread("SignalSender"), |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1054 | vm_tgid_(getpid()), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1055 | interval_(interval) {} |
| 1056 | |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 1057 | static void InstallSignalHandler() { |
| 1058 | struct sigaction sa; |
| 1059 | sa.sa_sigaction = ProfilerSignalHandler; |
| 1060 | sigemptyset(&sa.sa_mask); |
| 1061 | sa.sa_flags = SA_RESTART | SA_SIGINFO; |
| 1062 | signal_handler_installed_ = |
| 1063 | (sigaction(SIGPROF, &sa, &old_signal_handler_) == 0); |
| 1064 | } |
| 1065 | |
| 1066 | static void RestoreSignalHandler() { |
| 1067 | if (signal_handler_installed_) { |
| 1068 | sigaction(SIGPROF, &old_signal_handler_, 0); |
| 1069 | signal_handler_installed_ = false; |
| 1070 | } |
| 1071 | } |
| 1072 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1073 | static void AddActiveSampler(Sampler* sampler) { |
| 1074 | ScopedLock lock(mutex_); |
| 1075 | SamplerRegistry::AddActiveSampler(sampler); |
| 1076 | if (instance_ == NULL) { |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 1077 | // Start a thread that will send SIGPROF signal to VM threads, |
| 1078 | // when CPU profiling will be enabled. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1079 | instance_ = new SignalSender(sampler->interval()); |
| 1080 | instance_->Start(); |
| 1081 | } else { |
| 1082 | ASSERT(instance_->interval_ == sampler->interval()); |
| 1083 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1086 | static void RemoveActiveSampler(Sampler* sampler) { |
| 1087 | ScopedLock lock(mutex_); |
| 1088 | SamplerRegistry::RemoveActiveSampler(sampler); |
| 1089 | if (SamplerRegistry::GetState() == SamplerRegistry::HAS_NO_SAMPLERS) { |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 1090 | RuntimeProfiler::StopRuntimeProfilerThreadBeforeShutdown(instance_); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1091 | delete instance_; |
| 1092 | instance_ = NULL; |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 1093 | RestoreSignalHandler(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | // Implement Thread::Run(). |
| 1098 | virtual void Run() { |
| 1099 | SamplerRegistry::State state; |
| 1100 | while ((state = SamplerRegistry::GetState()) != |
| 1101 | SamplerRegistry::HAS_NO_SAMPLERS) { |
| 1102 | bool cpu_profiling_enabled = |
| 1103 | (state == SamplerRegistry::HAS_CPU_PROFILING_SAMPLERS); |
| 1104 | bool runtime_profiler_enabled = RuntimeProfiler::IsEnabled(); |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 1105 | if (cpu_profiling_enabled && !signal_handler_installed_) { |
| 1106 | InstallSignalHandler(); |
| 1107 | } else if (!cpu_profiling_enabled && signal_handler_installed_) { |
| 1108 | RestoreSignalHandler(); |
| 1109 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1110 | // When CPU profiling is enabled both JavaScript and C++ code is |
| 1111 | // profiled. We must not suspend. |
| 1112 | if (!cpu_profiling_enabled) { |
| 1113 | if (rate_limiter_.SuspendIfNecessary()) continue; |
| 1114 | } |
| 1115 | if (cpu_profiling_enabled && runtime_profiler_enabled) { |
| 1116 | if (!SamplerRegistry::IterateActiveSamplers(&DoCpuProfile, this)) { |
| 1117 | return; |
| 1118 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1119 | Sleep(HALF_INTERVAL); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1120 | if (!SamplerRegistry::IterateActiveSamplers(&DoRuntimeProfile, NULL)) { |
| 1121 | return; |
| 1122 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1123 | Sleep(HALF_INTERVAL); |
| 1124 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1125 | if (cpu_profiling_enabled) { |
| 1126 | if (!SamplerRegistry::IterateActiveSamplers(&DoCpuProfile, |
| 1127 | this)) { |
| 1128 | return; |
| 1129 | } |
| 1130 | } |
| 1131 | if (runtime_profiler_enabled) { |
| 1132 | if (!SamplerRegistry::IterateActiveSamplers(&DoRuntimeProfile, |
| 1133 | NULL)) { |
| 1134 | return; |
| 1135 | } |
| 1136 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1137 | Sleep(FULL_INTERVAL); |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 1138 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1139 | } |
| 1140 | } |
| 1141 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1142 | static void DoCpuProfile(Sampler* sampler, void* raw_sender) { |
| 1143 | if (!sampler->IsProfiling()) return; |
| 1144 | SignalSender* sender = reinterpret_cast<SignalSender*>(raw_sender); |
| 1145 | sender->SendProfilingSignal(sampler->platform_data()->vm_tid()); |
| 1146 | } |
| 1147 | |
| 1148 | static void DoRuntimeProfile(Sampler* sampler, void* ignored) { |
| 1149 | if (!sampler->isolate()->IsInitialized()) return; |
| 1150 | sampler->isolate()->runtime_profiler()->NotifyTick(); |
| 1151 | } |
| 1152 | |
| 1153 | void SendProfilingSignal(int tid) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1154 | if (!signal_handler_installed_) return; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1155 | // Glibc doesn't provide a wrapper for tgkill(2). |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1156 | #if defined(ANDROID) |
| 1157 | syscall(__NR_tgkill, vm_tgid_, tid, SIGPROF); |
| 1158 | #else |
| 1159 | syscall(SYS_tgkill, vm_tgid_, tid, SIGPROF); |
| 1160 | #endif |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | void Sleep(SleepInterval full_or_half) { |
| 1164 | // Convert ms to us and subtract 100 us to compensate delays |
| 1165 | // occuring during signal delivery. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1166 | useconds_t interval = interval_ * 1000 - 100; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1167 | if (full_or_half == HALF_INTERVAL) interval /= 2; |
| 1168 | int result = usleep(interval); |
| 1169 | #ifdef DEBUG |
| 1170 | if (result != 0 && errno != EINTR) { |
| 1171 | fprintf(stderr, |
| 1172 | "SignalSender usleep error; interval = %u, errno = %d\n", |
| 1173 | interval, |
| 1174 | errno); |
| 1175 | ASSERT(result == 0 || errno == EINTR); |
| 1176 | } |
| 1177 | #endif |
| 1178 | USE(result); |
| 1179 | } |
| 1180 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1181 | const int vm_tgid_; |
| 1182 | const int interval_; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1183 | RuntimeProfilerRateLimiter rate_limiter_; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1184 | |
| 1185 | // Protects the process wide state below. |
| 1186 | static Mutex* mutex_; |
| 1187 | static SignalSender* instance_; |
| 1188 | static bool signal_handler_installed_; |
| 1189 | static struct sigaction old_signal_handler_; |
| 1190 | |
| 1191 | DISALLOW_COPY_AND_ASSIGN(SignalSender); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1192 | }; |
| 1193 | |
| 1194 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1195 | Mutex* SignalSender::mutex_ = OS::CreateMutex(); |
| 1196 | SignalSender* SignalSender::instance_ = NULL; |
| 1197 | struct sigaction SignalSender::old_signal_handler_; |
| 1198 | bool SignalSender::signal_handler_installed_ = false; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1199 | |
| 1200 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1201 | Sampler::Sampler(Isolate* isolate, int interval) |
| 1202 | : isolate_(isolate), |
| 1203 | interval_(interval), |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1204 | profiling_(false), |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1205 | active_(false), |
| 1206 | samples_taken_(0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1207 | data_ = new PlatformData; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1211 | Sampler::~Sampler() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1212 | ASSERT(!IsActive()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1213 | delete data_; |
| 1214 | } |
| 1215 | |
| 1216 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1217 | void Sampler::Start() { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1218 | ASSERT(!IsActive()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1219 | SetActive(true); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1220 | SignalSender::AddActiveSampler(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1224 | void Sampler::Stop() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1225 | ASSERT(IsActive()); |
| 1226 | SignalSender::RemoveActiveSampler(this); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1227 | SetActive(false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1230 | |
| 1231 | } } // namespace v8::internal |