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