2012-01-20 16:17:08 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2009-12-02 13:38:50 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
2012-01-05 12:17:08 +00:00
|
|
|
// Platform specific code for OpenBSD and NetBSD goes here. For the POSIX
|
|
|
|
// comaptible parts the implementation is in platform-posix.cc.
|
2009-12-02 13:38:50 +00:00
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <semaphore.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
2011-11-09 12:01:45 +00:00
|
|
|
#include <sys/syscall.h>
|
2009-12-02 13:38:50 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <sys/types.h> // mmap & munmap
|
|
|
|
#include <sys/mman.h> // mmap & munmap
|
|
|
|
#include <sys/stat.h> // open
|
2011-11-09 12:01:45 +00:00
|
|
|
#include <fcntl.h> // open
|
|
|
|
#include <unistd.h> // sysconf
|
2009-12-02 13:38:50 +00:00
|
|
|
#include <execinfo.h> // backtrace, backtrace_symbols
|
|
|
|
#include <strings.h> // index
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#undef MAP_TYPE
|
|
|
|
|
|
|
|
#include "v8.h"
|
|
|
|
|
2012-03-30 14:30:46 +00:00
|
|
|
#include "platform-posix.h"
|
2009-12-02 13:38:50 +00:00
|
|
|
#include "platform.h"
|
2011-11-09 12:01:45 +00:00
|
|
|
#include "v8threads.h"
|
2010-12-07 11:31:57 +00:00
|
|
|
#include "vm-state-inl.h"
|
2009-12-02 13:38:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
2011-11-09 12:01:45 +00:00
|
|
|
// 0 is never a valid thread id on Linux and OpenBSD since tids and pids share a
|
|
|
|
// name space and pid 0 is reserved (see man 2 kill).
|
2009-12-02 13:38:50 +00:00
|
|
|
static const pthread_t kNoThread = (pthread_t) 0;
|
|
|
|
|
|
|
|
|
|
|
|
double ceiling(double x) {
|
2011-11-09 12:01:45 +00:00
|
|
|
return ceil(x);
|
2009-12-02 13:38:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-01 11:09:32 +00:00
|
|
|
static Mutex* limit_mutex = NULL;
|
|
|
|
|
|
|
|
|
2011-11-09 12:01:45 +00:00
|
|
|
static void* GetRandomMmapAddr() {
|
|
|
|
Isolate* isolate = Isolate::UncheckedCurrent();
|
|
|
|
// Note that the current isolate isn't set up in a call path via
|
|
|
|
// CpuFeatures::Probe. We don't care about randomization in this case because
|
|
|
|
// the code page is immediately freed.
|
|
|
|
if (isolate != NULL) {
|
|
|
|
#ifdef V8_TARGET_ARCH_X64
|
|
|
|
uint64_t rnd1 = V8::RandomPrivate(isolate);
|
|
|
|
uint64_t rnd2 = V8::RandomPrivate(isolate);
|
|
|
|
uint64_t raw_addr = (rnd1 << 32) ^ rnd2;
|
|
|
|
// Currently available CPUs have 48 bits of virtual addressing. Truncate
|
|
|
|
// the hint address to 46 bits to give the kernel a fighting chance of
|
|
|
|
// fulfilling our placement request.
|
|
|
|
raw_addr &= V8_UINT64_C(0x3ffffffff000);
|
|
|
|
#else
|
|
|
|
uint32_t raw_addr = V8::RandomPrivate(isolate);
|
|
|
|
// The range 0x20000000 - 0x60000000 is relatively unpopulated across a
|
|
|
|
// variety of ASLR modes (PAE kernel, NX compat mode, etc).
|
|
|
|
raw_addr &= 0x3ffff000;
|
|
|
|
raw_addr += 0x20000000;
|
|
|
|
#endif
|
|
|
|
return reinterpret_cast<void*>(raw_addr);
|
|
|
|
}
|
|
|
|
return NULL;
|
2009-12-02 13:38:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-30 14:30:46 +00:00
|
|
|
void OS::PostSetUp() {
|
2012-04-05 14:10:39 +00:00
|
|
|
POSIXPostSetUp();
|
2012-03-30 14:30:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-03 08:23:10 +00:00
|
|
|
uint64_t OS::CpuFeaturesImpliedByPlatform() {
|
2011-11-09 12:01:45 +00:00
|
|
|
return 0;
|
2009-12-03 08:23:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-02 13:38:50 +00:00
|
|
|
int OS::ActivationFrameAlignment() {
|
2011-11-09 12:01:45 +00:00
|
|
|
// With gcc 4.4 the tree vectorization optimizer can generate code
|
|
|
|
// that requires 16 byte alignment such as movdqa on x86.
|
2009-12-02 13:38:50 +00:00
|
|
|
return 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-09 12:01:45 +00:00
|
|
|
void OS::ReleaseStore(volatile AtomicWord* ptr, AtomicWord value) {
|
|
|
|
__asm__ __volatile__("" : : : "memory");
|
|
|
|
// An x86 store acts as a release barrier.
|
|
|
|
*ptr = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-25 12:37:31 +00:00
|
|
|
const char* OS::LocalTimezone(double time) {
|
|
|
|
if (isnan(time)) return "";
|
|
|
|
time_t tv = static_cast<time_t>(floor(time/msPerSecond));
|
|
|
|
struct tm* t = localtime(&tv);
|
|
|
|
if (NULL == t) return "";
|
|
|
|
return t->tm_zone;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double OS::LocalTimeOffset() {
|
|
|
|
time_t tv = time(NULL);
|
|
|
|
struct tm* t = localtime(&tv);
|
|
|
|
// tm_gmtoff includes any daylight savings offset, so subtract it.
|
|
|
|
return static_cast<double>(t->tm_gmtoff * msPerSecond -
|
|
|
|
(t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-02 13:38:50 +00:00
|
|
|
// We keep the lowest and highest addresses mapped as a quick way of
|
|
|
|
// determining that pointers are outside the heap (used mostly in assertions
|
2012-01-16 12:38:59 +00:00
|
|
|
// and verification). The estimate is conservative, i.e., not all addresses in
|
2009-12-02 13:38:50 +00:00
|
|
|
// 'allocated' space are actually allocated to our heap. The range is
|
|
|
|
// [lowest, highest), inclusive on the low and and exclusive on the high end.
|
|
|
|
static void* lowest_ever_allocated = reinterpret_cast<void*>(-1);
|
|
|
|
static void* highest_ever_allocated = reinterpret_cast<void*>(0);
|
|
|
|
|
|
|
|
|
|
|
|
static void UpdateAllocatedSpaceLimits(void* address, int size) {
|
2011-06-01 11:09:32 +00:00
|
|
|
ASSERT(limit_mutex != NULL);
|
|
|
|
ScopedLock lock(limit_mutex);
|
|
|
|
|
2009-12-02 13:38:50 +00:00
|
|
|
lowest_ever_allocated = Min(lowest_ever_allocated, address);
|
|
|
|
highest_ever_allocated =
|
|
|
|
Max(highest_ever_allocated,
|
|
|
|
reinterpret_cast<void*>(reinterpret_cast<char*>(address) + size));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool OS::IsOutsideAllocatedSpace(void* address) {
|
|
|
|
return address < lowest_ever_allocated || address >= highest_ever_allocated;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t OS::AllocateAlignment() {
|
2011-11-09 12:01:45 +00:00
|
|
|
return sysconf(_SC_PAGESIZE);
|
2009-12-02 13:38:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void* OS::Allocate(const size_t requested,
|
|
|
|
size_t* allocated,
|
2011-11-09 12:01:45 +00:00
|
|
|
bool is_executable) {
|
|
|
|
const size_t msize = RoundUp(requested, AllocateAlignment());
|
|
|
|
int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
|
|
|
|
void* addr = GetRandomMmapAddr();
|
|
|
|
void* mbase = mmap(addr, msize, prot, MAP_PRIVATE | MAP_ANON, -1, 0);
|
2009-12-02 13:38:50 +00:00
|
|
|
if (mbase == MAP_FAILED) {
|
2011-11-09 12:01:45 +00:00
|
|
|
LOG(i::Isolate::Current(),
|
|
|
|
StringEvent("OS::Allocate", "mmap failed"));
|
2009-12-02 13:38:50 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
*allocated = msize;
|
|
|
|
UpdateAllocatedSpaceLimits(mbase, msize);
|
|
|
|
return mbase;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-09 12:01:45 +00:00
|
|
|
void OS::Free(void* address, const size_t size) {
|
2011-06-01 11:09:32 +00:00
|
|
|
// TODO(1240712): munmap has a return value which is ignored here.
|
2011-11-09 12:01:45 +00:00
|
|
|
int result = munmap(address, size);
|
2009-12-02 13:38:50 +00:00
|
|
|
USE(result);
|
|
|
|
ASSERT(result == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OS::Sleep(int milliseconds) {
|
|
|
|
unsigned int ms = static_cast<unsigned int>(milliseconds);
|
|
|
|
usleep(1000 * ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OS::Abort() {
|
|
|
|
// Redirect to std abort to signal abnormal program termination.
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OS::DebugBreak() {
|
|
|
|
asm("int $3");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-14 14:27:06 +00:00
|
|
|
void OS::DumpBacktrace() {
|
|
|
|
// Currently unsupported.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-02 13:38:50 +00:00
|
|
|
class PosixMemoryMappedFile : public OS::MemoryMappedFile {
|
|
|
|
public:
|
|
|
|
PosixMemoryMappedFile(FILE* file, void* memory, int size)
|
|
|
|
: file_(file), memory_(memory), size_(size) { }
|
|
|
|
virtual ~PosixMemoryMappedFile();
|
|
|
|
virtual void* memory() { return memory_; }
|
2011-01-19 14:28:15 +00:00
|
|
|
virtual int size() { return size_; }
|
2009-12-02 13:38:50 +00:00
|
|
|
private:
|
|
|
|
FILE* file_;
|
|
|
|
void* memory_;
|
|
|
|
int size_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-01-19 14:28:15 +00:00
|
|
|
OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
|
2011-02-21 17:17:26 +00:00
|
|
|
FILE* file = fopen(name, "r+");
|
2011-01-19 14:28:15 +00:00
|
|
|
if (file == NULL) return NULL;
|
|
|
|
|
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
int size = ftell(file);
|
|
|
|
|
|
|
|
void* memory =
|
|
|
|
mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
|
|
|
|
return new PosixMemoryMappedFile(file, memory, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-02 13:38:50 +00:00
|
|
|
OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size,
|
|
|
|
void* initial) {
|
|
|
|
FILE* file = fopen(name, "w+");
|
|
|
|
if (file == NULL) return NULL;
|
|
|
|
int result = fwrite(initial, size, 1, file);
|
|
|
|
if (result < 1) {
|
|
|
|
fclose(file);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
void* memory =
|
|
|
|
mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
|
|
|
|
return new PosixMemoryMappedFile(file, memory, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PosixMemoryMappedFile::~PosixMemoryMappedFile() {
|
2011-09-29 12:23:05 +00:00
|
|
|
if (memory_) OS::Free(memory_, size_);
|
2009-12-02 13:38:50 +00:00
|
|
|
fclose(file_);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OS::LogSharedLibraryAddresses() {
|
2011-11-09 12:01:45 +00:00
|
|
|
// This function assumes that the layout of the file is as follows:
|
|
|
|
// hex_start_addr-hex_end_addr rwxp <unused data> [binary_file_name]
|
|
|
|
// If we encounter an unexpected situation we abort scanning further entries.
|
|
|
|
FILE* fp = fopen("/proc/self/maps", "r");
|
|
|
|
if (fp == NULL) return;
|
|
|
|
|
|
|
|
// Allocate enough room to be able to store a full file name.
|
|
|
|
const int kLibNameLen = FILENAME_MAX + 1;
|
|
|
|
char* lib_name = reinterpret_cast<char*>(malloc(kLibNameLen));
|
|
|
|
|
|
|
|
i::Isolate* isolate = ISOLATE;
|
|
|
|
// This loop will terminate once the scanning hits an EOF.
|
2009-12-02 13:38:50 +00:00
|
|
|
while (true) {
|
2011-11-09 12:01:45 +00:00
|
|
|
uintptr_t start, end;
|
|
|
|
char attr_r, attr_w, attr_x, attr_p;
|
|
|
|
// Parse the addresses and permission bits at the beginning of the line.
|
|
|
|
if (fscanf(fp, "%" V8PRIxPTR "-%" V8PRIxPTR, &start, &end) != 2) break;
|
|
|
|
if (fscanf(fp, " %c%c%c%c", &attr_r, &attr_w, &attr_x, &attr_p) != 4) break;
|
|
|
|
|
|
|
|
int c;
|
|
|
|
if (attr_r == 'r' && attr_w != 'w' && attr_x == 'x') {
|
|
|
|
// Found a read-only executable entry. Skip characters until we reach
|
|
|
|
// the beginning of the filename or the end of the line.
|
|
|
|
do {
|
|
|
|
c = getc(fp);
|
|
|
|
} while ((c != EOF) && (c != '\n') && (c != '/'));
|
|
|
|
if (c == EOF) break; // EOF: Was unexpected, just exit.
|
|
|
|
|
|
|
|
// Process the filename if found.
|
|
|
|
if (c == '/') {
|
|
|
|
ungetc(c, fp); // Push the '/' back into the stream to be read below.
|
|
|
|
|
|
|
|
// Read to the end of the line. Exit if the read fails.
|
|
|
|
if (fgets(lib_name, kLibNameLen, fp) == NULL) break;
|
|
|
|
|
|
|
|
// Drop the newline character read by fgets. We do not need to check
|
|
|
|
// for a zero-length string because we know that we at least read the
|
|
|
|
// '/' character.
|
|
|
|
lib_name[strlen(lib_name) - 1] = '\0';
|
|
|
|
} else {
|
|
|
|
// No library name found, just record the raw address range.
|
|
|
|
snprintf(lib_name, kLibNameLen,
|
|
|
|
"%08" V8PRIxPTR "-%08" V8PRIxPTR, start, end);
|
|
|
|
}
|
|
|
|
LOG(isolate, SharedLibraryEvent(lib_name, start, end));
|
|
|
|
} else {
|
2012-01-13 13:09:52 +00:00
|
|
|
// Entry not describing executable data. Skip to end of line to set up
|
2011-11-09 12:01:45 +00:00
|
|
|
// reading the next entry.
|
|
|
|
do {
|
|
|
|
c = getc(fp);
|
|
|
|
} while ((c != EOF) && (c != '\n'));
|
|
|
|
if (c == EOF) break;
|
|
|
|
}
|
2009-12-02 13:38:50 +00:00
|
|
|
}
|
2011-11-09 12:01:45 +00:00
|
|
|
free(lib_name);
|
|
|
|
fclose(fp);
|
2009-12-02 13:38:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-19 16:45:11 +00:00
|
|
|
void OS::SignalCodeMovingGC() {
|
2011-11-09 12:01:45 +00:00
|
|
|
// Support for ll_prof.py.
|
|
|
|
//
|
|
|
|
// The Linux profiler built into the kernel logs all mmap's with
|
|
|
|
// PROT_EXEC so that analysis tools can properly attribute ticks. We
|
|
|
|
// do a mmap with a name known by ll_prof.py and immediately munmap
|
|
|
|
// it. This injects a GC marker into the stream of events generated
|
|
|
|
// by the kernel and allows us to synchronize V8 code log and the
|
|
|
|
// kernel log.
|
|
|
|
int size = sysconf(_SC_PAGESIZE);
|
2012-09-06 15:01:20 +00:00
|
|
|
FILE* f = fopen(FLAG_gc_fake_mmap, "w+");
|
2011-11-09 12:01:45 +00:00
|
|
|
void* addr = mmap(NULL, size, PROT_READ | PROT_EXEC, MAP_PRIVATE,
|
|
|
|
fileno(f), 0);
|
|
|
|
ASSERT(addr != MAP_FAILED);
|
|
|
|
OS::Free(addr, size);
|
|
|
|
fclose(f);
|
2010-10-19 16:45:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-02 13:38:50 +00:00
|
|
|
int OS::StackWalk(Vector<OS::StackFrame> frames) {
|
2011-11-09 12:01:45 +00:00
|
|
|
// backtrace is a glibc extension.
|
2011-06-01 11:09:32 +00:00
|
|
|
int frames_size = frames.length();
|
|
|
|
ScopedVector<void*> addresses(frames_size);
|
|
|
|
|
|
|
|
int frames_count = backtrace(addresses.start(), frames_size);
|
|
|
|
|
|
|
|
char** symbols = backtrace_symbols(addresses.start(), frames_count);
|
|
|
|
if (symbols == NULL) {
|
|
|
|
return kStackWalkError;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < frames_count; i++) {
|
|
|
|
frames[i].address = addresses[i];
|
|
|
|
// Format a text representation of the frame based on the information
|
|
|
|
// available.
|
|
|
|
SNPrintF(MutableCStrVector(frames[i].text, kStackWalkMaxTextLen),
|
|
|
|
"%s",
|
|
|
|
symbols[i]);
|
|
|
|
// Make sure line termination is in place.
|
|
|
|
frames[i].text[kStackWalkMaxTextLen - 1] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
free(symbols);
|
|
|
|
|
|
|
|
return frames_count;
|
2009-12-02 13:38:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Constants used for mmap.
|
|
|
|
static const int kMmapFd = -1;
|
|
|
|
static const int kMmapFdOffset = 0;
|
|
|
|
|
2011-11-09 12:01:45 +00:00
|
|
|
VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
|
2009-12-02 13:38:50 +00:00
|
|
|
|
|
|
|
VirtualMemory::VirtualMemory(size_t size) {
|
2011-11-09 12:01:45 +00:00
|
|
|
address_ = ReserveRegion(size);
|
2009-12-02 13:38:50 +00:00
|
|
|
size_ = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-09 12:01:45 +00:00
|
|
|
VirtualMemory::VirtualMemory(size_t size, size_t alignment)
|
|
|
|
: address_(NULL), size_(0) {
|
|
|
|
ASSERT(IsAligned(alignment, static_cast<intptr_t>(OS::AllocateAlignment())));
|
|
|
|
size_t request_size = RoundUp(size + alignment,
|
|
|
|
static_cast<intptr_t>(OS::AllocateAlignment()));
|
|
|
|
void* reservation = mmap(GetRandomMmapAddr(),
|
|
|
|
request_size,
|
|
|
|
PROT_NONE,
|
|
|
|
MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
|
|
|
|
kMmapFd,
|
|
|
|
kMmapFdOffset);
|
|
|
|
if (reservation == MAP_FAILED) return;
|
|
|
|
|
|
|
|
Address base = static_cast<Address>(reservation);
|
|
|
|
Address aligned_base = RoundUp(base, alignment);
|
|
|
|
ASSERT_LE(base, aligned_base);
|
|
|
|
|
|
|
|
// Unmap extra memory reserved before and after the desired block.
|
|
|
|
if (aligned_base != base) {
|
|
|
|
size_t prefix_size = static_cast<size_t>(aligned_base - base);
|
|
|
|
OS::Free(base, prefix_size);
|
|
|
|
request_size -= prefix_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t aligned_size = RoundUp(size, OS::AllocateAlignment());
|
|
|
|
ASSERT_LE(aligned_size, request_size);
|
|
|
|
|
|
|
|
if (aligned_size != request_size) {
|
|
|
|
size_t suffix_size = request_size - aligned_size;
|
|
|
|
OS::Free(aligned_base + aligned_size, suffix_size);
|
|
|
|
request_size -= suffix_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(aligned_size == request_size);
|
|
|
|
|
|
|
|
address_ = static_cast<void*>(aligned_base);
|
|
|
|
size_ = aligned_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-02 13:38:50 +00:00
|
|
|
VirtualMemory::~VirtualMemory() {
|
|
|
|
if (IsReserved()) {
|
2011-11-09 12:01:45 +00:00
|
|
|
bool result = ReleaseRegion(address(), size());
|
|
|
|
ASSERT(result);
|
|
|
|
USE(result);
|
2009-12-02 13:38:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool VirtualMemory::IsReserved() {
|
2011-11-09 12:01:45 +00:00
|
|
|
return address_ != NULL;
|
2009-12-02 13:38:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-09 12:01:45 +00:00
|
|
|
void VirtualMemory::Reset() {
|
|
|
|
address_ = NULL;
|
|
|
|
size_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
|
|
|
|
return CommitRegion(address, size, is_executable);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool VirtualMemory::Uncommit(void* address, size_t size) {
|
|
|
|
return UncommitRegion(address, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-23 12:11:24 +00:00
|
|
|
bool VirtualMemory::Guard(void* address) {
|
|
|
|
OS::Guard(address, OS::CommitPageSize());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-09 12:01:45 +00:00
|
|
|
void* VirtualMemory::ReserveRegion(size_t size) {
|
|
|
|
void* result = mmap(GetRandomMmapAddr(),
|
|
|
|
size,
|
|
|
|
PROT_NONE,
|
|
|
|
MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
|
|
|
|
kMmapFd,
|
|
|
|
kMmapFdOffset);
|
|
|
|
|
|
|
|
if (result == MAP_FAILED) return NULL;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) {
|
|
|
|
int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
|
|
|
|
if (MAP_FAILED == mmap(base,
|
|
|
|
size,
|
|
|
|
prot,
|
2009-12-02 13:38:50 +00:00
|
|
|
MAP_PRIVATE | MAP_ANON | MAP_FIXED,
|
2011-11-09 12:01:45 +00:00
|
|
|
kMmapFd,
|
|
|
|
kMmapFdOffset)) {
|
2009-12-02 13:38:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-11-09 12:01:45 +00:00
|
|
|
UpdateAllocatedSpaceLimits(base, size);
|
2009-12-02 13:38:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-09 12:01:45 +00:00
|
|
|
bool VirtualMemory::UncommitRegion(void* base, size_t size) {
|
|
|
|
return mmap(base,
|
|
|
|
size,
|
|
|
|
PROT_NONE,
|
2011-06-01 11:09:32 +00:00
|
|
|
MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | MAP_FIXED,
|
2011-11-09 12:01:45 +00:00
|
|
|
kMmapFd,
|
|
|
|
kMmapFdOffset) != MAP_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
|
|
|
|
return munmap(base, size) == 0;
|
2009-12-02 13:38:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-22 16:33:10 +00:00
|
|
|
bool VirtualMemory::HasLazyCommits() {
|
|
|
|
// TODO(alph): implement for the platform.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-11 23:46:22 +00:00
|
|
|
class Thread::PlatformData : public Malloced {
|
2009-12-02 13:38:50 +00:00
|
|
|
public:
|
2011-11-09 12:01:45 +00:00
|
|
|
PlatformData() : thread_(kNoThread) {}
|
|
|
|
|
2009-12-02 13:38:50 +00:00
|
|
|
pthread_t thread_; // Thread handle for pthread.
|
|
|
|
};
|
|
|
|
|
2011-06-10 09:54:04 +00:00
|
|
|
Thread::Thread(const Options& options)
|
2011-11-09 12:01:45 +00:00
|
|
|
: data_(new PlatformData()),
|
2012-01-24 15:48:16 +00:00
|
|
|
stack_size_(options.stack_size()) {
|
|
|
|
set_name(options.name());
|
2009-12-02 13:38:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Thread::~Thread() {
|
2011-04-11 23:46:22 +00:00
|
|
|
delete data_;
|
2009-12-02 13:38:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void* ThreadEntry(void* arg) {
|
|
|
|
Thread* thread = reinterpret_cast<Thread*>(arg);
|
|
|
|
// This is also initialized by the first argument to pthread_create() but we
|
|
|
|
// don't know which thread will run first (the original thread or the new
|
|
|
|
// one) so we initialize it here too.
|
2011-11-09 12:01:45 +00:00
|
|
|
#ifdef PR_SET_NAME
|
|
|
|
prctl(PR_SET_NAME,
|
|
|
|
reinterpret_cast<unsigned long>(thread->name()), // NOLINT
|
|
|
|
0, 0, 0);
|
|
|
|
#endif
|
2011-04-11 23:46:22 +00:00
|
|
|
thread->data()->thread_ = pthread_self();
|
|
|
|
ASSERT(thread->data()->thread_ != kNoThread);
|
2009-12-02 13:38:50 +00:00
|
|
|
thread->Run();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-04 09:09:50 +00:00
|
|
|
void Thread::set_name(const char* name) {
|
|
|
|
strncpy(name_, name, sizeof(name_));
|
|
|
|
name_[sizeof(name_) - 1] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-02 13:38:50 +00:00
|
|
|
void Thread::Start() {
|
2011-03-21 15:04:17 +00:00
|
|
|
pthread_attr_t* attr_ptr = NULL;
|
|
|
|
pthread_attr_t attr;
|
|
|
|
if (stack_size_ > 0) {
|
|
|
|
pthread_attr_init(&attr);
|
|
|
|
pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_));
|
|
|
|
attr_ptr = &attr;
|
|
|
|
}
|
2011-04-11 23:46:22 +00:00
|
|
|
pthread_create(&data_->thread_, attr_ptr, ThreadEntry, this);
|
2011-06-01 11:09:32 +00:00
|
|
|
ASSERT(data_->thread_ != kNoThread);
|
2009-12-02 13:38:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Thread::Join() {
|
2011-04-11 23:46:22 +00:00
|
|
|
pthread_join(data_->thread_, NULL);
|
2009-12-02 13:38:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Thread::LocalStorageKey Thread::CreateThreadLocalKey() {
|
|
|
|
pthread_key_t key;
|
|
|
|
int result = pthread_key_create(&key, NULL);
|
|
|
|
USE(result);
|
|
|
|
ASSERT(result == 0);
|
|
|
|
return static_cast<LocalStorageKey>(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Thread::DeleteThreadLocalKey(LocalStorageKey key) {
|
|
|
|
pthread_key_t pthread_key = static_cast<pthread_key_t>(key);
|
|
|
|
int result = pthread_key_delete(pthread_key);
|
|
|
|
USE(result);
|
|
|
|
ASSERT(result == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void* Thread::GetThreadLocal(LocalStorageKey key) {
|
|
|
|
pthread_key_t pthread_key = static_cast<pthread_key_t>(key);
|
|
|
|
return pthread_getspecific(pthread_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Thread::SetThreadLocal(LocalStorageKey key, void* value) {
|
|
|
|
pthread_key_t pthread_key = static_cast<pthread_key_t>(key);
|
|
|
|
pthread_setspecific(pthread_key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Thread::YieldCPU() {
|
|
|
|
sched_yield();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class OpenBSDMutex : public Mutex {
|
|
|
|
public:
|
|
|
|
OpenBSDMutex() {
|
|
|
|
pthread_mutexattr_t attrs;
|
|
|
|
int result = pthread_mutexattr_init(&attrs);
|
|
|
|
ASSERT(result == 0);
|
|
|
|
result = pthread_mutexattr_settype(&attrs, PTHREAD_MUTEX_RECURSIVE);
|
|
|
|
ASSERT(result == 0);
|
|
|
|
result = pthread_mutex_init(&mutex_, &attrs);
|
|
|
|
ASSERT(result == 0);
|
2011-11-09 12:01:45 +00:00
|
|
|
USE(result);
|
2009-12-02 13:38:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~OpenBSDMutex() { pthread_mutex_destroy(&mutex_); }
|
|
|
|
|
|
|
|
virtual int Lock() {
|
|
|
|
int result = pthread_mutex_lock(&mutex_);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int Unlock() {
|
|
|
|
int result = pthread_mutex_unlock(&mutex_);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-06-01 11:09:32 +00:00
|
|
|
virtual bool TryLock() {
|
|
|
|
int result = pthread_mutex_trylock(&mutex_);
|
|
|
|
// Return false if the lock is busy and locking failed.
|
|
|
|
if (result == EBUSY) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ASSERT(result == 0); // Verify no other errors.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-12-02 13:38:50 +00:00
|
|
|
private:
|
|
|
|
pthread_mutex_t mutex_; // Pthread mutex for POSIX platforms.
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Mutex* OS::CreateMutex() {
|
|
|
|
return new OpenBSDMutex();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class OpenBSDSemaphore : public Semaphore {
|
|
|
|
public:
|
|
|
|
explicit OpenBSDSemaphore(int count) { sem_init(&sem_, 0, count); }
|
|
|
|
virtual ~OpenBSDSemaphore() { sem_destroy(&sem_); }
|
|
|
|
|
|
|
|
virtual void Wait();
|
|
|
|
virtual bool Wait(int timeout);
|
|
|
|
virtual void Signal() { sem_post(&sem_); }
|
|
|
|
private:
|
|
|
|
sem_t sem_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void OpenBSDSemaphore::Wait() {
|
|
|
|
while (true) {
|
|
|
|
int result = sem_wait(&sem_);
|
|
|
|
if (result == 0) return; // Successfully got semaphore.
|
|
|
|
CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-09 12:01:45 +00:00
|
|
|
#ifndef TIMEVAL_TO_TIMESPEC
|
|
|
|
#define TIMEVAL_TO_TIMESPEC(tv, ts) do { \
|
|
|
|
(ts)->tv_sec = (tv)->tv_sec; \
|
|
|
|
(ts)->tv_nsec = (tv)->tv_usec * 1000; \
|
|
|
|
} while (false)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2009-12-02 13:38:50 +00:00
|
|
|
bool OpenBSDSemaphore::Wait(int timeout) {
|
|
|
|
const long kOneSecondMicros = 1000000; // NOLINT
|
|
|
|
|
|
|
|
// Split timeout into second and nanosecond parts.
|
|
|
|
struct timeval delta;
|
|
|
|
delta.tv_usec = timeout % kOneSecondMicros;
|
|
|
|
delta.tv_sec = timeout / kOneSecondMicros;
|
|
|
|
|
|
|
|
struct timeval current_time;
|
|
|
|
// Get the current time.
|
|
|
|
if (gettimeofday(¤t_time, NULL) == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate time for end of timeout.
|
|
|
|
struct timeval end_time;
|
|
|
|
timeradd(¤t_time, &delta, &end_time);
|
|
|
|
|
|
|
|
struct timespec ts;
|
|
|
|
TIMEVAL_TO_TIMESPEC(&end_time, &ts);
|
2011-06-01 11:09:32 +00:00
|
|
|
|
|
|
|
int to = ts.tv_sec;
|
|
|
|
|
2009-12-02 13:38:50 +00:00
|
|
|
while (true) {
|
|
|
|
int result = sem_trywait(&sem_);
|
|
|
|
if (result == 0) return true; // Successfully got semaphore.
|
2011-06-01 11:09:32 +00:00
|
|
|
if (!to) return false; // Timeout.
|
2009-12-02 13:38:50 +00:00
|
|
|
CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup.
|
2011-06-01 11:09:32 +00:00
|
|
|
usleep(ts.tv_nsec / 1000);
|
|
|
|
to--;
|
2009-12-02 13:38:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Semaphore* OS::CreateSemaphore(int count) {
|
|
|
|
return new OpenBSDSemaphore(count);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-01 11:09:32 +00:00
|
|
|
static pthread_t GetThreadID() {
|
2011-11-09 12:01:45 +00:00
|
|
|
return pthread_self();
|
2011-06-01 11:09:32 +00:00
|
|
|
}
|
|
|
|
|
2009-12-02 13:38:50 +00:00
|
|
|
static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) {
|
|
|
|
USE(info);
|
|
|
|
if (signal != SIGPROF) return;
|
2011-06-01 11:09:32 +00:00
|
|
|
Isolate* isolate = Isolate::UncheckedCurrent();
|
|
|
|
if (isolate == NULL || !isolate->IsInitialized() || !isolate->IsInUse()) {
|
|
|
|
// We require a fully initialized and entered isolate.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (v8::Locker::IsActive() &&
|
|
|
|
!isolate->thread_manager()->IsLockedByCurrentThread()) {
|
|
|
|
return;
|
|
|
|
}
|
2009-12-02 13:38:50 +00:00
|
|
|
|
2011-06-01 11:09:32 +00:00
|
|
|
Sampler* sampler = isolate->logger()->sampler();
|
|
|
|
if (sampler == NULL || !sampler->IsActive()) return;
|
|
|
|
|
2012-10-17 12:24:31 +00:00
|
|
|
TickSample sample_obj;
|
2012-11-30 10:26:21 +00:00
|
|
|
TickSample* sample = CpuProfiler::TickSampleEvent(isolate);
|
2012-10-17 12:24:31 +00:00
|
|
|
if (sample == NULL) sample = &sample_obj;
|
2011-06-01 11:09:32 +00:00
|
|
|
|
|
|
|
// Extracting the sample from the context is extremely machine dependent.
|
|
|
|
sample->state = isolate->current_vm_state();
|
2012-01-05 12:17:08 +00:00
|
|
|
ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context);
|
|
|
|
#ifdef __NetBSD__
|
|
|
|
mcontext_t& mcontext = ucontext->uc_mcontext;
|
|
|
|
#if V8_HOST_ARCH_IA32
|
|
|
|
sample->pc = reinterpret_cast<Address>(mcontext.__gregs[_REG_EIP]);
|
|
|
|
sample->sp = reinterpret_cast<Address>(mcontext.__gregs[_REG_ESP]);
|
|
|
|
sample->fp = reinterpret_cast<Address>(mcontext.__gregs[_REG_EBP]);
|
|
|
|
#elif V8_HOST_ARCH_X64
|
|
|
|
sample->pc = reinterpret_cast<Address>(mcontext.__gregs[_REG_RIP]);
|
|
|
|
sample->sp = reinterpret_cast<Address>(mcontext.__gregs[_REG_RSP]);
|
|
|
|
sample->fp = reinterpret_cast<Address>(mcontext.__gregs[_REG_RBP]);
|
|
|
|
#endif // V8_HOST_ARCH
|
|
|
|
#else // OpenBSD
|
2011-06-01 11:09:32 +00:00
|
|
|
#if V8_HOST_ARCH_IA32
|
|
|
|
sample->pc = reinterpret_cast<Address>(ucontext->sc_eip);
|
|
|
|
sample->sp = reinterpret_cast<Address>(ucontext->sc_esp);
|
|
|
|
sample->fp = reinterpret_cast<Address>(ucontext->sc_ebp);
|
|
|
|
#elif V8_HOST_ARCH_X64
|
|
|
|
sample->pc = reinterpret_cast<Address>(ucontext->sc_rip);
|
|
|
|
sample->sp = reinterpret_cast<Address>(ucontext->sc_rsp);
|
|
|
|
sample->fp = reinterpret_cast<Address>(ucontext->sc_rbp);
|
2012-01-05 12:17:08 +00:00
|
|
|
#endif // V8_HOST_ARCH
|
|
|
|
#endif // __NetBSD__
|
2011-06-01 11:09:32 +00:00
|
|
|
sampler->SampleStack(sample);
|
|
|
|
sampler->Tick(sample);
|
2009-12-02 13:38:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-09 12:01:45 +00:00
|
|
|
class Sampler::PlatformData : public Malloced {
|
|
|
|
public:
|
|
|
|
PlatformData() : vm_tid_(GetThreadID()) {}
|
|
|
|
|
|
|
|
pthread_t vm_tid() const { return vm_tid_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
pthread_t vm_tid_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-06-01 11:09:32 +00:00
|
|
|
class SignalSender : public Thread {
|
2009-12-02 13:38:50 +00:00
|
|
|
public:
|
2012-02-07 08:00:36 +00:00
|
|
|
static const int kSignalSenderStackSize = 64 * KB;
|
2012-01-24 15:48:16 +00:00
|
|
|
|
2011-06-01 11:09:32 +00:00
|
|
|
explicit SignalSender(int interval)
|
2012-01-24 15:48:16 +00:00
|
|
|
: Thread(Thread::Options("SignalSender", kSignalSenderStackSize)),
|
2011-11-09 12:01:45 +00:00
|
|
|
vm_tgid_(getpid()),
|
2011-06-01 11:09:32 +00:00
|
|
|
interval_(interval) {}
|
|
|
|
|
2012-04-13 14:08:31 +00:00
|
|
|
static void SetUp() { if (!mutex_) mutex_ = OS::CreateMutex(); }
|
|
|
|
static void TearDown() { delete mutex_; }
|
2012-04-05 14:10:39 +00:00
|
|
|
|
2011-11-09 12:01:45 +00:00
|
|
|
static void InstallSignalHandler() {
|
|
|
|
struct sigaction sa;
|
|
|
|
sa.sa_sigaction = ProfilerSignalHandler;
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sa.sa_flags = SA_RESTART | SA_SIGINFO;
|
|
|
|
signal_handler_installed_ =
|
|
|
|
(sigaction(SIGPROF, &sa, &old_signal_handler_) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void RestoreSignalHandler() {
|
|
|
|
if (signal_handler_installed_) {
|
|
|
|
sigaction(SIGPROF, &old_signal_handler_, 0);
|
|
|
|
signal_handler_installed_ = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-01 11:09:32 +00:00
|
|
|
static void AddActiveSampler(Sampler* sampler) {
|
2012-04-05 14:10:39 +00:00
|
|
|
ScopedLock lock(mutex_);
|
2011-06-01 11:09:32 +00:00
|
|
|
SamplerRegistry::AddActiveSampler(sampler);
|
|
|
|
if (instance_ == NULL) {
|
2011-11-09 12:01:45 +00:00
|
|
|
// Start a thread that will send SIGPROF signal to VM threads,
|
|
|
|
// when CPU profiling will be enabled.
|
2011-06-01 11:09:32 +00:00
|
|
|
instance_ = new SignalSender(sampler->interval());
|
|
|
|
instance_->Start();
|
|
|
|
} else {
|
|
|
|
ASSERT(instance_->interval_ == sampler->interval());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void RemoveActiveSampler(Sampler* sampler) {
|
2012-04-05 14:10:39 +00:00
|
|
|
ScopedLock lock(mutex_);
|
2011-06-01 11:09:32 +00:00
|
|
|
SamplerRegistry::RemoveActiveSampler(sampler);
|
|
|
|
if (SamplerRegistry::GetState() == SamplerRegistry::HAS_NO_SAMPLERS) {
|
2011-07-05 15:49:39 +00:00
|
|
|
RuntimeProfiler::StopRuntimeProfilerThreadBeforeShutdown(instance_);
|
2011-06-01 11:09:32 +00:00
|
|
|
delete instance_;
|
|
|
|
instance_ = NULL;
|
2011-11-09 12:01:45 +00:00
|
|
|
RestoreSignalHandler();
|
2011-06-01 11:09:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implement Thread::Run().
|
|
|
|
virtual void Run() {
|
|
|
|
SamplerRegistry::State state;
|
|
|
|
while ((state = SamplerRegistry::GetState()) !=
|
|
|
|
SamplerRegistry::HAS_NO_SAMPLERS) {
|
|
|
|
// When CPU profiling is enabled both JavaScript and C++ code is
|
|
|
|
// profiled. We must not suspend.
|
2012-12-07 08:55:06 +00:00
|
|
|
if (state == SamplerRegistry::HAS_CPU_PROFILING_SAMPLERS) {
|
|
|
|
if (!signal_handler_installed_) InstallSignalHandler();
|
|
|
|
SamplerRegistry::IterateActiveSamplers(&DoCpuProfile, this);
|
2011-06-01 11:09:32 +00:00
|
|
|
} else {
|
2012-12-07 08:55:06 +00:00
|
|
|
if (signal_handler_installed_) RestoreSignalHandler();
|
2012-12-12 15:26:04 +00:00
|
|
|
if (RuntimeProfiler::WaitForSomeIsolateToEnterJS()) continue;
|
2011-06-01 11:09:32 +00:00
|
|
|
}
|
2012-12-07 08:55:06 +00:00
|
|
|
Sleep(); // TODO(svenpanne) Figure out if OS:Sleep(interval_) is enough.
|
2011-06-01 11:09:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DoCpuProfile(Sampler* sampler, void* raw_sender) {
|
|
|
|
if (!sampler->IsProfiling()) return;
|
|
|
|
SignalSender* sender = reinterpret_cast<SignalSender*>(raw_sender);
|
|
|
|
sender->SendProfilingSignal(sampler->platform_data()->vm_tid());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SendProfilingSignal(pthread_t tid) {
|
|
|
|
if (!signal_handler_installed_) return;
|
|
|
|
pthread_kill(tid, SIGPROF);
|
|
|
|
}
|
|
|
|
|
2012-12-07 08:55:06 +00:00
|
|
|
void Sleep() {
|
2011-06-01 11:09:32 +00:00
|
|
|
// Convert ms to us and subtract 100 us to compensate delays
|
|
|
|
// occuring during signal delivery.
|
|
|
|
useconds_t interval = interval_ * 1000 - 100;
|
|
|
|
int result = usleep(interval);
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (result != 0 && errno != EINTR) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"SignalSender usleep error; interval = %u, errno = %d\n",
|
|
|
|
interval,
|
|
|
|
errno);
|
|
|
|
ASSERT(result == 0 || errno == EINTR);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
USE(result);
|
|
|
|
}
|
|
|
|
|
2011-11-09 12:01:45 +00:00
|
|
|
const int vm_tgid_;
|
2011-06-01 11:09:32 +00:00
|
|
|
const int interval_;
|
|
|
|
|
|
|
|
// Protects the process wide state below.
|
2012-04-05 14:10:39 +00:00
|
|
|
static Mutex* mutex_;
|
2011-06-01 11:09:32 +00:00
|
|
|
static SignalSender* instance_;
|
|
|
|
static bool signal_handler_installed_;
|
|
|
|
static struct sigaction old_signal_handler_;
|
|
|
|
|
2012-01-20 16:17:08 +00:00
|
|
|
private:
|
2011-06-01 11:09:32 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(SignalSender);
|
2009-12-02 13:38:50 +00:00
|
|
|
};
|
|
|
|
|
2011-11-09 12:01:45 +00:00
|
|
|
|
2012-04-05 14:10:39 +00:00
|
|
|
Mutex* SignalSender::mutex_ = NULL;
|
2011-06-01 11:09:32 +00:00
|
|
|
SignalSender* SignalSender::instance_ = NULL;
|
|
|
|
struct sigaction SignalSender::old_signal_handler_;
|
|
|
|
bool SignalSender::signal_handler_installed_ = false;
|
|
|
|
|
2009-12-02 13:38:50 +00:00
|
|
|
|
2012-04-05 14:10:39 +00:00
|
|
|
void OS::SetUp() {
|
|
|
|
// Seed the random number generator. We preserve microsecond resolution.
|
|
|
|
uint64_t seed = Ticks() ^ (getpid() << 16);
|
|
|
|
srandom(static_cast<unsigned int>(seed));
|
|
|
|
limit_mutex = CreateMutex();
|
|
|
|
SignalSender::SetUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-13 14:08:31 +00:00
|
|
|
void OS::TearDown() {
|
|
|
|
SignalSender::TearDown();
|
|
|
|
delete limit_mutex;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
Sampler::Sampler(Isolate* isolate, int interval)
|
|
|
|
: isolate_(isolate),
|
|
|
|
interval_(interval),
|
2010-12-07 11:31:57 +00:00
|
|
|
profiling_(false),
|
2010-11-25 15:54:52 +00:00
|
|
|
active_(false),
|
|
|
|
samples_taken_(0) {
|
2011-06-01 11:09:32 +00:00
|
|
|
data_ = new PlatformData;
|
2009-12-02 13:38:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Sampler::~Sampler() {
|
2011-06-01 11:09:32 +00:00
|
|
|
ASSERT(!IsActive());
|
2009-12-02 13:38:50 +00:00
|
|
|
delete data_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Sampler::Start() {
|
2011-06-01 11:09:32 +00:00
|
|
|
ASSERT(!IsActive());
|
|
|
|
SetActive(true);
|
|
|
|
SignalSender::AddActiveSampler(this);
|
2009-12-02 13:38:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Sampler::Stop() {
|
2011-06-01 11:09:32 +00:00
|
|
|
ASSERT(IsActive());
|
|
|
|
SignalSender::RemoveActiveSampler(this);
|
|
|
|
SetActive(false);
|
2009-12-02 13:38:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} } // namespace v8::internal
|