Add files for new block stats in order to track things in our compression algorithms

This commit is contained in:
Pavel Krajcevski 2012-10-06 17:52:26 -04:00
parent f82173f423
commit fcbca9ca8d
5 changed files with 226 additions and 1 deletions

View File

@ -3,6 +3,7 @@ SET( SOURCES
"src/TexComp.cpp"
"src/CompressedImage.cpp"
"src/Image.cpp"
"src/BlockStats.cpp"
)
SET( HEADERS
@ -10,6 +11,7 @@ SET( HEADERS
"include/CompressedImage.h"
"include/TexCompTypes.h"
"include/Image.h"
"include/BlockStats.h"
)
# Make sure to add the appropriate stopwatch files...
@ -82,7 +84,7 @@ ELSE()
SET( SOURCES ${SOURCES} "src/ThreadGroup.cpp" )
SET( SOURCES ${SOURCES} "src/WorkerQueue.cpp" )
SET( HEADERS ${HEADERS} "src/Thread.h" )
SET( HEADERS ${HEADERS} "include/Thread.h" )
SET( HEADERS ${HEADERS} "src/ThreadGroup.h" )
SET( HEADERS ${HEADERS} "src/WorkerQueue.h" )
ENDIF()

59
Core/include/BlockStats.h Normal file
View File

@ -0,0 +1,59 @@
#ifndef __BLOCK_STATS_H__
#define __BLOCK_STATS_H__
#include "TexCompTypes.h"
#include "ReferenceCounter.h"
#include "Thread.h"
struct BlockStat {
friend class BlockStatManager;
public:
BlockStat(const CHAR *statName, uint64 stat);
BlockStat(const CHAR *statName, double stat);
BlockStat(const BlockStat &);
BlockStat &operator=(const BlockStat &);
private:
static const int kStatNameSz = 32;
char m_StatName[kStatNameSz];
union {
uint64 m_IntStat;
double m_FloatStat;
};
};
class BlockStatManager {
public:
BlockStatManager(int nBlocks);
~BlockStatManager();
uint32 BeginBlock();
void AddStat(uint32 blockIdx, const BlockStat &stat);
private:
class BlockStatList {
public:
BlockStatList();
~BlockStatList();
void AddStat(const BlockStat &stat);
private:
BlockStatList(const BlockStat &stat);
BlockStat m_Stat;
BlockStatList *m_Tail;
ReferenceCounter m_Counter;
} *m_BlockStatList;
uint32 m_BlockStatListSz;
TCMutex m_Mutex;
uint32 m_NextBlock;
ReferenceCounter m_Counter;
};
#endif // __BLOCK_STATS_H__

View File

@ -0,0 +1,49 @@
#ifndef __REFERENCE_COUNTER_H__
#define __REFERENCE_COUNTER_H__
#include "TexCompTypes.h"
class ReferenceCounter {
public:
ReferenceCounter() : m_ReferenceCount( new uint32 ) {
*m_ReferenceCount = 1;
}
ReferenceCounter(const ReferenceCounter &other)
: m_ReferenceCount(other.m_ReferenceCount) {
IncRefCount();
}
ReferenceCounter &operator=(const ReferenceCounter &other) {
DecRefCount();
m_ReferenceCount = other.m_ReferenceCount;
IncRefCount();
}
uint32 GetRefCount() const {
if(m_ReferenceCount)
return *m_ReferenceCount;
else
return 0;
}
void DecRefCount() {
if(!m_ReferenceCount) return;
(*m_ReferenceCount)--;
if(*m_ReferenceCount == 0) {
delete m_ReferenceCount;
m_ReferenceCount = 0;
}
}
void IncRefCount() {
if(!m_ReferenceCount) return;
(*m_ReferenceCount)++;
}
private:
uint32 *m_ReferenceCount;
};
#endif // __REFERENCE_COUNTER_H__

115
Core/src/BlockStats.cpp Normal file
View File

@ -0,0 +1,115 @@
#include "BlockStats.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
template <typename T>
static T max(const T &a, const T &b) {
return (a > b)? a : b;
}
////////////////////////////////////////////////////////////////////////////////
//
// BlockStat implementation
//
////////////////////////////////////////////////////////////////////////////////
BlockStat::BlockStat(const CHAR *statName, double stat) :
m_FloatStat(stat)
{
strncpy(m_StatName, statName, kStatNameSz);
}
BlockStat::BlockStat(const BlockStat &other) {
memcpy(this, &other, sizeof(*this));
}
BlockStat &BlockStat::operator=(const BlockStat &other) {
memcpy(this, &other, sizeof(*this));
}
////////////////////////////////////////////////////////////////////////////////
//
// BlockStat Manager Implementation
//
////////////////////////////////////////////////////////////////////////////////
BlockStatManager::BlockStatManager(int nBlocks)
: m_BlockStatListSz(max(nBlocks, 0))
, m_NextBlock(0)
{
m_BlockStatList = new BlockStatList[m_BlockStatListSz];
if(!m_BlockStatList) {
fprintf(stderr, "Out of memory!\n");
assert(false);
exit(1);
}
}
BlockStatManager::~BlockStatManager() {
if(m_Counter.GetRefCount() == 0) {
delete [] m_BlockStatList;
}
}
uint32 BlockStatManager::BeginBlock() {
if((m_NextBlock + 1) == m_BlockStatListSz) {
fprintf(stderr, "WARNING -- BlockStatManager::BeginBlock(), reached end of block list.\n");
assert(false);
return m_NextBlock;
}
TCLock lock(m_Mutex);
return m_NextBlock++;
}
void BlockStatManager::AddStat(uint32 blockIdx, const BlockStat &stat) {
if(blockIdx >= m_BlockStatListSz) {
fprintf(stderr, "WARNING -- BlockStatManager::AddStat(), block index out of bounds!\n");
assert(false);
return;
}
m_BlockStatList[blockIdx].AddStat(stat);
}
////////////////////////////////////////////////////////////////////////////////
//
// BlockStat List Implementation
//
////////////////////////////////////////////////////////////////////////////////
static const CHAR *kNullBlockString = "NULL_BLOCK_STAT";
static const uint32 kNullBlockStringLength = strlen(kNullBlockString);
BlockStatManager::BlockStatList::BlockStatList()
: m_Tail(0)
, m_Stat(kNullBlockString, 0.0)
{ }
BlockStatManager::BlockStatList::BlockStatList(const BlockStat &stat)
: m_Tail(0)
, m_Stat(stat)
{
}
BlockStatManager::BlockStatList::~BlockStatList() {
if(m_Counter.GetRefCount() == 0 && m_Tail) {
delete m_Tail;
}
}
void BlockStatManager::BlockStatList::AddStat(const BlockStat &stat) {
if(!m_Tail) {
m_Tail->AddStat(stat);
}
else {
if(strncmp(m_Stat.m_StatName, kNullBlockString, kNullBlockStringLength) == 0) {
m_Stat = stat;
}
else {
m_Tail = new BlockStatList(stat);
}
}
}