Skip to content

Commit

Permalink
No longer require highway.h for profiler.h
Browse files Browse the repository at this point in the history
hn::timer is still available via timer-inl.h but deprecated.
PiperOrigin-RevId: 712826044
  • Loading branch information
jan-wassenberg authored and copybara-github committed Jan 7, 2025
1 parent fdfce1f commit ea4dda1
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 192 deletions.
2 changes: 0 additions & 2 deletions hwy/contrib/sort/bench_sort.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@
#include "hwy/contrib/sort/traits-inl.h"
#include "hwy/contrib/sort/traits128-inl.h"
#include "hwy/tests/test_util-inl.h"
#include "hwy/timer-inl.h"
#include "hwy/nanobenchmark.h"
#include "hwy/timer.h"
#include "hwy/per_target.h"
// clang-format on

#if HWY_OS_LINUX
Expand Down
3 changes: 0 additions & 3 deletions hwy/nanobenchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,10 @@

#include "hwy/base.h"
#include "hwy/robust_statistics.h"
#include "hwy/timer-inl.h"
#include "hwy/timer.h"

namespace hwy {
namespace {
namespace timer = hwy::HWY_NAMESPACE::timer;

static const timer::Ticks timer_resolution = platform::TimerResolution();

// Estimates the expected value of "lambda" values with a variable number of
Expand Down
6 changes: 2 additions & 4 deletions hwy/perf_counters_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "hwy/nanobenchmark.h" // Unpredictable1
#include "hwy/tests/hwy_gtest.h"
#include "hwy/tests/test_util-inl.h"
#include "hwy/timer-inl.h"
#include "hwy/timer.h"

#if !HWY_OS_WIN
Expand All @@ -39,11 +38,10 @@ using ::hwy::platform::PerfCounters;
void ReadAndPrint(uint64_t r, double* values) {
char cpu100[100];
const bool have_stop = hwy::platform::HaveTimerStop(cpu100);
const uint64_t t0 = HWY_STATIC_DISPATCH(timer::Start());
const uint64_t t0 = timer::Start();

PerfCounters counters;
const uint64_t t1 = have_stop ? HWY_STATIC_DISPATCH(timer::Stop())
: HWY_STATIC_DISPATCH(timer::Start());
const uint64_t t1 = have_stop ? timer::Stop() : timer::Start();
const double elapsed_ns =
static_cast<double>(t1 - t0) * 1E9 / platform::InvariantTicksPerSecond();
fprintf(stderr, "r: %d, any valid %d extrapolate %f, overhead %.1f ns\n",
Expand Down
25 changes: 10 additions & 15 deletions hwy/profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
#include "hwy/cache_control.h" // FlushStream
// #include "hwy/contrib/sort/vqsort.h"
#include "hwy/robust_statistics.h"
#include "hwy/timer-inl.h"
#include "hwy/timer.h"

#define PROFILER_PRINT_OVERHEAD 0
Expand Down Expand Up @@ -228,8 +227,7 @@ class Results {
// Draw all required information from the packets, which can be discarded
// afterwards. Called whenever this thread's storage is full.
void AnalyzePackets(const Packet* packets, const size_t num_packets) {
namespace hn = HWY_NAMESPACE;
const uint64_t t0 = hn::timer::Start();
const uint64_t t0 = timer::Start();

for (size_t i = 0; i < num_packets; ++i) {
const Packet p = packets[i];
Expand Down Expand Up @@ -260,15 +258,14 @@ class Results {
}
}

const uint64_t t1 = hn::timer::Stop();
const uint64_t t1 = timer::Stop();
analyze_elapsed_ += t1 - t0;
}

// Incorporates results from another thread. Call after all threads have
// exited any zones.
void Assimilate(Results& other) {
namespace hn = HWY_NAMESPACE;
const uint64_t t0 = hn::timer::Start();
const uint64_t t0 = timer::Start();
HWY_DASSERT(depth_ == 0);
HWY_DASSERT(other.depth_ == 0);

Expand All @@ -277,14 +274,13 @@ class Results {
UpdateOrAdd(zone.BiasedOffset(), zone.NumCalls(), zone.Duration());
}
other.num_zones_ = 0;
const uint64_t t1 = hn::timer::Stop();
const uint64_t t1 = timer::Stop();
analyze_elapsed_ += t1 - t0 + other.analyze_elapsed_;
}

// Single-threaded.
void Print() {
namespace hn = HWY_NAMESPACE;
const uint64_t t0 = hn::timer::Start();
const uint64_t t0 = timer::Start();
MergeDuplicates();

// Sort by decreasing total (self) cost.
Expand All @@ -307,7 +303,7 @@ class Results {
}
num_zones_ = 0;

const uint64_t t1 = hn::timer::Stop();
const uint64_t t1 = timer::Stop();
analyze_elapsed_ += t1 - t0;
printf("Total analysis [s]: %f\n",
static_cast<double>(analyze_elapsed_) * inv_freq);
Expand Down Expand Up @@ -550,13 +546,13 @@ class Zone {

// (Capture timestamp ASAP, not inside WriteEntry.)
HWY_FENCE;
const uint64_t timestamp = HWY_NAMESPACE::timer::Start();
const uint64_t timestamp = timer::Start();
thread_specific->WriteEntry(name, timestamp);
}

HWY_NOINLINE ~Zone() {
HWY_FENCE;
const uint64_t timestamp = HWY_NAMESPACE::timer::Stop();
const uint64_t timestamp = timer::Stop();
StaticThreadSpecific()->WriteExit(timestamp);
HWY_FENCE;
}
Expand Down Expand Up @@ -597,7 +593,6 @@ class Zone {
#define PROFILER_PRINT_RESULTS hwy::Zone::PrintResults

inline void ThreadSpecific::ComputeOverhead() {
namespace hn = HWY_NAMESPACE;
// Delay after capturing timestamps before/after the actual zone runs. Even
// with frequency throttling disabled, this has a multimodal distribution,
// including 32, 34, 48, 52, 59, 62.
Expand Down Expand Up @@ -643,12 +638,12 @@ inline void ThreadSpecific::ComputeOverhead() {
// Analysis time should not be included => must fit within buffer.
HWY_DASSERT(kReps * 2 < max_packets_);
std::atomic_thread_fence(std::memory_order_seq_cst);
const uint64_t t0 = hn::timer::Start();
const uint64_t t0 = timer::Start();
for (size_t i = 0; i < kReps; ++i) {
PROFILER_ZONE("Dummy");
}
FlushStream();
const uint64_t t1 = hn::timer::Stop();
const uint64_t t1 = timer::Stop();
HWY_DASSERT(num_packets_ + buffer_size_ == kReps * 2);
buffer_size_ = 0;
num_packets_ = 0;
Expand Down
170 changes: 8 additions & 162 deletions hwy/timer-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// High-resolution and high-precision timer
// DEPRECATED, use timer.h instead.

#include "hwy/timer.h"

// Per-target include guard
// NOTE: this file could/should be a normal header, but user code may reference
// hn::timer, and defining that here requires highway.h.
#if defined(HIGHWAY_HWY_TIMER_INL_H_) == defined(HWY_TARGET_TOGGLE)
#ifdef HIGHWAY_HWY_TIMER_INL_H_
#undef HIGHWAY_HWY_TIMER_INL_H_
Expand All @@ -27,170 +26,17 @@

#include "hwy/highway.h"

#if defined(_WIN32) || defined(_WIN64)
#ifndef NOMINMAX
#define NOMINMAX
#endif // NOMINMAX
#include <windows.h>
#endif

#if defined(__APPLE__)
#include <mach/mach.h>
#include <mach/mach_time.h>
#endif

#if defined(__HAIKU__)
#include <OS.h>
#endif

#if HWY_ARCH_PPC && defined(__GLIBC__) && defined(__powerpc64__)
#include <sys/platform/ppc.h> // NOLINT __ppc_get_timebase_freq
#endif

#if HWY_ARCH_X86 && HWY_COMPILER_MSVC
#include <intrin.h>
#endif

#include <stdint.h>
#include <time.h> // clock_gettime

HWY_BEFORE_NAMESPACE();
namespace hwy {
namespace HWY_NAMESPACE {
namespace timer {

// Ticks := platform-specific timer values (CPU cycles on x86). Must be
// unsigned to guarantee wraparound on overflow.
using Ticks = uint64_t;
// Deprecated aliases so that old code still compiles. Prefer to use
// `hwy::timer::*` from timer.h because that does not require highway.h.
using Ticks = hwy::timer::Ticks;

// Start/Stop return absolute timestamps and must be placed immediately before
// and after the region to measure. We provide separate Start/Stop functions
// because they use different fences.
//
// Background: RDTSC is not 'serializing'; earlier instructions may complete
// after it, and/or later instructions may complete before it. 'Fences' ensure
// regions' elapsed times are independent of such reordering. The only
// documented unprivileged serializing instruction is CPUID, which acts as a
// full fence (no reordering across it in either direction). Unfortunately
// the latency of CPUID varies wildly (perhaps made worse by not initializing
// its EAX input). Because it cannot reliably be deducted from the region's
// elapsed time, it must not be included in the region to measure (i.e.
// between the two RDTSC).
//
// The newer RDTSCP is sometimes described as serializing, but it actually
// only serves as a half-fence with release semantics. Although all
// instructions in the region will complete before the final timestamp is
// captured, subsequent instructions may leak into the region and increase the
// elapsed time. Inserting another fence after the final RDTSCP would prevent
// such reordering without affecting the measured region.
//
// Fortunately, such a fence exists. The LFENCE instruction is only documented
// to delay later loads until earlier loads are visible. However, Intel's
// reference manual says it acts as a full fence (waiting until all earlier
// instructions have completed, and delaying later instructions until it
// completes). AMD assigns the same behavior to MFENCE.
//
// We need a fence before the initial RDTSC to prevent earlier instructions
// from leaking into the region, and arguably another after RDTSC to avoid
// region instructions from completing before the timestamp is recorded.
// When surrounded by fences, the additional RDTSCP half-fence provides no
// benefit, so the initial timestamp can be recorded via RDTSC, which has
// lower overhead than RDTSCP because it does not read TSC_AUX. In summary,
// we define Start = LFENCE/RDTSC/LFENCE; Stop = RDTSCP/LFENCE.
//
// Using Start+Start leads to higher variance and overhead than Stop+Stop.
// However, Stop+Stop includes an LFENCE in the region measurements, which
// adds a delay dependent on earlier loads. The combination of Start+Stop
// is faster than Start+Start and more consistent than Stop+Stop because
// the first LFENCE already delayed subsequent loads before the measured
// region. This combination seems not to have been considered in prior work:
// http://akaros.cs.berkeley.edu/lxr/akaros/kern/arch/x86/rdtsc_test.c
//
// Note: performance counters can measure 'exact' instructions-retired or
// (unhalted) cycle counts. The RDPMC instruction is not serializing and also
// requires fences. Unfortunately, it is not accessible on all OSes and we
// prefer to avoid kernel-mode drivers. Performance counters are also affected
// by several under/over-count errata, so we use the TSC instead.

// Returns a 64-bit timestamp in unit of 'ticks'; to convert to seconds,
// divide by InvariantTicksPerSecond.
inline Ticks Start() {
Ticks t;
#if HWY_ARCH_PPC && defined(__GLIBC__) && defined(__powerpc64__)
asm volatile("mfspr %0, %1" : "=r"(t) : "i"(268));
#elif HWY_ARCH_ARM_A64 && !HWY_COMPILER_MSVC
// pmccntr_el0 is privileged but cntvct_el0 is accessible in Linux and QEMU.
asm volatile("mrs %0, cntvct_el0" : "=r"(t));
#elif HWY_ARCH_X86 && HWY_COMPILER_MSVC
_ReadWriteBarrier();
_mm_lfence();
_ReadWriteBarrier();
t = __rdtsc();
_ReadWriteBarrier();
_mm_lfence();
_ReadWriteBarrier();
#elif HWY_ARCH_X86_64
asm volatile(
"lfence\n\t"
"rdtsc\n\t"
"shl $32, %%rdx\n\t"
"or %%rdx, %0\n\t"
"lfence"
: "=a"(t)
:
// "memory" avoids reordering. rdx = TSC >> 32.
// "cc" = flags modified by SHL.
: "rdx", "memory", "cc");
#elif HWY_ARCH_RISCV
asm volatile("fence; rdtime %0" : "=r"(t));
#elif defined(_WIN32) || defined(_WIN64)
LARGE_INTEGER counter;
(void)QueryPerformanceCounter(&counter);
t = counter.QuadPart;
#elif defined(__APPLE__)
t = mach_absolute_time();
#elif defined(__HAIKU__)
t = system_time_nsecs(); // since boot
#else // POSIX
timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
t = static_cast<Ticks>(ts.tv_sec * 1000000000LL + ts.tv_nsec);
#endif
return t;
}

// WARNING: on x86, caller must check HasRDTSCP before using this!
inline Ticks Stop() {
uint64_t t;
#if HWY_ARCH_PPC && defined(__GLIBC__) && defined(__powerpc64__)
asm volatile("mfspr %0, %1" : "=r"(t) : "i"(268));
#elif HWY_ARCH_ARM_A64 && !HWY_COMPILER_MSVC
// pmccntr_el0 is privileged but cntvct_el0 is accessible in Linux and QEMU.
asm volatile("mrs %0, cntvct_el0" : "=r"(t));
#elif HWY_ARCH_X86 && HWY_COMPILER_MSVC
_ReadWriteBarrier();
unsigned aux;
t = __rdtscp(&aux);
_ReadWriteBarrier();
_mm_lfence();
_ReadWriteBarrier();
#elif HWY_ARCH_X86_64
// Use inline asm because __rdtscp generates code to store TSC_AUX (ecx).
asm volatile(
"rdtscp\n\t"
"shl $32, %%rdx\n\t"
"or %%rdx, %0\n\t"
"lfence"
: "=a"(t)
:
// "memory" avoids reordering. rcx = TSC_AUX. rdx = TSC >> 32.
// "cc" = flags modified by SHL.
: "rcx", "rdx", "memory", "cc");
#else
t = Start();
#endif
return t;
}
inline Ticks Start() { return hwy::timer::Start(); }
inline Ticks Stop() { return hwy::timer::Stop(); }

} // namespace timer

Expand Down
3 changes: 0 additions & 3 deletions hwy/timer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,12 @@

#include "hwy/base.h"
#include "hwy/robust_statistics.h"
#include "hwy/timer-inl.h"

#if HWY_ARCH_X86 && !HWY_COMPILER_MSVC
#include <cpuid.h> // NOLINT
#endif

namespace hwy {
namespace timer = hwy::HWY_NAMESPACE::timer;

namespace platform {
namespace {

Expand Down
Loading

0 comments on commit ea4dda1

Please sign in to comment.