Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2017-10

rdtscp

5 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Nikolay Nikolov
to Wolf
Wolf wrote 10 lines
Hi, How do you measure time with a one cycle resolution? EpicTimer clearly does not cut it, and from what I gather <https://www.intel.com/content/www/us/en/embedded/training/ia-32-ia-64-benchmark-code-execution-paper.html>, rdtsc cannot do it either. You need to have a CPU capable of understanding rdtscp. But then, FPC 3.00 does not want to compile this assembly instruction. So how do you guys manage to measure it?

FPC trunk supports rdtscp. And if you're using a FPC version that doesn't support an instruction, you can always hardcode it with 'db' (make sure you add a comment with the real instruction to keep your code readable), e.g:

db 0fh, 01h, 0f9h  { rdtscp - not supported by FPC 3.0's inline assembler }

Nikolay

Reply by email

Martok
to Wolf: How do you measure time with a one cycle resolution? EpicTimer clearly does not cut it,…

Hi,

Wolf wrote
rdtsc cannot do it either. You need to have a CPU capable of understanding rdtscp. From what I understood, that doesn't give you cycles either, but only the same

timestamp intervals RDTSC returns.
Tangent: On Windows, RDTSC is wrapped by the QueryPerformanceCounter() call. QPC incidentally is complicated enough that it is very likely no out-of-order instructions are pending by the time it gets to actually executing RDTSC, but with less jitter than "abusing" CPUID for serialisation (QPC takes a rather stable 22 cycles). /Tangent

What I do to get cycle-accurate counts in microbenchmarks is first calibrate the timer with a known-cycle-length task, obtain a "timestamps per cylce" from that (depending on core clock, usually between 200 and 1300), and then measure the actual function. Note that depending on how well you can control multitasking, interrupts and power management, you will need thousands to millions of repeats of the function under test to be reasonably free from artefacts. Looks a bit statistical, but it's precise enough to actually see the instruction cache, instruction alignment and branch can also validate Agner Fog's Instruction Timing Tables with it, so it can't be that bad ;-)

Regards, Martok Ceterum censeo b32079 esse sanandam.

Reply by email

Marco van de Voort
to Martok

In our previous episode, Martok said:

Show quoted text 5 lines
Tangent: On Windows, RDTSC is wrapped by the QueryPerformanceCounter() call. QPC incidentally is complicated enough that it is very likely no out-of-order instructions are pending by the time it gets to actually executing RDTSC, but with less jitter than "abusing" CPUID for serialisation (QPC takes a rather stable 22 cycles). /Tangent

Afaik Query* also works reliable on systems with multiple processors. Might be important now that Ryzen is a multiple die solution.

Reply by email

Nikolay Nikolov
Wolf wrote 24 lines
On 23/10/17 02:53, Nikolay Nikolov wrote: > > FPC trunk supports rdtscp. And if you're using a FPC version that > doesn't support an instruction, you can always hardcode it with 'db' > (make sure you add a comment with the real instruction to keep your > code readable), e.g: > > db 0fh, 01h, 0f9h  { rdtscp - not supported by FPC 3.0's inline > assembler } > > Nikolay > db 0fh, 01h, 0f9h is a Delphi instruction, as far as I can make it out. It does not work with FPC 3.00 - at least with the settings Lazarus provides by default running under Linux. What does work is       .byte 0x0F, 0x01, 0xF9    // read the Time-Stamp Counter rdtscp (as op-code format,                                               // requires setting the compiler switch -aas                                               // in Lazarus: Options/Custom Options/All Options )

Yes, that's AT&T asm syntax. FPC supports both AT&T and Intel syntax modes with the {$asmmode} directive:

https://www.freepascal.org/docs-html/prog/progsu3.html

AT&T is the default for historical reasons, but Intel also works fine.

Nikolay

Reply by email