Skip to content

Instantly share code, notes, and snippets.

@nasacj
Created March 11, 2019 07:20
Show Gist options
  • Save nasacj/5656dcc80a24b5db17af0e4b35d5d83c to your computer and use it in GitHub Desktop.
Save nasacj/5656dcc80a24b5db17af0e4b35d5d83c to your computer and use it in GitHub Desktop.
#include <cstdint>
#include <iostream>
//#include <linux/preempt.h>
__attribute__((always_inline)) uint64_t perf_counter(void)
{
uint32_t lo, hi;
// take time stamp counter, rdtscp does serialize by itself, and is much cheaper than using CPUID
__asm__ __volatile__ (
"rdtscp" : "=a"(lo), "=d"(hi)
);
return ((uint64_t)lo) | (((uint64_t)hi) << 32);
}
__attribute__((always_inline)) uint64_t tacc_rdtscp(int *chip, int *core)
{
unsigned long int x;
unsigned a, d, c;
__asm__ volatile("rdtscp" : "=a" (a), "=d" (d), "=c" (c));
*chip = (c & 0xFFF000)>>12;
*core = c & 0xFFF;
return ((uint64_t)a) | (((uint64_t)d) << 32);;
}
int main()
{
//preempt_disable();
uint64_t t1 = perf_counter();
uint64_t t2 = perf_counter();
//preempt_enable();
std::cout << t1 << " - " << t2 << ", diff = " << t2 - t1 << std::endl;
int chip = 0;
int core = 0;
uint64_t t3 = tacc_rdtscp(&chip, &core);
std::cout << "chip = " << chip << ", core = " << core << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment