Skip to content

Instantly share code, notes, and snippets.

@borman
Created January 19, 2014 21:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save borman/8511511 to your computer and use it in GitHub Desktop.
Save borman/8511511 to your computer and use it in GitHub Desktop.
Code snippet for ad-hoc profiling with RDTSC/RDTSCP.
struct tsc {
uint32_t cycles_low, cycles_high;
uint64_t cycles;
tsc() : cycles(0) {}
void begin() {
asm volatile (
"CPUID\n\t"/*serialize*/
"RDTSC\n\t"/*read the clock*/
"mov %%edx, %0\n\t"
"mov %%eax, %1\n\t"
: "=r" (cycles_high), "=r" (cycles_low)
:: "%rax", "%rbx", "%rcx", "%rdx");
}
void end() {
uint32_t cycles_high1, cycles_low1;
asm volatile (
"RDTSCP\n\t"/*read the clock*/
"mov %%edx, %0\n\t"
"mov %%eax, %1\n\t"
"CPUID\n\t"
: "=r" (cycles_high1), "=r" (cycles_low1)
:: "%rax", "%rbx", "%rcx", "%rdx");
cycles += ((uint64_t(cycles_high1) << 32) + uint64_t(cycles_low1))
- ((uint64_t(cycles_high) << 32) + uint64_t(cycles_low));
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment