Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>TSCs (what <em>rdtsc</em> uses) are often not synchronized on multi-processor systems. It may help to set the CPU affinity in order to bind the process to a single CPU.</p> <p>You could also get timestamps from <a href="http://en.wikipedia.org/wiki/HPET" rel="nofollow noreferrer">HPET timers</a> if available, which aren't prone to the same problem.</p> <p>As for repeatability, those variances are true. You could disable caching, give a realtime priority to the process and/or (if on Linux or something similar) recompile your kernel with a lower, fixed timer interrupt frequency (the one that does time-slicing). You can't eliminate the variance completely, at least not easily and not on regular CPU + OS combos.</p> <p>In general, for easy coding, reliability and portability reasons, I suggest you use what the OS has to offer. If it offers high-precision timers, use the appropriate OS helper.</p> <p>(Just in case you're trying a time attack on a crypto system, well, you're going to have to live with 1. this randomness and 2. general defenses that make the system unpredictable for good reasons, so the function might not be deterministic with respect to time.)</p> <p>EDIT: added paragraph about timers the OS can offer.</p> <p>EDIT: This refers to Linux. For binding a process to a single CPU (to have an accurate read from RDTSC), you can use <a href="http://linux.die.net/man/2/sched_setaffinity" rel="nofollow noreferrer">sched_setaffinity(2)</a>. And <a href="http://repo.or.cz/w/kmemtrace-user.git?a=blob;f=kmemtraced.c;h=217478ddca954a9d3fc4c4c99bd9f3c93e498bb1;hb=0d00b11cc4badc1da3313fd84335efedde27ad83#l73" rel="nofollow noreferrer">here</a>'s some code from one of my projects using it for some other purpose (mapping threads to CPUs). This should be your first try. As for HPETs, you can use regular POSIX calls like <a href="http://www.tin.org/bin/man.cgi?section=3&amp;topic=clock_gettime" rel="nofollow noreferrer">these</a>, as long as the kernel and the machine support these timers.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload