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