sync with work: add compile-time BITS and CeilLog2 and optional IO instrumentation

This was SVN commit r10324.
This commit is contained in:
janwas
2011-09-26 08:29:57 +00:00
parent 67d99f3001
commit 16f5016a1a
2 changed files with 40 additions and 0 deletions
+24
View File
@@ -152,6 +152,9 @@ inline bool is_pow2(T n)
return (n & (n-1)) == 0;
}
// as above; intended for use in static_assert
#define IS_POW2(n) (((n) != 0) && ((n) & ((n)-1)) == 0)
template<typename T>
inline T LeastSignificantBit(T x)
{
@@ -165,6 +168,7 @@ inline T ClearLeastSignificantBit(T x)
return x & (x-1);
}
/**
* ceil(log2(x))
*
@@ -186,6 +190,26 @@ inline size_t ceil_log2(T x)
return log;
}
// compile-time variant of the above
template<size_t N>
struct CeilLog2
{
enum { value = 1 + CeilLog2<(N+1)/2>::value };
};
template<>
struct CeilLog2<1>
{
enum { value = 0 };
};
template<>
struct CeilLog2<0>
{
enum { value = 0 };
};
/**
* floor(log2(f))
+16
View File
@@ -223,6 +223,10 @@ LIB_API Status WaitUntilComplete(aiocb& cb, size_t queueDepth);
//-----------------------------------------------------------------------------
// Run
#ifndef ENABLE_IO_STATS
#define ENABLE_IO_STATS 0
#endif
// (hooks must be passed by const reference to allow passing rvalues.
// functors with non-const member data can mark them as mutable.)
template<class CompletedHook, class IssueHook>
@@ -233,6 +237,11 @@ static inline Status Run(const Operation& op, const Parameters& p = Parameters()
ControlBlockRingBuffer controlBlockRingBuffer(op, p);
#if ENABLE_IO_STATS
const double t0 = timer_Time();
COMPILER_FENCE;
#endif
const off_t numBlocks = p.blockSize? (off_t)DivideRoundUp((u64)op.size, (u64)p.blockSize) : 1;
for(off_t blocksIssued = 0, blocksCompleted = 0; blocksCompleted < numBlocks; blocksCompleted++)
{
@@ -256,6 +265,13 @@ static inline Status Run(const Operation& op, const Parameters& p = Parameters()
RETURN_STATUS_FROM_CALLBACK(completedHook((u8*)cb.aio_buf, cb.aio_nbytes));
}
#if ENABLE_IO_STATS
COMPILER_FENCE;
const double t1 = timer_Time();
const off_t totalSize = p.blockSize? numBlocks*p.blockSize : op.size;
debug_printf(L"IO: %.2f MB/s (%.2f)\n", totalSize/(t1-t0)/1e6, (t1-t0)*1e3);
#endif
return INFO::OK;
}