日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

DPDK精准测量时间

發布時間:2024/2/28 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 DPDK精准测量时间 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

DPDK精準測量時間

    • DPDK實現方式
    • 實際操作
    • 參考鏈接


DPDK實現方式

簡單來說就是通過rdtsc指令來獲取CPU啟動起來的tick值,進行減法,然后結合頻率來得到時間差。
對應到spdk里面的話就是spdk_get_ticks和spdk_get_ticks_hz. spdk_get_ticks最終會調用到rte_rdtsc,其實現如下:

//dpdk/lib/librte_eal/common/include/arch/x86/rte_cycles.h static inline uint64_t rte_rdtsc(void) {union {uint64_t tsc_64;RTE_STD_C11struct {uint32_t lo_32;uint32_t hi_32;};} tsc;#ifdef RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORTif (unlikely(rte_cycles_vmware_tsc_map)) {/* ecx = 0x10000 corresponds to the physical TSC for VMware */asm volatile("rdpmc" :"=a" (tsc.lo_32),"=d" (tsc.hi_32) :"c"(0x10000));return tsc.tsc_64;} #endifasm volatile("rdtsc" :"=a" (tsc.lo_32),"=d" (tsc.hi_32));return tsc.tsc_64; }static inline uint64_t rte_rdtsc_precise(void) {rte_mb();return 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 請參考以下文章。

  • https://en.wikipedia.org/wiki/Time_Stamp_Counter
  • https://www.intel.com/content/dam/www/public/us/en/documents/white-papers/ia-32-ia-64-benchmark-code-execution-paper.pdf
  • 值得注意的是以下兩點:

  • TSC ticks是以CPU名義頻率在跳動,而不是CPU的實際運行頻率,所以CPU在power-saving模式下和高負載時候,TSC的速率不會改變。
  • TSC ticks記錄的是實際時間的流逝,而不是真正CPU時鐘。
  • 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精准测量时间的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。