Skip to content

Commit

Permalink
v8: don't busy loop in cpu profiler thread
Browse files Browse the repository at this point in the history
Reduce the overhead of the CPU profiler by replacing sched_yield() with
nanosleep() in V8's tick event processor thread.  The former only yields
the CPU when there is another process scheduled on the same CPU.

Before this commit, the thread would effectively busy loop and consume
100% CPU time.  By forcing a one nanosecond sleep period rounded up to
the task scheduler's granularity (about 50 us on Linux), CPU usage for
the processor thread now hovers around 10-20% for a busy application.

PR-URL: nodejs/node-v0.x-archive#8789
Ref: strongloop/strong-agent#3
Reviewed-by: Trevor Norris <trev.norris@gmail.com>

Signed-off-by: Jeroen Ooms <jeroenooms@gmail.com>
  • Loading branch information
bnoordhuis authored and jeroen committed Jun 30, 2016
1 parent e6e4767 commit 2e4c603
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 26 deletions.
5 changes: 0 additions & 5 deletions src/platform-freebsd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -539,11 +539,6 @@ void Thread::SetThreadLocal(LocalStorageKey key, void* value) {
}


void Thread::YieldCPU() {
sched_yield();
}


class FreeBSDMutex : public Mutex {
public:
FreeBSDMutex() {
Expand Down
5 changes: 0 additions & 5 deletions src/platform-linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -812,11 +812,6 @@ void Thread::SetThreadLocal(LocalStorageKey key, void* value) {
}


void Thread::YieldCPU() {
sched_yield();
}


class LinuxMutex : public Mutex {
public:
LinuxMutex() {
Expand Down
5 changes: 0 additions & 5 deletions src/platform-macos.cc
Original file line number Diff line number Diff line change
Expand Up @@ -640,11 +640,6 @@ void Thread::SetThreadLocal(LocalStorageKey key, void* value) {
}


void Thread::YieldCPU() {
sched_yield();
}


class MacOSMutex : public Mutex {
public:
MacOSMutex() {
Expand Down
5 changes: 0 additions & 5 deletions src/platform-openbsd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -593,11 +593,6 @@ void Thread::SetThreadLocal(LocalStorageKey key, void* value) {
}


void Thread::YieldCPU() {
sched_yield();
}


class OpenBSDMutex : public Mutex {
public:
OpenBSDMutex() {
Expand Down
6 changes: 6 additions & 0 deletions src/platform-posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,12 @@ void OS::StrNCpy(Vector<char> dest, const char* src, size_t n) {
}


void Thread::YieldCPU() {
const timespec delay = { 0, 1 };
nanosleep(&delay, NULL);
}


// ----------------------------------------------------------------------------
// POSIX socket support.
//
Expand Down
5 changes: 0 additions & 5 deletions src/platform-solaris.cc
Original file line number Diff line number Diff line change
Expand Up @@ -531,11 +531,6 @@ void Thread::SetThreadLocal(LocalStorageKey key, void* value) {
}


void Thread::YieldCPU() {
sched_yield();
}


class SolarisMutex : public Mutex {
public:
SolarisMutex() {
Expand Down
2 changes: 1 addition & 1 deletion tools/gyp/v8.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@
['OS=="solaris"', {
'link_settings': {
'libraries': [
'-lsocket -lnsl',
'-lsocket -lnsl -lrt',
]},
'sources': [
'../../src/platform-solaris.cc',
Expand Down

0 comments on commit 2e4c603

Please sign in to comment.