Skip to content

Commit e922f86

Browse files
committed
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); } ```
1 parent 42e523e commit e922f86

File tree

5 files changed

+44
-39
lines changed

5 files changed

+44
-39
lines changed

scripts/perf-counters/src/hardware-counter.cpp

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,10 @@
77
* of patent rights can be found in the PATENTS file in the same directory.
88
*/
99

10-
#include "hphp/util/hardware-counter.h"
10+
#include "hardware-counter.h"
1111

1212
#ifndef NO_HARDWARE_COUNTERS
1313

14-
#include <folly/ScopeGuard.h>
15-
16-
#include "hphp/util/logger.h"
17-
1814
#define _GNU_SOURCE 1
1915
#include <stdio.h>
2016
#include <stdlib.h>
@@ -28,8 +24,6 @@
2824
#include <asm/unistd.h>
2925
#include <sys/prctl.h>
3026
#include <linux/perf_event.h>
31-
#include <folly/String.h>
32-
#include <folly/Memory.h>
3327

3428
namespace HPHP {
3529
///////////////////////////////////////////////////////////////////////////////
@@ -80,14 +74,14 @@ class HardwareCounterImpl {
8074
inited = true;
8175
m_fd = syscall(__NR_perf_event_open, &pe, 0, -1, -1, 0);
8276
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());
8579
m_err = -1;
8680
return;
8781
}
8882
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());
9185
close();
9286
m_err = -1;
9387
return;
@@ -136,15 +130,15 @@ class HardwareCounterImpl {
136130
extra = 0;
137131
if (m_fd > 0) {
138132
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());
141135
m_err = -1;
142136
return;
143137
}
144138
auto ret = ::read(m_fd, reset_values, sizeof(reset_values));
145139
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());
148142
m_err = -1;
149143
return;
150144
}
@@ -358,7 +352,7 @@ bool HardwareCounter::addPerfEvent(const char* event) {
358352
found = true;
359353
type = perfTable[i].type;
360354
} 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);
362356
return false;
363357
}
364358
config |= perfTable[i].config;
@@ -377,12 +371,13 @@ bool HardwareCounter::addPerfEvent(const char* event) {
377371
}
378372

379373
if (!found || *ev) {
380-
Logger::Warning("failed to find perf event: %s", event);
374+
// Logger::Warning("failed to find perf event: %s", event);
381375
return false;
382376
}
383-
auto hwc = folly::make_unique<HardwareCounterImpl>(type, config, event);
377+
std::unique_ptr<HardwareCounterImpl> hwc(
378+
new HardwareCounterImpl(type, config, event));
384379
if (hwc->m_err) {
385-
Logger::Warning("failed to set perf event: %s", event);
380+
// Logger::Warning("failed to set perf event: %s", event);
386381
return false;
387382
}
388383
m_counters.emplace_back(std::move(hwc));
@@ -407,25 +402,27 @@ bool HardwareCounter::eventExists(const char *event) {
407402
return false;
408403
}
409404

410-
bool HardwareCounter::setPerfEvents(folly::StringPiece sevents) {
405+
bool HardwareCounter::setPerfEvents(std::string sevents) {
411406
// Make a copy of the string for use with strtok.
412407
auto const sevents_buf = static_cast<char*>(malloc(sevents.size() + 1));
413-
SCOPE_EXIT { free(sevents_buf); };
414408
memcpy(sevents_buf, sevents.data(), sevents.size());
415409
sevents_buf[sevents.size()] = '\0';
416410

417411
char* strtok_buf = nullptr;
418412
char* s = strtok_r(sevents_buf, ",", &strtok_buf);
413+
bool success = true;
419414
while (s) {
420415
if (!eventExists(s) && !addPerfEvent(s)) {
421-
return false;
416+
success = false;
417+
break;
422418
}
423419
s = strtok_r(nullptr, ",", &strtok_buf);
424420
}
425-
return true;
421+
free(sevents_buf);
422+
return success;
426423
}
427424

428-
bool HardwareCounter::SetPerfEvents(folly::StringPiece events) {
425+
bool HardwareCounter::SetPerfEvents(std::string events) {
429426
return s_counter->setPerfEvents(events);
430427
}
431428

scripts/perf-counters/src/hardware-counter.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@
1010
#ifndef incl_HPHP_UTIL_HARDWARE_COUNTER_H_
1111
#define incl_HPHP_UTIL_HARDWARE_COUNTER_H_
1212

13-
#include "hphp/util/thread-local.h"
14-
15-
#include <folly/Range.h>
13+
#include "thread-local.h"
1614

1715
#include <cstdint>
16+
#include <memory>
1817
#include <vector>
1918

2019
namespace HPHP {
@@ -43,7 +42,7 @@ class HardwareCounter {
4342
static int64_t GetInstructionCount();
4443
static int64_t GetLoadCount();
4544
static int64_t GetStoreCount();
46-
static bool SetPerfEvents(folly::StringPiece events);
45+
static bool SetPerfEvents(std::string events);
4746
static void IncInstructionCount(int64_t amount);
4847
static void IncLoadCount(int64_t amount);
4948
static void IncStoreCount(int64_t amount);
@@ -61,7 +60,7 @@ class HardwareCounter {
6160
int64_t getStoreCount();
6261
bool eventExists(const char* event);
6362
bool addPerfEvent(const char* event);
64-
bool setPerfEvents(folly::StringPiece events);
63+
bool setPerfEvents(std::string events);
6564
void getPerfEvents(PerfEventCallback f, void* data);
6665
void clearPerfEvents();
6766

scripts/perf-counters/src/portability.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@
1010
#ifndef incl_HPHP_PORTABILITY_H_
1111
#define incl_HPHP_PORTABILITY_H_
1212

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
1621

1722
//////////////////////////////////////////////////////////////////////
1823

scripts/perf-counters/src/thread-local.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* of patent rights can be found in the PATENTS file in the same directory.
88
*/
99

10-
#include "hphp/util/thread-local.h"
10+
#include "thread-local.h"
1111

1212
#ifdef __linux__
1313
#include <link.h>

scripts/perf-counters/src/thread-local.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@
1010
#ifndef incl_HPHP_THREAD_LOCAL_H_
1111
#define incl_HPHP_THREAD_LOCAL_H_
1212

13+
#include <assert.h>
14+
#include <stdio.h>
15+
#include <stdlib.h>
16+
#include <stdint.h>
1317
#include <pthread.h>
14-
#include "hphp/util/exception.h"
1518
#include <errno.h>
16-
#include <folly/String.h>
19+
#include <stdexcept>
1720
#include <type_traits>
21+
#include <utility>
22+
#include "portability.h"
1823

1924
namespace HPHP {
2025

@@ -85,8 +90,7 @@ inline void ThreadLocalCheckReturn(int ret, const char *funcName) {
8590
if (ret != 0) {
8691
// This is used from global constructors so the safest thing to do is just
8792
// 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);
9094
exit(1);
9195
}
9296
}
@@ -357,7 +361,7 @@ template<typename T, bool throwOnNull = true>
357361
struct ThreadLocalProxy {
358362
T *get() const {
359363
if (m_p == nullptr && throwOnNull) {
360-
throw Exception("ThreadLocalProxy::get() called before set()");
364+
throw std::runtime_error("ThreadLocalProxy::get() called before set()");
361365
}
362366
return m_p;
363367
}
@@ -708,7 +712,7 @@ class ThreadLocalProxy {
708712
T *get() const {
709713
T *obj = (T*)pthread_getspecific(m_key);
710714
if (obj == nullptr && throwOnNull) {
711-
throw Exception("ThreadLocalProxy::get() called before set()");
715+
throw std::runtime_error("ThreadLocalProxy::get() called before set()");
712716
}
713717
return obj;
714718
}

0 commit comments

Comments
 (0)