Skip to content

Commit

Permalink
Rename (un)likely to avoid conflict with boost (#127)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #127

Boost has defintions for likely and unlikely that conflict and can cause compiler errors.  Renaming to __hbt_likely and __hbt_unlikley to resolve the conflict.

Reviewed By: jj10306

Differential Revision: D44730441

fbshipit-source-id: dbab963a7afce2504052916b0e4866b9950023cd
  • Loading branch information
bigzachattack authored and facebook-github-bot committed Apr 7, 2023
1 parent 7a7b645 commit 72cb3f9
Show file tree
Hide file tree
Showing 15 changed files with 112 additions and 112 deletions.
42 changes: 21 additions & 21 deletions hbt/src/common/Defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ inline pid_t gettid() noexcept {
#endif

// Branch hint macros. C++20 will include them as part of language.
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#define __hbt_likely(x) __builtin_expect(!!(x), 1)
#define __hbt_unlikely(x) __builtin_expect(!!(x), 0)

template <class TStream>
TStream& LogCtxt(TStream& oss) {
Expand Down Expand Up @@ -132,41 +132,41 @@ class EnvironmentError : public std::exception {

#define HBT_THROW_EINVAL() HBT_THROW(std::invalid_argument)
#define HBT_THROW_EINVAL_IF(cond) \
if (unlikely(cond)) \
if (__hbt_unlikely(cond)) \
HBT_THROW_EINVAL() << "\t" << HBT_STRINGIFY(cond) << ". "

#define HBT_THROW_ENVIRONMENT(err) HBT_THROW(EnvironmentError, err)
#define HBT_THROW_ENVIRONMENT_IF(cond, err) \
if (unlikely(cond)) \
if (__hbt_unlikely(cond)) \
HBT_THROW_ENVIRONMENT(err) << "\t" << HBT_STRINGIFY(cond) << ". "

#define HBT_THROW_SYSTEM(err) \
HBT_THROW(std::system_error, err, std::system_category())
#define HBT_THROW_SYSTEM_IF(cond, err) \
if (unlikely(cond)) \
if (__hbt_unlikely(cond)) \
HBT_THROW_SYSTEM(err) << "\t" << HBT_STRINGIFY(cond) << ". "

#define HBT_THROW_SYSTEM_CODE(err) HBT_THROW(std::system_error, err)
#define HBT_THROW_SYSTEM_CODE_IF(cond, err) \
if (unlikely(cond)) \
if (__hbt_unlikely(cond)) \
HBT_THROW_SYSTEM_CODE(err) << "\t" << HBT_STRINGIFY(cond) << ". "

#define HBT_THROW_ASSERT() HBT_THROW(std::runtime_error)
#define HBT_THROW_ASSERT_IF(cond) \
if (unlikely(cond)) \
if (__hbt_unlikely(cond)) \
HBT_THROW_ASSERT() << "\t" << HBT_STRINGIFY(cond) << ". "

// Conditional throwing exception
#define HBT_THROW_IF_NULLPTR(ptr) \
if (unlikely(ptr == nullptr)) \
#define HBT_THROW_IF_NULLPTR(ptr) \
if (__hbt_unlikely(ptr == nullptr)) \
HBT_THROW_EINVAL() << HBT_STRINGIFY(ptr) << " has nullptr value. "

// Safe-cast to std::error_code
inline std::error_code toErrorCode(ssize_t e) {
if (unlikely(e <= 0)) {
if (__hbt_unlikely(e <= 0)) {
HBT_THROW_EINVAL() << "\n\tError " << e << " is not a positive number. "
<< " Is this value really an error?";
} else if (unlikely(e > std::numeric_limits<int>::max())) {
} else if (__hbt_unlikely(e > std::numeric_limits<int>::max())) {
HBT_THROW_EINVAL() << "\n\tError " << e << " is out of range. "
<< " Is this really an error?";
}
Expand Down Expand Up @@ -206,17 +206,17 @@ class LogEntry final {
<< "\n " << HBT_LOG_PREFFIX << "]\n => \033[0m"

#define HBT_LOG_INFO_IF(cond) \
if (unlikely(cond)) \
if (__hbt_unlikely(cond)) \
HBT_LOG_INFO()
#define HBT_LOG_WARNING_IF(cond) \
if (unlikely(cond)) \
if (__hbt_unlikely(cond)) \
HBT_LOG_WARNING()
#define HBT_LOG_ERROR_IF(cond) \
if (unlikely(cond)) \
if (__hbt_unlikely(cond)) \
HBT_LOG_ERROR()

#define HBT_DCHECK_NOT_NULLPTR(t) \
if (unlikely((t) == nullptr)) \
#define HBT_DCHECK_NOT_NULLPTR(t) \
if (__hbt_unlikely((t) == nullptr)) \
HBT_THROW_ASSERT() << "\n\tExpected argument to be not null."

#define __HBT_EXPAND_OPD(opd) HBT_STRINGIFY(opd) << " (" << (opd) << ")"
Expand All @@ -227,12 +227,12 @@ class LogEntry final {
// must handle all errors explicitly.
//

#define __HBT_DCHECK(a) \
if (unlikely(!((a)))) \
#define __HBT_DCHECK(a) \
if (__hbt_unlikely(!((a)))) \
HBT_THROW_ASSERT() << "\n\tExpected true for " << __HBT_EXPAND_OPD(a) << ". "

#define __HBT_DCHECK_CMP(a, b, op) \
if (unlikely(!((a)op(b)))) \
if (__hbt_unlikely(!((a)op(b)))) \
HBT_THROW_ASSERT() << "\n\tExpected " << __HBT_EXPAND_OPD(a) << " " \
<< HBT_STRINGIFY(op) << " " << __HBT_EXPAND_OPD(b) \
<< ". "
Expand Down Expand Up @@ -269,12 +269,12 @@ class LogEntry final {
// Argument checks
//
#define HBT_ARG_CHECK(a) \
if (unlikely(!((a)))) \
if (__hbt_unlikely(!((a)))) \
HBT_THROW_EINVAL() << "\n\tExpected argument to be true: " \
<< __HBT_EXPAND_OPD(a) << ". "

#define _HBT_ARG_CMP(a, b, op) \
if (unlikely(!((a)op(b)))) \
if (__hbt_unlikely(!((a)op(b)))) \
HBT_THROW_EINVAL() << "\n\tExpected argument " << __HBT_EXPAND_OPD(a) \
<< " " HBT_STRINGIFY(op) << " " << __HBT_EXPAND_OPD(b) \
<< ". "
Expand Down
2 changes: 1 addition & 1 deletion hbt/src/common/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ CpuId cpu_first_set(const cpu_set_t& cpu_set) noexcept {

CpuId getCpu() {
int ret = sched_getcpu();
if (unlikely(0 > ret)) {
if (__hbt_unlikely(0 > ret)) {
HBT_THROW_SYSTEM(errno) << "Error reading ID of current CPU.";
}
if (ret > kMaxCpus - 1) {
Expand Down
4 changes: 2 additions & 2 deletions hbt/src/common/System.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,9 @@ struct CpuSet {
cpu_set_t cpus;
CPU_ZERO(&cpus);
for (auto cpu : s) {
if (unlikely(cpu < 0)) {
if (__hbt_unlikely(cpu < 0)) {
HBT_THROW_EINVAL() << "Invalid CPU ID: " << cpu;
} else if (unlikely(cpu >= kMaxCpus)) {
} else if (__hbt_unlikely(cpu >= kMaxCpus)) {
HBT_THROW_EINVAL() << "Maximum CPU ID is " << kMaxCpus - 1
<< " Got CPU ID: " << cpu
<< ". Do you want to increase kMaxCpus?";
Expand Down
24 changes: 12 additions & 12 deletions hbt/src/perf_event/CpuEventsGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -1356,7 +1356,7 @@ ssize_t CpuEventsGroup<TImpl, TMode>::consume(unsigned max_num_records) {
reinterpret_cast<const struct perf_event_header*>(record_begin);
auto record_end =
data_start + ((data_tail + event_header->size) & data_offset_mask);
if (unlikely(record_end < record_begin)) {
if (__hbt_unlikely(record_end < record_begin)) {
// perf event is wrapped around the ring buffer, make a contiguous copy.
void* buffer = enlargeAuxBuffer(event_header->size);
const uint8_t* sentinel = data_start + data_size;
Expand All @@ -1376,7 +1376,7 @@ ssize_t CpuEventsGroup<TImpl, TMode>::consume(unsigned max_num_records) {
auto r = reinterpret_cast<const TRecordLost*>(event_header);
HBT_DCHECK_EQ(r->header.size, recordSize(*r));
err = this->asImpl_().handleRecordLost(*r);
if (unlikely(0 > err)) {
if (__hbt_unlikely(0 > err)) {
goto exit;
}
num_record_lost_ += r->num_lost;
Expand All @@ -1401,7 +1401,7 @@ ssize_t CpuEventsGroup<TImpl, TMode>::consume(unsigned max_num_records) {
auto r = reinterpret_cast<const TRecordExit*>(event_header);
HBT_DCHECK_EQ(r->header.size, recordSize(*r));
err = this->asImpl_().handleRecordExit(*r);
if (unlikely(0 > err)) {
if (__hbt_unlikely(0 > err)) {
goto exit;
}
++num_record_exit_;
Expand All @@ -1424,7 +1424,7 @@ ssize_t CpuEventsGroup<TImpl, TMode>::consume(unsigned max_num_records) {
auto r = reinterpret_cast<const TRecordFork*>(event_header);
HBT_DCHECK_EQ(r->header.size, recordSize(*r));
err = this->asImpl_().handleRecordFork(*r);
if (unlikely(0 > err)) {
if (__hbt_unlikely(0 > err)) {
goto exit;
}
++num_record_fork_;
Expand All @@ -1433,14 +1433,14 @@ ssize_t CpuEventsGroup<TImpl, TMode>::consume(unsigned max_num_records) {
case PERF_RECORD_SAMPLE: {
if constexpr (!std::is_void<TRecordSample>::value) {
auto r = reinterpret_cast<const TRecordSample*>(event_header);
if (unlikely(r->header.size != recordSize(*r))) {
if (__hbt_unlikely(r->header.size != recordSize(*r))) {
HBT_LOG_ERROR() << "Invalid record size of: " << r->header.size
<< " expected " << recordSize(*r);
err = -EPERM;
goto exit;
}
err = this->asImpl_().handleRecordSample(*r);
if (unlikely(0 > err)) {
if (__hbt_unlikely(0 > err)) {
goto exit;
}
++num_record_sample_;
Expand All @@ -1449,14 +1449,14 @@ ssize_t CpuEventsGroup<TImpl, TMode>::consume(unsigned max_num_records) {
case PERF_RECORD_READ: {
if constexpr (!std::is_void<TRecordRead>::value) {
auto r = reinterpret_cast<const TRecordRead*>(event_header);
if (unlikely(r->header.size != recordSize(*r))) {
if (__hbt_unlikely(r->header.size != recordSize(*r))) {
HBT_LOG_ERROR() << "Invalid record read size of: " << r->header.size
<< " expected " << recordSize(*r);
err = -EPERM;
goto exit;
}
err = this->asImpl_().handleRecordRead(*r);
if (unlikely(0 > err)) {
if (__hbt_unlikely(0 > err)) {
goto exit;
}
++num_record_read_;
Expand All @@ -1467,7 +1467,7 @@ ssize_t CpuEventsGroup<TImpl, TMode>::consume(unsigned max_num_records) {
auto r = reinterpret_cast<const TRecordAux*>(event_header);
HBT_DCHECK_EQ(r->header.size, recordSize(*r));
err = this->asImpl_().handleRecordAux(*r);
if (unlikely(0 > err)) {
if (__hbt_unlikely(0 > err)) {
goto exit;
}
++num_record_aux_;
Expand All @@ -1478,7 +1478,7 @@ ssize_t CpuEventsGroup<TImpl, TMode>::consume(unsigned max_num_records) {
auto r = reinterpret_cast<const TRecordItraceStart*>(event_header);
HBT_DCHECK_EQ(r->header.size, recordSize(*r));
err = this->asImpl_().handleRecordItraceStart(*r);
if (unlikely(0 > err)) {
if (__hbt_unlikely(0 > err)) {
goto exit;
}
++num_record_itrace_start_;
Expand All @@ -1489,7 +1489,7 @@ ssize_t CpuEventsGroup<TImpl, TMode>::consume(unsigned max_num_records) {
auto r = reinterpret_cast<const TRecordSwitchCpuWide*>(event_header);
HBT_DCHECK_EQ(r->header.size, recordSize(*r));
err = this->asImpl_().handleRecordSwitchCpuWide(*r);
if (unlikely(0 > err)) {
if (__hbt_unlikely(0 > err)) {
goto exit;
}
++num_record_switch_cpu_wide_;
Expand Down Expand Up @@ -1557,7 +1557,7 @@ void CpuEventsGroup<TImpl, TMode>::onCpuDataBufferRead(
std::min(rb_num_bytes, static_cast<size_t>(sentinel - begin_ptr));
CpuId cpuId = this->getCpu();

if (unlikely(end_ptr < begin_ptr)) {
if (__hbt_unlikely(end_ptr < begin_ptr)) {
// Ring buffer wrapped, so there are two slices
callback(cpuId, RbDataSlices(begin_ptr, len, data_start, data_size - len));
} else {
Expand Down
8 changes: 4 additions & 4 deletions hbt/src/perf_event/PerCpuCountSampleGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class CpuCountSampleGenerator final
ssize_t handleRecordSample(const mode::Sampling::Sample& r) noexcept {
HBT_DCHECK_EQ(r.nr, kNumEvents);

if (unlikely(count_.tstamp == kInvalidTimeStamp)) {
if (__hbt_unlikely(count_.tstamp == kInvalidTimeStamp)) {
// Discard this count because we don't know the start of
// of the counting interval due to the lost package.

Expand All @@ -153,7 +153,7 @@ class CpuCountSampleGenerator final
return 0;
}

if (unlikely(count_.tstamp > r.tstamp)) {
if (__hbt_unlikely(count_.tstamp > r.tstamp)) {
HBT_LOG_ERROR() << fmt::format(
"New record's timestamp ({}) precedes last timestamp ({})",
r.tstamp,
Expand All @@ -180,15 +180,15 @@ class CpuCountSampleGenerator final

// Do not write the "values" part of Count.
auto ret = output_producer_.write(&count_, kWriteByteSize);
if (unlikely(kNumBytesDropIfFull > 0 && ret == -ENOSPC)) {
if (__hbt_unlikely(kNumBytesDropIfFull > 0 && ret == -ENOSPC)) {
auto err = output_producer_.dropN(kNumBytesDropIfFull);
HBT_THROW_ASSERT_IF(0 > err);
// Retry now that space has been cleared. There is
// no another producer for this ringbuffer so it
// cannot fail due too lack of space again.
ret = output_producer_.write(&count_, kWriteByteSize);
}
if (unlikely(0 > ret)) {
if (__hbt_unlikely(0 > ret)) {
count_.tstamp = kInvalidTimeStamp;
return ret;
}
Expand Down
2 changes: 1 addition & 1 deletion hbt/src/perf_event/PerCpuSampleGeneratorBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class PerCpuSampleGeneratorBase : public PerCpuBase<TCpuGenerator> {

// At this point it either finished because it hit stop_ts or
// an unhandled error.
if (likely(ret >= 0)) {
if (__hbt_likely(ret >= 0)) {
++num_done;
}
}
Expand Down
24 changes: 12 additions & 12 deletions hbt/src/perf_event/PerCpuThreadSwitchGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ class CpuThreadSwitchGenerator final

auto handleRecordLost(const mode::ContextSwitch::Lost& r) noexcept {
auto ret = pendingRecordLost_(r.sample_id.tstamp);
if (unlikely(0 > ret)) {
if (__hbt_unlikely(0 > ret)) {
return ret;
}

Expand All @@ -242,7 +242,7 @@ class CpuThreadSwitchGenerator final
auto ev_start =
tagstack::Event::makeWriteErrorsStart(last_tstamp_ + 1, comp_unit_id_);
ret = output_producer_.write(ev_start);
if (likely(ret >= 0)) {
if (__hbt_likely(ret >= 0)) {
last_tstamp_ = r.sample_id.tstamp;
in_write_errors_ = true;
// We've lost track of which threads are active. TID could've wrapped
Expand All @@ -266,7 +266,7 @@ class CpuThreadSwitchGenerator final

TimeStamp new_tstamp = sample_id_ptr->tstamp;
auto ret = pendingRecordLost_(new_tstamp);
if (unlikely(0 > ret)) {
if (__hbt_unlikely(0 > ret)) {
return ret;
}

Expand Down Expand Up @@ -304,15 +304,15 @@ class CpuThreadSwitchGenerator final
HBT_DCHECK_GE(r.tid, 0);

auto ret = pendingRecordLost_(new_tstamp);
if (unlikely(0 > ret)) {
if (__hbt_unlikely(0 > ret)) {
return ret;
}
auto [vid, is_new_vid] =
thread_stats_->selectNextVid(static_cast<pid_t>(r.tid));
auto ev = tagstack::Event::makeThreadDestruction(
new_tstamp, tid_level_, vid, comp_unit_id_);
ret = output_producer_.write(ev);
if (likely(ret >= 0)) {
if (__hbt_likely(ret >= 0)) {
if (is_new_vid) {
auto& tinfo = thread_stats_->createThreadInfo(
static_cast<pid_t>(r.ppid),
Expand All @@ -339,7 +339,7 @@ class CpuThreadSwitchGenerator final
HBT_DCHECK_GE(r.tid, 0);

auto ret = pendingRecordLost_(new_tstamp);
if (unlikely(0 > ret)) {
if (__hbt_unlikely(0 > ret)) {
return ret;
}

Expand All @@ -348,7 +348,7 @@ class CpuThreadSwitchGenerator final
auto ev = tagstack::Event::makeThreadCreation(
new_tstamp, tid_level_, vid, comp_unit_id_);
ret = output_producer_.write(ev);
if (likely(ret >= 0)) {
if (__hbt_likely(ret >= 0)) {
if (!is_new_vid) {
// Has already been seen, probably coming from another CPU.
auto& tinfo = thread_stats_->info.at(vid);
Expand All @@ -375,7 +375,7 @@ class CpuThreadSwitchGenerator final
TimeStamp new_tstamp = r.sample_id.tstamp;

auto ret = pendingRecordLost_(new_tstamp);
if (unlikely(0 > ret)) {
if (__hbt_unlikely(0 > ret)) {
return ret;
}

Expand All @@ -395,7 +395,7 @@ class CpuThreadSwitchGenerator final
thread_stats_->selectNextVid(static_cast<pid_t>(r.sample_id.tid));
ret = output_producer_.write(tagstack::Event::makeSwitchIn(
new_tstamp, tid_level_, vid, comp_unit_id_));
if (unlikely(is_new_vid && ret >= 0)) {
if (__hbt_unlikely(is_new_vid && ret >= 0)) {
// Add ThreadInfo if this is a new vid.
pid_t pid = static_cast<pid_t>(r.sample_id.pid);
pid_t tid = static_cast<pid_t>(r.sample_id.tid);
Expand All @@ -405,7 +405,7 @@ class CpuThreadSwitchGenerator final
}
}

if (likely(ret >= 0)) {
if (__hbt_likely(ret >= 0)) {
last_tstamp_ = new_tstamp;
}
return ret;
Expand All @@ -429,12 +429,12 @@ class CpuThreadSwitchGenerator final

/// Try to write any pending record lost and clear error if succesful.
inline ssize_t pendingRecordLost_(TimeStamp tstamp) noexcept {
if (likely(!in_write_errors_)) {
if (__hbt_likely(!in_write_errors_)) {
return 0;
}
auto ev_end = tagstack::Event::makeWriteErrorsEnd(tstamp, comp_unit_id_);
auto ret = output_producer_.write(ev_end);
if (likely(ret >= 0)) {
if (__hbt_likely(ret >= 0)) {
last_tstamp_ = tstamp;
in_write_errors_ = false;
}
Expand Down
Loading

0 comments on commit 72cb3f9

Please sign in to comment.