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