@samitouri / QOS-React-2 / commits / e922f869e6

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); } ```

Ben Alpert committed Sep 18, 2015 at 21:43 UTC e922f869e6221dd05e8bf8d5a81680a68cfb669d
5 files changed +44 -39
scripts/perf-counters/src/hardware-counter.cpp
+21 -24
@@ -7,14 +7,10 @@
7 * of patent rights can be found in the PATENTS file in the same directory.
8 */
9
10 -#include "hphp/util/hardware-counter.h"
10 +#include "hardware-counter.h"
11
12 #ifndef NO_HARDWARE_COUNTERS
13
14 -#include <folly/ScopeGuard.h>
15 -
16 -#include "hphp/util/logger.h"
17 -
14 #define _GNU_SOURCE 1
15 #include <stdio.h>
16 #include <stdlib.h>
@@ -28,8 +24,6 @@
24 #include <asm/unistd.h>
25 #include <sys/prctl.h>
26 #include <linux/perf_event.h>
31 -#include <folly/String.h>
32 -#include <folly/Memory.h>
27
28 namespace HPHP {
29 ///////////////////////////////////////////////////////////////////////////////
@@ -80,14 +74,14 @@ public:
74 inited = true;
75 m_fd = syscall(__NR_perf_event_open, &pe, 0, -1, -1, 0);
76 if (m_fd < 0) {
83 - Logger::Verbose("perf_event_open failed with: %s",
84 - folly::errnoStr(errno).c_str());
77 + // Logger::Verbose("perf_event_open failed with: %s",
78 + // folly::errnoStr(errno).c_str());
79 m_err = -1;
80 return;
81 }
82 if (ioctl(m_fd, PERF_EVENT_IOC_ENABLE, 0) < 0) {
89 - Logger::Warning("perf_event failed to enable: %s",
90 - folly::errnoStr(errno).c_str());
83 + // Logger::Warning("perf_event failed to enable: %s",
84 + // folly::errnoStr(errno).c_str());
85 close();
86 m_err = -1;
87 return;
@@ -136,15 +130,15 @@ public:
130 extra = 0;
131 if (m_fd > 0) {
132 if (ioctl (m_fd, PERF_EVENT_IOC_RESET, 0) < 0) {
139 - Logger::Warning("perf_event failed to reset with: %s",
140 - folly::errnoStr(errno).c_str());
133 + // Logger::Warning("perf_event failed to reset with: %s",
134 + // folly::errnoStr(errno).c_str());
135 m_err = -1;
136 return;
137 }
138 auto ret = ::read(m_fd, reset_values, sizeof(reset_values));
139 if (ret != sizeof(reset_values)) {
146 - Logger::Warning("perf_event failed to reset with: %s",
147 - folly::errnoStr(errno).c_str());
140 + // Logger::Warning("perf_event failed to reset with: %s",
141 + // folly::errnoStr(errno).c_str());
142 m_err = -1;
143 return;
144 }
@@ -358,7 +352,7 @@ bool HardwareCounter::addPerfEvent(const char* event) {
352 found = true;
353 type = perfTable[i].type;
354 } else if (type != perfTable[i].type) {
361 - Logger::Warning("failed to find perf event: %s", event);
355 + // Logger::Warning("failed to find perf event: %s", event);
356 return false;
357 }
358 config |= perfTable[i].config;
@@ -377,12 +371,13 @@ bool HardwareCounter::addPerfEvent(const char* event) {
371 }
372
373 if (!found || *ev) {
380 - Logger::Warning("failed to find perf event: %s", event);
374 + // Logger::Warning("failed to find perf event: %s", event);
375 return false;
376 }
383 - auto hwc = folly::make_unique<HardwareCounterImpl>(type, config, event);
377 + std::unique_ptr<HardwareCounterImpl> hwc(
378 + new HardwareCounterImpl(type, config, event));
379 if (hwc->m_err) {
385 - Logger::Warning("failed to set perf event: %s", event);
380 + // Logger::Warning("failed to set perf event: %s", event);
381 return false;
382 }
383 m_counters.emplace_back(std::move(hwc));
@@ -407,25 +402,27 @@ bool HardwareCounter::eventExists(const char *event) {
402 return false;
403 }
404
410 -bool HardwareCounter::setPerfEvents(folly::StringPiece sevents) {
405 +bool HardwareCounter::setPerfEvents(std::string sevents) {
406 // Make a copy of the string for use with strtok.
407 auto const sevents_buf = static_cast<char*>(malloc(sevents.size() + 1));
413 - SCOPE_EXIT { free(sevents_buf); };
408 memcpy(sevents_buf, sevents.data(), sevents.size());
409 sevents_buf[sevents.size()] = '\0';
410
411 char* strtok_buf = nullptr;
412 char* s = strtok_r(sevents_buf, ",", &strtok_buf);
413 + bool success = true;
414 while (s) {
415 if (!eventExists(s) && !addPerfEvent(s)) {
421 - return false;
416 + success = false;
417 + break;
418 }
419 s = strtok_r(nullptr, ",", &strtok_buf);
420 }
425 - return true;
421 + free(sevents_buf);
422 + return success;
423 }
424
428 -bool HardwareCounter::SetPerfEvents(folly::StringPiece events) {
425 +bool HardwareCounter::SetPerfEvents(std::string events) {
426 return s_counter->setPerfEvents(events);
427 }
428
scripts/perf-counters/src/hardware-counter.h
+4 -5
@@ -10,11 +10,10 @@
10 #ifndef incl_HPHP_UTIL_HARDWARE_COUNTER_H_
11 #define incl_HPHP_UTIL_HARDWARE_COUNTER_H_
12
13 -#include "hphp/util/thread-local.h"
14 -
15 -#include <folly/Range.h>
13 +#include "thread-local.h"
14
15 #include <cstdint>
16 +#include <memory>
17 #include <vector>
18
19 namespace HPHP {
@@ -43,7 +42,7 @@ public:
42 static int64_t GetInstructionCount();
43 static int64_t GetLoadCount();
44 static int64_t GetStoreCount();
46 - static bool SetPerfEvents(folly::StringPiece events);
45 + static bool SetPerfEvents(std::string events);
46 static void IncInstructionCount(int64_t amount);
47 static void IncLoadCount(int64_t amount);
48 static void IncStoreCount(int64_t amount);
@@ -61,7 +60,7 @@ private:
60 int64_t getStoreCount();
61 bool eventExists(const char* event);
62 bool addPerfEvent(const char* event);
64 - bool setPerfEvents(folly::StringPiece events);
63 + bool setPerfEvents(std::string events);
64 void getPerfEvents(PerfEventCallback f, void* data);
65 void clearPerfEvents();
66
scripts/perf-counters/src/portability.h
+8 -3
@@ -10,9 +10,14 @@
10 #ifndef incl_HPHP_PORTABILITY_H_
11 #define incl_HPHP_PORTABILITY_H_
12
13 -#include <folly/Likely.h> // defining LIKELY/UNLIKELY is part of this header
14 -#include <folly/Portability.h>
15 -#include <folly/CPortability.h> // defining FOLLY_DISABLE_ADDRESS_SANITIZER
13 +// From folly/Likely.h
14 +#if defined(__GNUC__) && __GNUC__ >= 4
15 +#define LIKELY(x) (__builtin_expect((x), 1))
16 +#define UNLIKELY(x) (__builtin_expect((x), 0))
17 +#else
18 +#define LIKELY(x) (x)
19 +#define UNLIKELY(x) (x)
20 +#endif
21
22 //////////////////////////////////////////////////////////////////////
23
scripts/perf-counters/src/thread-local.cpp
+1 -1
@@ -7,7 +7,7 @@
7 * of patent rights can be found in the PATENTS file in the same directory.
8 */
9
10 -#include "hphp/util/thread-local.h"
10 +#include "thread-local.h"
11
12 #ifdef __linux__
13 #include <link.h>
scripts/perf-counters/src/thread-local.h
+10 -6
@@ -10,11 +10,16 @@
10 #ifndef incl_HPHP_THREAD_LOCAL_H_
11 #define incl_HPHP_THREAD_LOCAL_H_
12
13 +#include <assert.h>
14 +#include <stdio.h>
15 +#include <stdlib.h>
16 +#include <stdint.h>
17 #include <pthread.h>
14 -#include "hphp/util/exception.h"
18 #include <errno.h>
16 -#include <folly/String.h>
19 +#include <stdexcept>
20 #include <type_traits>
21 +#include <utility>
22 +#include "portability.h"
23
24 namespace HPHP {
25
@@ -85,8 +90,7 @@ inline void ThreadLocalCheckReturn(int ret, const char *funcName) {
90 if (ret != 0) {
91 // This is used from global constructors so the safest thing to do is just
92 // print to stderr and exit().
88 - fprintf(stderr, "%s returned %d: %s", funcName, ret,
89 - folly::errnoStr(ret).c_str());
93 + fprintf(stderr, "%s returned %d", funcName, ret);
94 exit(1);
95 }
96 }
@@ -357,7 +361,7 @@ template<typename T, bool throwOnNull = true>
361 struct ThreadLocalProxy {
362 T *get() const {
363 if (m_p == nullptr && throwOnNull) {
360 - throw Exception("ThreadLocalProxy::get() called before set()");
364 + throw std::runtime_error("ThreadLocalProxy::get() called before set()");
365 }
366 return m_p;
367 }
@@ -708,7 +712,7 @@ public:
712 T *get() const {
713 T *obj = (T*)pthread_getspecific(m_key);
714 if (obj == nullptr && throwOnNull) {
711 - throw Exception("ThreadLocalProxy::get() called before set()");
715 + throw std::runtime_error("ThreadLocalProxy::get() called before set()");
716 }
717 return obj;
718 }