diff --git a/Makefile.am b/Makefile.am
index 5eb4d42..b9f3321 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1123,6 +1123,18 @@ CPU_PROFILER_SYMBOLS = '(ProfilerStart|ProfilerStartWithOptions|ProfilerStop|Pro
 libprofiler_la_LDFLAGS = -export-symbols-regex $(CPU_PROFILER_SYMBOLS) \
                          -version-info @PROFILER_SO_VERSION@
 
+### perf_events based library
+lib_LTLIBRARIES += libperf_profiler.la
+libperf_profiler_la_SOURCES = src/profiler.cc \
+                              src/perf-profile-handler.cc \
+                              src/profiledata.cc \
+                              $(CPU_PROFILER_INCLUDES)
+libperf_profiler_la_LIBADD = libstacktrace.la
+# We have to include ProfileData for profiledata_unittest
+PERF_PROFILER_SYMBOLS = '(ProfilerStart|ProfilerStartWithOptions|ProfilerStop|ProfilerFlush|ProfilerEnable|ProfilerDisable|ProfilingIsEnabledForAllThreads|ProfilerRegisterThread|ProfilerGetCurrentState|ProfilerState|ProfileData|ProfileHandler)'
+libperf_profiler_la_LDFLAGS = -export-symbols-regex $(CPU_PROFILER_SYMBOLS) \
+                         -version-info @PROFILER_SO_VERSION@
+
 # See discussion above (under LIBTCMALLOC_MINIMAL) for why we do this.
 # Basically it's to work around systems where --rpath doesn't work right.
 LIBPROFILER = libstacktrace.la libprofiler.la
