|
Mixe for Privacy and Anonymity in the Internet
|
#include <CAReplayDatabase.hpp>
Public Member Functions | |
| CAReplayDatabase () | |
| ~CAReplayDatabase () | |
| SINT32 | insert (UINT8 key[16]) |
| Inserts this key in the replay DB. | |
| SINT32 | start () |
| SINT32 | stop () |
| SINT32 | getCurrentReplayTimestamp (tReplayTimestamp &replayTimestamp) const |
| Returns the current Replay timestamp for this database. | |
| UINT32 | getRefTime () const |
| Returns the local time in seconds since epoch for replay timestamp='0' for this database. | |
Static Public Member Functions | |
| static SINT32 | getReplayTimestampForTime (tReplayTimestamp &replayTimestamp, UINT32 aktTime, UINT32 refTime) |
| Returns the replay timestamp for this reference time (seconds since epoch) and time. | |
| static SINT32 | getTimeForReplayTimestamp (UINT32 &refTime, tReplayTimestamp replayTimestamp) |
| Returns the refernce time (seconds since epoch) for the given replay timestamp. | |
| static SINT32 | test () |
| static SINT32 | measurePerformance (UINT8 *strLogFile, UINT32 lowerBoundEntries, UINT32 upperBoundEntries, UINT32 stepBy, UINT32 meassuresPerStep, UINT32 insertsPerMeasure) |
| This mehtod can be used to measure the performance of the Replay database. | |
Private Member Functions | |
| LP_replay_databaseEntry | getNewDBEntry (t_replay_databaseInfo *pDB) |
| t_replay_databaseInfo * | createDBInfo () |
| Creates and initialises a dbinfo struct. | |
| SINT32 | clearDB (t_replay_databaseInfo *pDB) |
| clears the whole database pDB - but does not delete the hashtable pDB | |
| SINT32 | deleteDB (t_replay_databaseInfo *&pDB) |
| Deletes the whole database pDB. | |
| SINT32 | nextClock () |
| SINT32 | fill (UINT32 nrOfEntries) |
| Pre fills the database with nrOfEntries random entries. | |
| SINT32 | simulateInsert (UINT8 key[16]) |
| This is a modified copy of insert() which simulates the insert() function as close as possible without actually changing the replay database. | |
Private Attributes | |
| t_replay_databaseInfo * | m_currDatabase |
| t_replay_databaseInfo * | m_nextDatabase |
| t_replay_databaseInfo * | m_prevDatabase |
| volatile bool | m_bRun |
| UINT32 | m_refTime |
| volatile SINT32 | m_currentClock |
| CAMutex * | m_pMutex |
| CAThread * | m_pThread |
Friends | |
| THREAD_RETURN | replaydb_loopMaintenance (void *param) |
Definition at line 64 of file CAReplayDatabase.hpp.
Definition at line 34 of file CAReplayDatabase.cpp.
References createDBInfo(), m_currDatabase, m_currentClock, m_nextDatabase, m_pMutex, m_prevDatabase, m_pThread, and m_refTime.
Referenced by measurePerformance().
{
m_currDatabase=createDBInfo();
m_prevDatabase=createDBInfo();
m_nextDatabase=createDBInfo();
m_refTime=time(NULL);
m_currentClock=0;
m_pThread=NULL;
m_pMutex=new CAMutex();
}
Definition at line 52 of file CAReplayDatabase.cpp.
References deleteDB(), CAMutex::lock(), m_currDatabase, m_nextDatabase, m_pMutex, m_prevDatabase, stop(), and CAMutex::unlock().
{
m_pMutex->lock();
stop();
deleteDB(m_currDatabase);
deleteDB(m_nextDatabase);
deleteDB(m_prevDatabase);
m_pMutex->unlock();
delete m_pMutex;
}
| SINT32 CAReplayDatabase::clearDB | ( | t_replay_databaseInfo * | pDB | ) | [private] |
clears the whole database pDB - but does not delete the hashtable pDB
| pDB | database to delete |
Definition at line 63 of file CAReplayDatabase.cpp.
References E_SUCCESS, __t_database_info::m_pHeap, __t_database_info::m_pLastHeap, and __t_replay_database_heap::next.
Referenced by deleteDB(), and nextClock().
{
while(pDBInfo->m_pHeap!=NULL)
{
pDBInfo->m_pLastHeap=pDBInfo->m_pHeap;
pDBInfo->m_pHeap=pDBInfo->m_pHeap->next;
delete pDBInfo->m_pLastHeap;
}
memset(pDBInfo,0,sizeof(t_replay_databaseInfo));
return E_SUCCESS;
}
| t_replay_databaseInfo * CAReplayDatabase::createDBInfo | ( | ) | [private] |
Creates and initialises a dbinfo struct.
Definition at line 45 of file CAReplayDatabase.cpp.
Referenced by CAReplayDatabase().
{
t_replay_databaseInfo* pInfo=new t_replay_databaseInfo;
memset(pInfo,0,sizeof(t_replay_databaseInfo));
return pInfo;
}
| SINT32 CAReplayDatabase::deleteDB | ( | t_replay_databaseInfo *& | pDB | ) | [private] |
Deletes the whole database pDB.
| pDB | database to delete |
Definition at line 75 of file CAReplayDatabase.cpp.
References clearDB(), and E_SUCCESS.
Referenced by ~CAReplayDatabase().
| SINT32 CAReplayDatabase::fill | ( | UINT32 | nrOfEntries | ) | [private] |
Pre fills the database with nrOfEntries random entries.
| nrOfEntries | number of entries to put in the database |
Definition at line 321 of file CAReplayDatabase.cpp.
References E_SUCCESS, getRandom(), and insert().
Referenced by measurePerformance().
{
UINT32 i=0;
UINT8 key[16];
key[14]=0;
key[15]=0;
while(i<nrOfEntries)
{
getRandom(key,14);
if(insert(key)==E_SUCCESS)
i++;
}
return E_SUCCESS;
}
| SINT32 CAReplayDatabase::getCurrentReplayTimestamp | ( | tReplayTimestamp & | replayTimestamp | ) | const |
Returns the current Replay timestamp for this database.
Returns the current replay timestamp for this database.
| replayTimestamp | stores the current replay timestamp |
Definition at line 249 of file CAReplayDatabase.cpp.
References getReplayTimestampForTime(), and m_refTime.
Referenced by nextClock(), and replaydb_loopMaintenance().
{
return getReplayTimestampForTime(replayTimestamp,time(NULL),m_refTime);
}
| LP_replay_databaseEntry CAReplayDatabase::getNewDBEntry | ( | t_replay_databaseInfo * | pDB | ) | [inline, private] |
Definition at line 112 of file CAReplayDatabase.hpp.
References __t_replay_database_heap::m_pEntries, __t_database_info::m_pHeap, __t_database_info::m_pLastHeap, __t_database_info::m_s32FreeEntries, __t_replay_database_heap::next, and REPLAY_DB_ENTRIES_PER_HEAP.
Referenced by insert(), and simulateInsert().
{
if(pDB->m_pHeap==NULL)
{
pDB->m_pHeap=new t_replay_databaseHeap;
pDB->m_pHeap->next=NULL;
pDB->m_pLastHeap=pDB->m_pHeap;
pDB->m_s32FreeEntries=REPLAY_DB_ENTRIES_PER_HEAP;
}
else if(pDB->m_s32FreeEntries==0)
{
pDB->m_pLastHeap->next=new t_replay_databaseHeap;
pDB->m_pLastHeap=pDB->m_pLastHeap->next;
pDB->m_pLastHeap->next=NULL;
pDB->m_s32FreeEntries=REPLAY_DB_ENTRIES_PER_HEAP;
}
return &pDB->m_pLastHeap->m_pEntries[--pDB->m_s32FreeEntries];
}
| UINT32 CAReplayDatabase::getRefTime | ( | ) | const [inline] |
Returns the local time in seconds since epoch for replay timestamp='0' for this database.
Definition at line 76 of file CAReplayDatabase.hpp.
References m_refTime.
{
return m_refTime;
}
| SINT32 CAReplayDatabase::getReplayTimestampForTime | ( | tReplayTimestamp & | replayTimestamp, |
| UINT32 | aktTime, | ||
| UINT32 | refTime | ||
| ) | [static] |
Returns the replay timestamp for this reference time (seconds since epoch) and time.
Definition at line 255 of file CAReplayDatabase.cpp.
References E_SUCCESS, t_replay_timestamp::interval, t_replay_timestamp::offset, and SECONDS_PER_INTERVALL.
Referenced by getCurrentReplayTimestamp().
{
UINT32 timeDiff=aktTime-refTime;
replayTimestamp.interval=timeDiff/SECONDS_PER_INTERVALL;
replayTimestamp.offset=timeDiff%SECONDS_PER_INTERVALL;
return E_SUCCESS;
}
| static SINT32 CAReplayDatabase::getTimeForReplayTimestamp | ( | UINT32 & | refTime, |
| tReplayTimestamp | replayTimestamp | ||
| ) | [inline, static] |
Returns the refernce time (seconds since epoch) for the given replay timestamp.
Definition at line 85 of file CAReplayDatabase.hpp.
References E_SUCCESS, t_replay_timestamp::interval, t_replay_timestamp::offset, and SECONDS_PER_INTERVALL.
{
time_t now=time(NULL);
refTime=(UINT32)(now-replayTimestamp.interval*SECONDS_PER_INTERVALL-replayTimestamp.offset);
return E_SUCCESS;
}
| SINT32 CAReplayDatabase::insert | ( | UINT8 | key[16] | ) |
Inserts this key in the replay DB.
The last two bytes are the timestamp
Definition at line 84 of file CAReplayDatabase.cpp.
References E_SUCCESS, E_UNKNOWN, getNewDBEntry(), __t_replay_database_entry::key, __t_replay_database_entry::left, CAMutex::lock(), m_currDatabase, m_currentClock, m_nextDatabase, __t_database_info::m_pHashTable, m_pMutex, m_prevDatabase, __t_database_info::m_u32Size, __t_replay_database_entry::right, and CAMutex::unlock().
Referenced by fill(), and test().
{
m_pMutex->lock();
UINT16 timestamp=(key[14]<<8)|key[15];
if(timestamp<m_currentClock-1||timestamp>m_currentClock+1)
{
m_pMutex->unlock();
return E_UNKNOWN;
}
t_replay_databaseInfo* aktDB=m_currDatabase;
if(timestamp>m_currentClock)
{
aktDB=m_nextDatabase;
}
else if(timestamp<m_currentClock)
{
aktDB=m_prevDatabase;
}
UINT16 hashKey=(key[8]<<8)|key[9];
LP_replay_databaseEntry hashList=aktDB->m_pHashTable[hashKey];
if(hashList==NULL)
{
LP_replay_databaseEntry newEntry=getNewDBEntry(aktDB);
newEntry->left=NULL;
newEntry->right=NULL;
newEntry->key=key[0]<<24|key[1]<<16|key[2]<<8|key[3];
aktDB->m_pHashTable[hashKey]=newEntry;
aktDB->m_u32Size++;
m_pMutex->unlock();
return E_SUCCESS;
}
else
{
UINT32 ret=key[0]<<24|key[1]<<16|key[2]<<8|key[3];;
LP_replay_databaseEntry before=NULL;
do
{
//newEntry->keymemcmp(key,hashList->key,6);
if(ret==hashList->key)
{
m_pMutex->unlock();
return E_UNKNOWN;
}
before=hashList;
if(hashList->key<ret)
{
hashList=hashList->right;
}
else
{
hashList=hashList->left;
}
} while(hashList!=NULL);
LP_replay_databaseEntry newEntry=getNewDBEntry(aktDB);
newEntry->left=newEntry->right=NULL;
//memcpy(newEntry->key,key,6);
newEntry->key=ret;
if(before->key<ret)
{
before->right=newEntry;
}
else
before->left=newEntry;
}
aktDB->m_u32Size++;
m_pMutex->unlock();
return E_SUCCESS;
}
| SINT32 CAReplayDatabase::measurePerformance | ( | UINT8 * | strLogFile, |
| UINT32 | lowerBoundEntries, | ||
| UINT32 | upperBoundEntries, | ||
| UINT32 | stepBy, | ||
| UINT32 | meassuresPerStep, | ||
| UINT32 | insertsPerMeasure | ||
| ) | [static] |
This mehtod can be used to measure the performance of the Replay database.
The results are stored in a file in csv format. Ths method will do several measures with different numbers of elements in the database. These number could be specified using owerBoundEntries,upperBoundEntries and stepBy.
| strLogFile | the log file name |
| lowerBoundEntries | the number of entries in the database (at beginn) |
| upperBoundEntries | the number of entries in the database (at end) |
| stepBy | how many entries should be added for each new measurement |
| meassuresPerStep | how many measure values should be generate per step. That means that the experiement is repeated this many times. |
| insertsPerMeasure | one measure value will be the time: (Total Insertion Time)/insertsPerMeasure |
Definition at line 264 of file CAReplayDatabase.cpp.
References CAReplayDatabase(), diff64(), E_SUCCESS, fill(), getcurrentTimeMicros(), getRandom(), initRandom(), m_currentClock, CAMsg::printMsg(), and simulateInsert().
Referenced by main().
{
initRandom();
CAReplayDatabase* pDatabase=NULL;
UINT32 aktNrOfEntries=lowerBoundEntries;
UINT8* key=new UINT8[insertsPerMeasure*16];
UINT8* aktKey;
SINT32 file=open((char*)strLogFile,O_CREAT|O_WRONLY|O_LARGEFILE|O_TRUNC,S_IREAD|S_IWRITE);
char buff[255];
const char* atemplate="%u,%u,%u\n";
const char* header="#The format is as follows: Number of Entries in DB, Number of Inserts done, Total time for Inserts (in micro seconds)\n";
write(file,header,strlen(header));
while(aktNrOfEntries<=upperBoundEntries)
{
CAMsg::printMsg(LOG_DEBUG,"Starting measurement with %u entries in the replay database\n",aktNrOfEntries);
for(UINT32 i=0;i<meassuresPerStep;i++)
{
pDatabase=new CAReplayDatabase();
pDatabase->m_currentClock=0;
pDatabase->fill(aktNrOfEntries);
UINT64 startTime,endTime;
getRandom(key,insertsPerMeasure*16);
aktKey=key+14;
for(UINT32 j=0;j<insertsPerMeasure;j++)
{
*aktKey=0;
aktKey++;
*aktKey=0;
aktKey+=15;
}
aktKey=key;
getcurrentTimeMicros(startTime);
for(UINT32 j=0;j<insertsPerMeasure;j++)
{
pDatabase->simulateInsert(aktKey);
aktKey+=16;
}
getcurrentTimeMicros(endTime);
sprintf(buff,atemplate,aktNrOfEntries,insertsPerMeasure,diff64(endTime,startTime));
write(file,buff,strlen(buff));
printf("Start delete \n");
getcurrentTimeMicros(startTime);
delete pDatabase;
getcurrentTimeMicros(endTime);
printf("delete takes %u microsecs\n",diff64(endTime,startTime));
}
aktNrOfEntries+=stepBy;
}
delete[] key;
return E_SUCCESS;
}
| SINT32 CAReplayDatabase::nextClock | ( | ) | [private] |
Definition at line 197 of file CAReplayDatabase.cpp.
References clearDB(), E_SUCCESS, getCurrentReplayTimestamp(), t_replay_timestamp::interval, CAMutex::lock(), m_currDatabase, m_currentClock, m_nextDatabase, m_pMutex, m_prevDatabase, __t_database_info::m_u32Size, CAMsg::printMsg(), and CAMutex::unlock().
Referenced by replaydb_loopMaintenance().
{
m_pMutex->lock();
tReplayTimestamp rt;
getCurrentReplayTimestamp(rt);
m_currentClock=rt.interval;
CAMsg::printMsg(LOG_DEBUG,"Replay DB Size was: %u\n",m_prevDatabase->m_u32Size);
clearDB(m_prevDatabase);
t_replay_databaseInfo* tmpDB=m_prevDatabase;
m_prevDatabase=m_currDatabase;
m_currDatabase=m_nextDatabase;
m_nextDatabase=tmpDB;
m_pMutex->unlock();
return E_SUCCESS;
}
| SINT32 CAReplayDatabase::simulateInsert | ( | UINT8 | key[16] | ) | [private] |
This is a modified copy of insert() which simulates the insert() function as close as possible without actually changing the replay database.
Definition at line 336 of file CAReplayDatabase.cpp.
References E_SUCCESS, E_UNKNOWN, getNewDBEntry(), __t_replay_database_entry::key, __t_replay_database_entry::left, CAMutex::lock(), m_currDatabase, m_currentClock, m_nextDatabase, __t_database_info::m_pHashTable, m_pMutex, m_prevDatabase, __t_database_info::m_u32Size, __t_replay_database_entry::right, and CAMutex::unlock().
Referenced by measurePerformance().
{
m_pMutex->lock();
UINT16 timestamp=(key[14]<<8)|key[15];
if(timestamp<m_currentClock-1||timestamp>m_currentClock+1)
{
m_pMutex->unlock();
return E_UNKNOWN;
}
t_replay_databaseInfo* aktDB=m_currDatabase;
if(timestamp>m_currentClock)
{
aktDB=m_nextDatabase;
}
else if(timestamp<m_currentClock)
{
aktDB=m_prevDatabase;
}
UINT16 hashKey=(key[8]<<8)|key[9];
LP_replay_databaseEntry hashList=aktDB->m_pHashTable[hashKey];
if(hashList==NULL)
{
LP_replay_databaseEntry newEntry=getNewDBEntry(aktDB);
newEntry->left=NULL;
newEntry->right=NULL;
newEntry->key=key[0]<<24|key[1]<<16|key[2]<<8|key[3];
m_nextDatabase->m_pHashTable[hashKey]=newEntry; //we simply use the 'next' Database to simulate insert while not inserting anythign in the current db
aktDB->m_u32Size++;
m_pMutex->unlock();
return E_SUCCESS;
}
else
{
UINT32 ret=key[0]<<24|key[1]<<16|key[2]<<8|key[3];
LP_replay_databaseEntry before=NULL;
do
{
//newEntry->keymemcmp(key,hashList->key,6);
if(ret==hashList->key)
{
m_pMutex->unlock();
return E_UNKNOWN;
}
before=hashList;
if(hashList->key<ret)
{
hashList=hashList->right;
}
else
{
hashList=hashList->left;
}
} while(hashList!=NULL);
LP_replay_databaseEntry newEntry=getNewDBEntry(aktDB);
newEntry->left=newEntry->right=NULL;
//memcpy(newEntry->key,key,6);
newEntry->key=ret;
if(before->key<ret)
{
newEntry->right=newEntry; //do the pointer operation without actually changing the DB
}
else
{
newEntry->left=newEntry; //do the pointer operation without actually changing the DB
}
}
aktDB->m_u32Size++;
m_pMutex->unlock();
return E_SUCCESS;
}
Definition at line 154 of file CAReplayDatabase.cpp.
References m_bRun, m_pThread, replaydb_loopMaintenance, CAThread::setMainLoop(), and CAThread::start().
Referenced by test().
{
m_pThread=new CAThread((UINT8*)"DB Maintenance Thread");
m_bRun=true;
m_pThread->setMainLoop(replaydb_loopMaintenance);
return m_pThread->start(this);
}
Definition at line 162 of file CAReplayDatabase.cpp.
References E_SUCCESS, CAThread::join(), m_bRun, and m_pThread.
Referenced by test(), and ~CAReplayDatabase().
{
m_bRun=false;
SINT32 ret=E_SUCCESS;
if(m_pThread!=NULL)
{
ret=m_pThread->join();
delete m_pThread;
m_pThread=NULL;
}
return ret;
}
| SINT32 CAReplayDatabase::test | ( | ) | [static] |
TODO WRONG - fixme
Definition at line 213 of file CAReplayDatabase.cpp.
References E_SUCCESS, getRandom(), insert(), start(), and stop().
{
CAReplayDatabase oDatabase;
oDatabase.start();
UINT8 key[16];
memset(key,0,16);
UINT32 i;
for(i=0;i<20;i++)
{
getRandom(key,1);
oDatabase.insert(key);
}
for(i=0;i<200000;i++)
{
getRandom(key,16);
oDatabase.insert(key);//TODO WRONG - Fixme
}
oDatabase.stop();
//check it
//TODO fixme!
/* for(i=0;i<0x10000;i++)
{
LP_databaseEntry tmp=oDatabase.m_currDatabase[i];
while(tmp!=NULL&&tmp->next!=NULL)
{
if(memcmp(tmp->key,tmp->next->key,14)>=0)
return E_UNKNOWN;
tmp=tmp->next;
}
}*/
return E_SUCCESS;
}
| THREAD_RETURN replaydb_loopMaintenance | ( | void * | param | ) | [friend] |
Definition at line 175 of file CAReplayDatabase.cpp.
Referenced by start().
{
INIT_STACK;
BEGIN_STACK("CADatabase::db_loopMaintenance");
CAReplayDatabase* pDatabase=(CAReplayDatabase*)param;
tReplayTimestamp rt;
pDatabase->getCurrentReplayTimestamp(rt);
pDatabase->m_currentClock=rt.interval;
while(pDatabase->m_bRun)
{
sSleep(10);
SINT32 secondsTilNextClock=((pDatabase->m_currentClock+1)*SECONDS_PER_INTERVALL)+pDatabase->m_refTime-time(NULL);
if(secondsTilNextClock<=0&&pDatabase->m_bRun)
pDatabase->nextClock();
}
FINISH_STACK("CADatabase::db_loopMaintenance");
THREAD_RETURN_SUCCESS;
}
volatile bool CAReplayDatabase::m_bRun [private] |
Definition at line 158 of file CAReplayDatabase.hpp.
Referenced by replaydb_loopMaintenance(), start(), and stop().
Definition at line 155 of file CAReplayDatabase.hpp.
Referenced by CAReplayDatabase(), insert(), nextClock(), simulateInsert(), and ~CAReplayDatabase().
volatile SINT32 CAReplayDatabase::m_currentClock [private] |
Definition at line 160 of file CAReplayDatabase.hpp.
Referenced by CAReplayDatabase(), insert(), measurePerformance(), nextClock(), replaydb_loopMaintenance(), and simulateInsert().
Definition at line 156 of file CAReplayDatabase.hpp.
Referenced by CAReplayDatabase(), insert(), nextClock(), simulateInsert(), and ~CAReplayDatabase().
CAMutex* CAReplayDatabase::m_pMutex [private] |
Definition at line 161 of file CAReplayDatabase.hpp.
Referenced by CAReplayDatabase(), insert(), nextClock(), simulateInsert(), and ~CAReplayDatabase().
Definition at line 157 of file CAReplayDatabase.hpp.
Referenced by CAReplayDatabase(), insert(), nextClock(), simulateInsert(), and ~CAReplayDatabase().
CAThread* CAReplayDatabase::m_pThread [private] |
Definition at line 162 of file CAReplayDatabase.hpp.
Referenced by CAReplayDatabase(), start(), and stop().
UINT32 CAReplayDatabase::m_refTime [private] |
Definition at line 159 of file CAReplayDatabase.hpp.
Referenced by CAReplayDatabase(), getCurrentReplayTimestamp(), getRefTime(), and replaydb_loopMaintenance().
1.7.6.1