26.02.2014 Views

Getting Started with QNX Neutrino - QNX Software Systems

Getting Started with QNX Neutrino - QNX Software Systems

Getting Started with QNX Neutrino - QNX Software Systems

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

© 2009, <strong>QNX</strong> <strong>Software</strong> <strong>Systems</strong> GmbH & Co. KG. Advanced topics<br />

An accurate timestamp<br />

resolution for a little while yet!) The nsec member indicates how many nanoseconds<br />

elapse between ticks of the base timing clock. The default is 10 milliseconds (1<br />

millisecond on machines <strong>with</strong> CPU speeds of greater than 40 MHz), so the nsec<br />

member (if you use the “get” form of the call by specifying the old parameter) will<br />

show approximately 10 million nanoseconds. (As we discussed above, in “Clock<br />

interrupt sources,” it’s not going to be exactly 10 millisecond.)<br />

While you can certainly feel free to try to set the base timing resolution on your<br />

system to something ridiculously small, the kernel will step in and prevent you from<br />

doing that. Generally, you can set most systems in the 1 millisecond to hundreds of<br />

microseconds range.<br />

There is one timebase that might be available on your processor that doesn’t obey the<br />

rules of “base timing resolution” we just described. Some processors have a<br />

high-frequency (high-accuracy) counter built right into them, which <strong>Neutrino</strong> can let<br />

you have access to via the ClockCycles() call. For example, on a Pentium processor<br />

running at 200 MHz, this counter increments at 200 MHz as well, so it can give you<br />

timing samples right down to 5 nanoseconds. This is particularly useful if you want to<br />

figure out exactly how long a piece of code takes to execute (assuming of course, that<br />

you don’t get preempted). You’d call ClockCycles() before your code and after your<br />

code, and then compute the delta. See the <strong>Neutrino</strong> Library Reference for more<br />

details.<br />

Note that on an SMP system, you may run into a little problem. If your thread gets a<br />

ClockCycles() value from one CPU and then eventually runs on another CPU, you<br />

may get inconsistent results. This stems from the fact that the counters used by<br />

ClockCycles() are stored in the CPU chips themselves, and are not synchronized<br />

between CPUs. The solution to this is to use thread affinity to force the thread to run<br />

on a particular CPU.<br />

Advanced topics<br />

Other clock sources<br />

Now that we’ve seen the basics of timers, we’ll look at a few advanced topics:<br />

1 the CLOCK_SOFTTIME and CLOCK_MONOTONIC timer types, and<br />

2 kernel timeouts<br />

We’ve seen the clock source CLOCK_REALTIME, and mentioned that a POSIX<br />

conforming implementation may supply as many different clock sources as it feels<br />

like, provided that it at least provides CLOCK_REALTIME.<br />

What is a clock source? Simply put, it’s an abstract source of timing information. If<br />

you want to put it into real life concepts, your personal watch is a clock source; it<br />

measures how fast time goes by. Your watch will have a different level of accuracy<br />

April 30, 2009 Chapter 3 • Clocks, Timers, and <strong>Getting</strong> a Kick Every So Often 159

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!