0

i am not sure how a function that will read the Intelx86 Time stamp counter would look like. i'm trying to measure the performance of a single function so I assume that I would need 2 functions:

1 - void startFunctionCounter(int *cycleval)
2 - void endFunctionCounter(int *cycleval)

I referred to this SO question Using Time stamp counter to get the time stamp and __rdtsc() is mentioned. However I am not very familiar with asm and am not sure how 2 functions should look like. How should I adapt the code from the post I have linked to make it what I need?

Could someone tell me how this should be done? Appreciate any help. Thank you in advance! :)

edit: from reading the article linked, I have done something like this but I am getting a error message when compiling:

#include <stdio.h>
#include <stdint.h>
#include <intrin.h>
#include <x86intrin.h>


inline uint64_t readTSC() {
    uint64_t tsc = __rdtsc();
    return tsc;
}



void main(){
    double initTSC = readTSC(), finalTSC= 0;
    double array[5] = {5,6,4.6,1};
    double x=6.0;
    long deg = 3;
    double res = calcFun(array,x,deg);
    
    printf("hello there %f \n", res);
    finalTSC = readTSC();
    printf("hello timings \n 1: %f \n 2: %f", initTSC, finalTSC);

    
}

The error is :

/usr/lib/gcc/x86_64-pc-cygwin/10/../../../../x86_64-pc-cygwin/bin/ld: /tmp/ccqnytar.o:opt_poly.c:(.text+0x3f6): undefined reference to `readTSC'

I am using GCC compiler and C language.

Megan Darcy
  • 530
  • 5
  • 15
  • You don't need asm. [How to get the CPU cycle count in x86\_64 from C++?](https://stackoverflow.com/q/13772567) shows which header to include for `__rdtsc()`. You'll also want `_mm_lfence()` before rdtsc at the bottom of the timed region, so RDTSC can't execute early. Also, you can just return a `uint64_t` value; no need for a pointer. – Peter Cordes Jun 21 '21 at 13:54
  • @PeterCordes thanks! I tried to use the function but seems like I am facing an error with compilation. :( – Megan Darcy Jun 21 '21 at 14:37
  • 3
    You're compiling as C, not C++, so `inline uint64_t readTSC` needs a matching `extern inline` in exactly one `.c` file. With optimization disabled, gcc can choose not to inline it, and emit a call to the asm definition. But `inline ...() {}` doesn't emit a definition, so it can be used in a header file without forcing the linker to discard duplicates. (And BTW, GCC 9 or 10 changed how this works. You used to be able to get away with this even with optimization disabled.) Anyway, use `static` instead of `inline`. – Peter Cordes Jun 21 '21 at 14:41
  • 2
    [Is "inline" without "static" or "extern" ever useful in C99?](https://stackoverflow.com/q/6312597) / [What's the difference between "static" and "static inline" function?](https://stackoverflow.com/a/7767858) / [What does extern inline do?](https://stackoverflow.com/q/216510). This is of course totally unrelated to what's in the body of your `readTSC` function. – Peter Cordes Jun 21 '21 at 14:43
  • @PeterCordes perfect explanation and resources :) thanks – Megan Darcy Jun 21 '21 at 16:03

0 Answers0