diff --git a/src/perf-profile-handler.cc b/src/perf-profile-handler.cc
new file mode 100644
index 0000000..2c2f25b
--- /dev/null
+++ b/src/perf-profile-handler.cc
@@ -0,0 +1,477 @@
+// Copyright (c) 2009, Google Inc.
+// Copyright (c) 2011, Facebook Inc.
+// All rights reserved.
+// 
+// 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.
+
+// ---
+// Author: Sanjay Ghemawat
+//         Nabeel Mian
+//
+// Implements management of profile timers and the corresponding signal handler.
+
+#include "config.h"
+#include "profile-handler.h"
+
+#if !(defined(__CYGWIN__) || defined(__CYGWIN32__))
+
+#include <stdio.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <perfmon/perf_event.h>
+#include <pthread.h>
+#include <sys/mman.h>
+#include <sys/time.h>
+#include <sys/types.h>
+
+#include <list>
+#include <map>
+#include <string>
+
+#include "base/dynamic_annotations.h"
+#include "base/logging.h"
+#include "base/spinlock.h"
+#include "maybe_threads.h"
+
+using std::list;
+using std::string;
+
+// This structure is used by ProfileHandlerRegisterCallback and
+// ProfileHandlerUnregisterCallback as a handle to a registered callback.
+struct ProfileHandlerToken {
+  // Sets the callback and associated arg.
+  ProfileHandlerToken(ProfileHandlerCallback cb, void* cb_arg)
+      : callback(cb),
+        callback_arg(cb_arg) {
+  }
+
+  // Callback function to be invoked on receiving a profile timer interrupt.
+  ProfileHandlerCallback callback;
+  // Argument for the callback function.
+  void* callback_arg;
+};
+
+// This class manages profile timers and associated signal handler. This is a
+// a singleton.
+class ProfileHandler {
+ public:
+  // Registers the current thread with the profile handler. On systems which
+  // have a separate interval timer for each thread, this function starts the
+  // timer for the current thread.
+  //
+  // The function also attempts to determine whether or not timers are shared by
+  // all threads in the process.  (With LinuxThreads, and with NPTL on some
+  // Linux kernel versions, each thread has separate timers.)
+  //
+  // Prior to determining whether timers are shared, this function will
+  // unconditionally start the timer.  However, if this function determines
+  // that timers are shared, then it will stop the timer if no callbacks are
+  // currently registered.
+  void RegisterThread();
+
+  // Registers a callback routine to receive profile timer ticks. The returned
+  // token is to be used when unregistering this callback and must not be
+  // deleted by the caller. Registration of the first callback enables the
+  // SIGPROF handler (or SIGALRM if using ITIMER_REAL).
+  ProfileHandlerToken* RegisterCallback(ProfileHandlerCallback callback,
+                                        void* callback_arg);
+
+  // Unregisters a previously registered callback. Expects the token returned
+  // by the corresponding RegisterCallback routine. Unregistering the last
+  // callback disables the SIGPROF handler (or SIGALRM if using ITIMER_REAL).
+  void UnregisterCallback(ProfileHandlerToken* token)
+      NO_THREAD_SAFETY_ANALYSIS;
+
+  // Unregisters all the callbacks, stops the timer if shared, disables the
+  // SIGPROF (or SIGALRM) handler and clears the timer_sharing_ state.
+  void Reset();
+
+  // Gets the current state of profile handler.
+  void GetState(ProfileHandlerState* state);
+
+  // Initializes and returns the ProfileHandler singleton.
+  static ProfileHandler* Instance();
+
+ private:
+  ProfileHandler();
+  ~ProfileHandler();
+
+  // Largest allowed frequency.
+  static const int32 kMaxFrequency = 4000;
+  // Default frequency.
+  static const int32 kDefaultFrequency = 100;
+
+  // ProfileHandler singleton.
+  static ProfileHandler* instance_;
+
+  // pthread_once_t for one time initialization of ProfileHandler singleton.
+  static pthread_once_t once_;
+
+  // Initializes the ProfileHandler singleton via GoogleOnceInit.
+  static void Init();
+
+  // The number of SIGPROF (or SIGALRM for ITIMER_REAL) interrupts received.
+  int64 interrupts_ GUARDED_BY(signal_lock_);
+
+  // SIGPROF/SIGALRM interrupt frequency, read-only after construction.
+  int32 frequency_;
+
+  // Counts the number of callbacks registered.
+  int32 callback_count_ GUARDED_BY(control_lock_);
+
+  // This lock serializes the registration of threads and protects the
+  // callbacks_ list below.
+  // Locking order:
+  // In the context of a signal handler, acquire signal_lock_ to walk the
+  // callback list. Otherwise, acquire control_lock_, disable the signal
+  // handler and then acquire signal_lock_.
+  SpinLock control_lock_ ACQUIRED_BEFORE(signal_lock_);
+  SpinLock signal_lock_;
+
+  // Holds the list of registered callbacks. We expect the list to be pretty
+  // small. Currently, the cpu profiler (base/profiler) and thread module
+  // (base/thread.h) are the only two components registering callbacks.
+  // Following are the locking requirements for callbacks_:
+  // For read-write access outside the SIGPROF handler:
+  //  - Acquire control_lock_
+  //  - Disable SIGPROF handler.
+  //  - Acquire signal_lock_
+  // For read-only access in the context of SIGPROF handler
+  // (Read-write access is *not allowed* in the SIGPROF handler)
+  //  - Acquire signal_lock_
+  // For read-only access outside SIGPROF handler:
+  //  - Acquire control_lock_
+  typedef list<ProfileHandlerToken*> CallbackList;
+  typedef CallbackList::iterator CallbackIterator;
+  CallbackList callbacks_ GUARDED_BY(signal_lock_);
+
+  // Maps threads to perf_event file descriptors
+  typedef struct {
+    int fd;
+    void *buf;
+  } PerfHandle;
+  typedef std::map<pthread_t, PerfHandle> FdMap;
+  FdMap fdmap_;
+
+  // Starts the interval timer.  If the thread library shares timers between
+  // threads, this function starts the shared timer. Otherwise, this will start
+  // the timer in the current thread.
+  void StartTimer() EXCLUSIVE_LOCKS_REQUIRED(control_lock_);
+
+  // Stops the interval timer. If the thread library shares timers between
+  // threads, this fucntion stops the shared timer. Otherwise, this will stop
+  // the timer in the current thread.
+  void StopTimer() EXCLUSIVE_LOCKS_REQUIRED(control_lock_);
+
+  // Returns true if the profile interval timer is enabled in the current
+  // thread.  This actually checks the kernel's interval timer setting.  (It is
+  // used to detect whether timers are shared or separate.)
+  bool IsTimerRunning() EXCLUSIVE_LOCKS_REQUIRED(control_lock_);
+
+  // Sets the timer interrupt signal handler.
+  void EnableHandler() EXCLUSIVE_LOCKS_REQUIRED(control_lock_);
+
+  // Disables (ignores) the timer interrupt signal.
+  void DisableHandler() EXCLUSIVE_LOCKS_REQUIRED(control_lock_);
+
+  // SIGPROF/SIGALRM handler. Iterate over and call all the registered callbacks.
+  static void SignalHandler(int sig, siginfo_t* sinfo, void* ucontext);
+
+  DISALLOW_COPY_AND_ASSIGN(ProfileHandler);
+};
+
+ProfileHandler* ProfileHandler::instance_ = NULL;
+pthread_once_t ProfileHandler::once_ = PTHREAD_ONCE_INIT;
+
+const int32 ProfileHandler::kMaxFrequency;
+const int32 ProfileHandler::kDefaultFrequency;
+
+// If we are LD_PRELOAD-ed against a non-pthreads app, then
+// pthread_once won't be defined.  We declare it here, for that
+// case (with weak linkage) which will cause the non-definition to
+// resolve to NULL.  We can then check for NULL or not in Instance.
+extern "C" int pthread_once(pthread_once_t *, void (*)(void))
+    ATTRIBUTE_WEAK;
+
+void ProfileHandler::Init() {
+  instance_ = new ProfileHandler();
+}
+
+ProfileHandler* ProfileHandler::Instance() {
+  if (pthread_once) {
+    pthread_once(&once_, Init);
+  }
+  if (instance_ == NULL) {
+    // This will be true on systems that don't link in pthreads,
+    // including on FreeBSD where pthread_once has a non-zero address
+    // (but doesn't do anything) even when pthreads isn't linked in.
+    Init();
+    assert(instance_ != NULL);
+  }
+  return instance_;
+}
+
+ProfileHandler::ProfileHandler()
+    : interrupts_(0),
+      callback_count_(0) {
+  SpinLockHolder cl(&control_lock_);
+
+  // Get frequency of interrupts (if specified)
+  char junk;
+  const char* fr = getenv("CPUPROFILE_FREQUENCY");
+  if (fr != NULL && (sscanf(fr, "%u%c", &frequency_, &junk) == 1) &&
+      (frequency_ > 0)) {
+    // Limit to kMaxFrequency
+    frequency_ = (frequency_ > kMaxFrequency) ? kMaxFrequency : frequency_;
+  } else {
+    frequency_ = kDefaultFrequency;
+  }
+
+  // Ignore signals until we decide to turn profiling on.  (Paranoia;
+  // should already be ignored.)
+  DisableHandler();
+}
+
+ProfileHandler::~ProfileHandler() {
+  Reset();
+}
+
+pid_t gettid() {
+  return syscall(__NR_gettid);
+}
+
+void ProfileHandler::RegisterThread() {
+  int fd;
+  perf_event_attr attr;
+
+  memset(&attr, 0, sizeof(attr));
+  attr.type = PERF_TYPE_HARDWARE;
+  attr.config = PERF_COUNT_HW_CPU_CYCLES;
+  attr.size = sizeof(attr);
+  attr.freq = 1;
+  attr.disabled = 1;
+  attr.wakeup_events = 1;
+  attr.sample_freq = frequency_;
+  attr.sample_type = PERF_SAMPLE_IP;
+  CHECK_ERR(fd = perf_event_open(&attr, 0, -1, -1, 0));
+  CHECK_ERR(fcntl(fd, F_SETSIG, SIGPROF));
+  int flags;
+  CHECK_ERR(flags = fcntl(fd, F_GETFL, 0));
+  CHECK_ERR(fcntl(fd, F_SETFL, flags | O_ASYNC));
+  struct f_owner_ex fown_ex;
+  fown_ex.type = F_OWNER_TID;
+  fown_ex.pid  = gettid();
+  CHECK_ERR(fcntl(fd, F_SETOWN_EX, &fown_ex));
+  CHECK_ERR(ioctl(fd, PERF_EVENT_IOC_REFRESH , 1));
+  PerfHandle handle;
+  handle.fd = fd;
+  handle.buf = mmap(NULL, 2 * getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
+  CHECK(handle.buf != MAP_FAILED);
+  fdmap_[pthread_self()] = handle;
+}
+
+ProfileHandlerToken* ProfileHandler::RegisterCallback(
+    ProfileHandlerCallback callback, void* callback_arg) {
+  ProfileHandlerToken* token = new ProfileHandlerToken(callback, callback_arg);
+
+  SpinLockHolder cl(&control_lock_);
+  DisableHandler();
+  {
+    SpinLockHolder sl(&signal_lock_);
+    callbacks_.push_back(token);
+  }
+  StartTimer();
+  ++callback_count_;
+  EnableHandler();
+  return token;
+}
+
+void ProfileHandler::UnregisterCallback(ProfileHandlerToken* token) {
+  SpinLockHolder cl(&control_lock_);
+  for (CallbackIterator it = callbacks_.begin(); it != callbacks_.end();
+       ++it) {
+    if ((*it) == token) {
+      RAW_CHECK(callback_count_ > 0, "Invalid callback count");
+      DisableHandler();
+      {
+        SpinLockHolder sl(&signal_lock_);
+        delete *it;
+        callbacks_.erase(it);
+      }
+      --callback_count_;
+      if (callback_count_ > 0) {
+        EnableHandler();
+      } 
+      StopTimer();
+      return;
+    }
+  }
+  // Unknown token.
+  RAW_LOG(FATAL, "Invalid token");
+}
+
+void ProfileHandler::Reset() {
+  SpinLockHolder cl(&control_lock_);
+  DisableHandler();
+  {
+    SpinLockHolder sl(&signal_lock_);
+    CallbackIterator it = callbacks_.begin();
+    while (it != callbacks_.end()) {
+      CallbackIterator tmp = it;
+      ++it;
+      delete *tmp;
+      callbacks_.erase(tmp);
+    }
+  }
+  callback_count_ = 0;
+  StopTimer();
+  for (FdMap::const_iterator it = fdmap_.begin(); it != fdmap_.end(); it++) {
+    PerfHandle handle = it->second;
+    munmap(handle.buf, 2 * getpagesize());
+    close(handle.fd);
+  }
+  fdmap_.clear();
+}
+
+void ProfileHandler::GetState(ProfileHandlerState* state) {
+  SpinLockHolder cl(&control_lock_);
+  DisableHandler();
+  {
+    SpinLockHolder sl(&signal_lock_);  // Protects interrupts_.
+    state->interrupts = interrupts_;
+  }
+  if (callback_count_ > 0) {
+    EnableHandler();
+  }
+  state->frequency = frequency_;
+  state->callback_count = callback_count_;
+}
+
+void ProfileHandler::StartTimer() {
+}
+
+void ProfileHandler::StopTimer() {
+}
+
+bool ProfileHandler::IsTimerRunning() {
+}
+
+void ProfileHandler::EnableHandler() {
+  struct sigaction sa;
+  sa.sa_sigaction = SignalHandler;
+  sa.sa_flags = SA_RESTART | SA_SIGINFO;
+  sigemptyset(&sa.sa_mask);
+  const int signal_number = SIGPROF;
+  RAW_CHECK(sigaction(signal_number, &sa, NULL) == 0, "sigprof (enable)");
+  RAW_CHECK(prctl(PR_TASK_PERF_EVENTS_ENABLE) >= 0, "prctl(enable)");
+}
+
+void ProfileHandler::DisableHandler() {
+  struct sigaction sa;
+  sa.sa_handler = SIG_IGN;
+  sa.sa_flags = SA_RESTART;
+  sigemptyset(&sa.sa_mask);
+  const int signal_number = SIGPROF;
+  RAW_CHECK(sigaction(signal_number, &sa, NULL) == 0, "sigprof (disable)");
+  RAW_CHECK(prctl(PR_TASK_PERF_EVENTS_DISABLE) >= 0, "prctl(disable)");
+}
+
+void ProfileHandler::SignalHandler(int sig, siginfo_t* sinfo, void* ucontext) {
+  int saved_errno = errno;
+  RAW_CHECK(instance_ != NULL, "ProfileHandler is not initialized");
+  {
+    SpinLockHolder sl(&instance_->signal_lock_);
+    ++instance_->interrupts_;
+    for (CallbackIterator it = instance_->callbacks_.begin();
+         it != instance_->callbacks_.end();
+         ++it) {
+      (*it)->callback(sig, sinfo, ucontext, (*it)->callback_arg);
+    }
+  }
+  errno = saved_errno;
+}
+
+// The sole purpose of this class is to initialize the ProfileHandler singleton
+// when the global static objects are created. Note that the main thread will
+// be registered at this time.
+class ProfileHandlerInitializer {
+ public:
+  ProfileHandlerInitializer() {
+    ProfileHandler::Instance()->RegisterThread();
+  }
+
+ private:
+  DISALLOW_COPY_AND_ASSIGN(ProfileHandlerInitializer);
+};
+// ProfileHandlerInitializer singleton
+static ProfileHandlerInitializer profile_handler_initializer;
+
+extern "C" void ProfileHandlerRegisterThread() {
+  ProfileHandler::Instance()->RegisterThread();
+}
+
+extern "C" ProfileHandlerToken* ProfileHandlerRegisterCallback(
+    ProfileHandlerCallback callback, void* callback_arg) {
+  return ProfileHandler::Instance()->RegisterCallback(callback, callback_arg);
+}
+
+extern "C" void ProfileHandlerUnregisterCallback(ProfileHandlerToken* token) {
+  ProfileHandler::Instance()->UnregisterCallback(token);
+}
+
+extern "C" void ProfileHandlerReset() {
+  return ProfileHandler::Instance()->Reset();
+}
+
+extern "C" void ProfileHandlerGetState(ProfileHandlerState* state) {
+  ProfileHandler::Instance()->GetState(state);
+}
+
+#else  // OS_CYGWIN
+
+// ITIMER_PROF doesn't work under cygwin.  ITIMER_REAL is available, but doesn't
+// work as well for profiling, and also interferes with alarm().  Because of
+// these issues, unless a specific need is identified, profiler support is
+// disabled under Cygwin.
+extern "C" void ProfileHandlerRegisterThread() {
+}
+
+extern "C" ProfileHandlerToken* ProfileHandlerRegisterCallback(
+    ProfileHandlerCallback callback, void* callback_arg) {
+  return NULL;
+}
+
+extern "C" void ProfileHandlerUnregisterCallback(ProfileHandlerToken* token) {
+}
+
+extern "C" void ProfileHandlerReset() {
+}
+
+extern "C" void ProfileHandlerGetState(ProfileHandlerState* state) {
+}
+
+#endif  // OS_CYGWIN
