Skip to content

Commit df9ab60

Browse files
* Fix build errors on FreeBSD 8.2
* Fix Crankshaft on FreeBSD. * Partially fix profiling on FreeBSD. * Remove bash-isms from tick processor script. Review URL: http://codereview.chromium.org/6673045 git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@7200 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
1 parent b0e3967 commit df9ab60

File tree

6 files changed

+145
-45
lines changed

6 files changed

+145
-45
lines changed

SConstruct

+1
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ LIBRARY_FLAGS = {
174174
'CPPPATH' : ['/usr/local/include'],
175175
'LIBPATH' : ['/usr/local/lib'],
176176
'CCFLAGS': ['-ansi'],
177+
'LIBS': ['execinfo']
177178
},
178179
'os:openbsd': {
179180
'CPPPATH' : ['/usr/local/include'],

src/d8-posix.cc

+2
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,10 @@ static Handle<Value> GetStdout(int child_fd,
375375
// a parent process hangs on waiting while a child process is already a zombie.
376376
// See http://code.google.com/p/v8/issues/detail?id=401.
377377
#if defined(WNOWAIT) && !defined(ANDROID) && !defined(__APPLE__)
378+
#if !defined(__FreeBSD__)
378379
#define HAS_WAITID 1
379380
#endif
381+
#endif
380382

381383

382384
// Get exit status of child.

src/platform-freebsd.cc

+126-42
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include <sys/stat.h> // open
4343
#include <sys/fcntl.h> // open
4444
#include <unistd.h> // getpagesize
45+
// If you don't have execinfo.h then you need devel/libexecinfo from ports.
4546
#include <execinfo.h> // backtrace, backtrace_symbols
4647
#include <strings.h> // index
4748
#include <errno.h>
@@ -526,6 +527,16 @@ class FreeBSDMutex : public Mutex {
526527
return result;
527528
}
528529

530+
virtual bool TryLock() {
531+
int result = pthread_mutex_trylock(&mutex_);
532+
// Return false if the lock is busy and locking failed.
533+
if (result == EBUSY) {
534+
return false;
535+
}
536+
ASSERT(result == 0); // Verify no other errors.
537+
return true;
538+
}
539+
529540
private:
530541
pthread_mutex_t mutex_; // Pthread mutex for POSIX platforms.
531542
};
@@ -595,60 +606,124 @@ Semaphore* OS::CreateSemaphore(int count) {
595606
#ifdef ENABLE_LOGGING_AND_PROFILING
596607

597608
static Sampler* active_sampler_ = NULL;
609+
static pthread_t vm_tid_ = NULL;
610+
611+
612+
static pthread_t GetThreadID() {
613+
pthread_t thread_id = pthread_self();
614+
return thread_id;
615+
}
616+
617+
618+
class Sampler::PlatformData : public Malloced {
619+
public:
620+
enum SleepInterval {
621+
FULL_INTERVAL,
622+
HALF_INTERVAL
623+
};
624+
625+
explicit PlatformData(Sampler* sampler)
626+
: sampler_(sampler),
627+
signal_handler_installed_(false),
628+
signal_sender_launched_(false) {
629+
}
630+
631+
void SignalSender() {
632+
while (sampler_->IsActive()) {
633+
if (rate_limiter_.SuspendIfNecessary()) continue;
634+
if (sampler_->IsProfiling() && RuntimeProfiler::IsEnabled()) {
635+
Sleep(FULL_INTERVAL);
636+
RuntimeProfiler::NotifyTick();
637+
} else {
638+
if (RuntimeProfiler::IsEnabled()) RuntimeProfiler::NotifyTick();
639+
Sleep(FULL_INTERVAL);
640+
}
641+
}
642+
}
643+
644+
void Sleep(SleepInterval full_or_half) {
645+
// Convert ms to us and subtract 100 us to compensate delays
646+
// occuring during signal delivery.
647+
useconds_t interval = sampler_->interval_ * 1000 - 100;
648+
if (full_or_half == HALF_INTERVAL) interval /= 2;
649+
int result = usleep(interval);
650+
#ifdef DEBUG
651+
if (result != 0 && errno != EINTR) {
652+
fprintf(stderr,
653+
"SignalSender usleep error; interval = %u, errno = %d\n",
654+
interval,
655+
errno);
656+
ASSERT(result == 0 || errno == EINTR);
657+
}
658+
#endif
659+
USE(result);
660+
}
661+
662+
Sampler* sampler_;
663+
bool signal_handler_installed_;
664+
struct sigaction old_signal_handler_;
665+
struct itimerval old_timer_value_;
666+
bool signal_sender_launched_;
667+
pthread_t signal_sender_thread_;
668+
RuntimeProfilerRateLimiter rate_limiter_;
669+
};
670+
598671

599672
static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) {
600673
USE(info);
601674
if (signal != SIGPROF) return;
602675
if (active_sampler_ == NULL) return;
676+
if (!active_sampler_->IsActive()) {
677+
// Restore old signal handler
678+
Sampler::PlatformData* data = active_sampler_->data();
679+
if (data->signal_handler_installed_) {
680+
sigaction(SIGPROF, &data->old_signal_handler_, 0);
681+
data->signal_handler_installed_ = false;
682+
}
683+
return;
684+
}
603685

604-
TickSample sample;
686+
if (vm_tid_ != GetThreadID()) return;
605687

606-
// We always sample the VM state.
607-
sample.state = VMState::current_state();
688+
TickSample sample_obj;
689+
TickSample* sample = CpuProfiler::TickSampleEvent();
690+
if (sample == NULL) sample = &sample_obj;
608691

609-
// If profiling, we extract the current pc and sp.
610-
if (active_sampler_->IsProfiling()) {
611-
// Extracting the sample from the context is extremely machine dependent.
612-
ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context);
613-
mcontext_t& mcontext = ucontext->uc_mcontext;
692+
// Extracting the sample from the context is extremely machine dependent.
693+
ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context);
694+
mcontext_t& mcontext = ucontext->uc_mcontext;
614695
#if V8_HOST_ARCH_IA32
615-
sample.pc = reinterpret_cast<Address>(mcontext.mc_eip);
616-
sample.sp = reinterpret_cast<Address>(mcontext.mc_esp);
617-
sample.fp = reinterpret_cast<Address>(mcontext.mc_ebp);
696+
sample->pc = reinterpret_cast<Address>(mcontext.mc_eip);
697+
sample->sp = reinterpret_cast<Address>(mcontext.mc_esp);
698+
sample->fp = reinterpret_cast<Address>(mcontext.mc_ebp);
618699
#elif V8_HOST_ARCH_X64
619-
sample.pc = reinterpret_cast<Address>(mcontext.mc_rip);
620-
sample.sp = reinterpret_cast<Address>(mcontext.mc_rsp);
621-
sample.fp = reinterpret_cast<Address>(mcontext.mc_rbp);
700+
sample->pc = reinterpret_cast<Address>(mcontext.mc_rip);
701+
sample->sp = reinterpret_cast<Address>(mcontext.mc_rsp);
702+
sample->fp = reinterpret_cast<Address>(mcontext.mc_rbp);
622703
#elif V8_HOST_ARCH_ARM
623-
sample.pc = reinterpret_cast<Address>(mcontext.mc_r15);
624-
sample.sp = reinterpret_cast<Address>(mcontext.mc_r13);
625-
sample.fp = reinterpret_cast<Address>(mcontext.mc_r11);
704+
sample->pc = reinterpret_cast<Address>(mcontext.mc_r15);
705+
sample->sp = reinterpret_cast<Address>(mcontext.mc_r13);
706+
sample->fp = reinterpret_cast<Address>(mcontext.mc_r11);
626707
#endif
627-
active_sampler_->SampleStack(&sample);
628-
}
629-
630-
active_sampler_->Tick(&sample);
708+
active_sampler_->SampleStack(sample);
709+
active_sampler_->Tick(sample);
631710
}
632711

