ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 28 | // Platform specific code for Linux goes here. For the POSIX comaptible parts |
| 29 | // the implementation is in platform-posix.cc. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 30 | |
| 31 | #include <pthread.h> |
| 32 | #include <semaphore.h> |
| 33 | #include <signal.h> |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 34 | #include <sys/prctl.h> |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 35 | #include <sys/time.h> |
| 36 | #include <sys/resource.h> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 37 | #include <sys/syscall.h> |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 38 | #include <sys/types.h> |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 39 | #include <stdlib.h> |
| 40 | |
| 41 | // Ubuntu Dapper requires memory pages to be marked as |
| 42 | // executable. Otherwise, OS raises an exception when executing code |
| 43 | // in that page. |
| 44 | #include <sys/types.h> // mmap & munmap |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 45 | #include <sys/mman.h> // mmap & munmap |
| 46 | #include <sys/stat.h> // open |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 47 | #include <fcntl.h> // open |
| 48 | #include <unistd.h> // sysconf |
| 49 | #ifdef __GLIBC__ |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 50 | #include <execinfo.h> // backtrace, backtrace_symbols |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 51 | #endif // def __GLIBC__ |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 52 | #include <strings.h> // index |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 53 | #include <errno.h> |
| 54 | #include <stdarg.h> |
| 55 | |
| 56 | #undef MAP_TYPE |
| 57 | |
| 58 | #include "v8.h" |
| 59 | |
| 60 | #include "platform.h" |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 61 | #include "top.h" |
| 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 | |
| 79 | void OS::Setup() { |
| 80 | // Seed the random number generator. |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 81 | // Convert the current time to a 64-bit integer first, before converting it |
| 82 | // to an unsigned. Going directly can cause an overflow and the seed to be |
| 83 | // set to all ones. The seed will be identical for different instances that |
| 84 | // call this setup code within the same millisecond. |
| 85 | uint64_t seed = static_cast<uint64_t>(TimeCurrentMillis()); |
| 86 | srandom(static_cast<unsigned int>(seed)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 90 | uint64_t OS::CpuFeaturesImpliedByPlatform() { |
| 91 | #if (defined(__VFP_FP__) && !defined(__SOFTFP__)) |
| 92 | // Here gcc is telling us that we are on an ARM and gcc is assuming that we |
| 93 | // have VFP3 instructions. If gcc can assume it then so can we. |
| 94 | return 1u << VFP3; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 95 | #elif CAN_USE_ARMV7_INSTRUCTIONS |
| 96 | return 1u << ARMv7; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 97 | #else |
| 98 | return 0; // Linux runs on anything. |
| 99 | #endif |
| 100 | } |
| 101 | |
| 102 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 103 | #ifdef __arm__ |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 104 | static bool CPUInfoContainsString(const char * search_string) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 105 | const char* file_name = "/proc/cpuinfo"; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 106 | // This is written as a straight shot one pass parser |
| 107 | // and not using STL string and ifstream because, |
| 108 | // on Linux, it's reading from a (non-mmap-able) |
| 109 | // character special device. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 110 | FILE* f = NULL; |
| 111 | const char* what = search_string; |
| 112 | |
| 113 | if (NULL == (f = fopen(file_name, "r"))) |
| 114 | return false; |
| 115 | |
| 116 | int k; |
| 117 | while (EOF != (k = fgetc(f))) { |
| 118 | if (k == *what) { |
| 119 | ++what; |
| 120 | while ((*what != '\0') && (*what == fgetc(f))) { |
| 121 | ++what; |
| 122 | } |
| 123 | if (*what == '\0') { |
| 124 | fclose(f); |
| 125 | return true; |
| 126 | } else { |
| 127 | what = search_string; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | fclose(f); |
| 132 | |
| 133 | // Did not find string in the proc file. |
| 134 | return false; |
| 135 | } |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 136 | |
| 137 | bool OS::ArmCpuHasFeature(CpuFeature feature) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 138 | const char* search_string = NULL; |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 139 | // Simple detection of VFP at runtime for Linux. |
| 140 | // It is based on /proc/cpuinfo, which reveals hardware configuration |
| 141 | // to user-space applications. According to ARM (mid 2009), no similar |
| 142 | // facility is universally available on the ARM architectures, |
| 143 | // so it's up to individual OSes to provide such. |
| 144 | switch (feature) { |
| 145 | case VFP3: |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 146 | search_string = "vfpv3"; |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 147 | break; |
| 148 | case ARMv7: |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 149 | search_string = "ARMv7"; |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 150 | break; |
| 151 | default: |
| 152 | UNREACHABLE(); |
| 153 | } |
| 154 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 155 | if (CPUInfoContainsString(search_string)) { |
| 156 | return true; |
| 157 | } |
| 158 | |
| 159 | if (feature == VFP3) { |
| 160 | // Some old kernels will report vfp not vfpv3. Here we make a last attempt |
| 161 | // to detect vfpv3 by checking for vfp *and* neon, since neon is only |
| 162 | // available on architectures with vfpv3. |
| 163 | // Checking neon on its own is not enough as it is possible to have neon |
| 164 | // without vfp. |
| 165 | if (CPUInfoContainsString("vfp") && CPUInfoContainsString("neon")) { |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 166 | return true; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | return false; |
| 171 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 172 | #endif // def __arm__ |
| 173 | |
| 174 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 175 | int OS::ActivationFrameAlignment() { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 176 | #ifdef V8_TARGET_ARCH_ARM |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 177 | // On EABI ARM targets this is required for fp correctness in the |
| 178 | // runtime system. |
ager@chromium.org | 3a6061e | 2009-03-12 14:24:36 +0000 | [diff] [blame] | 179 | return 8; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 180 | #elif V8_TARGET_ARCH_MIPS |
| 181 | return 8; |
| 182 | #endif |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 183 | // With gcc 4.4 the tree vectorization optimizer can generate code |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 184 | // that requires 16 byte alignment such as movdqa on x86. |
| 185 | return 16; |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 186 | } |
| 187 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 188 | |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 189 | void OS::ReleaseStore(volatile AtomicWord* ptr, AtomicWord value) { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 190 | #if defined(V8_TARGET_ARCH_ARM) && defined(__arm__) |
| 191 | // Only use on ARM hardware. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 192 | MemoryBarrier(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 193 | #else |
| 194 | __asm__ __volatile__("" : : : "memory"); |
| 195 | // An x86 store acts as a release barrier. |
| 196 | #endif |
| 197 | *ptr = value; |
| 198 | } |
| 199 | |
| 200 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 201 | const char* OS::LocalTimezone(double time) { |
| 202 | if (isnan(time)) return ""; |
| 203 | time_t tv = static_cast<time_t>(floor(time/msPerSecond)); |
| 204 | struct tm* t = localtime(&tv); |
| 205 | if (NULL == t) return ""; |
| 206 | return t->tm_zone; |
| 207 | } |
| 208 | |
| 209 | |
| 210 | double OS::LocalTimeOffset() { |
| 211 | time_t tv = time(NULL); |
| 212 | struct tm* t = localtime(&tv); |
| 213 | // tm_gmtoff includes any daylight savings offset, so subtract it. |
| 214 | return static_cast<double>(t->tm_gmtoff * msPerSecond - |
| 215 | (t->tm_isdst > 0 ? 3600 * msPerSecond : 0)); |
| 216 | } |
| 217 | |
| 218 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 219 | // We keep the lowest and highest addresses mapped as a quick way of |
| 220 | // determining that pointers are outside the heap (used mostly in assertions |
| 221 | // and verification). The estimate is conservative, ie, not all addresses in |
| 222 | // 'allocated' space are actually allocated to our heap. The range is |
| 223 | // [lowest, highest), inclusive on the low and and exclusive on the high end. |
| 224 | static void* lowest_ever_allocated = reinterpret_cast<void*>(-1); |
| 225 | static void* highest_ever_allocated = reinterpret_cast<void*>(0); |
| 226 | |
| 227 | |
| 228 | static void UpdateAllocatedSpaceLimits(void* address, int size) { |
| 229 | lowest_ever_allocated = Min(lowest_ever_allocated, address); |
| 230 | highest_ever_allocated = |
| 231 | Max(highest_ever_allocated, |
| 232 | reinterpret_cast<void*>(reinterpret_cast<char*>(address) + size)); |
| 233 | } |
| 234 | |
| 235 | |
| 236 | bool OS::IsOutsideAllocatedSpace(void* address) { |
| 237 | return address < lowest_ever_allocated || address >= highest_ever_allocated; |
| 238 | } |
| 239 | |
| 240 | |
| 241 | size_t OS::AllocateAlignment() { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 242 | return sysconf(_SC_PAGESIZE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 246 | void* OS::Allocate(const size_t requested, |
| 247 | size_t* allocated, |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 248 | bool is_executable) { |
sgjesse@chromium.org | c3a0197 | 2010-08-04 09:46:24 +0000 | [diff] [blame] | 249 | // TODO(805): Port randomization of allocated executable memory to Linux. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 250 | const size_t msize = RoundUp(requested, sysconf(_SC_PAGESIZE)); |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 251 | int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 252 | void* mbase = mmap(NULL, msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 253 | if (mbase == MAP_FAILED) { |
| 254 | LOG(StringEvent("OS::Allocate", "mmap failed")); |
| 255 | return NULL; |
| 256 | } |
| 257 | *allocated = msize; |
| 258 | UpdateAllocatedSpaceLimits(mbase, msize); |
| 259 | return mbase; |
| 260 | } |
| 261 | |
| 262 | |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 263 | void OS::Free(void* address, const size_t size) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 264 | // TODO(1240712): munmap has a return value which is ignored here. |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 265 | int result = munmap(address, size); |
| 266 | USE(result); |
| 267 | ASSERT(result == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 271 | #ifdef ENABLE_HEAP_PROTECTION |
| 272 | |
| 273 | void OS::Protect(void* address, size_t size) { |
| 274 | // TODO(1240712): mprotect has a return value which is ignored here. |
| 275 | mprotect(address, size, PROT_READ); |
| 276 | } |
| 277 | |
| 278 | |
| 279 | void OS::Unprotect(void* address, size_t size, bool is_executable) { |
| 280 | // TODO(1240712): mprotect has a return value which is ignored here. |
| 281 | int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); |
| 282 | mprotect(address, size, prot); |
| 283 | } |
| 284 | |
| 285 | #endif |
| 286 | |
| 287 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 288 | void OS::Sleep(int milliseconds) { |
| 289 | unsigned int ms = static_cast<unsigned int>(milliseconds); |
| 290 | usleep(1000 * ms); |
| 291 | } |
| 292 | |
| 293 | |
| 294 | void OS::Abort() { |
| 295 | // Redirect to std abort to signal abnormal program termination. |
| 296 | abort(); |
| 297 | } |
| 298 | |
| 299 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 300 | void OS::DebugBreak() { |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 301 | // TODO(lrn): Introduce processor define for runtime system (!= V8_ARCH_x, |
| 302 | // which is the architecture of generated code). |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 303 | #if (defined(__arm__) || defined(__thumb__)) |
| 304 | # if defined(CAN_USE_ARMV5_INSTRUCTIONS) |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 305 | asm("bkpt 0"); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 306 | # endif |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 307 | #elif defined(__mips__) |
| 308 | asm("break"); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 309 | #else |
| 310 | asm("int $3"); |
| 311 | #endif |
| 312 | } |
| 313 | |
| 314 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 315 | class PosixMemoryMappedFile : public OS::MemoryMappedFile { |
| 316 | public: |
| 317 | PosixMemoryMappedFile(FILE* file, void* memory, int size) |
| 318 | : file_(file), memory_(memory), size_(size) { } |
| 319 | virtual ~PosixMemoryMappedFile(); |
| 320 | virtual void* memory() { return memory_; } |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 321 | virtual int size() { return size_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 322 | private: |
| 323 | FILE* file_; |
| 324 | void* memory_; |
| 325 | int size_; |
| 326 | }; |
| 327 | |
| 328 | |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 329 | OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) { |
| 330 | FILE* file = fopen(name, "w+"); |
| 331 | if (file == NULL) return NULL; |
| 332 | |
| 333 | fseek(file, 0, SEEK_END); |
| 334 | int size = ftell(file); |
| 335 | |
| 336 | void* memory = |
| 337 | mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); |
| 338 | return new PosixMemoryMappedFile(file, memory, size); |
| 339 | } |
| 340 | |
| 341 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 342 | OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size, |
| 343 | void* initial) { |
| 344 | FILE* file = fopen(name, "w+"); |
| 345 | if (file == NULL) return NULL; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 346 | int result = fwrite(initial, size, 1, file); |
| 347 | if (result < 1) { |
| 348 | fclose(file); |
| 349 | return NULL; |
| 350 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 351 | void* memory = |
| 352 | mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); |
| 353 | return new PosixMemoryMappedFile(file, memory, size); |
| 354 | } |
| 355 | |
| 356 | |
| 357 | PosixMemoryMappedFile::~PosixMemoryMappedFile() { |
| 358 | if (memory_) munmap(memory_, size_); |
| 359 | fclose(file_); |
| 360 | } |
| 361 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 362 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 363 | void OS::LogSharedLibraryAddresses() { |
| 364 | #ifdef ENABLE_LOGGING_AND_PROFILING |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 365 | // This function assumes that the layout of the file is as follows: |
| 366 | // hex_start_addr-hex_end_addr rwxp <unused data> [binary_file_name] |
| 367 | // If we encounter an unexpected situation we abort scanning further entries. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 368 | FILE* fp = fopen("/proc/self/maps", "r"); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 369 | if (fp == NULL) return; |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 370 | |
| 371 | // Allocate enough room to be able to store a full file name. |
| 372 | const int kLibNameLen = FILENAME_MAX + 1; |
| 373 | char* lib_name = reinterpret_cast<char*>(malloc(kLibNameLen)); |
| 374 | |
| 375 | // This loop will terminate once the scanning hits an EOF. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 376 | while (true) { |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 377 | uintptr_t start, end; |
| 378 | char attr_r, attr_w, attr_x, attr_p; |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 379 | // 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] | 380 | if (fscanf(fp, "%" V8PRIxPTR "-%" V8PRIxPTR, &start, &end) != 2) break; |
| 381 | 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] | 382 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 383 | int c; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 384 | if (attr_r == 'r' && attr_w != 'w' && attr_x == 'x') { |
| 385 | // Found a read-only executable entry. Skip characters until we reach |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 386 | // the beginning of the filename or the end of the line. |
| 387 | do { |
| 388 | c = getc(fp); |
| 389 | } while ((c != EOF) && (c != '\n') && (c != '/')); |
| 390 | if (c == EOF) break; // EOF: Was unexpected, just exit. |
| 391 | |
| 392 | // Process the filename if found. |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 393 | if (c == '/') { |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 394 | ungetc(c, fp); // Push the '/' back into the stream to be read below. |
| 395 | |
| 396 | // Read to the end of the line. Exit if the read fails. |
| 397 | if (fgets(lib_name, kLibNameLen, fp) == NULL) break; |
| 398 | |
| 399 | // Drop the newline character read by fgets. We do not need to check |
| 400 | // for a zero-length string because we know that we at least read the |
| 401 | // '/' character. |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 402 | lib_name[strlen(lib_name) - 1] = '\0'; |
| 403 | } else { |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 404 | // No library name found, just record the raw address range. |
| 405 | snprintf(lib_name, kLibNameLen, |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 406 | "%08" V8PRIxPTR "-%08" V8PRIxPTR, start, end); |
| 407 | } |
| 408 | LOG(SharedLibraryEvent(lib_name, start, end)); |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 409 | } else { |
| 410 | // Entry not describing executable data. Skip to end of line to setup |
| 411 | // reading the next entry. |
| 412 | do { |
| 413 | c = getc(fp); |
| 414 | } while ((c != EOF) && (c != '\n')); |
| 415 | if (c == EOF) break; |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 416 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 417 | } |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 418 | free(lib_name); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 419 | fclose(fp); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 420 | #endif |
| 421 | } |
| 422 | |
| 423 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 424 | static const char kGCFakeMmap[] = "/tmp/__v8_gc__"; |
| 425 | |
| 426 | |
| 427 | void OS::SignalCodeMovingGC() { |
| 428 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 429 | // Support for ll_prof.py. |
| 430 | // |
| 431 | // The Linux profiler built into the kernel logs all mmap's with |
| 432 | // PROT_EXEC so that analysis tools can properly attribute ticks. We |
| 433 | // do a mmap with a name known by ll_prof.py and immediately munmap |
| 434 | // it. This injects a GC marker into the stream of events generated |
| 435 | // by the kernel and allows us to synchronize V8 code log and the |
| 436 | // kernel log. |
| 437 | int size = sysconf(_SC_PAGESIZE); |
| 438 | FILE* f = fopen(kGCFakeMmap, "w+"); |
| 439 | void* addr = mmap(NULL, size, PROT_READ | PROT_EXEC, MAP_PRIVATE, |
| 440 | fileno(f), 0); |
| 441 | ASSERT(addr != MAP_FAILED); |
| 442 | munmap(addr, size); |
| 443 | fclose(f); |
| 444 | #endif |
| 445 | } |
| 446 | |
| 447 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 448 | int OS::StackWalk(Vector<OS::StackFrame> frames) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 449 | // backtrace is a glibc extension. |
| 450 | #ifdef __GLIBC__ |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 451 | int frames_size = frames.length(); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 452 | ScopedVector<void*> addresses(frames_size); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 453 | |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 454 | int frames_count = backtrace(addresses.start(), frames_size); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 455 | |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 456 | char** symbols = backtrace_symbols(addresses.start(), frames_count); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 457 | if (symbols == NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 458 | return kStackWalkError; |
| 459 | } |
| 460 | |
| 461 | for (int i = 0; i < frames_count; i++) { |
| 462 | frames[i].address = addresses[i]; |
| 463 | // Format a text representation of the frame based on the information |
| 464 | // available. |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 465 | SNPrintF(MutableCStrVector(frames[i].text, kStackWalkMaxTextLen), |
| 466 | "%s", |
| 467 | symbols[i]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 468 | // Make sure line termination is in place. |
| 469 | frames[i].text[kStackWalkMaxTextLen - 1] = '\0'; |
| 470 | } |
| 471 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 472 | free(symbols); |
| 473 | |
| 474 | return frames_count; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 475 | #else // ndef __GLIBC__ |
| 476 | return 0; |
| 477 | #endif // ndef __GLIBC__ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | |
| 481 | // Constants used for mmap. |
| 482 | static const int kMmapFd = -1; |
| 483 | static const int kMmapFdOffset = 0; |
| 484 | |
| 485 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 486 | VirtualMemory::VirtualMemory(size_t size) { |
| 487 | address_ = mmap(NULL, size, PROT_NONE, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 488 | MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, |
| 489 | kMmapFd, kMmapFdOffset); |
| 490 | size_ = size; |
| 491 | } |
| 492 | |
| 493 | |
| 494 | VirtualMemory::~VirtualMemory() { |
| 495 | if (IsReserved()) { |
| 496 | if (0 == munmap(address(), size())) address_ = MAP_FAILED; |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | |
| 501 | bool VirtualMemory::IsReserved() { |
| 502 | return address_ != MAP_FAILED; |
| 503 | } |
| 504 | |
| 505 | |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 506 | bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) { |
| 507 | int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 508 | if (MAP_FAILED == mmap(address, size, prot, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 509 | MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, |
| 510 | kMmapFd, kMmapFdOffset)) { |
| 511 | return false; |
| 512 | } |
| 513 | |
| 514 | UpdateAllocatedSpaceLimits(address, size); |
| 515 | return true; |
| 516 | } |
| 517 | |
| 518 | |
| 519 | bool VirtualMemory::Uncommit(void* address, size_t size) { |
| 520 | return mmap(address, size, PROT_NONE, |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 521 | MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE | MAP_FIXED, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 522 | kMmapFd, kMmapFdOffset) != MAP_FAILED; |
| 523 | } |
| 524 | |
| 525 | |
| 526 | class ThreadHandle::PlatformData : public Malloced { |
| 527 | public: |
| 528 | explicit PlatformData(ThreadHandle::Kind kind) { |
| 529 | Initialize(kind); |
| 530 | } |
| 531 | |
| 532 | void Initialize(ThreadHandle::Kind kind) { |
| 533 | switch (kind) { |
| 534 | case ThreadHandle::SELF: thread_ = pthread_self(); break; |
| 535 | case ThreadHandle::INVALID: thread_ = kNoThread; break; |
| 536 | } |
| 537 | } |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 538 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 539 | pthread_t thread_; // Thread handle for pthread. |
| 540 | }; |
| 541 | |
| 542 | |
| 543 | ThreadHandle::ThreadHandle(Kind kind) { |
| 544 | data_ = new PlatformData(kind); |
| 545 | } |
| 546 | |
| 547 | |
| 548 | void ThreadHandle::Initialize(ThreadHandle::Kind kind) { |
| 549 | data_->Initialize(kind); |
| 550 | } |
| 551 | |
| 552 | |
| 553 | ThreadHandle::~ThreadHandle() { |
| 554 | delete data_; |
| 555 | } |
| 556 | |
| 557 | |
| 558 | bool ThreadHandle::IsSelf() const { |
| 559 | return pthread_equal(data_->thread_, pthread_self()); |
| 560 | } |
| 561 | |
| 562 | |
| 563 | bool ThreadHandle::IsValid() const { |
| 564 | return data_->thread_ != kNoThread; |
| 565 | } |
| 566 | |
| 567 | |
| 568 | Thread::Thread() : ThreadHandle(ThreadHandle::INVALID) { |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 569 | set_name("v8:<unknown>"); |
| 570 | } |
| 571 | |
| 572 | |
| 573 | Thread::Thread(const char* name) : ThreadHandle(ThreadHandle::INVALID) { |
| 574 | set_name(name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | |
| 578 | Thread::~Thread() { |
| 579 | } |
| 580 | |
| 581 | |
| 582 | static void* ThreadEntry(void* arg) { |
| 583 | Thread* thread = reinterpret_cast<Thread*>(arg); |
| 584 | // This is also initialized by the first argument to pthread_create() but we |
| 585 | // don't know which thread will run first (the original thread or the new |
| 586 | // one) so we initialize it here too. |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 587 | prctl(PR_SET_NAME, thread->name(), 0, 0, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 588 | thread->thread_handle_data()->thread_ = pthread_self(); |
| 589 | ASSERT(thread->IsValid()); |
| 590 | thread->Run(); |
| 591 | return NULL; |
| 592 | } |
| 593 | |
| 594 | |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 595 | void Thread::set_name(const char* name) { |
| 596 | strncpy(name_, name, sizeof(name_)); |
| 597 | name_[sizeof(name_) - 1] = '\0'; |
| 598 | } |
| 599 | |
| 600 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 601 | void Thread::Start() { |
| 602 | pthread_create(&thread_handle_data()->thread_, NULL, ThreadEntry, this); |
| 603 | ASSERT(IsValid()); |
| 604 | } |
| 605 | |
| 606 | |
| 607 | void Thread::Join() { |
| 608 | pthread_join(thread_handle_data()->thread_, NULL); |
| 609 | } |
| 610 | |
| 611 | |
| 612 | Thread::LocalStorageKey Thread::CreateThreadLocalKey() { |
| 613 | pthread_key_t key; |
| 614 | int result = pthread_key_create(&key, NULL); |
| 615 | USE(result); |
| 616 | ASSERT(result == 0); |
| 617 | return static_cast<LocalStorageKey>(key); |
| 618 | } |
| 619 | |
| 620 | |
| 621 | void Thread::DeleteThreadLocalKey(LocalStorageKey key) { |
| 622 | pthread_key_t pthread_key = static_cast<pthread_key_t>(key); |
| 623 | int result = pthread_key_delete(pthread_key); |
| 624 | USE(result); |
| 625 | ASSERT(result == 0); |
| 626 | } |
| 627 | |
| 628 | |
| 629 | void* Thread::GetThreadLocal(LocalStorageKey key) { |
| 630 | pthread_key_t pthread_key = static_cast<pthread_key_t>(key); |
| 631 | return pthread_getspecific(pthread_key); |
| 632 | } |
| 633 | |
| 634 | |
| 635 | void Thread::SetThreadLocal(LocalStorageKey key, void* value) { |
| 636 | pthread_key_t pthread_key = static_cast<pthread_key_t>(key); |
| 637 | pthread_setspecific(pthread_key, value); |
| 638 | } |
| 639 | |
| 640 | |
| 641 | void Thread::YieldCPU() { |
| 642 | sched_yield(); |
| 643 | } |
| 644 | |
| 645 | |
| 646 | class LinuxMutex : public Mutex { |
| 647 | public: |
| 648 | |
| 649 | LinuxMutex() { |
| 650 | pthread_mutexattr_t attrs; |
| 651 | int result = pthread_mutexattr_init(&attrs); |
| 652 | ASSERT(result == 0); |
| 653 | result = pthread_mutexattr_settype(&attrs, PTHREAD_MUTEX_RECURSIVE); |
| 654 | ASSERT(result == 0); |
| 655 | result = pthread_mutex_init(&mutex_, &attrs); |
| 656 | ASSERT(result == 0); |
| 657 | } |
| 658 | |
| 659 | virtual ~LinuxMutex() { pthread_mutex_destroy(&mutex_); } |
| 660 | |
| 661 | virtual int Lock() { |
| 662 | int result = pthread_mutex_lock(&mutex_); |
| 663 | return result; |
| 664 | } |
| 665 | |
| 666 | virtual int Unlock() { |
| 667 | int result = pthread_mutex_unlock(&mutex_); |
| 668 | return result; |
| 669 | } |
| 670 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 671 | virtual bool TryLock() { |
| 672 | int result = pthread_mutex_trylock(&mutex_); |
| 673 | // Return false if the lock is busy and locking failed. |
| 674 | if (result == EBUSY) { |
| 675 | return false; |
| 676 | } |
| 677 | ASSERT(result == 0); // Verify no other errors. |
| 678 | return true; |
| 679 | } |
| 680 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 681 | private: |
| 682 | pthread_mutex_t mutex_; // Pthread mutex for POSIX platforms. |
| 683 | }; |
| 684 | |
| 685 | |
| 686 | Mutex* OS::CreateMutex() { |
| 687 | return new LinuxMutex(); |
| 688 | } |
| 689 | |
| 690 | |
| 691 | class LinuxSemaphore : public Semaphore { |
| 692 | public: |
| 693 | explicit LinuxSemaphore(int count) { sem_init(&sem_, 0, count); } |
| 694 | virtual ~LinuxSemaphore() { sem_destroy(&sem_); } |
| 695 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 696 | virtual void Wait(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 697 | virtual bool Wait(int timeout); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 698 | virtual void Signal() { sem_post(&sem_); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 699 | private: |
| 700 | sem_t sem_; |
| 701 | }; |
| 702 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 703 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 704 | void LinuxSemaphore::Wait() { |
| 705 | while (true) { |
| 706 | int result = sem_wait(&sem_); |
| 707 | if (result == 0) return; // Successfully got semaphore. |
| 708 | CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup. |
| 709 | } |
| 710 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 711 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 712 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 713 | #ifndef TIMEVAL_TO_TIMESPEC |
| 714 | #define TIMEVAL_TO_TIMESPEC(tv, ts) do { \ |
| 715 | (ts)->tv_sec = (tv)->tv_sec; \ |
| 716 | (ts)->tv_nsec = (tv)->tv_usec * 1000; \ |
| 717 | } while (false) |
| 718 | #endif |
| 719 | |
| 720 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 721 | bool LinuxSemaphore::Wait(int timeout) { |
| 722 | const long kOneSecondMicros = 1000000; // NOLINT |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 723 | |
| 724 | // Split timeout into second and nanosecond parts. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 725 | struct timeval delta; |
| 726 | delta.tv_usec = timeout % kOneSecondMicros; |
| 727 | delta.tv_sec = timeout / kOneSecondMicros; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 728 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 729 | struct timeval current_time; |
| 730 | // Get the current time. |
| 731 | if (gettimeofday(¤t_time, NULL) == -1) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 732 | return false; |
| 733 | } |
| 734 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 735 | // Calculate time for end of timeout. |
| 736 | struct timeval end_time; |
| 737 | timeradd(¤t_time, &delta, &end_time); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 738 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 739 | struct timespec ts; |
| 740 | TIMEVAL_TO_TIMESPEC(&end_time, &ts); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 741 | // Wait for semaphore signalled or timeout. |
| 742 | while (true) { |
| 743 | int result = sem_timedwait(&sem_, &ts); |
| 744 | if (result == 0) return true; // Successfully got semaphore. |
| 745 | if (result > 0) { |
| 746 | // For glibc prior to 2.3.4 sem_timedwait returns the error instead of -1. |
| 747 | errno = result; |
| 748 | result = -1; |
| 749 | } |
| 750 | if (result == -1 && errno == ETIMEDOUT) return false; // Timeout. |
| 751 | CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup. |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 756 | Semaphore* OS::CreateSemaphore(int count) { |
| 757 | return new LinuxSemaphore(count); |
| 758 | } |
| 759 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 760 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 761 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 762 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 763 | static Sampler* active_sampler_ = NULL; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 764 | static int vm_tid_ = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 765 | |
kasperl@chromium.org | acae378 | 2009-04-11 09:17:08 +0000 | [diff] [blame] | 766 | |
| 767 | #if !defined(__GLIBC__) && (defined(__arm__) || defined(__thumb__)) |
| 768 | // Android runs a fairly new Linux kernel, so signal info is there, |
| 769 | // but the C library doesn't have the structs defined. |
| 770 | |
| 771 | struct sigcontext { |
| 772 | uint32_t trap_no; |
| 773 | uint32_t error_code; |
| 774 | uint32_t oldmask; |
| 775 | uint32_t gregs[16]; |
| 776 | uint32_t arm_cpsr; |
| 777 | uint32_t fault_address; |
| 778 | }; |
| 779 | typedef uint32_t __sigset_t; |
| 780 | typedef struct sigcontext mcontext_t; |
| 781 | typedef struct ucontext { |
| 782 | uint32_t uc_flags; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 783 | struct ucontext* uc_link; |
kasperl@chromium.org | acae378 | 2009-04-11 09:17:08 +0000 | [diff] [blame] | 784 | stack_t uc_stack; |
| 785 | mcontext_t uc_mcontext; |
| 786 | __sigset_t uc_sigmask; |
| 787 | } ucontext_t; |
| 788 | enum ArmRegisters {R15 = 15, R13 = 13, R11 = 11}; |
| 789 | |
| 790 | #endif |
| 791 | |
| 792 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 793 | static int GetThreadID() { |
| 794 | // Glibc doesn't provide a wrapper for gettid(2). |
| 795 | return syscall(SYS_gettid); |
| 796 | } |
| 797 | |
| 798 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 799 | static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 800 | #ifndef V8_HOST_ARCH_MIPS |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 801 | USE(info); |
| 802 | if (signal != SIGPROF) return; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 803 | if (active_sampler_ == NULL || !active_sampler_->IsActive()) return; |
| 804 | if (vm_tid_ != GetThreadID()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 805 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 806 | TickSample sample_obj; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 807 | TickSample* sample = CpuProfiler::TickSampleEvent(); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 808 | if (sample == NULL) sample = &sample_obj; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 809 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 810 | // Extracting the sample from the context is extremely machine dependent. |
| 811 | ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context); |
| 812 | mcontext_t& mcontext = ucontext->uc_mcontext; |
| 813 | sample->state = Top::current_vm_state(); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 814 | #if V8_HOST_ARCH_IA32 |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 815 | sample->pc = reinterpret_cast<Address>(mcontext.gregs[REG_EIP]); |
| 816 | sample->sp = reinterpret_cast<Address>(mcontext.gregs[REG_ESP]); |
| 817 | sample->fp = reinterpret_cast<Address>(mcontext.gregs[REG_EBP]); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 818 | #elif V8_HOST_ARCH_X64 |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 819 | sample->pc = reinterpret_cast<Address>(mcontext.gregs[REG_RIP]); |
| 820 | sample->sp = reinterpret_cast<Address>(mcontext.gregs[REG_RSP]); |
| 821 | sample->fp = reinterpret_cast<Address>(mcontext.gregs[REG_RBP]); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 822 | #elif V8_HOST_ARCH_ARM |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 823 | // An undefined macro evaluates to 0, so this applies to Android's Bionic also. |
| 824 | #if (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3)) |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 825 | sample->pc = reinterpret_cast<Address>(mcontext.gregs[R15]); |
| 826 | sample->sp = reinterpret_cast<Address>(mcontext.gregs[R13]); |
| 827 | sample->fp = reinterpret_cast<Address>(mcontext.gregs[R11]); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 828 | #else |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 829 | sample->pc = reinterpret_cast<Address>(mcontext.arm_pc); |
| 830 | sample->sp = reinterpret_cast<Address>(mcontext.arm_sp); |
| 831 | sample->fp = reinterpret_cast<Address>(mcontext.arm_fp); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 832 | #endif |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 833 | #elif V8_HOST_ARCH_MIPS |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 834 | // Implement this on MIPS. |
| 835 | UNIMPLEMENTED(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 836 | #endif |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 837 | active_sampler_->SampleStack(sample); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 838 | active_sampler_->Tick(sample); |
| 839 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 843 | class Sampler::PlatformData : public Malloced { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 844 | public: |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 845 | enum SleepInterval { |
| 846 | FULL_INTERVAL, |
| 847 | HALF_INTERVAL |
| 848 | }; |
| 849 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 850 | explicit PlatformData(Sampler* sampler) |
| 851 | : sampler_(sampler), |
| 852 | signal_handler_installed_(false), |
| 853 | vm_tgid_(getpid()), |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 854 | signal_sender_launched_(false) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 855 | } |
| 856 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 857 | void SignalSender() { |
| 858 | while (sampler_->IsActive()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 859 | if (rate_limiter_.SuspendIfNecessary()) continue; |
| 860 | if (sampler_->IsProfiling() && RuntimeProfiler::IsEnabled()) { |
| 861 | SendProfilingSignal(); |
| 862 | Sleep(HALF_INTERVAL); |
| 863 | RuntimeProfiler::NotifyTick(); |
| 864 | Sleep(HALF_INTERVAL); |
| 865 | } else { |
| 866 | if (sampler_->IsProfiling()) SendProfilingSignal(); |
| 867 | if (RuntimeProfiler::IsEnabled()) RuntimeProfiler::NotifyTick(); |
| 868 | Sleep(FULL_INTERVAL); |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 869 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 870 | } |
| 871 | } |
| 872 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 873 | void SendProfilingSignal() { |
| 874 | // Glibc doesn't provide a wrapper for tgkill(2). |
| 875 | syscall(SYS_tgkill, vm_tgid_, vm_tid_, SIGPROF); |
| 876 | } |
| 877 | |
| 878 | void Sleep(SleepInterval full_or_half) { |
| 879 | // Convert ms to us and subtract 100 us to compensate delays |
| 880 | // occuring during signal delivery. |
| 881 | useconds_t interval = sampler_->interval_ * 1000 - 100; |
| 882 | if (full_or_half == HALF_INTERVAL) interval /= 2; |
| 883 | int result = usleep(interval); |
| 884 | #ifdef DEBUG |
| 885 | if (result != 0 && errno != EINTR) { |
| 886 | fprintf(stderr, |
| 887 | "SignalSender usleep error; interval = %u, errno = %d\n", |
| 888 | interval, |
| 889 | errno); |
| 890 | ASSERT(result == 0 || errno == EINTR); |
| 891 | } |
| 892 | #endif |
| 893 | USE(result); |
| 894 | } |
| 895 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 896 | Sampler* sampler_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 897 | bool signal_handler_installed_; |
| 898 | struct sigaction old_signal_handler_; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 899 | int vm_tgid_; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 900 | bool signal_sender_launched_; |
| 901 | pthread_t signal_sender_thread_; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 902 | RuntimeProfilerRateLimiter rate_limiter_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 903 | }; |
| 904 | |
| 905 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 906 | static void* SenderEntry(void* arg) { |
| 907 | Sampler::PlatformData* data = |
| 908 | reinterpret_cast<Sampler::PlatformData*>(arg); |
| 909 | data->SignalSender(); |
| 910 | return 0; |
| 911 | } |
| 912 | |
| 913 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 914 | Sampler::Sampler(int interval) |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 915 | : interval_(interval), |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 916 | profiling_(false), |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 917 | active_(false), |
| 918 | samples_taken_(0) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 919 | data_ = new PlatformData(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 923 | Sampler::~Sampler() { |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 924 | ASSERT(!data_->signal_sender_launched_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 925 | delete data_; |
| 926 | } |
| 927 | |
| 928 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 929 | void Sampler::Start() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 930 | // There can only be one active sampler at the time on POSIX |
| 931 | // platforms. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 932 | ASSERT(!IsActive()); |
| 933 | vm_tid_ = GetThreadID(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 934 | |
| 935 | // Request profiling signals. |
| 936 | struct sigaction sa; |
| 937 | sa.sa_sigaction = ProfilerSignalHandler; |
| 938 | sigemptyset(&sa.sa_mask); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 939 | sa.sa_flags = SA_RESTART | SA_SIGINFO; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 940 | if (sigaction(SIGPROF, &sa, &data_->old_signal_handler_) != 0) return; |
| 941 | data_->signal_handler_installed_ = true; |
| 942 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 943 | // Start a thread that sends SIGPROF signal to VM thread. |
| 944 | // Sending the signal ourselves instead of relying on itimer provides |
| 945 | // much better accuracy. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 946 | SetActive(true); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 947 | if (pthread_create( |
| 948 | &data_->signal_sender_thread_, NULL, SenderEntry, data_) == 0) { |
| 949 | data_->signal_sender_launched_ = true; |
| 950 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 951 | |
| 952 | // Set this sampler as the active sampler. |
| 953 | active_sampler_ = this; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 957 | void Sampler::Stop() { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 958 | SetActive(false); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 959 | |
| 960 | // Wait for signal sender termination (it will exit after setting |
| 961 | // active_ to false). |
| 962 | if (data_->signal_sender_launched_) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 963 | Top::WakeUpRuntimeProfilerThreadBeforeShutdown(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 964 | pthread_join(data_->signal_sender_thread_, NULL); |
| 965 | data_->signal_sender_launched_ = false; |
| 966 | } |
| 967 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 968 | // Restore old signal handler |
| 969 | if (data_->signal_handler_installed_) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 970 | sigaction(SIGPROF, &data_->old_signal_handler_, 0); |
| 971 | data_->signal_handler_installed_ = false; |
| 972 | } |
| 973 | |
| 974 | // This sampler is no longer the active sampler. |
| 975 | active_sampler_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 976 | } |
| 977 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 978 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 979 | #endif // ENABLE_LOGGING_AND_PROFILING |
| 980 | |
| 981 | } } // namespace v8::internal |