Page 1 of 1

Weird timer bug

Posted: Mon Oct 24, 2011 8:12 am
by pirateofserbia
Hello all,

I set PIT on IRQ0 @ 1mS intervals, and on every tick I increment one variable. That work perfect, I even measured time with external stopwatch, and it`s fine :)

The problem is when I want to measure time between some CPU activity. I set while loop @ 900M and increment some variable. Before I start to do this, I reed current number of ticks and store it in variable timerStart. Now after 900M while loop I read time again and it said that only ~1.5sec has passed, and in reality it`s about 10sec. First I thought that when CPU do it`s work I don`t get interrupt @ IRQ0, but no, if I check number of ticks later on, they are OK???

Here`s the code so u can see what I mean...

Code: Select all

struct CLOCK
{
    int32u H;   
    int32u M;  
    int32u S;   
    int32u mS;
};

void printSysTime()
{
__asm__ __volatile__("cli"); 		
	itoa(time.H, out);
	puts(out); putch(':');
	
	itoa(time.M, out);
	puts(out); putch(':');
	
	itoa(time.S, out);
	puts(out); putch('.');
	
	itoa(time.mS, out); 
	puts(out);	
__asm__ __volatile__("sti"); 
}
...

void startTimer()
{

	__asm__ __volatile__("cli"); 	
		timerStart.H  = time.H;
		timerStart.M  = time.M;
		timerStart.S  = time.S;
		timerStart.mS = time.mS;
	__asm__ __volatile__("sti"); 	
	
	puts("\nTimer started @: ");
	printTime(&timerStart);
	putch('\n');

}

struct CLOCK getTimer()
{
	struct CLOCK current;

	__asm__ __volatile__("cli"); 	
		current.H  = time.H;
		current.M  = time.M;
		current.S  = time.S;
		current.mS = time.mS;
	__asm__ __volatile__("sti"); 	
	

	puts("\nTimer ended @: ");
	printTime(&current);
	putch('\n');
	
        //empty for now, i have some other logic to do here...
        struct CLOCK ret;	
	return ret;	
}

...

void someFunc()
{
startTimer();

int i = 900000000;
int k = 0;
while(i-- > 0)
{
    k++;
}
getTimer();

}
and here he do k++ for ~10sec, but says that only 1.5 has passed. And if I call printSysTime() it shows the REAL values...

Re: Weird timer bug

Posted: Mon Oct 24, 2011 8:18 am
by bluemoon

Code: Select all

startTimer();
int i = 900000000;
int k = 0;
while(i-- > 0)
{
    k++;
}
getTimer();
Who tell you it last for ~10 seconds?
By the way, a smart optimizer probably eliminate the loop and generate i=0; k=900000000;

EDIT: a smarter optimizer would probably generate nothing in between your timer, since nobody reference i & k.

Re: Weird timer bug

Posted: Mon Oct 24, 2011 9:29 am
by pirateofserbia
well computing all that last ~10 sec. Because I started external stopwatch, run code in VirtualBox, run command that display start of measurement time, and after ~10sec message with finished time shows and says that 1.5sec has elapsed. I think I made something wrong with accessing variables, but I don`t know what...

Re: Weird timer bug

Posted: Mon Oct 24, 2011 10:27 am
by pirateofserbia
Ups, actually it does not last 10sec, but 1.5sec... Thing is that virtual machine stuck up for 10sec, but on real PC it works like a charm... Please sorry for stupid post.