DPDK精准测量时间
生活随笔
收集整理的這篇文章主要介紹了
DPDK精准测量时间
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
DPDK精準測量時間
- DPDK實現方式
- 實際操作
- 參考鏈接
DPDK實現方式
簡單來說就是通過rdtsc指令來獲取CPU啟動起來的tick值,進行減法,然后結合頻率來得到時間差。
對應到spdk里面的話就是spdk_get_ticks和spdk_get_ticks_hz. spdk_get_ticks最終會調用到rte_rdtsc,其實現如下:
從以上代碼可以看出,其實際就是調用了rdtsc指令。
spdk_get_ticks_hz最終會調用到get_tsc_freq_arch,其實現如下:
//dpdk/lib/librte_eal/common/arch/x86/rte_cycles.cuint64_t get_tsc_freq_arch(void) {uint64_t tsc_hz = 0;uint32_t a, b, c, d, maxleaf;uint8_t mult, model;int32_t ret;/** Time Stamp Counter and Nominal Core Crystal Clock* Information Leaf*/maxleaf = __get_cpuid_max(0, NULL);if (maxleaf >= 0x15) {__cpuid(0x15, a, b, c, d);/* EBX : TSC/Crystal ratio, ECX : Crystal Hz */if (b && c)return c * (b / a);}__cpuid(0x1, a, b, c, d);model = rte_cpu_get_model(a);if (check_model_wsm_nhm(model))mult = 133;else if ((c & bit_AVX) || check_model_gdm_dnv(model))mult = 100;elsereturn 0;ret = rdmsr(0xCE, &tsc_hz);if (ret < 0)return 0;return ((tsc_hz >> 8) & 0xff) * mult * 1E6; } static int32_t rdmsr(int msr, uint64_t *val) { #ifdef RTE_EXEC_ENV_LINUXAPPint fd;int ret;fd = open("/dev/cpu/0/msr", O_RDONLY);if (fd < 0)return fd;ret = pread(fd, val, sizeof(uint64_t), msr);close(fd);return ret; #elseRTE_SET_USED(msr);RTE_SET_USED(val);return -1; #endif }實際操作
從以上代碼可以看出,頻率的獲取是通過讀取/dev/cpu/0/msr的0xCE偏移獲取的,以下是對當前機器進行的實驗。
$ hexdump -C /dev/cpu/0/msr -s 206 -n 8 000000ce 00 17 81 f3 2c 0a 07 00 |....,...| 000000d6 $ python Python 2.7.5 (default, Jul 13 2018, 13:06:57) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> mult=100 >>> tsc_hz=0x70a2cf3811700 >>> ((tsc_hz >> 8) & 0xff) * mult * 1E6; 2300000000.0 >>>參考鏈接
關于TSC 請參考以下文章。
值得注意的是以下兩點:
With these processors, the TSC ticks at the processor’s nominal frequency, regardless of the actual CPU clock frequency due to turbo or power saving states. Hence TSC ticks are counting the passage of time, not the number of CPU clock cycles elapsed.
總結
以上是生活随笔為你收集整理的DPDK精准测量时间的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux性能优化--cpu篇
- 下一篇: 谷歌GFS论文笔记