633712

634-
class Sampler::PlatformData : public Malloced {
635-
public:
636-
PlatformData() {
637-
signal_handler_installed_ = false;
638-
}
639-
640-
bool signal_handler_installed_;
641-
struct sigaction old_signal_handler_;
642-
struct itimerval old_timer_value_;
643-
};
713+
static void* SenderEntry(void* arg) {
714+
Sampler::PlatformData* data =
715+
reinterpret_cast<Sampler::PlatformData*>(arg);
716+
data->SignalSender();
717+
return 0;
718+
}
644719

645720

646721
Sampler::Sampler(int interval)
647722
: interval_(interval),
648723
profiling_(false),
649724
active_(false),
650725
samples_taken_(0) {
651-
data_ = new PlatformData();
726+
data_ = new PlatformData(this);
652727
}
653728

654729

@@ -660,7 +735,8 @@ Sampler::~Sampler() {
660735
void Sampler::Start() {
661736
// There can only be one active sampler at the time on POSIX
662737
// platforms.
663-
if (active_sampler_ != NULL) return;
738+
ASSERT(!IsActive());
739+
vm_tid_ = GetThreadID();
664740

665741
// Request profiling signals.
666742
struct sigaction sa;
@@ -680,21 +756,29 @@ void Sampler::Start() {
680756

681757
// Set this sampler as the active sampler.
682758
active_sampler_ = this;
683-
active_ = true;
759+
SetActive(true);
760+
761+
// There's no way to send a signal to a thread on FreeBSD, but we can
762+
// start a thread that uses the stack guard to interrupt the JS thread.
763+
if (pthread_create(
764+
&data_->signal_sender_thread_, NULL, SenderEntry, data_) == 0) {
765+
data_->signal_sender_launched_ = true;
766+
}
684767
}
685768

686769

687770
void Sampler::Stop() {
688-
// Restore old signal handler
689-
if (data_->signal_handler_installed_) {
690-
setitimer(ITIMER_PROF, &data_->old_timer_value_, NULL);
691-
sigaction(SIGPROF, &data_->old_signal_handler_, 0);
692-
data_->signal_handler_installed_ = false;
693-
}
694-
695771
// This sampler is no longer the active sampler.
696772
active_sampler_ = NULL;
697-
active_ = false;
773+
SetActive(false);
774+
775+
// Wait for signal sender termination (it will exit after setting
776+
// active_ to false).
777+
if (data_->signal_sender_launched_) {
778+
Top::WakeUpRuntimeProfilerThreadBeforeShutdown();
779+
pthread_join(data_->signal_sender_thread_, NULL);
780+
data_->signal_sender_launched_ = false;
781+
}
698782
}
699783

700784
#endif // ENABLE_LOGGING_AND_PROFILING

src/platform.h

+1
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,7 @@ class Sampler {
613613
void ResetSamplesTaken() { samples_taken_ = 0; }
614614

615615
class PlatformData;
616+
PlatformData* data() { return data_; }
616617

617618
protected:
618619
virtual void DoSampleStack(TickSample* sample) = 0;

tools/freebsd-tick-processor

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/sh
2+
3+
# A wrapper script to call 'linux-tick-processor'.
4+
5+
# Known issues on FreeBSD:
6+
# No ticks from C++ code.
7+
# You must have d8 built and in your path before calling this.
8+
9+
tools_path=`cd $(dirname "$0");pwd`
10+
$tools_path/linux-tick-processor "$@"

tools/linux-tick-processor

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,30 @@
33
tools_path=`cd $(dirname "$0");pwd`
44
if [ ! "$D8_PATH" ]; then
55
d8_public=`which d8`
6-
if [ $d8_public ]; then D8_PATH=$(dirname "$d8_public"); fi
6+
if [ -x $d8_public ]; then D8_PATH=$(dirname "$d8_public"); fi
77
fi
88
[ "$D8_PATH" ] || D8_PATH=$tools_path/..
99
d8_exec=$D8_PATH/d8
1010

11-
if [ "$1" == "--no-build" ]; then
11+
if [ "$1" = "--no-build" ]; then
1212
shift
1313
else
1414
# compile d8 if it doesn't exist, assuming this script
1515
# resides in the repository.
1616
[ -x $d8_exec ] || scons -j4 -C $D8_PATH -Y $tools_path/.. d8
1717
fi
1818

19+
1920
# find the name of the log file to process, it must not start with a dash.
2021
log_file="v8.log"
2122
for arg in "$@"
2223
do
23-
if [[ "${arg}" != -* ]]; then
24+
if ! expr "X${arg}" : "^X-" > /dev/null; then
2425
log_file=${arg}
2526
fi
2627
done
2728

29+
2830
# nm spits out 'no symbols found' messages to stderr.
2931
cat $log_file | $d8_exec $tools_path/splaytree.js $tools_path/codemap.js \
3032
$tools_path/csvparser.js $tools_path/consarray.js \

0 commit comments

Comments
 (0)