0

I have been asked a question but I am not sure if I answered it correctly.

"Is it possible to rely only on software timer?"

My answer was "yes, in theory".

But then I added:

"Just relying on hardware timer at the kernel loading (rtc) and then software only is a mess to manage since we must be able to know how many cpu cycles each instruction took + eventual cache miss + branching cost + memory speed and put a counter after each one or group (good luck with out-of-order cpu).

And do the calculation to derivate the current cpu cycle. That is insane.

Not talking about the overall performance drop.

The best we could have is a brittle approximation of the time which become more wrong over time. Even possibly on short laps."

But even if it seems logical to me, did my thinking go wrong?

Thanks

3
  • This isn't clear. What are you trying to time? What is "software hardware"? Jul 26, 2015 at 6:34
  • You probably mean "software timer", not "software hardware" Jul 26, 2015 at 6:36
  • Definitely, what a stupid typo! sorry
    – Kroma
    Jul 26, 2015 at 6:37

1 Answer 1

1

On current processors and hardware (e.g. Intel or AMD or ARM in laptops or desktops or tablets) with common operating systems (Linux, Windows, FreeBSD, MacOSX, Android, iOS, ...) processes are scheduled at random times. So cache behavior is non deterministic. Hence, instruction timing is non reproducible. You need some hardware time measurement.

A typical desktop or laptop gets hundreds, or thousands, of interrupts every second, most of them time related. Try running cat /proc/interrupts on a Linux machine twice, with a few seconds between the runs.

I guess that even with a single-tasked MS-DOS like operating system, you'll still get random behavior (e.g. induced by ACPI, or SMM). On some laptops, the processor frequency can be throttled by its temperature, which depends upon the CPU load and the external temperature...

In practice you really want to use some timer provided by the operating system. For Linux, read time(7)

So you practically cannot rely on a purely software timer. However, the processor has internal timers.... Even in principle, you cannot avoid timers on current processors ....

You might be able, if you can put your hardware in a very controlled environment (thermostatically) to run a very limited software (an OS-like free standing thing) sitting entirely in the processor cache and perhaps then get some determinism, but in practice current laptop or desktop (or tablet) hardware is non-deterministic and you cannot predict the time needed for a given small machine routine.

Timers are extremely useful in interesting (non-trivial) software, see e.g. J.Pitrat CAIA, a sleeping beauty blog entry for an interesting point. Also look at the many uses of watchdog timers in software (e.g. in the Parma Polyhedra Library) Read also about Worst Case Execution Time (WCET).

So I would say that even in theory it is not possible to rely upon a purely software timer (unless of course that software uses the processor timers, which are hardware circuits). In the previous century (up to 1980s or 1990s) hardware was much more deterministic, and the amount of clock cycles or microsecond needed for each machine instruction was documented (but some instructions, e.g. division, needed a variable amount of time, depending on the actual data!).

6
  • Thanks Basile, so you agree that there is no way to derivate in software the current timestamp from the last hardware timestamp. I accept your answer in.. 5 minutes from now
    – Kroma
    Jul 26, 2015 at 6:39
  • In practice, no... Perhaps you could setup things to make it doable, but I don't see the interest... (you'll run one useless loop on your PC sitting in cache, without doing any IO, not even screen display) Jul 26, 2015 at 6:51
  • Sorry, I am a father of two and had to take care of them :)
    – Kroma
    Jul 26, 2015 at 7:20
  • No problem... I've got 4 kids (the oldest is 36 and the youngest is 17) and 6 grand children... Jul 26, 2015 at 7:21
  • BTW, that is a very detailed answer. Some interesting reading ahead :)
    – Kroma
    Jul 26, 2015 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.