3

I have the following code

static unsigned long long GetCurrentTimeInNanoseconds()
{
    timespec t;
    clock_gettime(CLOCK_MONOTONIC, &t);
    return (unsigned long long)(t.tv_sec*1000000000ULL) + (unsigned long long)t.tv_nsec;
}

When I use it like this:

unsigned long long current = GetCurrentTimeInNanoseconds();
usleep(5000);
LOG_MESSAGE1( "sleep:  %llu ns", GetCurrentTimeInNanoseconds() - current);

It prints this:

"sleep: 5126953 ns"

But if I use it like this:

current = GetCurrentTimeInNanoseconds();
glFinish();
LOG_MESSAGE1( "finish:  %llu ns", GetCurrentTimeInNanoseconds() - current);

Then it prints:

"finish: 0 ns"

glFinish is a blocking call in opengl.. and when I change that one line of code in there my app slows down considerably. So how can it possibly take 0ns to do a glFinish on android? Can it be a nop? Otherwise does anyone see any problem with how I am getting the time or using the time?

BoredAndroidDeveloper
  • 1,251
  • 1
  • 11
  • 26
  • I think this is more of a question of why does `glFinish()` take 0 ns, rather than a question of how to get time on android. And it probably doesn't take 0 ns, but the clock just isn't giving you ns resolution (even though you requested it to). – Leif Andersen Feb 19 '12 at 23:08
  • I changed the title to reflect a little more what the problem is but I'm less concerned about glFinish supposedly taking 0ns as I am that possibly all of my time deltas are wrong because I'm not reading elapsed time right. If anyone can just give me a way to get an accurate time so I can profile my code I will gladly accept that as the answer. – BoredAndroidDeveloper Feb 20 '12 at 23:07

1 Answers1

2

This may help you out.

Android(Linux) uptime using CLOCK_MONOTONIC

Specifically the first answer.

CLOCK_MONOTONIC stops when the system is suspended. Some people felt this was a mistake, and subsequently there have been patches for adding a CLOCK_BOOTTIME clock: https://lwn.net/Articles/428176/ . I don't know if these patches have yet been included in the mainline kernel.

Community
  • 1
  • 1
NothingMore
  • 1,211
  • 9
  • 19
  • Wow, that would explain it. So there's no real way to get the time elapsed then huh? Interesting problem. Well ill mark this as correct cause that definitely explains it. If I can ill edit the title as well. Thanks guys! – BoredAndroidDeveloper Feb 20 '12 at 02:30
  • Spoke too soon! I just changed it to be CLOCK_REALTIME I still get 0ns. Even if glFinish is a NOP though wouldn't reading in the time take longer than 0ns? I feel like I'm doing something wrong in the time code. – BoredAndroidDeveloper Feb 20 '12 at 23:03
  • glFinish() might not actually do anything. Since glFinish() only blocks if there are still unprocessed opengl operations. http://stackoverflow.com/questions/2143240/opengl-glflush-vs-glfinish . – NothingMore Feb 21 '12 at 07:04
  • Well even if it doesn't do anything if I comment out that line of code my app returns to normal speed. But regardless if I just comment out that line of code then the time code states that time has elapsed. I mean if the way I request time is right I guess I'll move on.. but something doesn't seem right – BoredAndroidDeveloper Feb 21 '12 at 16:30
  • Yeah this is rather strange, there has to be some weird thing going on with glFinish() that is causing some issues since your code seems correct. – NothingMore Feb 21 '12 at 21:04