From 1de07f3e956cc4d679220cac9016710f33c054be Mon Sep 17 00:00:00 2001 From: Henry Qin Date: Mon, 29 Oct 2018 18:19:26 -0600 Subject: [PATCH] Add prefetch to PerfUtils --- src/Util.h | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/Util.h b/src/Util.h index 88caddf..16fd3d8 100644 --- a/src/Util.h +++ b/src/Util.h @@ -32,6 +32,9 @@ #include #include +#include +#include + namespace PerfUtils { /** @@ -195,6 +198,40 @@ serialReadPmc(int ecx) { return retVal; } +/** + * Prefetch the cache lines containing [object, object + numBytes) into the + * processor's caches. + * The best docs for this are in the Intel instruction set reference under + * PREFETCH. + * \param object + * The start of the region of memory to prefetch. + * \param numBytes + * The size of the region of memory to prefetch. + */ +static inline void +prefetch(const void* object, uint64_t numBytes) +{ + uint64_t offset = reinterpret_cast(object) & 0x3fUL; + const char* p = reinterpret_cast(object) - offset; + for (uint64_t i = 0; i < offset + numBytes; i += 64) + _mm_prefetch(p + i, _MM_HINT_T0); +} + +/** + * Prefetch the cache lines containing the given object into the + * processor's caches. + * The best docs for this are in the Intel instruction set reference under + * PREFETCHh. + * \param object + * A pointer to the object in memory to prefetch. + */ +template +static inline void +prefetch(const T* object) +{ + prefetch(object, sizeof(*object)); +} + #define PERFUTILS_DIE(format_, ...) \ do { \ fprintf(stderr, format_, ##__VA_ARGS__); \