blob: e89d2cc9c8a861360fd3d704ae0baf650700d9f5 [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;
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000159 default:
160 UNREACHABLE();
161 }
162
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000163 if (CPUInfoContainsString(search_string)) {
164 return true;
165 }
166
167 if (feature == VFP3) {
168 // Some old kernels will report vfp not vfpv3. Here we make a last attempt
169 // to detect vfpv3 by checking for vfp *and* neon, since neon is only
170 // available on architectures with vfpv3.
171 // Checking neon on its own is not enough as it is possible to have neon
172 // without vfp.
173 if (CPUInfoContainsString("vfp") && CPUInfoContainsString("neon")) {
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000174 return true;
175 }
176 }
177
178 return false;
179}
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000180
181
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000182CpuImplementer OS::GetCpuImplementer() {
183 static bool use_cached_value = false;
184 static CpuImplementer cached_value = UNKNOWN_IMPLEMENTER;
185 if (use_cached_value) {
186 return cached_value;
187 }
188 if (CPUInfoContainsString("CPU implementer\t: 0x41")) {
189 cached_value = ARM_IMPLEMENTER;
190 } else if (CPUInfoContainsString("CPU implementer\t: 0x51")) {
191 cached_value = QUALCOMM_IMPLEMENTER;
192 } else {
193 cached_value = UNKNOWN_IMPLEMENTER;
194 }
195 use_cached_value = true;
196 return cached_value;
197}
198
199
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000200bool OS::ArmUsingHardFloat() {
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +0000201 // GCC versions 4.6 and above define __ARM_PCS or __ARM_PCS_VFP to specify
202 // the Floating Point ABI used (PCS stands for Procedure Call Standard).
203 // We use these as well as a couple of other defines to statically determine
204 // what FP ABI used.
205 // GCC versions 4.4 and below don't support hard-fp.
206 // GCC versions 4.5 may support hard-fp without defining __ARM_PCS or
207 // __ARM_PCS_VFP.
208
209#define GCC_VERSION (__GNUC__ * 10000 \
210 + __GNUC_MINOR__ * 100 \
211 + __GNUC_PATCHLEVEL__)
212#if GCC_VERSION >= 40600
213#if defined(__ARM_PCS_VFP)
214 return true;
215#else
216 return false;
217#endif
218
219#elif GCC_VERSION < 40500
220 return false;
221
222#else
223#if defined(__ARM_PCS_VFP)
224 return true;
225#elif defined(__ARM_PCS) || defined(__SOFTFP) || !defined(__VFP_FP__)
226 return false;
227#else
228#error "Your version of GCC does not report the FP ABI compiled for." \
229 "Please report it on this issue" \
230 "http://code.google.com/p/v8/issues/detail?id=2140"
231
232#endif
233#endif
234#undef GCC_VERSION
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000235}
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +0000236
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000237#endif // def __arm__
238
239
lrn@chromium.org7516f052011-03-30 08:52:27 +0000240#ifdef __mips__
241bool OS::MipsCpuHasFeature(CpuFeature feature) {
242 const char* search_string = NULL;
243 const char* file_name = "/proc/cpuinfo";
244 // Simple detection of FPU at runtime for Linux.
245 // It is based on /proc/cpuinfo, which reveals hardware configuration
246 // to user-space applications. According to MIPS (early 2010), no similar
247 // facility is universally available on the MIPS architectures,
248 // so it's up to individual OSes to provide such.
249 //
250 // This is written as a straight shot one pass parser
251 // and not using STL string and ifstream because,
252 // on Linux, it's reading from a (non-mmap-able)
253 // character special device.
254
255 switch (feature) {
256 case FPU:
257 search_string = "FPU";
258 break;
259 default:
260 UNREACHABLE();
261 }
262
263 FILE* f = NULL;
264 const char* what = search_string;
265
266 if (NULL == (f = fopen(file_name, "r")))
267 return false;
268
269 int k;
270 while (EOF != (k = fgetc(f))) {
271 if (k == *what) {
272 ++what;
273 while ((*what != '\0') && (*what == fgetc(f))) {
274 ++what;
275 }
276 if (*what == '\0') {
277 fclose(f);
278 return true;
279 } else {
280 what = search_string;
281 }
282 }
283 }
284 fclose(f);
285
286 // Did not find string in the proc file.
287 return false;
288}
289#endif // def __mips__
290
291
ager@chromium.org236ad962008-09-25 09:45:57 +0000292int OS::ActivationFrameAlignment() {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000293#ifdef V8_TARGET_ARCH_ARM
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000294 // On EABI ARM targets this is required for fp correctness in the
295 // runtime system.
ager@chromium.org3a6061e2009-03-12 14:24:36 +0000296 return 8;
ager@chromium.org5c838252010-02-19 08:53:10 +0000297#elif V8_TARGET_ARCH_MIPS
298 return 8;
299#endif
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000300 // With gcc 4.4 the tree vectorization optimizer can generate code
ager@chromium.orge2902be2009-06-08 12:21:35 +0000301 // that requires 16 byte alignment such as movdqa on x86.
302 return 16;
ager@chromium.org236ad962008-09-25 09:45:57 +0000303}
304
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000305
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000306void OS::ReleaseStore(volatile AtomicWord* ptr, AtomicWord value) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000307#if (defined(V8_TARGET_ARCH_ARM) && defined(__arm__)) || \
308 (defined(V8_TARGET_ARCH_MIPS) && defined(__mips__))
309 // Only use on ARM or MIPS hardware.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000310 MemoryBarrier();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000311#else
312 __asm__ __volatile__("" : : : "memory");
313 // An x86 store acts as a release barrier.
314#endif
315 *ptr = value;
316}
317
318
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000319const char* OS::LocalTimezone(double time) {
320 if (isnan(time)) return "";
321 time_t tv = static_cast<time_t>(floor(time/msPerSecond));
322 struct tm* t = localtime(&tv);
323 if (NULL == t) return "";
324 return t->tm_zone;
325}
326
327
328double OS::LocalTimeOffset() {
329 time_t tv = time(NULL);
330 struct tm* t = localtime(&tv);
331 // tm_gmtoff includes any daylight savings offset, so subtract it.
332 return static_cast<double>(t->tm_gmtoff * msPerSecond -
333 (t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
334}
335
336
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000337// We keep the lowest and highest addresses mapped as a quick way of
338// determining that pointers are outside the heap (used mostly in assertions
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000339// and verification). The estimate is conservative, i.e., not all addresses in
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000340// 'allocated' space are actually allocated to our heap. The range is
341// [lowest, highest), inclusive on the low and and exclusive on the high end.
342static void* lowest_ever_allocated = reinterpret_cast<void*>(-1);
343static void* highest_ever_allocated = reinterpret_cast<void*>(0);
344
345
346static void UpdateAllocatedSpaceLimits(void* address, int size) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000347 ASSERT(limit_mutex != NULL);
348 ScopedLock lock(limit_mutex);
349
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000350 lowest_ever_allocated = Min(lowest_ever_allocated, address);
351 highest_ever_allocated =
352 Max(highest_ever_allocated,
353 reinterpret_cast<void*>(reinterpret_cast<char*>(address) + size));
354}
355
356
357bool OS::IsOutsideAllocatedSpace(void* address) {
358 return address < lowest_ever_allocated || address >= highest_ever_allocated;
359}
360
361
362size_t OS::AllocateAlignment() {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000363 return sysconf(_SC_PAGESIZE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000364}
365
366
kasper.lund7276f142008-07-30 08:49:36 +0000367void* OS::Allocate(const size_t requested,
368 size_t* allocated,
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000369 bool is_executable) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000370 const size_t msize = RoundUp(requested, AllocateAlignment());
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000371 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000372 void* addr = OS::GetRandomMmapAddr();
ricow@chromium.org9fa09672011-07-25 11:05:35 +0000373 void* mbase = mmap(addr, msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000374 if (mbase == MAP_FAILED) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000375 LOG(i::Isolate::Current(),
376 StringEvent("OS::Allocate", "mmap failed"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000377 return NULL;
378 }
379 *allocated = msize;
380 UpdateAllocatedSpaceLimits(mbase, msize);
381 return mbase;
382}
383
384
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000385void OS::Free(void* address, const size_t size) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000386 // TODO(1240712): munmap has a return value which is ignored here.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000387 int result = munmap(address, size);
388 USE(result);
389 ASSERT(result == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000390}
391
392
393void OS::Sleep(int milliseconds) {
394 unsigned int ms = static_cast<unsigned int>(milliseconds);
395 usleep(1000 * ms);
396}
397
398
399void OS::Abort() {
400 // Redirect to std abort to signal abnormal program termination.
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000401 if (FLAG_break_on_abort) {
402 DebugBreak();
403 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000404 abort();
405}
406
407
kasper.lund7276f142008-07-30 08:49:36 +0000408void OS::DebugBreak() {
ager@chromium.org5ec48922009-05-05 07:25:34 +0000409// TODO(lrn): Introduce processor define for runtime system (!= V8_ARCH_x,
410// which is the architecture of generated code).
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000411#if (defined(__arm__) || defined(__thumb__))
412# if defined(CAN_USE_ARMV5_INSTRUCTIONS)
kasper.lund7276f142008-07-30 08:49:36 +0000413 asm("bkpt 0");
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000414# endif
ager@chromium.org5c838252010-02-19 08:53:10 +0000415#elif defined(__mips__)
416 asm("break");
kasper.lund7276f142008-07-30 08:49:36 +0000417#else
418 asm("int $3");
419#endif
420}
421
422
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000423void OS::DumpBacktrace() {
424#if defined(__GLIBC__)
425 void* trace[100];
426 int size = backtrace(trace, ARRAY_SIZE(trace));
427 char** symbols = backtrace_symbols(trace, size);
428 fprintf(stderr, "\n==== C stack trace ===============================\n\n");
429 if (size == 0) {
430 fprintf(stderr, "(empty)\n");
431 } else if (symbols == NULL) {
432 fprintf(stderr, "(no symbols)\n");
433 } else {
434 for (int i = 1; i < size; ++i) {
435 fprintf(stderr, "%2d: ", i);
436 char mangled[201];
437 if (sscanf(symbols[i], "%*[^(]%*[(]%200[^)+]", mangled) == 1) { // NOLINT
438 int status;
439 size_t length;
440 char* demangled = abi::__cxa_demangle(mangled, NULL, &length, &status);
441 fprintf(stderr, "%s\n", demangled ? demangled : mangled);
442 free(demangled);
443 } else {
444 fprintf(stderr, "??\n");
445 }
446 }
447 }
448 fflush(stderr);
449 free(symbols);
450#endif
451}
452
453
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000454class PosixMemoryMappedFile : public OS::MemoryMappedFile {
455 public:
456 PosixMemoryMappedFile(FILE* file, void* memory, int size)
457 : file_(file), memory_(memory), size_(size) { }
458 virtual ~PosixMemoryMappedFile();
459 virtual void* memory() { return memory_; }
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000460 virtual int size() { return size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000461 private:
462 FILE* file_;
463 void* memory_;
464 int size_;
465};
466
467
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000468OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000469 FILE* file = fopen(name, "r+");
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000470 if (file == NULL) return NULL;
471
472 fseek(file, 0, SEEK_END);
473 int size = ftell(file);
474
475 void* memory =
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000476 mmap(OS::GetRandomMmapAddr(),
477 size,
478 PROT_READ | PROT_WRITE,
479 MAP_SHARED,
480 fileno(file),
481 0);
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000482 return new PosixMemoryMappedFile(file, memory, size);
483}
484
485
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000486OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size,
487 void* initial) {
488 FILE* file = fopen(name, "w+");
489 if (file == NULL) return NULL;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000490 int result = fwrite(initial, size, 1, file);
491 if (result < 1) {
492 fclose(file);
493 return NULL;
494 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000495 void* memory =
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000496 mmap(OS::GetRandomMmapAddr(),
497 size,
498 PROT_READ | PROT_WRITE,
499 MAP_SHARED,
500 fileno(file),
501 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000502 return new PosixMemoryMappedFile(file, memory, size);
503}
504
505
506PosixMemoryMappedFile::~PosixMemoryMappedFile() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000507 if (memory_) OS::Free(memory_, size_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000508 fclose(file_);
509}
510
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000511
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000512void OS::LogSharedLibraryAddresses() {
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000513 // This function assumes that the layout of the file is as follows:
514 // hex_start_addr-hex_end_addr rwxp <unused data> [binary_file_name]
515 // If we encounter an unexpected situation we abort scanning further entries.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000516 FILE* fp = fopen("/proc/self/maps", "r");
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000517 if (fp == NULL) return;
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000518
519 // Allocate enough room to be able to store a full file name.
520 const int kLibNameLen = FILENAME_MAX + 1;
521 char* lib_name = reinterpret_cast<char*>(malloc(kLibNameLen));
522
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000523 i::Isolate* isolate = ISOLATE;
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000524 // This loop will terminate once the scanning hits an EOF.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000525 while (true) {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000526 uintptr_t start, end;
527 char attr_r, attr_w, attr_x, attr_p;
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000528 // Parse the addresses and permission bits at the beginning of the line.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000529 if (fscanf(fp, "%" V8PRIxPTR "-%" V8PRIxPTR, &start, &end) != 2) break;
530 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 +0000531
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000532 int c;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000533 if (attr_r == 'r' && attr_w != 'w' && attr_x == 'x') {
534 // Found a read-only executable entry. Skip characters until we reach
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000535 // the beginning of the filename or the end of the line.
536 do {
537 c = getc(fp);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000538 } while ((c != EOF) && (c != '\n') && (c != '/') && (c != '['));
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000539 if (c == EOF) break; // EOF: Was unexpected, just exit.
540
541 // Process the filename if found.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000542 if ((c == '/') || (c == '[')) {
543 // Push the '/' or '[' back into the stream to be read below.
544 ungetc(c, fp);
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000545
546 // Read to the end of the line. Exit if the read fails.
547 if (fgets(lib_name, kLibNameLen, fp) == NULL) break;
548
549 // Drop the newline character read by fgets. We do not need to check
550 // for a zero-length string because we know that we at least read the
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000551 // '/' or '[' character.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000552 lib_name[strlen(lib_name) - 1] = '\0';
553 } else {
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000554 // No library name found, just record the raw address range.
555 snprintf(lib_name, kLibNameLen,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000556 "%08" V8PRIxPTR "-%08" V8PRIxPTR, start, end);
557 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000558 LOG(isolate, SharedLibraryEvent(lib_name, start, end));
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000559 } else {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000560 // Entry not describing executable data. Skip to end of line to set up
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000561 // reading the next entry.
562 do {
563 c = getc(fp);
564 } while ((c != EOF) && (c != '\n'));
565 if (c == EOF) break;
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000566 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000567 }
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000568 free(lib_name);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000569 fclose(fp);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000570}
571
572
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000573void OS::SignalCodeMovingGC() {
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000574 // Support for ll_prof.py.
575 //
576 // The Linux profiler built into the kernel logs all mmap's with
577 // PROT_EXEC so that analysis tools can properly attribute ticks. We
578 // do a mmap with a name known by ll_prof.py and immediately munmap
579 // it. This injects a GC marker into the stream of events generated
580 // by the kernel and allows us to synchronize V8 code log and the
581 // kernel log.
582 int size = sysconf(_SC_PAGESIZE);
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +0000583 FILE* f = fopen(FLAG_gc_fake_mmap, "w+");
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000584 void* addr = mmap(OS::GetRandomMmapAddr(),
585 size,
586 PROT_READ | PROT_EXEC,
587 MAP_PRIVATE,
588 fileno(f),
589 0);
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000590 ASSERT(addr != MAP_FAILED);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000591 OS::Free(addr, size);
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000592 fclose(f);
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000593}
594
595
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000596int OS::StackWalk(Vector<OS::StackFrame> frames) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000597 // backtrace is a glibc extension.
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000598#if defined(__GLIBC__) && !defined(__UCLIBC__)
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000599 int frames_size = frames.length();
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000600 ScopedVector<void*> addresses(frames_size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000601
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000602 int frames_count = backtrace(addresses.start(), frames_size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000603
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000604 char** symbols = backtrace_symbols(addresses.start(), frames_count);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000605 if (symbols == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000606 return kStackWalkError;
607 }
608
609 for (int i = 0; i < frames_count; i++) {
610 frames[i].address = addresses[i];
611 // Format a text representation of the frame based on the information
612 // available.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000613 SNPrintF(MutableCStrVector(frames[i].text, kStackWalkMaxTextLen),
614 "%s",
615 symbols[i]);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000616 // Make sure line termination is in place.
617 frames[i].text[kStackWalkMaxTextLen - 1] = '\0';
618 }
619
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000620 free(symbols);
621
622 return frames_count;
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000623#else // defined(__GLIBC__) && !defined(__UCLIBC__)
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000624 return 0;
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000625#endif // defined(__GLIBC__) && !defined(__UCLIBC__)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000626}
627
628
629// Constants used for mmap.
630static const int kMmapFd = -1;
631static const int kMmapFdOffset = 0;
632
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000633VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000634
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000635VirtualMemory::VirtualMemory(size_t size) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000636 address_ = ReserveRegion(size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000637 size_ = size;
638}
639
640
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000641VirtualMemory::VirtualMemory(size_t size, size_t alignment)
642 : address_(NULL), size_(0) {
643 ASSERT(IsAligned(alignment, static_cast<intptr_t>(OS::AllocateAlignment())));
644 size_t request_size = RoundUp(size + alignment,
645 static_cast<intptr_t>(OS::AllocateAlignment()));
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000646 void* reservation = mmap(OS::GetRandomMmapAddr(),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000647 request_size,
648 PROT_NONE,
649 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE,
650 kMmapFd,
651 kMmapFdOffset);
652 if (reservation == MAP_FAILED) return;
653
654 Address base = static_cast<Address>(reservation);
655 Address aligned_base = RoundUp(base, alignment);
656 ASSERT_LE(base, aligned_base);
657
658 // Unmap extra memory reserved before and after the desired block.
659 if (aligned_base != base) {
660 size_t prefix_size = static_cast<size_t>(aligned_base - base);
661 OS::Free(base, prefix_size);
662 request_size -= prefix_size;
663 }
664
665 size_t aligned_size = RoundUp(size, OS::AllocateAlignment());
666 ASSERT_LE(aligned_size, request_size);
667
668 if (aligned_size != request_size) {
669 size_t suffix_size = request_size - aligned_size;
670 OS::Free(aligned_base + aligned_size, suffix_size);
671 request_size -= suffix_size;
672 }
673
674 ASSERT(aligned_size == request_size);
675
676 address_ = static_cast<void*>(aligned_base);
677 size_ = aligned_size;
678}
679
680
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000681VirtualMemory::~VirtualMemory() {
682 if (IsReserved()) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000683 bool result = ReleaseRegion(address(), size());
684 ASSERT(result);
685 USE(result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000686 }
687}
688
689
690bool VirtualMemory::IsReserved() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000691 return address_ != NULL;
692}
693
694
695void VirtualMemory::Reset() {
696 address_ = NULL;
697 size_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000698}
699
700
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000701bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000702 return CommitRegion(address, size, is_executable);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000703}
704
705
706bool VirtualMemory::Uncommit(void* address, size_t size) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000707 return UncommitRegion(address, size);
708}
709
710
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000711bool VirtualMemory::Guard(void* address) {
712 OS::Guard(address, OS::CommitPageSize());
713 return true;
714}
715
716
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000717void* VirtualMemory::ReserveRegion(size_t size) {
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000718 void* result = mmap(OS::GetRandomMmapAddr(),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000719 size,
720 PROT_NONE,
721 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE,
722 kMmapFd,
723 kMmapFdOffset);
724
725 if (result == MAP_FAILED) return NULL;
726
727 return result;
728}
729
730
731bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) {
732 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
733 if (MAP_FAILED == mmap(base,
734 size,
735 prot,
736 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
737 kMmapFd,
738 kMmapFdOffset)) {
739 return false;
740 }
741
742 UpdateAllocatedSpaceLimits(base, size);
743 return true;
744}
745
746
747bool VirtualMemory::UncommitRegion(void* base, size_t size) {
748 return mmap(base,
749 size,
750 PROT_NONE,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000751 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE | MAP_FIXED,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000752 kMmapFd,
753 kMmapFdOffset) != MAP_FAILED;
754}
755
756
757bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
758 return munmap(base, size) == 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000759}
760
761
danno@chromium.org72204d52012-10-31 10:02:10 +0000762bool VirtualMemory::HasLazyCommits() {
763 return true;
764}
765
766
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000767class Thread::PlatformData : public Malloced {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000768 public:
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000769 PlatformData() : thread_(kNoThread) {}
ager@chromium.org41826e72009-03-30 13:30:57 +0000770
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000771 pthread_t thread_; // Thread handle for pthread.
772};
773
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000774Thread::Thread(const Options& options)
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000775 : data_(new PlatformData()),
yangguo@chromium.org659ceec2012-01-26 07:37:54 +0000776 stack_size_(options.stack_size()) {
777 set_name(options.name());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000778}
779
780
781Thread::~Thread() {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000782 delete data_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000783}
784
785
786static void* ThreadEntry(void* arg) {
787 Thread* thread = reinterpret_cast<Thread*>(arg);
788 // This is also initialized by the first argument to pthread_create() but we
789 // don't know which thread will run first (the original thread or the new
790 // one) so we initialize it here too.
danno@chromium.orgb6451162011-08-17 14:33:23 +0000791#ifdef PR_SET_NAME
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000792 prctl(PR_SET_NAME,
793 reinterpret_cast<unsigned long>(thread->name()), // NOLINT
794 0, 0, 0);
danno@chromium.orgb6451162011-08-17 14:33:23 +0000795#endif
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000796 thread->data()->thread_ = pthread_self();
797 ASSERT(thread->data()->thread_ != kNoThread);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000798 thread->Run();
799 return NULL;
800}
801
802
lrn@chromium.org5d00b602011-01-05 09:51:43 +0000803void Thread::set_name(const char* name) {
804 strncpy(name_, name, sizeof(name_));
805 name_[sizeof(name_) - 1] = '\0';
806}
807
808
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000809void Thread::Start() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000810 pthread_attr_t* attr_ptr = NULL;
811 pthread_attr_t attr;
812 if (stack_size_ > 0) {
813 pthread_attr_init(&attr);
814 pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_));
815 attr_ptr = &attr;
816 }
danno@chromium.orgc612e022011-11-10 11:38:15 +0000817 int result = pthread_create(&data_->thread_, attr_ptr, ThreadEntry, this);
818 CHECK_EQ(0, result);
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000819 ASSERT(data_->thread_ != kNoThread);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000820}
821
822
823void Thread::Join() {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000824 pthread_join(data_->thread_, NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000825}
826
827
828Thread::LocalStorageKey Thread::CreateThreadLocalKey() {
829 pthread_key_t key;
830 int result = pthread_key_create(&key, NULL);
831 USE(result);
832 ASSERT(result == 0);
833 return static_cast<LocalStorageKey>(key);
834}
835
836
837void Thread::DeleteThreadLocalKey(LocalStorageKey key) {
838 pthread_key_t pthread_key = static_cast<pthread_key_t>(key);
839 int result = pthread_key_delete(pthread_key);
840 USE(result);
841 ASSERT(result == 0);
842}
843
844
845void* Thread::GetThreadLocal(LocalStorageKey key) {
846 pthread_key_t pthread_key = static_cast<pthread_key_t>(key);
847 return pthread_getspecific(pthread_key);
848}
849
850
851void Thread::SetThreadLocal(LocalStorageKey key, void* value) {
852 pthread_key_t pthread_key = static_cast<pthread_key_t>(key);
853 pthread_setspecific(pthread_key, value);
854}
855
856
857void Thread::YieldCPU() {
858 sched_yield();
859}
860
861
862class LinuxMutex : public Mutex {
863 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000864 LinuxMutex() {
865 pthread_mutexattr_t attrs;
866 int result = pthread_mutexattr_init(&attrs);
867 ASSERT(result == 0);
868 result = pthread_mutexattr_settype(&attrs, PTHREAD_MUTEX_RECURSIVE);
869 ASSERT(result == 0);
870 result = pthread_mutex_init(&mutex_, &attrs);
871 ASSERT(result == 0);
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000872 USE(result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000873 }
874
875 virtual ~LinuxMutex() { pthread_mutex_destroy(&mutex_); }
876
877 virtual int Lock() {
878 int result = pthread_mutex_lock(&mutex_);
879 return result;
880 }
881
882 virtual int Unlock() {
883 int result = pthread_mutex_unlock(&mutex_);
884 return result;
885 }
886
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000887 virtual bool TryLock() {
888 int result = pthread_mutex_trylock(&mutex_);
889 // Return false if the lock is busy and locking failed.
890 if (result == EBUSY) {
891 return false;
892 }
893 ASSERT(result == 0); // Verify no other errors.
894 return true;
895 }
896
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000897 private:
898 pthread_mutex_t mutex_; // Pthread mutex for POSIX platforms.
899};
900
901
902Mutex* OS::CreateMutex() {
903 return new LinuxMutex();
904}
905
906
907class LinuxSemaphore : public Semaphore {
908 public:
909 explicit LinuxSemaphore(int count) { sem_init(&sem_, 0, count); }
910 virtual ~LinuxSemaphore() { sem_destroy(&sem_); }
911
kasper.lund7276f142008-07-30 08:49:36 +0000912 virtual void Wait();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000913 virtual bool Wait(int timeout);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000914 virtual void Signal() { sem_post(&sem_); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000915 private:
916 sem_t sem_;
917};
918
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000919
kasper.lund7276f142008-07-30 08:49:36 +0000920void LinuxSemaphore::Wait() {
921 while (true) {
922 int result = sem_wait(&sem_);
923 if (result == 0) return; // Successfully got semaphore.
924 CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup.
925 }
926}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000927
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000928
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000929#ifndef TIMEVAL_TO_TIMESPEC
930#define TIMEVAL_TO_TIMESPEC(tv, ts) do { \
931 (ts)->tv_sec = (tv)->tv_sec; \
932 (ts)->tv_nsec = (tv)->tv_usec * 1000; \
933} while (false)
934#endif
935
936
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000937bool LinuxSemaphore::Wait(int timeout) {
938 const long kOneSecondMicros = 1000000; // NOLINT
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000939
940 // Split timeout into second and nanosecond parts.
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000941 struct timeval delta;
942 delta.tv_usec = timeout % kOneSecondMicros;
943 delta.tv_sec = timeout / kOneSecondMicros;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000944
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000945 struct timeval current_time;
946 // Get the current time.
947 if (gettimeofday(&current_time, NULL) == -1) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000948 return false;
949 }
950
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000951 // Calculate time for end of timeout.
952 struct timeval end_time;
953 timeradd(&current_time, &delta, &end_time);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000954
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000955 struct timespec ts;
956 TIMEVAL_TO_TIMESPEC(&end_time, &ts);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000957 // Wait for semaphore signalled or timeout.
958 while (true) {
959 int result = sem_timedwait(&sem_, &ts);
960 if (result == 0) return true; // Successfully got semaphore.
961 if (result > 0) {
962 // For glibc prior to 2.3.4 sem_timedwait returns the error instead of -1.
963 errno = result;
964 result = -1;
965 }
966 if (result == -1 && errno == ETIMEDOUT) return false; // Timeout.
967 CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup.
968 }
969}
970
971
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000972Semaphore* OS::CreateSemaphore(int count) {
973 return new LinuxSemaphore(count);
974}
975
ager@chromium.org381abbb2009-02-25 13:23:22 +0000976
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000977#if defined(__ANDROID__) && !defined(__BIONIC_HAVE_UCONTEXT_T)
kasperl@chromium.orgacae3782009-04-11 09:17:08 +0000978
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000979// Not all versions of Android's C library provide ucontext_t.
980// Detect this and provide custom but compatible definitions. Note that these
981// follow the GLibc naming convention to access register values from
982// mcontext_t.
983//
984// See http://code.google.com/p/android/issues/detail?id=34784
985
986#if defined(__arm__)
987
kasperl@chromium.orgacae3782009-04-11 09:17:08 +0000988typedef struct sigcontext mcontext_t;
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000989
kasperl@chromium.orgacae3782009-04-11 09:17:08 +0000990typedef struct ucontext {
991 uint32_t uc_flags;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000992 struct ucontext* uc_link;
kasperl@chromium.orgacae3782009-04-11 09:17:08 +0000993 stack_t uc_stack;
994 mcontext_t uc_mcontext;
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000995 // Other fields are not used by V8, don't define them here.
kasperl@chromium.orgacae3782009-04-11 09:17:08 +0000996} ucontext_t;
kasperl@chromium.orgacae3782009-04-11 09:17:08 +0000997
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000998#elif defined(__mips__)
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000999// MIPS version of sigcontext, for Android bionic.
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001000typedef struct {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001001 uint32_t regmask;
1002 uint32_t status;
1003 uint64_t pc;
1004 uint64_t gregs[32];
1005 uint64_t fpregs[32];
1006 uint32_t acx;
1007 uint32_t fpc_csr;
1008 uint32_t fpc_eir;
1009 uint32_t used_math;
1010 uint32_t dsp;
1011 uint64_t mdhi;
1012 uint64_t mdlo;
1013 uint32_t hi1;
1014 uint32_t lo1;
1015 uint32_t hi2;
1016 uint32_t lo2;
1017 uint32_t hi3;
1018 uint32_t lo3;
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001019} mcontext_t;
1020
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001021typedef struct ucontext {
1022 uint32_t uc_flags;
1023 struct ucontext* uc_link;
1024 stack_t uc_stack;
1025 mcontext_t uc_mcontext;
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001026 // Other fields are not used by V8, don't define them here.
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001027} ucontext_t;
1028
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001029#elif defined(__i386__)
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001030// x86 version for Android.
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001031typedef struct {
yangguo@chromium.orgcb9affa2012-05-15 12:16:38 +00001032 uint32_t gregs[19];
1033 void* fpregs;
1034 uint32_t oldmask;
1035 uint32_t cr2;
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001036} mcontext_t;
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001037
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001038typedef uint32_t kernel_sigset_t[2]; // x86 kernel uses 64-bit signal masks
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001039typedef struct ucontext {
yangguo@chromium.orgcb9affa2012-05-15 12:16:38 +00001040 uint32_t uc_flags;
1041 struct ucontext* uc_link;
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001042 stack_t uc_stack;
1043 mcontext_t uc_mcontext;
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001044 // Other fields are not used by V8, don't define them here.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001045} ucontext_t;
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001046enum { REG_EBP = 6, REG_ESP = 7, REG_EIP = 14 };
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001047#endif
1048
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001049#endif // __ANDROID__ && !defined(__BIONIC_HAVE_UCONTEXT_T)
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001050
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001051static int GetThreadID() {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001052#if defined(__ANDROID__)
1053 // Android's C library provides gettid(2).
1054 return gettid();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001055#else
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001056 // Glibc doesn't provide a wrapper for gettid(2).
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001057 return syscall(SYS_gettid);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001058#endif
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001059}
1060
1061
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001062static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) {
1063 USE(info);
1064 if (signal != SIGPROF) return;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001065 Isolate* isolate = Isolate::UncheckedCurrent();
1066 if (isolate == NULL || !isolate->IsInitialized() || !isolate->IsInUse()) {
1067 // We require a fully initialized and entered isolate.
1068 return;
1069 }
vitalyr@chromium.org0ec56d62011-04-15 22:22:08 +00001070 if (v8::Locker::IsActive() &&
1071 !isolate->thread_manager()->IsLockedByCurrentThread()) {
1072 return;
1073 }
1074
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001075 Sampler* sampler = isolate->logger()->sampler();
1076 if (sampler == NULL || !sampler->IsActive()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001077
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00001078 TickSample sample_obj;
mmassi@chromium.org49a44672012-12-04 13:52:03 +00001079 TickSample* sample = CpuProfiler::TickSampleEvent(isolate);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00001080 if (sample == NULL) sample = &sample_obj;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001081
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001082 // Extracting the sample from the context is extremely machine dependent.
1083 ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context);
1084 mcontext_t& mcontext = ucontext->uc_mcontext;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001085 sample->state = isolate->current_vm_state();
ager@chromium.org9085a012009-05-11 19:22:57 +00001086#if V8_HOST_ARCH_IA32
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001087 sample->pc = reinterpret_cast<Address>(mcontext.gregs[REG_EIP]);
1088 sample->sp = reinterpret_cast<Address>(mcontext.gregs[REG_ESP]);
1089 sample->fp = reinterpret_cast<Address>(mcontext.gregs[REG_EBP]);
ager@chromium.org9085a012009-05-11 19:22:57 +00001090#elif V8_HOST_ARCH_X64
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001091 sample->pc = reinterpret_cast<Address>(mcontext.gregs[REG_RIP]);
1092 sample->sp = reinterpret_cast<Address>(mcontext.gregs[REG_RSP]);
1093 sample->fp = reinterpret_cast<Address>(mcontext.gregs[REG_RBP]);
ager@chromium.org9085a012009-05-11 19:22:57 +00001094#elif V8_HOST_ARCH_ARM
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001095#if defined(__GLIBC__) && !defined(__UCLIBC__) && \
1096 (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3))
1097 // Old GLibc ARM versions used a gregs[] array to access the register
1098 // values from mcontext_t.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001099 sample->pc = reinterpret_cast<Address>(mcontext.gregs[R15]);
1100 sample->sp = reinterpret_cast<Address>(mcontext.gregs[R13]);
1101 sample->fp = reinterpret_cast<Address>(mcontext.gregs[R11]);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00001102#else
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001103 sample->pc = reinterpret_cast<Address>(mcontext.arm_pc);
1104 sample->sp = reinterpret_cast<Address>(mcontext.arm_sp);
1105 sample->fp = reinterpret_cast<Address>(mcontext.arm_fp);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001106#endif // defined(__GLIBC__) && !defined(__UCLIBC__) &&
1107 // (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3))
ager@chromium.org5c838252010-02-19 08:53:10 +00001108#elif V8_HOST_ARCH_MIPS
danno@chromium.orgc612e022011-11-10 11:38:15 +00001109 sample->pc = reinterpret_cast<Address>(mcontext.pc);
1110 sample->sp = reinterpret_cast<Address>(mcontext.gregs[29]);
1111 sample->fp = reinterpret_cast<Address>(mcontext.gregs[30]);
1112#endif // V8_HOST_ARCH_*
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001113 sampler->SampleStack(sample);
1114 sampler->Tick(sample);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001115}
1116
1117
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001118class Sampler::PlatformData : public Malloced {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001119 public:
mmassi@chromium.org49a44672012-12-04 13:52:03 +00001120 PlatformData() : vm_tid_(GetThreadID()) {}
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001121
mmassi@chromium.org49a44672012-12-04 13:52:03 +00001122 int vm_tid() const { return vm_tid_; }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001123
1124 private:
1125 const int vm_tid_;
1126};
1127
1128
1129class SignalSender : public Thread {
1130 public:
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001131 static const int kSignalSenderStackSize = 64 * KB;
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001132
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001133 explicit SignalSender(int interval)
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001134 : Thread(Thread::Options("SignalSender", kSignalSenderStackSize)),
mmassi@chromium.org49a44672012-12-04 13:52:03 +00001135 vm_tgid_(getpid()),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001136 interval_(interval) {}
1137
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001138 static void SetUp() { if (!mutex_) mutex_ = OS::CreateMutex(); }
1139 static void TearDown() { delete mutex_; }
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001140
mmassi@chromium.org49a44672012-12-04 13:52:03 +00001141 static void InstallSignalHandler() {
1142 struct sigaction sa;
1143 sa.sa_sigaction = ProfilerSignalHandler;
1144 sigemptyset(&sa.sa_mask);
1145 sa.sa_flags = SA_RESTART | SA_SIGINFO;
1146 signal_handler_installed_ =
1147 (sigaction(SIGPROF, &sa, &old_signal_handler_) == 0);
1148 }
1149
1150 static void RestoreSignalHandler() {
1151 if (signal_handler_installed_) {
1152 sigaction(SIGPROF, &old_signal_handler_, 0);
1153 signal_handler_installed_ = false;
1154 }
1155 }
1156
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001157 static void AddActiveSampler(Sampler* sampler) {
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001158 ScopedLock lock(mutex_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001159 SamplerRegistry::AddActiveSampler(sampler);
1160 if (instance_ == NULL) {
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001161 // Start a thread that will send SIGPROF signal to VM threads,
1162 // when CPU profiling will be enabled.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001163 instance_ = new SignalSender(sampler->interval());
1164 instance_->Start();
1165 } else {
1166 ASSERT(instance_->interval_ == sampler->interval());
1167 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001168 }
1169
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001170 static void RemoveActiveSampler(Sampler* sampler) {
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001171 ScopedLock lock(mutex_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001172 SamplerRegistry::RemoveActiveSampler(sampler);
1173 if (SamplerRegistry::GetState() == SamplerRegistry::HAS_NO_SAMPLERS) {
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001174 RuntimeProfiler::StopRuntimeProfilerThreadBeforeShutdown(instance_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001175 delete instance_;
1176 instance_ = NULL;
mmassi@chromium.org49a44672012-12-04 13:52:03 +00001177 RestoreSignalHandler();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001178 }
1179 }
1180
1181 // Implement Thread::Run().
1182 virtual void Run() {
1183 SamplerRegistry::State state;
1184 while ((state = SamplerRegistry::GetState()) !=
1185 SamplerRegistry::HAS_NO_SAMPLERS) {
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00001186 // When CPU profiling is enabled both JavaScript and C++ code is
1187 // profiled. We must not suspend.
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001188 if (state == SamplerRegistry::HAS_CPU_PROFILING_SAMPLERS) {
1189 if (!signal_handler_installed_) InstallSignalHandler();
1190 SamplerRegistry::IterateActiveSamplers(&DoCpuProfile, this);
1191 } else {
1192 if (signal_handler_installed_) RestoreSignalHandler();
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001193 if (RuntimeProfiler::WaitForSomeIsolateToEnterJS()) continue;
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00001194 }
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001195 Sleep(); // TODO(svenpanne) Figure out if OS:Sleep(interval_) is enough.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001196 }
1197 }
1198
mmassi@chromium.org49a44672012-12-04 13:52:03 +00001199 static void DoCpuProfile(Sampler* sampler, void* raw_sender) {
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00001200 if (!sampler->IsProfiling()) return;
mmassi@chromium.org49a44672012-12-04 13:52:03 +00001201 SignalSender* sender = reinterpret_cast<SignalSender*>(raw_sender);
1202 sender->SendProfilingSignal(sampler->platform_data()->vm_tid());
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00001203 }
1204
mmassi@chromium.org49a44672012-12-04 13:52:03 +00001205 void SendProfilingSignal(int tid) {
1206 if (!signal_handler_installed_) return;
1207 // Glibc doesn't provide a wrapper for tgkill(2).
1208#if defined(ANDROID)
1209 syscall(__NR_tgkill, vm_tgid_, tid, SIGPROF);
1210#else
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001211 int result = syscall(SYS_tgkill, vm_tgid_, tid, SIGPROF);
1212 USE(result);
1213 ASSERT(result == 0);
mmassi@chromium.org49a44672012-12-04 13:52:03 +00001214#endif
1215 }
1216
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001217 void Sleep() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001218 // Convert ms to us and subtract 100 us to compensate delays
1219 // occuring during signal delivery.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001220 useconds_t interval = interval_ * 1000 - 100;
ulan@chromium.org2efb9002012-01-19 15:36:35 +00001221#if defined(ANDROID)
1222 usleep(interval);
1223#else
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001224 int result = usleep(interval);
1225#ifdef DEBUG
1226 if (result != 0 && errno != EINTR) {
1227 fprintf(stderr,
1228 "SignalSender usleep error; interval = %u, errno = %d\n",
1229 interval,
1230 errno);
1231 ASSERT(result == 0 || errno == EINTR);
1232 }
ulan@chromium.org2efb9002012-01-19 15:36:35 +00001233#endif // DEBUG
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001234 USE(result);
ulan@chromium.org2efb9002012-01-19 15:36:35 +00001235#endif // ANDROID
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001236 }
1237
mmassi@chromium.org49a44672012-12-04 13:52:03 +00001238 const int vm_tgid_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001239 const int interval_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001240
1241 // Protects the process wide state below.
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001242 static Mutex* mutex_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001243 static SignalSender* instance_;
mmassi@chromium.org49a44672012-12-04 13:52:03 +00001244 static bool signal_handler_installed_;
1245 static struct sigaction old_signal_handler_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001246
jkummerow@chromium.org05ed9dd2012-01-23 14:42:48 +00001247 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001248 DISALLOW_COPY_AND_ASSIGN(SignalSender);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001249};
1250
1251
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001252Mutex* SignalSender::mutex_ = NULL;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001253SignalSender* SignalSender::instance_ = NULL;
mmassi@chromium.org49a44672012-12-04 13:52:03 +00001254struct sigaction SignalSender::old_signal_handler_;
1255bool SignalSender::signal_handler_installed_ = false;
lrn@chromium.org303ada72010-10-27 09:33:13 +00001256
1257
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001258void OS::SetUp() {
1259 // Seed the random number generator. We preserve microsecond resolution.
1260 uint64_t seed = Ticks() ^ (getpid() << 16);
1261 srandom(static_cast<unsigned int>(seed));
1262 limit_mutex = CreateMutex();
1263
1264#ifdef __arm__
1265 // When running on ARM hardware check that the EABI used by V8 and
1266 // by the C code is the same.
1267 bool hard_float = OS::ArmUsingHardFloat();
1268 if (hard_float) {
1269#if !USE_EABI_HARDFLOAT
1270 PrintF("ERROR: Binary compiled with -mfloat-abi=hard but without "
1271 "-DUSE_EABI_HARDFLOAT\n");
1272 exit(1);
1273#endif
1274 } else {
1275#if USE_EABI_HARDFLOAT
1276 PrintF("ERROR: Binary not compiled with -mfloat-abi=hard but with "
1277 "-DUSE_EABI_HARDFLOAT\n");
1278 exit(1);
1279#endif
1280 }
1281#endif
1282 SignalSender::SetUp();
1283}
1284
1285
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001286void OS::TearDown() {
1287 SignalSender::TearDown();
1288 delete limit_mutex;
1289}
1290
1291
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001292Sampler::Sampler(Isolate* isolate, int interval)
1293 : isolate_(isolate),
1294 interval_(interval),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001295 profiling_(false),
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001296 active_(false),
1297 samples_taken_(0) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001298 data_ = new PlatformData;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001299}
1300
1301
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001302Sampler::~Sampler() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001303 ASSERT(!IsActive());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001304 delete data_;
1305}
1306
1307
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001308void Sampler::Start() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001309 ASSERT(!IsActive());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001310 SetActive(true);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001311 SignalSender::AddActiveSampler(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001312}
1313
1314
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001315void Sampler::Stop() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001316 ASSERT(IsActive());
1317 SignalSender::RemoveActiveSampler(this);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001318 SetActive(false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001319}
1320
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001321
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001322} } // namespace v8::internal