Fast cross platform timer?

I am looking to improve garbage collector D by adding some heuristics to avoid garbage collection failures that are unlikely to result in significant release. One heuristic that I would like to add is that the GC should not start more than once at X times (maybe once per second or so). To do this, I need a timer with the following properties:

  • He should be able to capture the right time with minimal overhead. Calling core.stdc.time takes some time, roughly equivalent to a small memory allocation, so it is not a good option.

  • Ideally, it should be cross-platform (both OS and CPU), for ease of maintenance.

  • Super high resolution is not very important. If the times are accurate, maybe 1/4 of a second, that's good enough.

  • Should work in a multi-threaded / multi-processor context. The x86 rdtsc instruction rdtsc not work.

EDIT: The plain old C clock() function seems fast enough. However, overflow is a problem here. On 32-bit Windows and Linux, clock_t is defined as a 32-bit signed integer. When it overflows, does it become negative, or does the clock() function use additional logic to make it turn to zero? If it turns to zero, then this will do the trick. If it is wrapped in negative (which also represents error codes, etc.), then it will not work.

Edit # 2: I tried using heuristics using clock() and ignoring the overflow problem as a test. It works so badly that it’s not worth further study.

+6
garbage-collection time timer d
source share
2 answers

Ideally, it should be cross-platform (both OS and CPU), for ease of maintenance.

I think this reduces you to what you can find in the C standard library.

Maybe clock ?

+3
source share

Well, my first suggestion would be to use core.time or std.datetime. It has a StopWatch . Isn't that fast enough? He uses any standard monotonous clock for the system, and I hope that it will be fast enough. This is by far the highest accuracy you are going to get (although I don’t know how high accuracy affects speed). However, if it's not fast enough, I'm not sure if your other options are better. As a rule, you get either the second accuracy or high accuracy. There is not much between them. And with high accuracy, as a rule, at least microsecond accuracy, and the question is how much superior it is. 1/4 second accuracy is not entirely normal.

The Cybershadow clock proposal can do the trick - of course, this is your only option, which is standard C as far as I know - but it may or may not be fast enough.

Besides clock ... On Linux, if you want a non-monotonous way to get time with higher accuracy than one second, I find gettimeofday to be your only real option, but I don't know how fast it is. On Windows, the best non-monotonous solution I know of is getSystemTimeAsFiletime , but Windows has a few time functions that you could play with.

You can look at std.datetime Clock.currStdTime to find out how to make any of them (although it uses clock_gettime in Posix, if available, and this is what core.time uses - albeit with different clocks - so you probably , you want the else part of this function for Linux, if StopWatch too slow for you). However, it will convert to hnsecs from midnight January 1, 1 AD, so depending on what you do with the number, you might want to skip this part of the calculation.

+3
source share

All Articles