RDTSC 是 x86 中最为轻量级的计时方案,虽然它不甚精确坑很多,但特定场景下依然好用。
海光的 lscpu flags 中支持 RDTSC,本文通过一个简单 benchmark 来看海光的 RDTSC 实现效率(还不错)。
// $cat test.cpp
#ifndef _X86_64_RDTSC_H_
#define _X86_64_RDTSC_H_
#include
static inline uint64_t rdtsc()
{
uint64_t rax,rdx;
asm volatile ( "rdtsc" : "=a" (rax), "=d" (rdx) :: "%rcx" );
return ((uint64_t) rax) | (((uint64_t) rdx) << 32);
}
static inline uint64_t rdtscp()
{
uint64_t rax,rdx;
// rdtscp will record cpuid in rcx register, record it in modify domain if not need to avoid misuse of rcs by compiler.
asm volatile ( "rdtscp" : "=a" (rax), "=d" (rdx) :: "%rcx" );
return ((uint64_t) rax) | (((uint64_t) rdx) << 32);
}
static inline uint64_t rdtscp_id(uint64_t &cpuid)
{
uint64_t rax, rdx, rcx;
asm volatile ( "rdtscp" : "=a" (rax), "=d" (rdx), "=c" (rcx) : : );
cpuid = rcx;
return ((uint64_t) rax) | (((uint64_t) rdx) << 32);
}
#endif
#include
#include
#include
using namespace std;
std::vector<int64_t> my_map;
int64_t g_found = 0;
inline void init()
{
for (int64_t i = 0; i < 1000; ++i) {
my_map.push_back(i);
}
}
inline void do_something()
{
int size = my_map.size();
int64_t target = random() % size;
for (int64_t i = 0; i < size; ++i) {
if (my_map[i] == target) {
g_found++;
break;
}
}
}
int main(int argc, const char *argv[])
{
init();
int64_t loops = 10000000;
int64_t begin = 0;
int64_t total = 0;
int64_t work_begin = 0;
int64_t work_total = 0;
begin = rdtsc();
for (int64_t i = 0; i < loops; ++i) {
work_begin = rdtsc();
do_something();
work_total += rdtsc() - work_begin;
}
total = rdtsc() - begin;
printf("g_found=%ld, total cycles = %ld, do_something cycles = %ld, delta = %ld, work_percent=%.2f\n",
g_found, total, work_total, total - work_total, work_total * 100.0 / total);
return 0;
}
main 函数对应的主要汇编代码(编译器对指令稍微做了一点重排):
2: 41 55 push %r13
4: 41 54 push %r12
6: 55 push %rbp
7: 53 push %rbx
8: e8 00 00 00 00 callq d
d: 0f 31 rdtsc
f: 48 c1 e2 20 shl $0x20,%rdx
13: bb 80 96 98 00 mov $0x989680,%ebx
18: 31 ed xor %ebp,%ebp
1a: 49 89 d4 mov %rdx,%r12
1d: 49 09 c4 or %rax,%r12
20: 0f 31 rdtsc
22: 4c 8b 2d 00 00 00 00 mov 0x0(%rip),%r13 # 29
29: 4c 2b 2d 00 00 00 00 sub 0x0(%rip),%r13 # 30
30: 48 c1 e2 20 shl $0x20,%rdx
34: 49 89 d6 mov %rdx,%r14
37: 49 09 c6 or %rax,%r14
3a: e8 00 00 00 00 callq 3f
3f: 49 c1 fd 03 sar $0x3,%r13
43: 48 99 cqto
45: 49 63 fd movslq %r13d,%rdi
48: 48 f7 ff idiv %rdi
4b: 48 85 ff test %rdi,%rdi
4e: 7e 1f jle 6f
50: 48 8b 05 00 00 00 00 mov 0x0(%rip),%rax # 57
57: 31 f6 xor %esi,%esi
59: 48 3b 10 cmp (%rax),%rdx
5c: 75 08 jne 66
5e: eb 70 jmp d0
60: 48 3b 14 f0 cmp (%rax,%rsi,8),%rdx
64: 74 6a je d0
66: 48 83 c6 01 add $0x1,%rsi
6a: 48 39 f7 cmp %rsi,%rdi
6d: 7f f1 jg 60
6f: 0f 31 rdtsc
71: 48 c1 e2 20 shl $0x20,%rdx
75: 4c 29 f5 sub %r14,%rbp
78: 48 09 c2 or %rax,%rdx
7b: 48 01 d5 add %rdx,%rbp
7e: 48 83 eb 01 sub $0x1,%rbx
82: 75 9c jne 20
84: 0f 31 rdtsc
86: f2 48 0f 2a c5 cvtsi2sd %rbp,%xmm0
8b: 48 c1 e2 20 shl $0x20,%rdx
8f: 48 09 c2 or %rax,%rdx
92: 48 8b 35 00 00 00 00 mov 0x0(%rip),%rsi # 99
99: 48 89 e9 mov %rbp,%rcx
9c: 4c 29 e2 sub %r12,%rdx
9f: bf 00 00 00 00 mov $0x0,%edi
a4: b8 01 00 00 00 mov $0x1,%eax
a9: f2 48 0f 2a ca cvtsi2sd %rdx,%xmm1
ae: 49 89 d0 mov %rdx,%r8
b1: 49 29 e8 sub %rbp,%r8
b4: f2 0f 59 05 00 00 00 mulsd 0x0(%rip),%xmm0 # bc
海光
$lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 128
On-line CPU(s) list: 0-127
Thread(s) per core: 2
Core(s) per socket: 32
Socket(s): 2
NUMA node(s): 8
Vendor ID: HygonGenuine
CPU family: 24
Model: 1
Model name: Hygon C86 7285 32-core Processor
Stepping: 1
CPU MHz: 2480.918
CPU max MHz: 2000.0000
CPU min MHz: 1200.0000
BogoMIPS: 3999.88
Virtualization: AMD-V
L1d cache: 32K
L1i cache: 64K
L2 cache: 512K
L3 cache: 8192K
NUMA node0 CPU(s): 0-7,64-71
NUMA node1 CPU(s): 8-15,72-79
NUMA node2 CPU(s): 16-23,80-87
NUMA node3 CPU(s): 24-31,88-95
NUMA node4 CPU(s): 32-39,96-103
NUMA node5 CPU(s): 40-47,104-111
NUMA node6 CPU(s): 48-55,112-119
NUMA node7 CPU(s): 56-63,120-127
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc cpuid extd_apicid amd_dcm aperfmperf pni pclmulqdq monitor ssse3 fma cx16 sse4_1 sse4_2 movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb hw_pstate sme ssbd sev ibpb vmmcall fsgsbase bmi1 avx2 smep bmi2 rdseed adx smap clflushopt sha_ni xsaveopt xsavec xgetbv1 xsaves clzero irperf xsaveerptr arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif overflow_recov succor smca
Intel
[xiaochu.yh ~/tools/rdtsc] $lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 96
On-line CPU(s) list: 5,8,18,19,53,56,66,67
Off-line CPU(s) list: 0-4,6,7,9-17,20-52,54,55,57-65,68-95
Thread(s) per core: 0
Core(s) per socket: 24
Socket(s): 2
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 85
Model name: Intel(R) Xeon(R) Platinum 8163 CPU @ 2.50GHz
Stepping: 4
CPU MHz: 2700.000
CPU max MHz: 3100.0000
CPU min MHz: 1000.0000
BogoMIPS: 4998.91
Virtualization: VT-x
L1d cache: 32K
L1i cache: 32K
L2 cache: 1024K
L3 cache: 33792K
NUMA node0 CPU(s): 0-95
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch ida arat epb invpcid_single pln pts dtherm spec_ctrl ibpb_support tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm mpx rdt avx512f avx512dq rdseed adx smap clflushopt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local cat_l3 mba
g++ -O2 -g test.cpp -o benchmark
海光
$time ./benchmark
g_found=10000000, total cycles = 5179339120, do_something cycles = 4881739940,
delta = 297599180, work_percent=94.25
real 0m2.591s
user 0m2.590s
sys 0m0.001s
Intel
$time ./benchmark
g_found=10000000, total cycles = 6082650118, do_something cycles = 5850051748,
delta = 232598370, work_percent=96.18
real 0m2.435s
user 0m2.437s
sys 0m0.001s