platform-freebsd.cc revision 5d4cdbf7a67d3662fa0bee4efdb7edd8daec9b0b
1// Copyright 2012 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6//     * Redistributions of source code must retain the above copyright
7//       notice, this list of conditions and the following disclaimer.
8//     * Redistributions in binary form must reproduce the above
9//       copyright notice, this list of conditions and the following
10//       disclaimer in the documentation and/or other materials provided
11//       with the distribution.
12//     * Neither the name of Google Inc. nor the names of its
13//       contributors may be used to endorse or promote products derived
14//       from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28// Platform specific code for FreeBSD goes here. For the POSIX comaptible parts
29// the implementation is in platform-posix.cc.
30
31#include <pthread.h>
32#include <semaphore.h>
33#include <signal.h>
34#include <sys/time.h>
35#include <sys/resource.h>
36#include <sys/types.h>
37#include <sys/ucontext.h>
38#include <stdlib.h>
39
40#include <sys/types.h>  // mmap & munmap
41#include <sys/mman.h>   // mmap & munmap
42#include <sys/stat.h>   // open
43#include <sys/fcntl.h>  // open
44#include <unistd.h>     // getpagesize
45// If you don't have execinfo.h then you need devel/libexecinfo from ports.
46#include <execinfo.h>   // backtrace, backtrace_symbols
47#include <strings.h>    // index
48#include <errno.h>
49#include <stdarg.h>
50#include <limits.h>
51
52#undef MAP_TYPE
53
54#include "v8.h"
55#include "v8threads.h"
56
57#include "platform.h"
58#include "vm-state-inl.h"
59
60
61namespace v8 {
62namespace internal {
63
64// 0 is never a valid thread id on FreeBSD since tids and pids share a
65// name space and pid 0 is used to kill the group (see man 2 kill).
66static const pthread_t kNoThread = (pthread_t) 0;
67
68
69double ceiling(double x) {
70    // Correct as on OS X
71    if (-1.0 < x && x < 0.0) {
72        return -0.0;
73    } else {
74        return ceil(x);
75    }
76}
77
78
79static Mutex* limit_mutex = NULL;
80
81
82void OS::SetUp() {
83  // Seed the random number generator.
84  // Convert the current time to a 64-bit integer first, before converting it
85  // to an unsigned. Going directly can cause an overflow and the seed to be
86  // set to all ones. The seed will be identical for different instances that
87  // call this setup code within the same millisecond.
88  uint64_t seed = static_cast<uint64_t>(TimeCurrentMillis());
89  srandom(static_cast<unsigned int>(seed));
90  limit_mutex = CreateMutex();
91}
92
93
94void OS::ReleaseStore(volatile AtomicWord* ptr, AtomicWord value) {
95  __asm__ __volatile__("" : : : "memory");
96  *ptr = value;
97}
98
99
100uint64_t OS::CpuFeaturesImpliedByPlatform() {
101  return 0;  // FreeBSD runs on anything.
102}
103
104
105int OS::ActivationFrameAlignment() {
106  // 16 byte alignment on FreeBSD
107  return 16;
108}
109
110
111const char* OS::LocalTimezone(double time) {
112  if (isnan(time)) return "";
113  time_t tv = static_cast<time_t>(floor(time/msPerSecond));
114  struct tm* t = localtime(&tv);
115  if (NULL == t) return "";
116  return t->tm_zone;
117}
118
119
120double OS::LocalTimeOffset() {
121  time_t tv = time(NULL);
122  struct tm* t = localtime(&tv);
123  // tm_gmtoff includes any daylight savings offset, so subtract it.
124  return static_cast<double>(t->tm_gmtoff * msPerSecond -
125                             (t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
126}
127
128
129// We keep the lowest and highest addresses mapped as a quick way of
130// determining that pointers are outside the heap (used mostly in assertions
131// and verification).  The estimate is conservative, i.e., not all addresses in
132// 'allocated' space are actually allocated to our heap.  The range is
133// [lowest, highest), inclusive on the low and and exclusive on the high end.
134static void* lowest_ever_allocated = reinterpret_cast<void*>(-1);
135static void* highest_ever_allocated = reinterpret_cast<void*>(0);
136
137
138static void UpdateAllocatedSpaceLimits(void* address, int size) {
139  ASSERT(limit_mutex != NULL);
140  ScopedLock lock(limit_mutex);
141
142  lowest_ever_allocated = Min(lowest_ever_allocated, address);
143  highest_ever_allocated =
144      Max(highest_ever_allocated,
145          reinterpret_cast<void*>(reinterpret_cast<char*>(address) + size));
146}
147
148
149bool OS::IsOutsideAllocatedSpace(void* address) {
150  return address < lowest_ever_allocated || address >= highest_ever_allocated;
151}
152
153
154size_t OS::AllocateAlignment() {
155  return getpagesize();
156}
157
158
159void* OS::Allocate(const size_t requested,
160                   size_t* allocated,
161                   bool executable) {
162  const size_t msize = RoundUp(requested, getpagesize());
163  int prot = PROT_READ | PROT_WRITE | (executable ? PROT_EXEC : 0);
164  void* mbase = mmap(NULL, msize, prot, MAP_PRIVATE | MAP_ANON, -1, 0);
165
166  if (mbase == MAP_FAILED) {
167    LOG(ISOLATE, StringEvent("OS::Allocate", "mmap failed"));
168    return NULL;
169  }
170  *allocated = msize;
171  UpdateAllocatedSpaceLimits(mbase, msize);
172  return mbase;
173}
174
175
176void OS::Free(void* buf, const size_t length) {
177  // TODO(1240712): munmap has a return value which is ignored here.
178  int result = munmap(buf, length);
179  USE(result);
180  ASSERT(result == 0);
181}
182
183
184void OS::Sleep(int milliseconds) {
185  unsigned int ms = static_cast<unsigned int>(milliseconds);
186  usleep(1000 * ms);
187}
188
189
190void OS::Abort() {
191  // Redirect to std abort to signal abnormal program termination.
192  abort();
193}
194
195
196void OS::DebugBreak() {
197#if (defined(__arm__) || defined(__thumb__))
198# if defined(CAN_USE_ARMV5_INSTRUCTIONS)
199  asm("bkpt 0");
200# endif
201#else
202  asm("int $3");
203#endif
204}
205
206
207class PosixMemoryMappedFile : public OS::MemoryMappedFile {
208 public:
209  PosixMemoryMappedFile(FILE* file, void* memory, int size)
210    : file_(file), memory_(memory), size_(size) { }
211  virtual ~PosixMemoryMappedFile();
212  virtual void* memory() { return memory_; }
213  virtual int size() { return size_; }
214 private:
215  FILE* file_;
216  void* memory_;
217  int size_;
218};
219
220
221OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
222  FILE* file = fopen(name, "r+");
223  if (file == NULL) return NULL;
224
225  fseek(file, 0, SEEK_END);
226  int size = ftell(file);
227
228  void* memory =
229      mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
230  return new PosixMemoryMappedFile(file, memory, size);
231}
232
233
234OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size,
235    void* initial) {
236  FILE* file = fopen(name, "w+");
237  if (file == NULL) return NULL;
238  int result = fwrite(initial, size, 1, file);
239  if (result < 1) {
240    fclose(file);
241    return NULL;
242  }
243  void* memory =
244      mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
245  return new PosixMemoryMappedFile(file, memory, size);
246}
247
248
249PosixMemoryMappedFile::~PosixMemoryMappedFile() {
250  if (memory_) munmap(memory_, size_);
251  fclose(file_);
252}
253
254
255static unsigned StringToLong(char* buffer) {
256  return static_cast<unsigned>(strtol(buffer, NULL, 16));  // NOLINT
257}
258
259
260void OS::LogSharedLibraryAddresses() {
261  static const int MAP_LENGTH = 1024;
262  int fd = open("/proc/self/maps", O_RDONLY);
263  if (fd < 0) return;
264  while (true) {
265    char addr_buffer[11];
266    addr_buffer[0] = '0';
267    addr_buffer[1] = 'x';
268    addr_buffer[10] = 0;
269    int result = read(fd, addr_buffer + 2, 8);
270    if (result < 8) break;
271    unsigned start = StringToLong(addr_buffer);
272    result = read(fd, addr_buffer + 2, 1);
273    if (result < 1) break;
274    if (addr_buffer[2] != '-') break;
275    result = read(fd, addr_buffer + 2, 8);
276    if (result < 8) break;
277    unsigned end = StringToLong(addr_buffer);
278    char buffer[MAP_LENGTH];
279    int bytes_read = -1;
280    do {
281      bytes_read++;
282      if (bytes_read >= MAP_LENGTH - 1)
283        break;
284      result = read(fd, buffer + bytes_read, 1);
285      if (result < 1) break;
286    } while (buffer[bytes_read] != '\n');
287    buffer[bytes_read] = 0;
288    // Ignore mappings that are not executable.
289    if (buffer[3] != 'x') continue;
290    char* start_of_path = index(buffer, '/');
291    // There may be no filename in this line.  Skip to next.
292    if (start_of_path == NULL) continue;
293    buffer[bytes_read] = 0;
294    LOG(i::Isolate::Current(), SharedLibraryEvent(start_of_path, start, end));
295  }
296  close(fd);
297}
298
299
300void OS::SignalCodeMovingGC() {
301}
302
303
304int OS::StackWalk(Vector<OS::StackFrame> frames) {
305  int frames_size = frames.length();
306  ScopedVector<void*> addresses(frames_size);
307
308  int frames_count = backtrace(addresses.start(), frames_size);
309
310  char** symbols = backtrace_symbols(addresses.start(), frames_count);
311  if (symbols == NULL) {
312    return kStackWalkError;
313  }
314
315  for (int i = 0; i < frames_count; i++) {
316    frames[i].address = addresses[i];
317    // Format a text representation of the frame based on the information
318    // available.
319    SNPrintF(MutableCStrVector(frames[i].text, kStackWalkMaxTextLen),
320             "%s",
321             symbols[i]);
322    // Make sure line termination is in place.
323    frames[i].text[kStackWalkMaxTextLen - 1] = '\0';
324  }
325
326  free(symbols);
327
328  return frames_count;
329}
330
331
332// Constants used for mmap.
333static const int kMmapFd = -1;
334static const int kMmapFdOffset = 0;
335
336VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
337
338VirtualMemory::VirtualMemory(size_t size) {
339  address_ = ReserveRegion(size);
340  size_ = size;
341}
342
343
344VirtualMemory::VirtualMemory(size_t size, size_t alignment)
345    : address_(NULL), size_(0) {
346  ASSERT(IsAligned(alignment, static_cast<intptr_t>(OS::AllocateAlignment())));
347  size_t request_size = RoundUp(size + alignment,
348                                static_cast<intptr_t>(OS::AllocateAlignment()));
349  void* reservation = mmap(OS::GetRandomMmapAddr(),
350                           request_size,
351                           PROT_NONE,
352                           MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
353                           kMmapFd,
354                           kMmapFdOffset);
355  if (reservation == MAP_FAILED) return;
356
357  Address base = static_cast<Address>(reservation);
358  Address aligned_base = RoundUp(base, alignment);
359  ASSERT_LE(base, aligned_base);
360
361  // Unmap extra memory reserved before and after the desired block.
362  if (aligned_base != base) {
363    size_t prefix_size = static_cast<size_t>(aligned_base - base);
364    OS::Free(base, prefix_size);
365    request_size -= prefix_size;
366  }
367
368  size_t aligned_size = RoundUp(size, OS::AllocateAlignment());
369  ASSERT_LE(aligned_size, request_size);
370
371  if (aligned_size != request_size) {
372    size_t suffix_size = request_size - aligned_size;
373    OS::Free(aligned_base + aligned_size, suffix_size);
374    request_size -= suffix_size;
375  }
376
377  ASSERT(aligned_size == request_size);
378
379  address_ = static_cast<void*>(aligned_base);
380  size_ = aligned_size;
381}
382
383
384VirtualMemory::~VirtualMemory() {
385  if (IsReserved()) {
386    bool result = ReleaseRegion(address(), size());
387    ASSERT(result);
388    USE(result);
389  }
390}
391
392
393bool VirtualMemory::IsReserved() {
394  return address_ != NULL;
395}
396
397
398void VirtualMemory::Reset() {
399  address_ = NULL;
400  size_ = 0;
401}
402
403
404bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
405  return CommitRegion(address, size, is_executable);
406}
407
408
409bool VirtualMemory::Uncommit(void* address, size_t size) {
410  return UncommitRegion(address, size);
411}
412
413
414bool VirtualMemory::Guard(void* address) {
415  OS::Guard(address, OS::CommitPageSize());
416  return true;
417}
418
419
420void* VirtualMemory::ReserveRegion(size_t size) {
421  void* result = mmap(OS::GetRandomMmapAddr(),
422                      size,
423                      PROT_NONE,
424                      MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
425                      kMmapFd,
426                      kMmapFdOffset);
427
428  if (result == MAP_FAILED) return NULL;
429
430  return result;
431}
432
433
434bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) {
435  int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
436  if (MAP_FAILED == mmap(base,
437                         size,
438                         prot,
439                         MAP_PRIVATE | MAP_ANON | MAP_FIXED,
440                         kMmapFd,
441                         kMmapFdOffset)) {
442    return false;
443  }
444
445  UpdateAllocatedSpaceLimits(base, size);
446  return true;
447}
448
449
450bool VirtualMemory::UncommitRegion(void* base, size_t size) {
451  return mmap(base,
452              size,
453              PROT_NONE,
454              MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | MAP_FIXED,
455              kMmapFd,
456              kMmapFdOffset) != MAP_FAILED;
457}
458
459
460bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
461  return munmap(base, size) == 0;
462}
463
464
465class Thread::PlatformData : public Malloced {
466 public:
467  pthread_t thread_;  // Thread handle for pthread.
468};
469
470
471Thread::Thread(const Options& options)
472    : data_(new PlatformData),
473      stack_size_(options.stack_size()) {
474  set_name(options.name());
475}
476
477
478Thread::~Thread() {
479  delete data_;
480}
481
482
483static void* ThreadEntry(void* arg) {
484  Thread* thread = reinterpret_cast<Thread*>(arg);
485  // This is also initialized by the first argument to pthread_create() but we
486  // don't know which thread will run first (the original thread or the new
487  // one) so we initialize it here too.
488  thread->data()->thread_ = pthread_self();
489  ASSERT(thread->data()->thread_ != kNoThread);
490  thread->Run();
491  return NULL;
492}
493
494
495void Thread::set_name(const char* name) {
496  strncpy(name_, name, sizeof(name_));
497  name_[sizeof(name_) - 1] = '\0';
498}
499
500
501void Thread::Start() {
502  pthread_attr_t* attr_ptr = NULL;
503  pthread_attr_t attr;
504  if (stack_size_ > 0) {
505    pthread_attr_init(&attr);
506    pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_));
507    attr_ptr = &attr;
508  }
509  pthread_create(&data_->thread_, attr_ptr, ThreadEntry, this);
510  ASSERT(data_->thread_ != kNoThread);
511}
512
513
514void Thread::Join() {
515  pthread_join(data_->thread_, NULL);
516}
517
518
519Thread::LocalStorageKey Thread::CreateThreadLocalKey() {
520  pthread_key_t key;
521  int result = pthread_key_create(&key, NULL);
522  USE(result);
523  ASSERT(result == 0);
524  return static_cast<LocalStorageKey>(key);
525}
526
527
528void Thread::DeleteThreadLocalKey(LocalStorageKey key) {
529  pthread_key_t pthread_key = static_cast<pthread_key_t>(key);
530  int result = pthread_key_delete(pthread_key);
531  USE(result);
532  ASSERT(result == 0);
533}
534
535
536void* Thread::GetThreadLocal(LocalStorageKey key) {
537  pthread_key_t pthread_key = static_cast<pthread_key_t>(key);
538  return pthread_getspecific(pthread_key);
539}
540
541
542void Thread::SetThreadLocal(LocalStorageKey key, void* value) {
543  pthread_key_t pthread_key = static_cast<pthread_key_t>(key);
544  pthread_setspecific(pthread_key, value);
545}
546
547
548void Thread::YieldCPU() {
549  sched_yield();
550}
551
552
553class FreeBSDMutex : public Mutex {
554 public:
555  FreeBSDMutex() {
556    pthread_mutexattr_t attrs;
557    int result = pthread_mutexattr_init(&attrs);
558    ASSERT(result == 0);
559    result = pthread_mutexattr_settype(&attrs, PTHREAD_MUTEX_RECURSIVE);
560    ASSERT(result == 0);
561    result = pthread_mutex_init(&mutex_, &attrs);
562    ASSERT(result == 0);
563  }
564
565  virtual ~FreeBSDMutex() { pthread_mutex_destroy(&mutex_); }
566
567  virtual int Lock() {
568    int result = pthread_mutex_lock(&mutex_);
569    return result;
570  }
571
572  virtual int Unlock() {
573    int result = pthread_mutex_unlock(&mutex_);
574    return result;
575  }
576
577  virtual bool TryLock() {
578    int result = pthread_mutex_trylock(&mutex_);
579    // Return false if the lock is busy and locking failed.
580    if (result == EBUSY) {
581      return false;
582    }
583    ASSERT(result == 0);  // Verify no other errors.
584    return true;
585  }
586
587 private:
588  pthread_mutex_t mutex_;   // Pthread mutex for POSIX platforms.
589};
590
591
592Mutex* OS::CreateMutex() {
593  return new FreeBSDMutex();
594}
595
596
597class FreeBSDSemaphore : public Semaphore {
598 public:
599  explicit FreeBSDSemaphore(int count) {  sem_init(&sem_, 0, count); }
600  virtual ~FreeBSDSemaphore() { sem_destroy(&sem_); }
601
602  virtual void Wait();
603  virtual bool Wait(int timeout);
604  virtual void Signal() { sem_post(&sem_); }
605 private:
606  sem_t sem_;
607};
608
609
610void FreeBSDSemaphore::Wait() {
611  while (true) {
612    int result = sem_wait(&sem_);
613    if (result == 0) return;  // Successfully got semaphore.
614    CHECK(result == -1 && errno == EINTR);  // Signal caused spurious wakeup.
615  }
616}
617
618
619bool FreeBSDSemaphore::Wait(int timeout) {
620  const long kOneSecondMicros = 1000000;  // NOLINT
621
622  // Split timeout into second and nanosecond parts.
623  struct timeval delta;
624  delta.tv_usec = timeout % kOneSecondMicros;
625  delta.tv_sec = timeout / kOneSecondMicros;
626
627  struct timeval current_time;
628  // Get the current time.
629  if (gettimeofday(&current_time, NULL) == -1) {
630    return false;
631  }
632
633  // Calculate time for end of timeout.
634  struct timeval end_time;
635  timeradd(&current_time, &delta, &end_time);
636
637  struct timespec ts;
638  TIMEVAL_TO_TIMESPEC(&end_time, &ts);
639  while (true) {
640    int result = sem_timedwait(&sem_, &ts);
641    if (result == 0) return true;  // Successfully got semaphore.
642    if (result == -1 && errno == ETIMEDOUT) return false;  // Timeout.
643    CHECK(result == -1 && errno == EINTR);  // Signal caused spurious wakeup.
644  }
645}
646
647
648Semaphore* OS::CreateSemaphore(int count) {
649  return new FreeBSDSemaphore(count);
650}
651
652
653static pthread_t GetThreadID() {
654  pthread_t thread_id = pthread_self();
655  return thread_id;
656}
657
658
659class Sampler::PlatformData : public Malloced {
660 public:
661  PlatformData() : vm_tid_(GetThreadID()) {}
662
663  pthread_t vm_tid() const { return vm_tid_; }
664
665 private:
666  pthread_t vm_tid_;
667};
668
669
670static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) {
671  USE(info);
672  if (signal != SIGPROF) return;
673  Isolate* isolate = Isolate::UncheckedCurrent();
674  if (isolate == NULL || !isolate->IsInitialized() || !isolate->IsInUse()) {
675    // We require a fully initialized and entered isolate.
676    return;
677  }
678  if (v8::Locker::IsActive() &&
679      !isolate->thread_manager()->IsLockedByCurrentThread()) {
680    return;
681  }
682
683  Sampler* sampler = isolate->logger()->sampler();
684  if (sampler == NULL || !sampler->IsActive()) return;
685
686  TickSample sample_obj;
687  TickSample* sample = CpuProfiler::TickSampleEvent(isolate);
688  if (sample == NULL) sample = &sample_obj;
689
690  // Extracting the sample from the context is extremely machine dependent.
691  ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context);
692  mcontext_t& mcontext = ucontext->uc_mcontext;
693  sample->state = isolate->current_vm_state();
694#if V8_HOST_ARCH_IA32
695  sample->pc = reinterpret_cast<Address>(mcontext.mc_eip);
696  sample->sp = reinterpret_cast<Address>(mcontext.mc_esp);
697  sample->fp = reinterpret_cast<Address>(mcontext.mc_ebp);
698#elif V8_HOST_ARCH_X64
699  sample->pc = reinterpret_cast<Address>(mcontext.mc_rip);
700  sample->sp = reinterpret_cast<Address>(mcontext.mc_rsp);
701  sample->fp = reinterpret_cast<Address>(mcontext.mc_rbp);
702#elif V8_HOST_ARCH_ARM
703  sample->pc = reinterpret_cast<Address>(mcontext.mc_r15);
704  sample->sp = reinterpret_cast<Address>(mcontext.mc_r13);
705  sample->fp = reinterpret_cast<Address>(mcontext.mc_r11);
706#endif
707  sampler->SampleStack(sample);
708  sampler->Tick(sample);
709}
710
711
712class SignalSender : public Thread {
713 public:
714  enum SleepInterval {
715    HALF_INTERVAL,
716    FULL_INTERVAL
717  };
718
719  static const int kSignalSenderStackSize = 64 * KB;
720
721  explicit SignalSender(int interval)
722      : Thread(Thread::Options("SignalSender", kSignalSenderStackSize)),
723        interval_(interval) {}
724
725  static void AddActiveSampler(Sampler* sampler) {
726    ScopedLock lock(mutex_.Pointer());
727    SamplerRegistry::AddActiveSampler(sampler);
728    if (instance_ == NULL) {
729      // Install a signal handler.
730      struct sigaction sa;
731      sa.sa_sigaction = ProfilerSignalHandler;
732      sigemptyset(&sa.sa_mask);
733      sa.sa_flags = SA_RESTART | SA_SIGINFO;
734      signal_handler_installed_ =
735          (sigaction(SIGPROF, &sa, &old_signal_handler_) == 0);
736
737      // Start a thread that sends SIGPROF signal to VM threads.
738      instance_ = new SignalSender(sampler->interval());
739      instance_->Start();
740    } else {
741      ASSERT(instance_->interval_ == sampler->interval());
742    }
743  }
744
745  static void RemoveActiveSampler(Sampler* sampler) {
746    ScopedLock lock(mutex_.Pointer());
747    SamplerRegistry::RemoveActiveSampler(sampler);
748    if (SamplerRegistry::GetState() == SamplerRegistry::HAS_NO_SAMPLERS) {
749      RuntimeProfiler::StopRuntimeProfilerThreadBeforeShutdown(instance_);
750      delete instance_;
751      instance_ = NULL;
752
753      // Restore the old signal handler.
754      if (signal_handler_installed_) {
755        sigaction(SIGPROF, &old_signal_handler_, 0);
756        signal_handler_installed_ = false;
757      }
758    }
759  }
760
761  // Implement Thread::Run().
762  virtual void Run() {
763    SamplerRegistry::State state;
764    while ((state = SamplerRegistry::GetState()) !=
765           SamplerRegistry::HAS_NO_SAMPLERS) {
766      bool cpu_profiling_enabled =
767          (state == SamplerRegistry::HAS_CPU_PROFILING_SAMPLERS);
768      bool runtime_profiler_enabled = RuntimeProfiler::IsEnabled();
769      // When CPU profiling is enabled both JavaScript and C++ code is
770      // profiled. We must not suspend.
771      if (!cpu_profiling_enabled) {
772        if (rate_limiter_.SuspendIfNecessary()) continue;
773      }
774      if (cpu_profiling_enabled && runtime_profiler_enabled) {
775        if (!SamplerRegistry::IterateActiveSamplers(&DoCpuProfile, this)) {
776          return;
777        }
778        Sleep(HALF_INTERVAL);
779        if (!SamplerRegistry::IterateActiveSamplers(&DoRuntimeProfile, NULL)) {
780          return;
781        }
782        Sleep(HALF_INTERVAL);
783      } else {
784        if (cpu_profiling_enabled) {
785          if (!SamplerRegistry::IterateActiveSamplers(&DoCpuProfile,
786                                                      this)) {
787            return;
788          }
789        }
790        if (runtime_profiler_enabled) {
791          if (!SamplerRegistry::IterateActiveSamplers(&DoRuntimeProfile,
792                                                      NULL)) {
793            return;
794          }
795        }
796        Sleep(FULL_INTERVAL);
797      }
798    }
799  }
800
801  static void DoCpuProfile(Sampler* sampler, void* raw_sender) {
802    if (!sampler->IsProfiling()) return;
803    SignalSender* sender = reinterpret_cast<SignalSender*>(raw_sender);
804    sender->SendProfilingSignal(sampler->platform_data()->vm_tid());
805  }
806
807  static void DoRuntimeProfile(Sampler* sampler, void* ignored) {
808    if (!sampler->isolate()->IsInitialized()) return;
809    sampler->isolate()->runtime_profiler()->NotifyTick();
810  }
811
812  void SendProfilingSignal(pthread_t tid) {
813    if (!signal_handler_installed_) return;
814    pthread_kill(tid, SIGPROF);
815  }
816
817  void Sleep(SleepInterval full_or_half) {
818    // Convert ms to us and subtract 100 us to compensate delays
819    // occuring during signal delivery.
820    useconds_t interval = interval_ * 1000 - 100;
821    if (full_or_half == HALF_INTERVAL) interval /= 2;
822    int result = usleep(interval);
823#ifdef DEBUG
824    if (result != 0 && errno != EINTR) {
825      fprintf(stderr,
826              "SignalSender usleep error; interval = %u, errno = %d\n",
827              interval,
828              errno);
829      ASSERT(result == 0 || errno == EINTR);
830    }
831#endif
832    USE(result);
833  }
834
835  const int interval_;
836  RuntimeProfilerRateLimiter rate_limiter_;
837
838  // Protects the process wide state below.
839  static LazyMutex mutex_;
840  static SignalSender* instance_;
841  static bool signal_handler_installed_;
842  static struct sigaction old_signal_handler_;
843
844 private:
845  DISALLOW_COPY_AND_ASSIGN(SignalSender);
846};
847
848LazyMutex SignalSender::mutex_ = LAZY_MUTEX_INITIALIZER;
849SignalSender* SignalSender::instance_ = NULL;
850struct sigaction SignalSender::old_signal_handler_;
851bool SignalSender::signal_handler_installed_ = false;
852
853
854Sampler::Sampler(Isolate* isolate, int interval)
855    : isolate_(isolate),
856      interval_(interval),
857      profiling_(false),
858      active_(false),
859      samples_taken_(0) {
860  data_ = new PlatformData;
861}
862
863
864Sampler::~Sampler() {
865  ASSERT(!IsActive());
866  delete data_;
867}
868
869
870void Sampler::Start() {
871  ASSERT(!IsActive());
872  SetActive(true);
873  SignalSender::AddActiveSampler(this);
874}
875
876
877void Sampler::Stop() {
878  ASSERT(IsActive());
879  SignalSender::RemoveActiveSampler(this);
880  SetActive(false);
881}
882
883
884} }  // namespace v8::internal
885