0

I'm trying to measure how long it takes to execute a function 'check()' using rdtsc as follows:

a = rdtsc();
check(pw);
b = rdtsc();

return (b-a);

However, I am receiving very small time differences, which I think is due to my compiler (using G++, on windows) optimising the code. As 'check()' does not affect any other part of the program, I think the compiler is ignoring this call altogether.

I have read about using something called asm volatile to tell the compiler not to optimise a certain section of code, but I cannot figure out how to implement it.

Any help on this?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Harry Budd
  • 17
  • 4
  • 3
    If it's taking so little time that you have trouble measuring it, why worry about it at all? Surely you must have bigger (performance) fish to fry..? – Jesper Juhl Jun 06 '17 at 17:00
  • @JesperJuhl Apologies, I should have been less vague. I'm not aiming to optimise performance, I need to analyse the times for different inputs to find which inputs incur a timing leak. – Harry Budd Jun 06 '17 at 17:04
  • Set `-O0` to the compiler? – Yuki Jun 06 '17 at 17:09
  • A timing... leak? (P.S: You could also save yourself the trouble and use a benchmarking framework like [nonius](https://github.com/libnonius/nonius)) – Borgleader Jun 06 '17 at 17:13
  • If you are worried about timing leaks, then why don't you try to make all inputs take constant time regardless of size? Perhaps by inserting artificial delays if an input takes less time than the worst case.. – Jesper Juhl Jun 06 '17 at 17:14
  • @Yuki Thanks, but this didn't work. – Harry Budd Jun 06 '17 at 17:16
  • 2
    @Jesper Juhl My aim is to find timing leaks in cryptographic functions in order to attack them, so I'm not able to re-write the function itself. This is for a university project, so it's a completely hypothetical case, and as such, I want to build my own framework instead of using a pre-made one. – Harry Budd Jun 06 '17 at 17:19
  • By timing leak you mean a leak of information that can be inferred by the runtime of the function, yes? Nevermind, it seems you just answered that. – user4581301 Jun 06 '17 at 17:19
  • @user4581301That's correct. – Harry Budd Jun 06 '17 at 17:20
  • 2
    If you think the call is being optimised out, check the assembler listing for a definitive answer. – Richard Critten Jun 06 '17 at 17:21

2 Answers2

2

Presumably the function calculates and returns some value. Do something with that value, such as add it to a global variable (and eventually print out that variable), so that the compiler cannot easily optimise the function away.

0

1) You need run hundreds millions of iterations for receiving kinda avg. performance

2) DON'T benchmark such low-level things, because it's almost not related to real world. Real task work billions CPU circles and single volatile can add just 0.000001% overhead... or may increase it by 100000%, if yours threads constantly accessing to shared data. You may benchmark part of yours algorithm and then improve it, but not particular instructions.

Green_Wizard
  • 795
  • 5
  • 11