|
Mixe for Privacy and Anonymity in the Internet
|
#include <CADatabase.hpp>
Public Member Functions | |
| CADatabase () | |
| ~CADatabase () | |
| SINT32 | insert (UINT8 key[16], UINT64 timestamp) |
| Inserts this key in the replay DB. | |
| SINT32 | start () |
| SINT32 | stop () |
| SINT32 | test () |
Static Public Member Functions | |
| 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 | |
| t_databaseInfo * | createDBInfo () |
| Creates and initialises a dbinfo struct. | |
| SINT32 | clearDB (t_databaseInfo *pDB) |
| clears the whole database pDB - but does not delete the hashtable pDB | |
| SINT32 | deleteDB (t_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 | |
| UINT64 | m_lastSwitch |
| t_databaseInfo * | m_currDatabase |
| t_databaseInfo * | m_nextDatabase |
| t_databaseInfo * | m_prevDatabase |
| volatile bool | m_bRun |
| volatile SINT32 | m_currentClock |
| CAMutex * | m_pMutex |
| CAThread * | m_pThread |
Friends | |
| THREAD_RETURN | db_loopMaintenance (void *param) |
Definition at line 52 of file CADatabase.hpp.
Definition at line 34 of file CADatabase.cpp.
References createDBInfo(), m_currDatabase, m_currentClock, m_lastSwitch, m_nextDatabase, m_pMutex, m_prevDatabase, and m_pThread.
Referenced by measurePerformance().
{
m_currDatabase=createDBInfo();
m_prevDatabase=createDBInfo();
m_nextDatabase=createDBInfo();
m_lastSwitch=time(NULL);
m_currentClock=0;
m_pThread=NULL;
m_pMutex=new CAMutex();
}
Definition at line 52 of file CADatabase.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;
m_pMutex = NULL;
}
| SINT32 CADatabase::clearDB | ( | t_databaseInfo * | pDB | ) | [private] |
clears the whole database pDB - but does not delete the hashtable pDB
| pDB | database to delete |
Definition at line 64 of file CADatabase.cpp.
References E_SUCCESS, _t_database_info::m_pHashTable, _t_database_info::m_u32Size, and _t_database_entry::next.
Referenced by deleteDB(), and nextClock().
{
UINT16 tmp,tmp2;
for(tmp=0;tmp<256;tmp++){
for(tmp2=0;tmp2<256;tmp2++){
while (pDBInfo->m_pHashTable[tmp][tmp2]!=NULL){
t_databaseEntry* anker=pDBInfo->m_pHashTable[tmp][tmp2];
pDBInfo->m_pHashTable[tmp][tmp2]=anker->next;
delete anker;
anker = NULL;
}
}
}
pDBInfo->m_u32Size=0;
return E_SUCCESS;
}
| t_databaseInfo * CADatabase::createDBInfo | ( | ) | [private] |
Creates and initialises a dbinfo struct.
Definition at line 45 of file CADatabase.cpp.
Referenced by CADatabase().
{
t_databaseInfo* pInfo=new t_databaseInfo;
memset(pInfo,NULL,sizeof(t_databaseInfo));
return pInfo;
}
| SINT32 CADatabase::deleteDB | ( | t_databaseInfo *& | pDB | ) | [private] |
Deletes the whole database pDB.
| pDB | database to delete |
Definition at line 81 of file CADatabase.cpp.
References clearDB(), and E_SUCCESS.
Referenced by ~CADatabase().
| SINT32 CADatabase::fill | ( | UINT32 | nrOfEntries | ) | [private] |
Pre fills the database with nrOfEntries random entries.
| nrOfEntries | number of entries to put in the database |
Definition at line 334 of file CADatabase.cpp.
References E_SUCCESS, getRandom(), and insert().
Referenced by measurePerformance(), and test().
{
UINT32 i=0;
UINT8 key[16];
while(i<nrOfEntries)
{
getRandom(key,16);
if(insert(key,time(NULL))==E_SUCCESS)
i++;
}
return E_SUCCESS;
}
| SINT32 CADatabase::insert | ( | UINT8 | key[16], |
| UINT64 | timestamp | ||
| ) |
Inserts this key in the replay DB.
Definition at line 90 of file CADatabase.cpp.
References E_SUCCESS, E_UNKNOWN, FUTURE_TOLERANCE, _t_database_entry::key, CAMutex::lock(), m_currDatabase, m_lastSwitch, m_nextDatabase, _t_database_info::m_pHashTable, m_pMutex, m_prevDatabase, _t_database_info::m_u32Size, _t_database_entry::next, SECONDS_PER_INTERVALL, and CAMutex::unlock().
Referenced by fill(), CALastMixA::loop(), CAFirstMixA::loop(), CALastMixB::loop(), and mm_loopReadFromMixBefore().
{
// insert hash if timestamp valid in prevDB currDB nextDB
m_pMutex->lock();
// return E_UNKNOWN if the timestamp is too old or is too far in the future
if ((timestamp<(m_lastSwitch-SECONDS_PER_INTERVALL))||(timestamp>(time(NULL)+FUTURE_TOLERANCE))) {
// timestamp not valid!!
m_pMutex->unlock();
return E_UNKNOWN;
}
t_databaseInfo* aktDB=NULL;
t_databaseInfo* prevDB=NULL;
t_databaseInfo* nextDB=NULL;
if(timestamp<m_lastSwitch){
aktDB=m_prevDatabase;
prevDB=NULL;
nextDB=m_currDatabase;
}
else if(timestamp<(m_lastSwitch+SECONDS_PER_INTERVALL)){
aktDB=m_currDatabase;
prevDB=m_prevDatabase;
nextDB=m_nextDatabase;
}
else {
aktDB=m_nextDatabase;
prevDB=m_currDatabase;
nextDB=NULL;
}
UINT64 hashkey= (((UINT64)key[2])<<56)+
(((UINT64)key[3])<<48)+
(((UINT64)key[4])<<40)+
(((UINT64)key[5])<<32)+
(((UINT64)key[6])<<24)+
(((UINT64)key[7])<<16)+
(((UINT64)key[8])<<8)+
((UINT64)key[9]);
// insert
if (prevDB!=NULL){
t_databaseEntry* tmp=prevDB->m_pHashTable[key[0]][key[1]];
while(tmp!=NULL){
if (tmp->key!=hashkey) {
tmp=tmp->next;
}
else {
// duplicate found!!
m_pMutex->unlock();
return E_UNKNOWN;
}
}
// inserting in DB
tmp=new t_databaseEntry;
tmp->next=prevDB->m_pHashTable[key[0]][key[1]];
tmp->key=hashkey;
prevDB->m_pHashTable[key[0]][key[1]]=tmp;
prevDB->m_u32Size++;
}
t_databaseEntry* tmp=aktDB->m_pHashTable[key[0]][key[1]];
while(tmp!=NULL){
if (tmp->key!=hashkey) {
tmp=tmp->next;
}
else {
// duplicate found!!
m_pMutex->unlock();
return E_UNKNOWN;
}
}
// inserting in DB
tmp=new t_databaseEntry;
tmp->next=aktDB->m_pHashTable[key[0]][key[1]];
tmp->key=hashkey;
aktDB->m_pHashTable[key[0]][key[1]]=tmp;
aktDB->m_u32Size++;
if (nextDB!=NULL){
t_databaseEntry* tmp=nextDB->m_pHashTable[key[0]][key[1]];
while(tmp!=NULL){
if (tmp->key!=hashkey) {
tmp=tmp->next;
}
else {
// duplicate found!!
m_pMutex->unlock();
return E_UNKNOWN;
}
}
// inserting in DB
tmp=new t_databaseEntry;
tmp->next=nextDB->m_pHashTable[key[0]][key[1]];
tmp->key=hashkey;
nextDB->m_pHashTable[key[0]][key[1]]=tmp;
nextDB->m_u32Size++;
}
m_pMutex->unlock();
return E_SUCCESS;
}
| SINT32 CADatabase::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 283 of file CADatabase.cpp.
References CADatabase(), diff64(), E_SUCCESS, fill(), getcurrentTimeMicros(), getRandom(), initRandom(), CAMsg::printMsg(), and simulateInsert().
{
initRandom();
CADatabase* 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 CADatabase();
pDatabase->fill(aktNrOfEntries);
UINT64 startTime,endTime;
getRandom(key,insertsPerMeasure*16);
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;
pDatabase = NULL;
getcurrentTimeMicros(endTime);
printf("delete takes %u microsecs\n",diff64(endTime,startTime));
}
aktNrOfEntries+=stepBy;
}
delete[] key;
key = NULL;
return E_SUCCESS;
}
| SINT32 CADatabase::nextClock | ( | ) | [private] |
Definition at line 242 of file CADatabase.cpp.
References clearDB(), E_SUCCESS, CAMutex::lock(), m_currDatabase, m_lastSwitch, m_nextDatabase, m_pMutex, m_prevDatabase, _t_database_info::m_u32Size, CAMsg::printMsg(), SECONDS_PER_INTERVALL, and CAMutex::unlock().
Referenced by db_loopMaintenance().
{
m_pMutex->lock();
m_lastSwitch+=SECONDS_PER_INTERVALL;
CAMsg::printMsg(LOG_DEBUG,"Replay DB Size was: %u\n",m_prevDatabase->m_u32Size);
clearDB(m_prevDatabase);
t_databaseInfo* tmpDB=m_prevDatabase;
m_prevDatabase=m_currDatabase;
m_currDatabase=m_nextDatabase;
m_nextDatabase=tmpDB;
m_pMutex->unlock();
return E_SUCCESS;
}
| SINT32 CADatabase::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 348 of file CADatabase.cpp.
References E_SUCCESS, CAMutex::lock(), m_currDatabase, m_currentClock, m_pMutex, _t_database_info::m_u32Size, 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_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];
// t_databaseEntry* hashList=aktDB->m_pHashTable[hashKey];
// if(hashList==NULL)
// {
// LP_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_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_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; */
}
Definition at line 202 of file CADatabase.cpp.
References db_loopMaintenance, m_bRun, m_pThread, CAThread::setMainLoop(), and CAThread::start().
Referenced by CAMiddleMix::init(), CALastMix::init(), and CAFirstMix::init().
{
m_pThread=new CAThread();
m_bRun=true;
m_pThread->setMainLoop(db_loopMaintenance);
return m_pThread->start(this);
}
Definition at line 210 of file CADatabase.cpp.
References E_SUCCESS, CAThread::join(), m_bRun, and m_pThread.
Referenced by ~CADatabase().
{
m_bRun=false;
SINT32 ret=E_SUCCESS;
if(m_pThread!=NULL)
{
ret=m_pThread->join();
delete m_pThread;
m_pThread=NULL;
}
return ret;
}
Definition at line 257 of file CADatabase.cpp.
References E_SUCCESS, and fill().
{
/* CADatabase oDatabase;
oDatabase.start();
UINT8 key[16];
memset(key,0,16);
UINT32 i;
for(i=0;i<20;i++)
{
getRandom(key,4);
oDatabase.insert(key,time(NULL));///TODO WRONG - fixme
}
for(i=0;i<200000;i++)
{
getRandom(key,16);
oDatabase.insert(key,time(NULL));//TODO WRONG - Fixme
}
oDatabase.stop();
*/
UINT32 entries=10000;
fill(entries);
return E_SUCCESS;
// return CADatabase::fill(entries);
}
| THREAD_RETURN db_loopMaintenance | ( | void * | param | ) | [friend] |
Definition at line 223 of file CADatabase.cpp.
Referenced by start().
{
INIT_STACK;
BEGIN_STACK("CADatabase::db_loopMaintenance");
CADatabase* pDatabase=(CADatabase*)param;
while(pDatabase->m_bRun)
{
sSleep(10);
if (pDatabase->m_lastSwitch+SECONDS_PER_INTERVALL<=time(NULL)) {
pDatabase->nextClock();
}
}
FINISH_STACK("CADatabase::db_loopMaintenance");
THREAD_RETURN_SUCCESS;
}
volatile bool CADatabase::m_bRun [private] |
Definition at line 106 of file CADatabase.hpp.
Referenced by db_loopMaintenance(), start(), and stop().
t_databaseInfo* CADatabase::m_currDatabase [private] |
Definition at line 103 of file CADatabase.hpp.
Referenced by CADatabase(), insert(), nextClock(), simulateInsert(), and ~CADatabase().
volatile SINT32 CADatabase::m_currentClock [private] |
Definition at line 108 of file CADatabase.hpp.
Referenced by CADatabase(), and simulateInsert().
UINT64 CADatabase::m_lastSwitch [private] |
Definition at line 102 of file CADatabase.hpp.
Referenced by CADatabase(), db_loopMaintenance(), insert(), and nextClock().
t_databaseInfo* CADatabase::m_nextDatabase [private] |
Definition at line 104 of file CADatabase.hpp.
Referenced by CADatabase(), insert(), nextClock(), and ~CADatabase().
CAMutex* CADatabase::m_pMutex [private] |
Definition at line 109 of file CADatabase.hpp.
Referenced by CADatabase(), insert(), nextClock(), simulateInsert(), and ~CADatabase().
t_databaseInfo* CADatabase::m_prevDatabase [private] |
Definition at line 105 of file CADatabase.hpp.
Referenced by CADatabase(), insert(), nextClock(), and ~CADatabase().
CAThread* CADatabase::m_pThread [private] |
Definition at line 110 of file CADatabase.hpp.
Referenced by CADatabase(), start(), and stop().
1.7.6.1