From e922f869e6221dd05e8bf8d5a81680a68cfb669d Mon Sep 17 00:00:00 2001 From: Ben Alpert Date: Fri, 18 Sep 2015 21:43:47 -0700 Subject: [PATCH] Make hardware-counter.cpp compile `g++ -std=c++11 -lpthread src/hardware-counter.cpp src/thread-local.cpp src/test.cpp` works and runs where `src/test.cpp` is: ``` int main() { bool enable = true; std::string events = ""; bool recordSubprocesses = false; HPHP::HardwareCounter::Init(enable, events, recordSubprocesses); HPHP::HardwareCounter::s_counter.getCheck(); int64_t start = HPHP::HardwareCounter::GetInstructionCount(); volatile int x; for (int i = 0; i < 1000000; i++) { x += i; } int64_t end = HPHP::HardwareCounter::GetInstructionCount(); printf("%d\n", end - start); } ``` --- .../perf-counters/src/hardware-counter.cpp | 45 +++++++++---------- scripts/perf-counters/src/hardware-counter.h | 9 ++-- scripts/perf-counters/src/portability.h | 11 +++-- scripts/perf-counters/src/thread-local.cpp | 2 +- scripts/perf-counters/src/thread-local.h | 16 ++++--- 5 files changed, 44 insertions(+), 39 deletions(-) diff --git a/scripts/perf-counters/src/hardware-counter.cpp b/scripts/perf-counters/src/hardware-counter.cpp index 10a2bc3ca5..c002ff5263 100644 --- a/scripts/perf-counters/src/hardware-counter.cpp +++ b/scripts/perf-counters/src/hardware-counter.cpp @@ -7,14 +7,10 @@ * of patent rights can be found in the PATENTS file in the same directory. */ -#include "hphp/util/hardware-counter.h" +#include "hardware-counter.h" #ifndef NO_HARDWARE_COUNTERS -#include - -#include "hphp/util/logger.h" - #define _GNU_SOURCE 1 #include #include @@ -28,8 +24,6 @@ #include #include #include -#include -#include namespace HPHP { /////////////////////////////////////////////////////////////////////////////// @@ -80,14 +74,14 @@ public: inited = true; m_fd = syscall(__NR_perf_event_open, &pe, 0, -1, -1, 0); if (m_fd < 0) { - Logger::Verbose("perf_event_open failed with: %s", - folly::errnoStr(errno).c_str()); + // Logger::Verbose("perf_event_open failed with: %s", + // folly::errnoStr(errno).c_str()); m_err = -1; return; } if (ioctl(m_fd, PERF_EVENT_IOC_ENABLE, 0) < 0) { - Logger::Warning("perf_event failed to enable: %s", - folly::errnoStr(errno).c_str()); + // Logger::Warning("perf_event failed to enable: %s", + // folly::errnoStr(errno).c_str()); close(); m_err = -1; return; @@ -136,15 +130,15 @@ public: extra = 0; if (m_fd > 0) { if (ioctl (m_fd, PERF_EVENT_IOC_RESET, 0) < 0) { - Logger::Warning("perf_event failed to reset with: %s", - folly::errnoStr(errno).c_str()); + // Logger::Warning("perf_event failed to reset with: %s", + // folly::errnoStr(errno).c_str()); m_err = -1; return; } auto ret = ::read(m_fd, reset_values, sizeof(reset_values)); if (ret != sizeof(reset_values)) { - Logger::Warning("perf_event failed to reset with: %s", - folly::errnoStr(errno).c_str()); + // Logger::Warning("perf_event failed to reset with: %s", + // folly::errnoStr(errno).c_str()); m_err = -1; return; } @@ -358,7 +352,7 @@ bool HardwareCounter::addPerfEvent(const char* event) { found = true; type = perfTable[i].type; } else if (type != perfTable[i].type) { - Logger::Warning("failed to find perf event: %s", event); + // Logger::Warning("failed to find perf event: %s", event); return false; } config |= perfTable[i].config; @@ -377,12 +371,13 @@ bool HardwareCounter::addPerfEvent(const char* event) { } if (!found || *ev) { - Logger::Warning("failed to find perf event: %s", event); + // Logger::Warning("failed to find perf event: %s", event); return false; } - auto hwc = folly::make_unique(type, config, event); + std::unique_ptr hwc( + new HardwareCounterImpl(type, config, event)); if (hwc->m_err) { - Logger::Warning("failed to set perf event: %s", event); + // Logger::Warning("failed to set perf event: %s", event); return false; } m_counters.emplace_back(std::move(hwc)); @@ -407,25 +402,27 @@ bool HardwareCounter::eventExists(const char *event) { return false; } -bool HardwareCounter::setPerfEvents(folly::StringPiece sevents) { +bool HardwareCounter::setPerfEvents(std::string sevents) { // Make a copy of the string for use with strtok. auto const sevents_buf = static_cast(malloc(sevents.size() + 1)); - SCOPE_EXIT { free(sevents_buf); }; memcpy(sevents_buf, sevents.data(), sevents.size()); sevents_buf[sevents.size()] = '\0'; char* strtok_buf = nullptr; char* s = strtok_r(sevents_buf, ",", &strtok_buf); + bool success = true; while (s) { if (!eventExists(s) && !addPerfEvent(s)) { - return false; + success = false; + break; } s = strtok_r(nullptr, ",", &strtok_buf); } - return true; + free(sevents_buf); + return success; } -bool HardwareCounter::SetPerfEvents(folly::StringPiece events) { +bool HardwareCounter::SetPerfEvents(std::string events) { return s_counter->setPerfEvents(events); } diff --git a/scripts/perf-counters/src/hardware-counter.h b/scripts/perf-counters/src/hardware-counter.h index 9e266380e8..cc77ace140 100644 --- a/scripts/perf-counters/src/hardware-counter.h +++ b/scripts/perf-counters/src/hardware-counter.h @@ -10,11 +10,10 @@ #ifndef incl_HPHP_UTIL_HARDWARE_COUNTER_H_ #define incl_HPHP_UTIL_HARDWARE_COUNTER_H_ -#include "hphp/util/thread-local.h" - -#include +#include "thread-local.h" #include +#include #include namespace HPHP { @@ -43,7 +42,7 @@ public: static int64_t GetInstructionCount(); static int64_t GetLoadCount(); static int64_t GetStoreCount(); - static bool SetPerfEvents(folly::StringPiece events); + static bool SetPerfEvents(std::string events); static void IncInstructionCount(int64_t amount); static void IncLoadCount(int64_t amount); static void IncStoreCount(int64_t amount); @@ -61,7 +60,7 @@ private: int64_t getStoreCount(); bool eventExists(const char* event); bool addPerfEvent(const char* event); - bool setPerfEvents(folly::StringPiece events); + bool setPerfEvents(std::string events); void getPerfEvents(PerfEventCallback f, void* data); void clearPerfEvents(); diff --git a/scripts/perf-counters/src/portability.h b/scripts/perf-counters/src/portability.h index b1d3995ccf..ba5ff4acc9 100644 --- a/scripts/perf-counters/src/portability.h +++ b/scripts/perf-counters/src/portability.h @@ -10,9 +10,14 @@ #ifndef incl_HPHP_PORTABILITY_H_ #define incl_HPHP_PORTABILITY_H_ -#include // defining LIKELY/UNLIKELY is part of this header -#include -#include // defining FOLLY_DISABLE_ADDRESS_SANITIZER +// From folly/Likely.h +#if defined(__GNUC__) && __GNUC__ >= 4 +#define LIKELY(x) (__builtin_expect((x), 1)) +#define UNLIKELY(x) (__builtin_expect((x), 0)) +#else +#define LIKELY(x) (x) +#define UNLIKELY(x) (x) +#endif ////////////////////////////////////////////////////////////////////// diff --git a/scripts/perf-counters/src/thread-local.cpp b/scripts/perf-counters/src/thread-local.cpp index 3e9df4c207..a477be92dc 100644 --- a/scripts/perf-counters/src/thread-local.cpp +++ b/scripts/perf-counters/src/thread-local.cpp @@ -7,7 +7,7 @@ * of patent rights can be found in the PATENTS file in the same directory. */ -#include "hphp/util/thread-local.h" +#include "thread-local.h" #ifdef __linux__ #include diff --git a/scripts/perf-counters/src/thread-local.h b/scripts/perf-counters/src/thread-local.h index 919531bb28..1ed8e4f887 100644 --- a/scripts/perf-counters/src/thread-local.h +++ b/scripts/perf-counters/src/thread-local.h @@ -10,11 +10,16 @@ #ifndef incl_HPHP_THREAD_LOCAL_H_ #define incl_HPHP_THREAD_LOCAL_H_ +#include +#include +#include +#include #include -#include "hphp/util/exception.h" #include -#include +#include #include +#include +#include "portability.h" namespace HPHP { @@ -85,8 +90,7 @@ inline void ThreadLocalCheckReturn(int ret, const char *funcName) { if (ret != 0) { // This is used from global constructors so the safest thing to do is just // print to stderr and exit(). - fprintf(stderr, "%s returned %d: %s", funcName, ret, - folly::errnoStr(ret).c_str()); + fprintf(stderr, "%s returned %d", funcName, ret); exit(1); } } @@ -357,7 +361,7 @@ template struct ThreadLocalProxy { T *get() const { if (m_p == nullptr && throwOnNull) { - throw Exception("ThreadLocalProxy::get() called before set()"); + throw std::runtime_error("ThreadLocalProxy::get() called before set()"); } return m_p; } @@ -708,7 +712,7 @@ public: T *get() const { T *obj = (T*)pthread_getspecific(m_key); if (obj == nullptr && throwOnNull) { - throw Exception("ThreadLocalProxy::get() called before set()"); + throw std::runtime_error("ThreadLocalProxy::get() called before set()"); } return obj; }