blob: f571b990217b569b9ee1beeb95ce1e21d62c0920 [file] [log] [blame]
ulan@chromium.org2efb9002012-01-19 15:36:35 +00001// Copyright 2012 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// 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.orgbb29dc92009-03-24 13:25:23 +000028// Platform specific code for Linux goes here. For the POSIX comaptible parts
29// the implementation is in platform-posix.cc.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000030
31#include <pthread.h>
32#include <semaphore.h>
33#include <signal.h>
lrn@chromium.org5d00b602011-01-05 09:51:43 +000034#include <sys/prctl.h>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000035#include <sys/time.h>
36#include <sys/resource.h>
lrn@chromium.org303ada72010-10-27 09:33:13 +000037#include <sys/syscall.h>
ager@chromium.org381abbb2009-02-25 13:23:22 +000038#include <sys/types.h>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000039#include <stdlib.h>
40
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +000041#if defined(__GLIBC__)
42#include <execinfo.h>
43#include <cxxabi.h>
44#endif
45
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000046// 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.org236ad962008-09-25 09:45:57 +000050#include <sys/mman.h> // mmap & munmap
51#include <sys/stat.h> // open
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000052#include <fcntl.h> // open
53#include <unistd.h> // sysconf
fschneider@chromium.org7d10be52012-04-10 12:30:14 +000054#if defined(__GLIBC__) && !defined(__UCLIBC__)
ager@chromium.org236ad962008-09-25 09:45:57 +000055#include <execinfo.h> // backtrace, backtrace_symbols
fschneider@chromium.org7d10be52012-04-10 12:30:14 +000056#endif // defined(__GLIBC__) && !defined(__UCLIBC__)
ager@chromium.org236ad962008-09-25 09:45:57 +000057#include <strings.h> // index
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000058#include <errno.h>
59#include <stdarg.h>
60
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +000061// 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.hansen43d26ec2008-07-03 15:10:15 +000068#undef MAP_TYPE
69
70#include "v8.h"
71
danno@chromium.org8c0a43f2012-04-03 08:37:53 +000072#include "platform-posix.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000073#include "platform.h"
ager@chromium.orga1645e22009-09-09 19:27:10 +000074#include "v8threads.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000075#include "vm-state-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000076
77
kasperl@chromium.org71affb52009-05-26 05:44:31 +000078namespace v8 {
79namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000080
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).
83static const pthread_t kNoThread = (pthread_t) 0;
84
85
86double ceiling(double x) {
87 return ceil(x);
88}
89
90
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000091static Mutex* limit_mutex = NULL;
92
93
danno@chromium.org8c0a43f2012-04-03 08:37:53 +000094void OS::PostSetUp() {
fschneider@chromium.org7d10be52012-04-10 12:30:14 +000095 POSIXPostSetUp();
danno@chromium.org8c0a43f2012-04-03 08:37:53 +000096}
97
98
ager@chromium.orgc4c92722009-11-18 14:12:51 +000099uint64_t OS::CpuFeaturesImpliedByPlatform() {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000100 return 0; // Linux runs on anything.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000101}
102
103
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000104#ifdef __arm__
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000105static bool CPUInfoContainsString(const char * search_string) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000106 const char* file_name = "/proc/cpuinfo";
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000107 // 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.orgc4c92722009-11-18 14:12:51 +0000111 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.orgfa943b72010-11-03 08:14:36 +0000137
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000138
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000139bool OS::ArmCpuHasFeature(CpuFeature feature) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000140 const char* search_string = NULL;
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000141 // 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.orgb6d052d2012-07-27 08:03:27 +0000147 case VFP2:
148 search_string = "vfp";
149 break;
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000150 case VFP3:
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000151 search_string = "vfpv3";
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000152 break;
153 case ARMv7:
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000154 search_string = "ARMv7";
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000155 break;
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000156 case SUDIV:
157 search_string = "idiva";
158 break;
yangguo@chromium.org003650e2013-01-24 16:31:08 +0000159 case VFP32DREGS:
160 // This case is handled specially below.
161 break;
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000162 default:
163 UNREACHABLE();
164 }
165
yangguo@chromium.org003650e2013-01-24 16:31:08 +0000166 if (feature == VFP32DREGS) {
167 return ArmCpuHasFeature(VFP3) && !CPUInfoContainsString("d16");
168 }
169
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000170 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.orgfa943b72010-11-03 08:14:36 +0000181 return true;
182 }
183 }
184
185 return false;
186}
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000187
188
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000189CpuImplementer 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.org8e8294a2011-05-02 14:30:53 +0000207bool OS::ArmUsingHardFloat() {
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +0000208 // 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.org8e8294a2011-05-02 14:30:53 +0000242}
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +0000243
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000244#endif // def __arm__
245
246
lrn@chromium.org7516f052011-03-30 08:52:27 +0000247#ifdef __mips__
248bool 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.org236ad962008-09-25 09:45:57 +0000299int OS::ActivationFrameAlignment() {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000300#ifdef V8_TARGET_ARCH_ARM
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000301 // On EABI ARM targets this is required for fp correctness in the
302 // runtime system.
ager@chromium.org3a6061e2009-03-12 14:24:36 +0000303 return 8;
ager@chromium.org5c838252010-02-19 08:53:10 +0000304#elif V8_TARGET_ARCH_MIPS
305 return 8;
306#endif
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000307 // With gcc 4.4 the tree vectorization optimizer can generate code
ager@chromium.orge2902be2009-06-08 12:21:35 +0000308 // that requires 16 byte alignment such as movdqa on x86.
309 return 16;
ager@chromium.org236ad962008-09-25 09:45:57 +0000310}
311
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000312
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000313void OS::ReleaseStore(volatile AtomicWord* ptr, AtomicWord value) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000314#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.orga5551262010-12-07 12:49:48 +0000317 MemoryBarrier();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000318#else
319 __asm__ __volatile__("" : : : "memory");
320 // An x86 store acts as a release barrier.
321#endif
322 *ptr = value;
323}
324
325
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000326const 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
335double 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.hansen43d26ec2008-07-03 15:10:15 +0000344// 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.org2efb9002012-01-19 15:36:35 +0000346// and verification). The estimate is conservative, i.e., not all addresses in
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000347// '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.
349static void* lowest_ever_allocated = reinterpret_cast<void*>(-1);
350static void* highest_ever_allocated = reinterpret_cast<void*>(0);
351
352
353static void UpdateAllocatedSpaceLimits(void* address, int size) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000354 ASSERT(limit_mutex != NULL);
355 ScopedLock lock(limit_mutex);
356
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000357 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
364bool OS::IsOutsideAllocatedSpace(void* address) {
365 return address < lowest_ever_allocated || address >= highest_ever_allocated;
366}
367
368
369size_t OS::AllocateAlignment() {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000370 return sysconf(_SC_PAGESIZE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000371}
372
373
kasper.lund7276f142008-07-30 08:49:36 +0000374void* OS::Allocate(const size_t requested,
375 size_t* allocated,
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000376 bool is_executable) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000377 const size_t msize = RoundUp(requested, AllocateAlignment());
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000378 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000379 void* addr = OS::GetRandomMmapAddr();
ricow@chromium.org9fa09672011-07-25 11:05:35 +0000380 void* mbase = mmap(addr, msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000381 if (mbase == MAP_FAILED) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000382 LOG(i::Isolate::Current(),
383 StringEvent("OS::Allocate", "mmap failed"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000384 return NULL;
385 }
386 *allocated = msize;
387 UpdateAllocatedSpaceLimits(mbase, msize);
388 return mbase;
389}
390
391
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000392void OS::Free(void* address, const size_t size) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000393 // TODO(1240712): munmap has a return value which is ignored here.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000394 int result = munmap(address, size);
395 USE(result);
396 ASSERT(result == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000397}
398
399
400void OS::Sleep(int milliseconds) {
401 unsigned int ms = static_cast<unsigned int>(milliseconds);
402 usleep(1000 * ms);
403}
404
405
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000406int OS::NumberOfCores() {
407 return sysconf(_SC_NPROCESSORS_ONLN);
408}
409
410
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000411void OS::Abort() {
412 // Redirect to std abort to signal abnormal program termination.
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000413 if (FLAG_break_on_abort) {
414 DebugBreak();
415 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000416 abort();
417}
418
419
kasper.lund7276f142008-07-30 08:49:36 +0000420void OS::DebugBreak() {
ager@chromium.org5ec48922009-05-05 07:25:34 +0000421// TODO(lrn): Introduce processor define for runtime system (!= V8_ARCH_x,
422// which is the architecture of generated code).
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000423#if (defined(__arm__) || defined(__thumb__))
424# if defined(CAN_USE_ARMV5_INSTRUCTIONS)
kasper.lund7276f142008-07-30 08:49:36 +0000425 asm("bkpt 0");
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000426# endif
ager@chromium.org5c838252010-02-19 08:53:10 +0000427#elif defined(__mips__)
428 asm("break");
kasper.lund7276f142008-07-30 08:49:36 +0000429#else
430 asm("int $3");
431#endif
432}
433
434
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000435void 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.hansen43d26ec2008-07-03 15:10:15 +0000466class 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.org0a4e9012011-01-24 12:33:13 +0000472 virtual int size() { return size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000473 private:
474 FILE* file_;
475 void* memory_;
476 int size_;
477};
478
479
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000480OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000481 FILE* file = fopen(name, "r+");
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000482 if (file == NULL) return NULL;
483
484 fseek(file, 0, SEEK_END);
485 int size = ftell(file);
486
487 void* memory =
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000488 mmap(OS::GetRandomMmapAddr(),
489 size,
490 PROT_READ | PROT_WRITE,
491 MAP_SHARED,
492 fileno(file),
493 0);
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000494 return new PosixMemoryMappedFile(file, memory, size);
495}
496
497
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000498OS::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.org9258b6b2008-09-11 09:11:10 +0000502 int result = fwrite(initial, size, 1, file);
503 if (result < 1) {
504 fclose(file);
505 return NULL;
506 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000507 void* memory =
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000508 mmap(OS::GetRandomMmapAddr(),
509 size,
510 PROT_READ | PROT_WRITE,
511 MAP_SHARED,
512 fileno(file),
513 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000514 return new PosixMemoryMappedFile(file, memory, size);
515}
516
517
518PosixMemoryMappedFile::~PosixMemoryMappedFile() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000519 if (memory_) OS::Free(memory_, size_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000520 fclose(file_);
521}
522
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000523
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000524void OS::LogSharedLibraryAddresses() {
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000525 // 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.orgc4c92722009-11-18 14:12:51 +0000528 FILE* fp = fopen("/proc/self/maps", "r");
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000529 if (fp == NULL) return;
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000530
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.orgea88ce92011-03-23 11:19:56 +0000535 i::Isolate* isolate = ISOLATE;
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000536 // This loop will terminate once the scanning hits an EOF.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000537 while (true) {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000538 uintptr_t start, end;
539 char attr_r, attr_w, attr_x, attr_p;
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000540 // Parse the addresses and permission bits at the beginning of the line.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000541 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.orgb9d7da12009-08-05 08:38:10 +0000543
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000544 int c;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000545 if (attr_r == 'r' && attr_w != 'w' && attr_x == 'x') {
546 // Found a read-only executable entry. Skip characters until we reach
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000547 // the beginning of the filename or the end of the line.
548 do {
549 c = getc(fp);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000550 } while ((c != EOF) && (c != '\n') && (c != '/') && (c != '['));
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000551 if (c == EOF) break; // EOF: Was unexpected, just exit.
552
553 // Process the filename if found.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000554 if ((c == '/') || (c == '[')) {
555 // Push the '/' or '[' back into the stream to be read below.
556 ungetc(c, fp);
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000557
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.org46a2a512013-01-18 16:29:40 +0000563 // '/' or '[' character.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000564 lib_name[strlen(lib_name) - 1] = '\0';
565 } else {
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000566 // No library name found, just record the raw address range.
567 snprintf(lib_name, kLibNameLen,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000568 "%08" V8PRIxPTR "-%08" V8PRIxPTR, start, end);
569 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000570 LOG(isolate, SharedLibraryEvent(lib_name, start, end));
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000571 } else {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000572 // Entry not describing executable data. Skip to end of line to set up
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000573 // reading the next entry.
574 do {
575 c = getc(fp);
576 } while ((c != EOF) && (c != '\n'));
577 if (c == EOF) break;
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000578 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000579 }
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000580 free(lib_name);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000581 fclose(fp);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000582}
583
584
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000585void OS::SignalCodeMovingGC() {
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000586 // 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.orgde886792012-09-11 13:22:37 +0000595 FILE* f = fopen(FLAG_gc_fake_mmap, "w+");
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000596 void* addr = mmap(OS::GetRandomMmapAddr(),
597 size,
598 PROT_READ | PROT_EXEC,
599 MAP_PRIVATE,
600 fileno(f),
601 0);
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000602 ASSERT(addr != MAP_FAILED);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000603 OS::Free(addr, size);
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000604 fclose(f);
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000605}
606
607
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000608int OS::StackWalk(Vector<OS::StackFrame> frames) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000609 // backtrace is a glibc extension.
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000610#if defined(__GLIBC__) && !defined(__UCLIBC__)
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000611 int frames_size = frames.length();
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000612 ScopedVector<void*> addresses(frames_size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000613
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000614 int frames_count = backtrace(addresses.start(), frames_size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000615
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000616 char** symbols = backtrace_symbols(addresses.start(), frames_count);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000617 if (symbols == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000618 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.orgb9123622008-09-17 14:05:56 +0000625 SNPrintF(MutableCStrVector(frames[i].text, kStackWalkMaxTextLen),
626 "%s",
627 symbols[i]);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000628 // Make sure line termination is in place.
629 frames[i].text[kStackWalkMaxTextLen - 1] = '\0';
630 }
631
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000632 free(symbols);
633
634 return frames_count;
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000635#else // defined(__GLIBC__) && !defined(__UCLIBC__)
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000636 return 0;
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000637#endif // defined(__GLIBC__) && !defined(__UCLIBC__)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000638}
639
640
641// Constants used for mmap.
642static const int kMmapFd = -1;
643static const int kMmapFdOffset = 0;
644
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000645VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000646
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000647VirtualMemory::VirtualMemory(size_t size) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000648 address_ = ReserveRegion(size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000649 size_ = size;
650}
651
652
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000653VirtualMemory::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.orgb4b2aa62011-10-13 09:49:59 +0000658 void* reservation = mmap(OS::GetRandomMmapAddr(),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000659 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.hansen43d26ec2008-07-03 15:10:15 +0000693VirtualMemory::~VirtualMemory() {
694 if (IsReserved()) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000695 bool result = ReleaseRegion(address(), size());
696 ASSERT(result);
697 USE(result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000698 }
699}
700
701
702bool VirtualMemory::IsReserved() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000703 return address_ != NULL;
704}
705
706
707void VirtualMemory::Reset() {
708 address_ = NULL;
709 size_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000710}
711
712
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000713bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000714 return CommitRegion(address, size, is_executable);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000715}
716
717
718bool VirtualMemory::Uncommit(void* address, size_t size) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000719 return UncommitRegion(address, size);
720}
721
722
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000723bool VirtualMemory::Guard(void* address) {
724 OS::Guard(address, OS::CommitPageSize());
725 return true;
726}
727
728
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000729void* VirtualMemory::ReserveRegion(size_t size) {
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000730 void* result = mmap(OS::GetRandomMmapAddr(),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000731 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
743bool 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
759bool VirtualMemory::UncommitRegion(void* base, size_t size) {
760 return mmap(base,
761 size,
762 PROT_NONE,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000763 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE | MAP_FIXED,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000764 kMmapFd,
765 kMmapFdOffset) != MAP_FAILED;
766}
767
768
769bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
770 return munmap(base, size) == 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000771}
772
773
danno@chromium.org72204d52012-10-31 10:02:10 +0000774bool VirtualMemory::HasLazyCommits() {
775 return true;
776}
777
778
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000779class Thread::PlatformData : public Malloced {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000780 public:
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000781 PlatformData() : thread_(kNoThread) {}
ager@chromium.org41826e72009-03-30 13:30:57 +0000782
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000783 pthread_t thread_; // Thread handle for pthread.
784};
785
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000786Thread::Thread(const Options& options)
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000787 : data_(new PlatformData()),
yangguo@chromium.org659ceec2012-01-26 07:37:54 +0000788 stack_size_(options.stack_size()) {
789 set_name(options.name());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000790}
791
792
793Thread::~Thread() {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000794 delete data_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000795}
796
797
798static 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.orgb6451162011-08-17 14:33:23 +0000803#ifdef PR_SET_NAME
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000804 prctl(PR_SET_NAME,
805 reinterpret_cast<unsigned long>(thread->name()), // NOLINT
806 0, 0, 0);
danno@chromium.orgb6451162011-08-17 14:33:23 +0000807#endif
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000808 thread->data()->thread_ = pthread_self();
809 ASSERT(thread->data()->thread_ != kNoThread);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000810 thread->Run();
811 return NULL;
812}
813
814
lrn@chromium.org5d00b602011-01-05 09:51:43 +0000815void Thread::set_name(const char* name) {
816 strncpy(name_, name, sizeof(name_));
817 name_[sizeof(name_) - 1] = '\0';
818}
819
820
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000821void Thread::Start() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000822 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.orgc612e022011-11-10 11:38:15 +0000829 int result = pthread_create(&data_->thread_, attr_ptr, ThreadEntry, this);
830 CHECK_EQ(0, result);
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000831 ASSERT(data_->thread_ != kNoThread);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000832}
833
834
835void Thread::Join() {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000836 pthread_join(data_->thread_, NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000837}
838
839
840Thread::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
849void 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
857void* 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
863void 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
869void Thread::YieldCPU() {
870 sched_yield();
871}
872
873
874class LinuxMutex : public Mutex {
875 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000876 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.org717967f2011-07-20 13:44:42 +0000884 USE(result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000885 }
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.orga5551262010-12-07 12:49:48 +0000899 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.hansen43d26ec2008-07-03 15:10:15 +0000909 private:
910 pthread_mutex_t mutex_; // Pthread mutex for POSIX platforms.
911};
912
913
914Mutex* OS::CreateMutex() {
915 return new LinuxMutex();
916}
917
918
919class LinuxSemaphore : public Semaphore {
920 public:
921 explicit LinuxSemaphore(int count) { sem_init(&sem_, 0, count); }
922 virtual ~LinuxSemaphore() { sem_destroy(&sem_); }
923
kasper.lund7276f142008-07-30 08:49:36 +0000924 virtual void Wait();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000925 virtual bool Wait(int timeout);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000926 virtual void Signal() { sem_post(&sem_); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000927 private:
928 sem_t sem_;
929};
930
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000931
kasper.lund7276f142008-07-30 08:49:36 +0000932void 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.hansen43d26ec2008-07-03 15:10:15 +0000939
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000940
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000941#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.orgbb29dc92009-03-24 13:25:23 +0000949bool LinuxSemaphore::Wait(int timeout) {
950 const long kOneSecondMicros = 1000000; // NOLINT
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000951
952 // Split timeout into second and nanosecond parts.
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000953 struct timeval delta;
954 delta.tv_usec = timeout % kOneSecondMicros;
955 delta.tv_sec = timeout / kOneSecondMicros;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000956
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000957 struct timeval current_time;
958 // Get the current time.
959 if (gettimeofday(&current_time, NULL) == -1) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000960 return false;
961 }
962
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000963 // Calculate time for end of timeout.
964 struct timeval end_time;
965 timeradd(&current_time, &delta, &end_time);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000966
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000967 struct timespec ts;
968 TIMEVAL_TO_TIMESPEC(&end_time, &ts);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000969 // 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.hansen43d26ec2008-07-03 15:10:15 +0000984Semaphore* OS::CreateSemaphore(int count) {
985 return new LinuxSemaphore(count);
986}
987
ager@chromium.org381abbb2009-02-25 13:23:22 +0000988
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000989#if defined(__ANDROID__) && !defined(__BIONIC_HAVE_UCONTEXT_T)
kasperl@chromium.orgacae3782009-04-11 09:17:08 +0000990
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000991// 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.orgacae3782009-04-11 09:17:08 +00001000typedef struct sigcontext mcontext_t;
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001001
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001002typedef struct ucontext {
1003 uint32_t uc_flags;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001004 struct ucontext* uc_link;
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001005 stack_t uc_stack;
1006 mcontext_t uc_mcontext;
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001007 // Other fields are not used by V8, don't define them here.
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001008} ucontext_t;
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001009
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001010#elif defined(__mips__)
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001011// MIPS version of sigcontext, for Android bionic.
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001012typedef struct {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001013 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.org471f2f12012-08-10 14:46:33 +00001031} mcontext_t;
1032
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001033typedef struct ucontext {
1034 uint32_t uc_flags;
1035 struct ucontext* uc_link;
1036 stack_t uc_stack;
1037 mcontext_t uc_mcontext;
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001038 // Other fields are not used by V8, don't define them here.
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001039} ucontext_t;
1040
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001041#elif defined(__i386__)
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001042// x86 version for Android.
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001043typedef struct {
yangguo@chromium.orgcb9affa2012-05-15 12:16:38 +00001044 uint32_t gregs[19];
1045 void* fpregs;
1046 uint32_t oldmask;
1047 uint32_t cr2;
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001048} mcontext_t;
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001049
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001050typedef uint32_t kernel_sigset_t[2]; // x86 kernel uses 64-bit signal masks
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001051typedef struct ucontext {
yangguo@chromium.orgcb9affa2012-05-15 12:16:38 +00001052 uint32_t uc_flags;
1053 struct ucontext* uc_link;
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001054 stack_t uc_stack;
1055 mcontext_t uc_mcontext;
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001056 // Other fields are not used by V8, don't define them here.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001057} ucontext_t;
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001058enum { REG_EBP = 6, REG_ESP = 7, REG_EIP = 14 };
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001059#endif
1060
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001061#endif // __ANDROID__ && !defined(__BIONIC_HAVE_UCONTEXT_T)
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001062
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001063static int GetThreadID() {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001064#if defined(__ANDROID__)
1065 // Android's C library provides gettid(2).
1066 return gettid();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001067#else
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001068 // Glibc doesn't provide a wrapper for gettid(2).
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001069 return syscall(SYS_gettid);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001070#endif
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001071}
1072
1073
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001074static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) {
1075 USE(info);
1076 if (signal != SIGPROF) return;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001077 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.org0ec56d62011-04-15 22:22:08 +00001082 if (v8::Locker::IsActive() &&
1083 !isolate->thread_manager()->IsLockedByCurrentThread()) {
1084 return;
1085 }
1086
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001087 Sampler* sampler = isolate->logger()->sampler();
1088 if (sampler == NULL || !sampler->IsActive()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001089
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00001090 TickSample sample_obj;
mmassi@chromium.org49a44672012-12-04 13:52:03 +00001091 TickSample* sample = CpuProfiler::TickSampleEvent(isolate);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00001092 if (sample == NULL) sample = &sample_obj;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001093
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001094 // 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.orgea88ce92011-03-23 11:19:56 +00001097 sample->state = isolate->current_vm_state();
ager@chromium.org9085a012009-05-11 19:22:57 +00001098#if V8_HOST_ARCH_IA32
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001099 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.org9085a012009-05-11 19:22:57 +00001102#elif V8_HOST_ARCH_X64
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001103 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.org9085a012009-05-11 19:22:57 +00001106#elif V8_HOST_ARCH_ARM
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001107#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.orga5551262010-12-07 12:49:48 +00001111 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.orgb3284ad2009-05-18 06:12:45 +00001114#else
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001115 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.org471f2f12012-08-10 14:46:33 +00001118#endif // defined(__GLIBC__) && !defined(__UCLIBC__) &&
1119 // (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3))
ager@chromium.org5c838252010-02-19 08:53:10 +00001120#elif V8_HOST_ARCH_MIPS
danno@chromium.orgc612e022011-11-10 11:38:15 +00001121 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.orgea88ce92011-03-23 11:19:56 +00001125 sampler->SampleStack(sample);
1126 sampler->Tick(sample);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001127}
1128
1129
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001130class CpuProfilerSignalHandler {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001131 public:
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001132 static void SetUp() { if (!mutex_) mutex_ = OS::CreateMutex(); }
1133 static void TearDown() { delete mutex_; }
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001134
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001135 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.org49a44672012-12-04 13:52:03 +00001151 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.org71fc3462013-02-27 09:34:27 +00001167 // 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
1175Mutex* CpuProfilerSignalHandler::mutex_ = NULL;
1176int CpuProfilerSignalHandler::profiling_samplers_count_ = 0;
1177bool CpuProfilerSignalHandler::signal_handler_installed_ = false;
1178struct sigaction CpuProfilerSignalHandler::old_signal_handler_;
1179
1180
1181class 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
1211class 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.orgea88ce92011-03-23 11:19:56 +00001222 static void AddActiveSampler(Sampler* sampler) {
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001223 ScopedLock lock(mutex_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001224 SamplerRegistry::AddActiveSampler(sampler);
1225 if (instance_ == NULL) {
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001226 // Start a thread that will send SIGPROF signal to VM threads,
1227 // when CPU profiling will be enabled.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001228 instance_ = new SignalSender(sampler->interval());
1229 instance_->Start();
1230 } else {
1231 ASSERT(instance_->interval_ == sampler->interval());
1232 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001233 }
1234
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001235 static void RemoveActiveSampler(Sampler* sampler) {
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001236 ScopedLock lock(mutex_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001237 SamplerRegistry::RemoveActiveSampler(sampler);
1238 if (SamplerRegistry::GetState() == SamplerRegistry::HAS_NO_SAMPLERS) {
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001239 RuntimeProfiler::StopRuntimeProfilerThreadBeforeShutdown(instance_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001240 delete instance_;
1241 instance_ = NULL;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001242 }
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.org89e18f52012-10-22 13:09:53 +00001250 // When CPU profiling is enabled both JavaScript and C++ code is
1251 // profiled. We must not suspend.
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001252 if (state == SamplerRegistry::HAS_CPU_PROFILING_SAMPLERS) {
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001253 SamplerRegistry::IterateActiveSamplers(&DoCpuProfile, this);
1254 } else {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001255 if (RuntimeProfiler::WaitForSomeIsolateToEnterJS()) continue;
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00001256 }
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001257 Sleep(); // TODO(svenpanne) Figure out if OS:Sleep(interval_) is enough.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001258 }
1259 }
1260
mmassi@chromium.org49a44672012-12-04 13:52:03 +00001261 static void DoCpuProfile(Sampler* sampler, void* raw_sender) {
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00001262 if (!sampler->IsProfiling()) return;
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001263 sampler->DoSample();
mmassi@chromium.org49a44672012-12-04 13:52:03 +00001264 }
1265
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001266 void Sleep() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001267 // Convert ms to us and subtract 100 us to compensate delays
1268 // occuring during signal delivery.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001269 useconds_t interval = interval_ * 1000 - 100;
ulan@chromium.org2efb9002012-01-19 15:36:35 +00001270#if defined(ANDROID)
1271 usleep(interval);
1272#else
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001273 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.org2efb9002012-01-19 15:36:35 +00001282#endif // DEBUG
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001283 USE(result);
ulan@chromium.org2efb9002012-01-19 15:36:35 +00001284#endif // ANDROID
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001285 }
1286
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001287 const int interval_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001288
1289 // Protects the process wide state below.
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001290 static Mutex* mutex_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001291 static SignalSender* instance_;
mmassi@chromium.org49a44672012-12-04 13:52:03 +00001292 static struct sigaction old_signal_handler_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001293
jkummerow@chromium.org05ed9dd2012-01-23 14:42:48 +00001294 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001295 DISALLOW_COPY_AND_ASSIGN(SignalSender);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001296};
1297
1298
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001299Mutex* SignalSender::mutex_ = NULL;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001300SignalSender* SignalSender::instance_ = NULL;
mmassi@chromium.org49a44672012-12-04 13:52:03 +00001301struct sigaction SignalSender::old_signal_handler_;
lrn@chromium.org303ada72010-10-27 09:33:13 +00001302
1303
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001304void 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.org71fc3462013-02-27 09:34:27 +00001329 CpuProfilerSignalHandler::SetUp();
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001330}
1331
1332
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001333void OS::TearDown() {
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001334 CpuProfilerSignalHandler::TearDown();
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001335 SignalSender::TearDown();
1336 delete limit_mutex;
1337}
1338
1339
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001340Sampler::Sampler(Isolate* isolate, int interval)
1341 : isolate_(isolate),
1342 interval_(interval),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001343 profiling_(false),
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001344 has_processing_thread_(false),
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001345 active_(false),
1346 samples_taken_(0) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001347 data_ = new PlatformData;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001348}
1349
1350
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001351Sampler::~Sampler() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001352 ASSERT(!IsActive());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001353 delete data_;
1354}
1355
1356
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001357void Sampler::Start() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001358 ASSERT(!IsActive());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001359 SetActive(true);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001360 SignalSender::AddActiveSampler(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001361}
1362
1363
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001364void Sampler::Stop() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001365 ASSERT(IsActive());
1366 SignalSender::RemoveActiveSampler(this);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001367 SetActive(false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001368}
1369
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001370
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001371bool Sampler::CanSampleOnProfilerEventsProcessorThread() {
1372 return true;
1373}
1374
1375
1376void Sampler::DoSample() {
1377 platform_data()->SendProfilingSignal();
1378}
1379
1380
1381void Sampler::StartProfiling() {
1382 platform_data()->set_signal_handler_installed(
1383 CpuProfilerSignalHandler::RegisterProfilingSampler());
1384}
1385
1386
1387void Sampler::StopProfiling() {
1388 CpuProfilerSignalHandler::UnregisterProfilingSampler();
1389 platform_data()->set_signal_handler_installed(false);
1390}
1391
1392
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001393} } // namespace v8::internal