Skip to content

Commit

Permalink
Add prefetch to PerfUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
hq6 committed Oct 30, 2018
1 parent 5676231 commit 1de07f3
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
#include <unordered_set>
#include <vector>

#include <mmintrin.h>
#include <xmmintrin.h>

namespace PerfUtils {

/**
Expand Down Expand Up @@ -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<uint64_t>(object) & 0x3fUL;
const char* p = reinterpret_cast<const char*>(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<typename T>
static inline void
prefetch(const T* object)
{
prefetch(object, sizeof(*object));
}

#define PERFUTILS_DIE(format_, ...) \
do { \
fprintf(stderr, format_, ##__VA_ARGS__); \
Expand Down

0 comments on commit 1de07f3

Please sign in to comment.