void * updateStats_thread(void *)
{
double lastQueryAvg1 = -1 ;
double lastQueryAvg2 = -1 ;
double lastQueryAvg3 = -1 ;
double lastQueryAvg1Cnt = 0 ;
double lastQueryAvg2Cnt = 0 ;
double lastQueryAvg3Cnt = 0 ;
int period1 = 0 ;
int period2 = 0 ;
int period3 = 0 ;
double currentDiff ;
double totalQueryTime1=0.0;
double totalQueryTime2=0.0;
double totalQueryTime3=0.0;
struct timeval now ;
struct timeval then ;
gettimeofday(&then, NULL) ;
while (running) {
// First of all, let's only bother with updates if the window is visible.
if (visible) {
period1 = 0 ;
period2 = 0 ;
period3 = 0 ;
totalQueryTime1=0.0;
totalQueryTime2=0.0;
totalQueryTime3=0.0;
gettimeofday(&now, NULL) ;
// And only update every 5 seconds.
if (now.tv_sec - then.tv_sec >= UPDATE_PERIOD)
{
TimingQueue *tq_current = timeQ.next ;
TimingQueue *tq_prev = &timeQ ;
while (tq_current)
{
double currentDiff = timediff(tq_current->time, now) ;
if (currentDiff >= 0) {
if (currentDiff <= PERIOD1)
{
totalQueryTime1+=tq_current->qryTime;
period1++ ;
}
if (currentDiff <= PERIOD2)
{
totalQueryTime2+=tq_current->qryTime;
period2++ ;
}
if (currentDiff <= PERIOD3) {
totalQueryTime3+=tq_current->qryTime;
period3++ ;
} else {
// drop it from the queue
if (tq_current != timeQEnd)
{
tq_prev->next = tq_current->next ;
} else {
pthread_mutex_lock(&timingMutex) ;
timeQEnd->next = NULL ;
timeQEnd = tq_prev ;
pthread_mutex_unlock(&timingMutex) ;
}
delete(tq_current);
tq_current = NULL ;
break ;
}
}
tq_current = tq_current->next ;
}
/* Don't change the stats if we haven't been doing anything */
if (period1 > lastQueryAvg1Cnt)
{
lastQueryAvg1 = totalQueryTime1/period1;
}
lastQueryAvg1Cnt = period1;
if (period2 > lastQueryAvg2Cnt)
{
lastQueryAvg2 = totalQueryTime2/period2;
}
lastQueryAvg2Cnt = period2;
if (period3 > lastQueryAvg3Cnt)
{
lastQueryAvg3 = totalQueryTime3/period3;
}
lastQueryAvg3Cnt = period3;
char msg[100];
WCHAR wMsg[100] ;
sprintf (msg, "Avg qry load: %3.2f, %3.2f, %3.2f qry/s",
period1/PERIOD1, period2/PERIOD2, period3/PERIOD3) ;
wsprintf (wMsg, L"%hs",msg) ;
SetWindowText(StatusLines[queryThrougput], wMsg) ;
sprintf (msg, "Avg qry time: %3.2f, %3.2f, %3.2f s", lastQueryAvg1, lastQueryAvg2, lastQueryAvg3) ;
wsprintf (wMsg, L"%hs",msg) ;
SetWindowText(StatusLines[queryAvgTime], wMsg) ;
then = now ;
}
}
Sleep(1000) ;
}
return NULL ;
}