2008-09-09 20:08:45 +00:00
|
|
|
// Copyright 2006-2008 the V8 project authors. All rights reserved.
|
2008-07-03 15:10:15 +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.
|
|
|
|
|
2009-03-24 06:32:00 +00:00
|
|
|
// Platform specific code for Linux goes here. For the POSIX comaptible parts
|
|
|
|
// the implementation is in platform-posix.cc.
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <semaphore.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
2009-02-24 13:32:01 +00:00
|
|
|
#include <sys/types.h>
|
2008-07-03 15:10:15 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
// Ubuntu Dapper requires memory pages to be marked as
|
|
|
|
// executable. Otherwise, OS raises an exception when executing code
|
|
|
|
// in that page.
|
|
|
|
#include <sys/types.h> // mmap & munmap
|
2008-09-23 08:56:12 +00:00
|
|
|
#include <sys/mman.h> // mmap & munmap
|
|
|
|
#include <sys/stat.h> // open
|
2009-03-18 15:20:26 +00:00
|
|
|
#include <fcntl.h> // open
|
|
|
|
#include <unistd.h> // sysconf
|
2009-03-23 12:37:53 +00:00
|
|
|
#ifdef __GLIBC__
|
2008-09-23 08:56:12 +00:00
|
|
|
#include <execinfo.h> // backtrace, backtrace_symbols
|
2009-03-23 12:37:53 +00:00
|
|
|
#endif // def __GLIBC__
|
2008-09-23 08:56:12 +00:00
|
|
|
#include <strings.h> // index
|
2008-07-03 15:10:15 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#undef MAP_TYPE
|
|
|
|
|
|
|
|
#include "v8.h"
|
|
|
|
|
|
|
|
#include "platform.h"
|
|
|
|
|
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// 0 is never a valid thread id on Linux since tids and pids share a
|
|
|
|
// name space and pid 0 is reserved (see man 2 kill).
|
|
|
|
static const pthread_t kNoThread = (pthread_t) 0;
|
|
|
|
|
|
|
|
|
|
|
|
double ceiling(double x) {
|
|
|
|
return ceil(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OS::Setup() {
|
|
|
|
// Seed the random number generator.
|
2008-09-10 06:59:41 +00:00
|
|
|
// Convert the current time to a 64-bit integer first, before converting it
|
|
|
|
// to an unsigned. Going directly can cause an overflow and the seed to be
|
|
|
|
// set to all ones. The seed will be identical for different instances that
|
|
|
|
// call this setup code within the same millisecond.
|
|
|
|
uint64_t seed = static_cast<uint64_t>(TimeCurrentMillis());
|
|
|
|
srandom(static_cast<unsigned int>(seed));
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-23 10:06:58 +00:00
|
|
|
double OS::nan_value() {
|
|
|
|
return NAN;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int OS::ActivationFrameAlignment() {
|
2009-06-05 06:20:30 +00:00
|
|
|
#ifdef V8_TARGET_ARCH_ARM
|
2009-05-12 20:11:01 +00:00
|
|
|
// On EABI ARM targets this is required for fp correctness in the
|
|
|
|
// runtime system.
|
2009-03-12 10:20:32 +00:00
|
|
|
return 8;
|
2009-06-05 06:20:30 +00:00
|
|
|
#else
|
|
|
|
// With gcc 4.4 the tree vectorization optimiser can generate code
|
|
|
|
// that requires 16 byte alignment such as movdqa on x86.
|
|
|
|
return 16;
|
|
|
|
#endif
|
2008-09-23 10:06:58 +00:00
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +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
|
|
|
|
// and verification). The estimate is conservative, ie, not all addresses in
|
|
|
|
// '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) {
|
|
|
|
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() {
|
2009-03-18 15:20:26 +00:00
|
|
|
return sysconf(_SC_PAGESIZE);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-30 08:49:36 +00:00
|
|
|
void* OS::Allocate(const size_t requested,
|
|
|
|
size_t* allocated,
|
2009-03-24 12:47:53 +00:00
|
|
|
bool is_executable) {
|
2009-03-18 15:20:26 +00:00
|
|
|
const size_t msize = RoundUp(requested, sysconf(_SC_PAGESIZE));
|
2009-03-24 12:47:53 +00:00
|
|
|
int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
|
2008-07-30 08:49:36 +00:00
|
|
|
void* mbase = mmap(NULL, msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
2008-07-03 15:10:15 +00:00
|
|
|
if (mbase == MAP_FAILED) {
|
|
|
|
LOG(StringEvent("OS::Allocate", "mmap failed"));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
*allocated = msize;
|
|
|
|
UpdateAllocatedSpaceLimits(mbase, msize);
|
|
|
|
return mbase;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-24 12:47:53 +00:00
|
|
|
void OS::Free(void* address, const size_t size) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// TODO(1240712): munmap has a return value which is ignored here.
|
2009-03-24 12:47:53 +00:00
|
|
|
munmap(address, size);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-24 12:47:53 +00:00
|
|
|
#ifdef ENABLE_HEAP_PROTECTION
|
|
|
|
|
|
|
|
void OS::Protect(void* address, size_t size) {
|
|
|
|
// TODO(1240712): mprotect has a return value which is ignored here.
|
|
|
|
mprotect(address, size, PROT_READ);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OS::Unprotect(void* address, size_t size, bool is_executable) {
|
|
|
|
// TODO(1240712): mprotect has a return value which is ignored here.
|
|
|
|
int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
|
|
|
|
mprotect(address, size, prot);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-30 08:49:36 +00:00
|
|
|
void OS::DebugBreak() {
|
2009-04-29 13:11:48 +00:00
|
|
|
// TODO(lrn): Introduce processor define for runtime system (!= V8_ARCH_x,
|
|
|
|
// which is the architecture of generated code).
|
2009-03-24 13:21:34 +00:00
|
|
|
#if defined(__arm__) || defined(__thumb__)
|
2008-07-30 08:49:36 +00:00
|
|
|
asm("bkpt 0");
|
|
|
|
#else
|
|
|
|
asm("int $3");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +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_; }
|
|
|
|
private:
|
|
|
|
FILE* file_;
|
|
|
|
void* memory_;
|
|
|
|
int size_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size,
|
|
|
|
void* initial) {
|
|
|
|
FILE* file = fopen(name, "w+");
|
|
|
|
if (file == NULL) return NULL;
|
2008-09-05 07:46:32 +00:00
|
|
|
int result = fwrite(initial, size, 1, file);
|
|
|
|
if (result < 1) {
|
|
|
|
fclose(file);
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
void* memory =
|
|
|
|
mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
|
|
|
|
return new PosixMemoryMappedFile(file, memory, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PosixMemoryMappedFile::~PosixMemoryMappedFile() {
|
|
|
|
if (memory_) munmap(memory_, size_);
|
|
|
|
fclose(file_);
|
|
|
|
}
|
|
|
|
|
2008-11-21 08:36:30 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
#ifdef ENABLE_LOGGING_AND_PROFILING
|
2009-06-16 12:52:02 +00:00
|
|
|
static uintptr_t StringToULong(char* buffer) {
|
|
|
|
return strtoul(buffer, NULL, 16); // NOLINT
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-11-21 08:36:30 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
void OS::LogSharedLibraryAddresses() {
|
|
|
|
#ifdef ENABLE_LOGGING_AND_PROFILING
|
|
|
|
static const int MAP_LENGTH = 1024;
|
|
|
|
int fd = open("/proc/self/maps", O_RDONLY);
|
|
|
|
if (fd < 0) return;
|
|
|
|
while (true) {
|
|
|
|
char addr_buffer[11];
|
|
|
|
addr_buffer[0] = '0';
|
|
|
|
addr_buffer[1] = 'x';
|
|
|
|
addr_buffer[10] = 0;
|
2008-09-05 07:46:32 +00:00
|
|
|
int result = read(fd, addr_buffer + 2, 8);
|
|
|
|
if (result < 8) break;
|
2009-06-16 12:52:02 +00:00
|
|
|
uintptr_t start = StringToULong(addr_buffer);
|
2008-09-05 07:46:32 +00:00
|
|
|
result = read(fd, addr_buffer + 2, 1);
|
|
|
|
if (result < 1) break;
|
|
|
|
if (addr_buffer[2] != '-') break;
|
|
|
|
result = read(fd, addr_buffer + 2, 8);
|
|
|
|
if (result < 8) break;
|
2009-06-16 12:52:02 +00:00
|
|
|
uintptr_t end = StringToULong(addr_buffer);
|
2008-07-03 15:10:15 +00:00
|
|
|
char buffer[MAP_LENGTH];
|
|
|
|
int bytes_read = -1;
|
|
|
|
do {
|
|
|
|
bytes_read++;
|
2008-09-03 08:19:44 +00:00
|
|
|
if (bytes_read >= MAP_LENGTH - 1)
|
2008-07-03 15:10:15 +00:00
|
|
|
break;
|
2008-09-05 07:46:32 +00:00
|
|
|
result = read(fd, buffer + bytes_read, 1);
|
|
|
|
if (result < 1) break;
|
2008-07-03 15:10:15 +00:00
|
|
|
} while (buffer[bytes_read] != '\n');
|
|
|
|
buffer[bytes_read] = 0;
|
|
|
|
// Ignore mappings that are not executable.
|
|
|
|
if (buffer[3] != 'x') continue;
|
2008-09-23 08:56:12 +00:00
|
|
|
char* start_of_path = index(buffer, '/');
|
2009-06-16 12:52:02 +00:00
|
|
|
// If there is no filename for this line then log it as an anonymous
|
|
|
|
// mapping and use the address as its name.
|
|
|
|
if (start_of_path == NULL) {
|
|
|
|
// 40 is enough to print a 64 bit address range.
|
|
|
|
ASSERT(sizeof(buffer) > 40);
|
|
|
|
snprintf(buffer,
|
|
|
|
sizeof(buffer),
|
|
|
|
"%08" V8PRIxPTR "-%08" V8PRIxPTR,
|
|
|
|
start,
|
|
|
|
end);
|
|
|
|
LOG(SharedLibraryEvent(buffer, start, end));
|
|
|
|
} else {
|
|
|
|
buffer[bytes_read] = 0;
|
|
|
|
LOG(SharedLibraryEvent(start_of_path, start, end));
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2008-09-05 07:46:32 +00:00
|
|
|
close(fd);
|
2008-07-03 15:10:15 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-16 21:01:05 +00:00
|
|
|
int OS::StackWalk(Vector<OS::StackFrame> frames) {
|
2009-03-23 12:37:53 +00:00
|
|
|
// backtrace is a glibc extension.
|
|
|
|
#ifdef __GLIBC__
|
2009-04-16 21:01:05 +00:00
|
|
|
int frames_size = frames.length();
|
2008-07-03 15:10:15 +00:00
|
|
|
void** addresses = NewArray<void*>(frames_size);
|
|
|
|
|
|
|
|
int frames_count = backtrace(addresses, frames_size);
|
|
|
|
|
|
|
|
char** symbols;
|
|
|
|
symbols = backtrace_symbols(addresses, frames_count);
|
|
|
|
if (symbols == NULL) {
|
|
|
|
DeleteArray(addresses);
|
|
|
|
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.
|
2008-09-11 14:34:48 +00:00
|
|
|
SNPrintF(MutableCStrVector(frames[i].text, kStackWalkMaxTextLen),
|
|
|
|
"%s",
|
|
|
|
symbols[i]);
|
2008-07-03 15:10:15 +00:00
|
|
|
// Make sure line termination is in place.
|
|
|
|
frames[i].text[kStackWalkMaxTextLen - 1] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
DeleteArray(addresses);
|
|
|
|
free(symbols);
|
|
|
|
|
|
|
|
return frames_count;
|
2009-03-23 12:37:53 +00:00
|
|
|
#else // ndef __GLIBC__
|
|
|
|
return 0;
|
|
|
|
#endif // ndef __GLIBC__
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Constants used for mmap.
|
|
|
|
static const int kMmapFd = -1;
|
|
|
|
static const int kMmapFdOffset = 0;
|
|
|
|
|
|
|
|
|
2008-09-05 12:34:09 +00:00
|
|
|
VirtualMemory::VirtualMemory(size_t size) {
|
|
|
|
address_ = mmap(NULL, size, PROT_NONE,
|
2008-07-03 15:10:15 +00:00
|
|
|
MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE,
|
|
|
|
kMmapFd, kMmapFdOffset);
|
|
|
|
size_ = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
VirtualMemory::~VirtualMemory() {
|
|
|
|
if (IsReserved()) {
|
|
|
|
if (0 == munmap(address(), size())) address_ = MAP_FAILED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool VirtualMemory::IsReserved() {
|
|
|
|
return address_ != MAP_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-24 12:47:53 +00:00
|
|
|
bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
|
|
|
|
int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
|
2008-07-30 08:49:36 +00:00
|
|
|
if (MAP_FAILED == mmap(address, size, prot,
|
2008-07-03 15:10:15 +00:00
|
|
|
MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
|
|
|
|
kMmapFd, kMmapFdOffset)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateAllocatedSpaceLimits(address, size);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool VirtualMemory::Uncommit(void* address, size_t size) {
|
|
|
|
return mmap(address, size, PROT_NONE,
|
|
|
|
MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE,
|
|
|
|
kMmapFd, kMmapFdOffset) != MAP_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class ThreadHandle::PlatformData : public Malloced {
|
|
|
|
public:
|
|
|
|
explicit PlatformData(ThreadHandle::Kind kind) {
|
|
|
|
Initialize(kind);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Initialize(ThreadHandle::Kind kind) {
|
|
|
|
switch (kind) {
|
|
|
|
case ThreadHandle::SELF: thread_ = pthread_self(); break;
|
|
|
|
case ThreadHandle::INVALID: thread_ = kNoThread; break;
|
|
|
|
}
|
|
|
|
}
|
2009-03-27 00:24:49 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
pthread_t thread_; // Thread handle for pthread.
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
ThreadHandle::ThreadHandle(Kind kind) {
|
|
|
|
data_ = new PlatformData(kind);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ThreadHandle::Initialize(ThreadHandle::Kind kind) {
|
|
|
|
data_->Initialize(kind);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ThreadHandle::~ThreadHandle() {
|
|
|
|
delete data_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ThreadHandle::IsSelf() const {
|
|
|
|
return pthread_equal(data_->thread_, pthread_self());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ThreadHandle::IsValid() const {
|
|
|
|
return data_->thread_ != kNoThread;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Thread::Thread() : ThreadHandle(ThreadHandle::INVALID) {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Thread::~Thread() {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
thread->thread_handle_data()->thread_ = pthread_self();
|
|
|
|
ASSERT(thread->IsValid());
|
|
|
|
thread->Run();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Thread::Start() {
|
|
|
|
pthread_create(&thread_handle_data()->thread_, NULL, ThreadEntry, this);
|
|
|
|
ASSERT(IsValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Thread::Join() {
|
|
|
|
pthread_join(thread_handle_data()->thread_, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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 LinuxMutex : public Mutex {
|
|
|
|
public:
|
|
|
|
|
|
|
|
LinuxMutex() {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~LinuxMutex() { 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
pthread_mutex_t mutex_; // Pthread mutex for POSIX platforms.
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Mutex* OS::CreateMutex() {
|
|
|
|
return new LinuxMutex();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class LinuxSemaphore : public Semaphore {
|
|
|
|
public:
|
|
|
|
explicit LinuxSemaphore(int count) { sem_init(&sem_, 0, count); }
|
|
|
|
virtual ~LinuxSemaphore() { sem_destroy(&sem_); }
|
|
|
|
|
2008-07-30 08:49:36 +00:00
|
|
|
virtual void Wait();
|
2009-03-18 09:04:24 +00:00
|
|
|
virtual bool Wait(int timeout);
|
2008-07-03 15:10:15 +00:00
|
|
|
virtual void Signal() { sem_post(&sem_); }
|
|
|
|
private:
|
|
|
|
sem_t sem_;
|
|
|
|
};
|
|
|
|
|
2009-03-18 09:04:24 +00:00
|
|
|
|
2008-07-30 08:49:36 +00:00
|
|
|
void LinuxSemaphore::Wait() {
|
|
|
|
while (true) {
|
|
|
|
int result = sem_wait(&sem_);
|
|
|
|
if (result == 0) return; // Successfully got semaphore.
|
|
|
|
CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup.
|
|
|
|
}
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-03-18 09:04:24 +00:00
|
|
|
|
2009-04-17 13:27:36 +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; \
|
2009-04-17 13:57:15 +00:00
|
|
|
} while (false)
|
|
|
|
#endif
|
2009-04-17 13:27:36 +00:00
|
|
|
|
|
|
|
|
2009-03-18 09:04:24 +00:00
|
|
|
bool LinuxSemaphore::Wait(int timeout) {
|
|
|
|
const long kOneSecondMicros = 1000000; // NOLINT
|
|
|
|
|
|
|
|
// Split timeout into second and nanosecond parts.
|
2009-04-17 00:56:52 +00:00
|
|
|
struct timeval delta;
|
|
|
|
delta.tv_usec = timeout % kOneSecondMicros;
|
|
|
|
delta.tv_sec = timeout / kOneSecondMicros;
|
2009-03-18 09:04:24 +00:00
|
|
|
|
2009-04-17 00:56:52 +00:00
|
|
|
struct timeval current_time;
|
|
|
|
// Get the current time.
|
|
|
|
if (gettimeofday(¤t_time, NULL) == -1) {
|
2009-03-18 09:04:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-04-17 00:56:52 +00:00
|
|
|
// Calculate time for end of timeout.
|
|
|
|
struct timeval end_time;
|
|
|
|
timeradd(¤t_time, &delta, &end_time);
|
2009-03-18 09:04:24 +00:00
|
|
|
|
2009-04-17 00:56:52 +00:00
|
|
|
struct timespec ts;
|
|
|
|
TIMEVAL_TO_TIMESPEC(&end_time, &ts);
|
2009-03-18 09:04:24 +00:00
|
|
|
// Wait for semaphore signalled or timeout.
|
|
|
|
while (true) {
|
|
|
|
int result = sem_timedwait(&sem_, &ts);
|
|
|
|
if (result == 0) return true; // Successfully got semaphore.
|
2009-03-18 10:48:06 +00:00
|
|
|
if (result > 0) {
|
|
|
|
// For glibc prior to 2.3.4 sem_timedwait returns the error instead of -1.
|
|
|
|
errno = result;
|
|
|
|
result = -1;
|
|
|
|
}
|
2009-03-18 09:04:24 +00:00
|
|
|
if (result == -1 && errno == ETIMEDOUT) return false; // Timeout.
|
|
|
|
CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
Semaphore* OS::CreateSemaphore(int count) {
|
|
|
|
return new LinuxSemaphore(count);
|
|
|
|
}
|
|
|
|
|
2009-02-24 13:32:01 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
#ifdef ENABLE_LOGGING_AND_PROFILING
|
|
|
|
|
2008-08-22 13:33:59 +00:00
|
|
|
static Sampler* active_sampler_ = NULL;
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-04-07 13:33:39 +00:00
|
|
|
|
|
|
|
#if !defined(__GLIBC__) && (defined(__arm__) || defined(__thumb__))
|
|
|
|
// Android runs a fairly new Linux kernel, so signal info is there,
|
|
|
|
// but the C library doesn't have the structs defined.
|
|
|
|
|
|
|
|
struct sigcontext {
|
|
|
|
uint32_t trap_no;
|
|
|
|
uint32_t error_code;
|
|
|
|
uint32_t oldmask;
|
|
|
|
uint32_t gregs[16];
|
|
|
|
uint32_t arm_cpsr;
|
|
|
|
uint32_t fault_address;
|
|
|
|
};
|
|
|
|
typedef uint32_t __sigset_t;
|
|
|
|
typedef struct sigcontext mcontext_t;
|
|
|
|
typedef struct ucontext {
|
|
|
|
uint32_t uc_flags;
|
|
|
|
struct ucontext *uc_link;
|
|
|
|
stack_t uc_stack;
|
|
|
|
mcontext_t uc_mcontext;
|
|
|
|
__sigset_t uc_sigmask;
|
|
|
|
} ucontext_t;
|
|
|
|
enum ArmRegisters {R15 = 15, R13 = 13, R11 = 11};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) {
|
|
|
|
USE(info);
|
|
|
|
if (signal != SIGPROF) return;
|
2008-08-22 13:33:59 +00:00
|
|
|
if (active_sampler_ == NULL) return;
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
TickSample sample;
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// If profiling, we extract the current pc and sp.
|
|
|
|
if (active_sampler_->IsProfiling()) {
|
|
|
|
// Extracting the sample from the context is extremely machine dependent.
|
|
|
|
ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context);
|
|
|
|
mcontext_t& mcontext = ucontext->uc_mcontext;
|
2009-05-06 11:40:44 +00:00
|
|
|
#if V8_HOST_ARCH_IA32
|
2008-08-22 13:33:59 +00:00
|
|
|
sample.pc = mcontext.gregs[REG_EIP];
|
|
|
|
sample.sp = mcontext.gregs[REG_ESP];
|
2009-02-17 14:45:45 +00:00
|
|
|
sample.fp = mcontext.gregs[REG_EBP];
|
2009-05-06 11:19:50 +00:00
|
|
|
#elif V8_HOST_ARCH_X64
|
|
|
|
sample.pc = mcontext.gregs[REG_RIP];
|
|
|
|
sample.sp = mcontext.gregs[REG_RSP];
|
|
|
|
sample.fp = mcontext.gregs[REG_RBP];
|
|
|
|
#elif V8_HOST_ARCH_ARM
|
2009-05-13 13:44:25 +00:00
|
|
|
// An undefined macro evaluates to 0, so this applies to Android's Bionic also.
|
|
|
|
#if (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3))
|
2009-05-06 11:19:50 +00:00
|
|
|
sample.pc = mcontext.gregs[R15];
|
|
|
|
sample.sp = mcontext.gregs[R13];
|
|
|
|
sample.fp = mcontext.gregs[R11];
|
2009-05-13 13:44:25 +00:00
|
|
|
#else
|
|
|
|
sample.pc = mcontext.arm_pc;
|
|
|
|
sample.sp = mcontext.arm_sp;
|
|
|
|
sample.fp = mcontext.arm_fp;
|
|
|
|
#endif
|
2008-07-03 15:10:15 +00:00
|
|
|
#endif
|
2009-07-01 08:46:59 +00:00
|
|
|
active_sampler_->SampleStack(&sample);
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We always sample the VM state.
|
2008-07-03 15:10:15 +00:00
|
|
|
sample.state = Logger::state();
|
|
|
|
|
|
|
|
active_sampler_->Tick(&sample);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-22 13:33:59 +00:00
|
|
|
class Sampler::PlatformData : public Malloced {
|
2008-07-03 15:10:15 +00:00
|
|
|
public:
|
|
|
|
PlatformData() {
|
|
|
|
signal_handler_installed_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool signal_handler_installed_;
|
|
|
|
struct sigaction old_signal_handler_;
|
|
|
|
struct itimerval old_timer_value_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-08-22 13:33:59 +00:00
|
|
|
Sampler::Sampler(int interval, bool profiling)
|
|
|
|
: interval_(interval), profiling_(profiling), active_(false) {
|
2008-07-03 15:10:15 +00:00
|
|
|
data_ = new PlatformData();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-22 13:33:59 +00:00
|
|
|
Sampler::~Sampler() {
|
2008-07-03 15:10:15 +00:00
|
|
|
delete data_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-22 13:33:59 +00:00
|
|
|
void Sampler::Start() {
|
2008-07-03 15:10:15 +00:00
|
|
|
// There can only be one active sampler at the time on POSIX
|
|
|
|
// platforms.
|
|
|
|
if (active_sampler_ != NULL) return;
|
|
|
|
|
|
|
|
// Request profiling signals.
|
|
|
|
struct sigaction sa;
|
|
|
|
sa.sa_sigaction = ProfilerSignalHandler;
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sa.sa_flags = SA_SIGINFO;
|
|
|
|
if (sigaction(SIGPROF, &sa, &data_->old_signal_handler_) != 0) return;
|
|
|
|
data_->signal_handler_installed_ = true;
|
|
|
|
|
|
|
|
// Set the itimer to generate a tick for each interval.
|
|
|
|
itimerval itimer;
|
|
|
|
itimer.it_interval.tv_sec = interval_ / 1000;
|
|
|
|
itimer.it_interval.tv_usec = (interval_ % 1000) * 1000;
|
|
|
|
itimer.it_value.tv_sec = itimer.it_interval.tv_sec;
|
|
|
|
itimer.it_value.tv_usec = itimer.it_interval.tv_usec;
|
|
|
|
setitimer(ITIMER_PROF, &itimer, &data_->old_timer_value_);
|
|
|
|
|
|
|
|
// Set this sampler as the active sampler.
|
|
|
|
active_sampler_ = this;
|
|
|
|
active_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-22 13:33:59 +00:00
|
|
|
void Sampler::Stop() {
|
2008-07-03 15:10:15 +00:00
|
|
|
// Restore old signal handler
|
|
|
|
if (data_->signal_handler_installed_) {
|
|
|
|
setitimer(ITIMER_PROF, &data_->old_timer_value_, NULL);
|
|
|
|
sigaction(SIGPROF, &data_->old_signal_handler_, 0);
|
|
|
|
data_->signal_handler_installed_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This sampler is no longer the active sampler.
|
|
|
|
active_sampler_ = NULL;
|
|
|
|
active_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // ENABLE_LOGGING_AND_PROFILING
|
|
|
|
|
|
|
|
} } // namespace v8::internal
|