简体   繁体   English

gettimeofday()不使用vDSO?

[英]gettimeofday() not using vDSO?

I strace'd a java process that was triggering a lot of kernel time to see what syscalls were being used, and was surprised to see that gettimeofday() and clock_gettime() dominated (I suspect it's due to logging), which is strange considering that man vdso states: 我引用了一个触发大量内核时间的java进程来查看正在使用的系统调用,并且惊讶地看到gettimeofday()clock_gettime()占主导地位(我怀疑它是由于日志记录),这很奇怪考虑该man vdso说:

When tracing systems calls with strace (1), symbols (system calls) that are exported by the vDSO will not appear in the trace output. 使用strace (1)跟踪系统调用时,vDSO导出的符号(系统调用)不会出现在跟踪输出中。

How come these system calls are happening? 为什么这些系统调用正在发生? Is there a way to avoid them? 有没有办法避免它们?

The machine is running Ubuntu 16.04.1 on EC2. 该机器在EC2上运行Ubuntu 16.04.1。

To make things easier, I created a minimal test program in C ( testgtod.c ): 为了testgtod.c ,我在C( testgtod.c )中创建了一个最小的测试程序:

#include <stdlib.h>
#include <sys/time.h>

void main(void)
{
    struct timeval tv;
    for(int i = 0; i < 1000; i++) {
        /* glibc wrapped, shouldn't actually syscall */
        gettimeofday(&tv, NULL);
    }
}

I then compiled and ran the program under strace: gcc testgtod.c -o testgtod && sudo strace ./testgtod 然后我编译并运行strace下的程序: gcc testgtod.c -o testgtod && sudo strace ./testgtod

The output included a thousand calls to gettimeofday(), despite my expectation. 尽管我的期望,输出包括对gettimeofday()的一千次调用。

Things I tested to make sure I'm not seeing things: 我测试的东西,以确保我没有看到的东西:

  1. Made sure the binary is a 64-bit elf using file 确保二进制文件是使用file的64位精灵

  2. ldd ./testgtod to make sure vDSO is active: ldd ./testgtod以确保vDSO处于活动状态:

    linux-vdso.so.1 => (0x00007ffcee25d000) libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f6f6e161000) /lib64/ld-linux-x86-64.so.2 (0x0000559ed71f3000) linux-vdso.so.1 =>(0x00007ffcee25d000)libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6(0x00007f6f6e161000)/lib64/ld-linux-x86-64.so.2 (0x0000559ed71f3000)

  3. getauxval(AT_SYSINFO_EHDR) != NULL

  4. Replaced gettimeofday(&tv, NULL) calls with syscall(SYS_gettimeofday, &tv, NULL) , increased number of calls to 10 million, ran under time - runtime behavior was the same in both cases: ./testgtod 0.16s user 0.83s system 99% cpu 0.998 total . syscall(SYS_gettimeofday, &tv, NULL)替换gettimeofday(&tv, NULL) syscall(SYS_gettimeofday, &tv, NULL) ,调用次数增加到1000万,运行time - 两种情况下运行时行为相同: ./testgtod 0.16s user 0.83s system 99% cpu 0.998 total

The issue is related to this being a VM running on Xen, specifically, the Xen clocksource does not yet allow for vDSO access to the clock: 该问题与在Xen上运行的VM有关,具体而言,Xen clocksource尚不允许vDSO访问时钟:

ubuntu@machine:~% cat /sys/devices/system/clocksource/*/current_clocksource
xen

Then, I changed the clocksource to tsc : 然后,我将clocksource更改为tsc

ubuntu@machine:~% sudo sh -c "echo tsc >/sys/devices/system/clocksource/clocksource0/current_clocksource"

NOTE: it isn't recommended to move to the tsc clocksource on production machines since it may cause backwards drift for the clock. 注意:建议不要在生产机器上移动到tsc clocksource,因为它可能会导致时钟向后漂移。

See https://blog.packagecloud.io/eng/2017/03/08/system-calls-are-much-slower-on-ec2/ for a detailed write-up on the interaction between vDSO and the clocksource. 有关vDSO与clocksource之间交互的详细说明,请参阅https://blog.packagecloud.io/eng/2017/03/08/system-calls-are-much-slower-on-ec2/

NOTE 2 : it seems tsc support in Xen has improved with version 4.0 and with improved CPU support in Sandy Bridge+ platforms. 注2 :似乎Xen中的tsc支持已经在4.0版本中得到了改进,并且在Sandy Bridge +平台中具有改进的CPU支持。 Modern EC2 machines should be okay with tsc . 现代EC2机器应该可以用tsc Check Xen version using dmesg | grep "Xen version" 使用dmesg | grep "Xen version"检查Xen版本 dmesg | grep "Xen version" . dmesg | grep "Xen version" Amazon recommended the tsc clocksource already in re:Invent 2015 ( https://www.slideshare.net/AmazonWebServices/cmp402-amazon-ec2-instances-deep-dive ). 亚马逊在re:Invent 2015( https://www.slideshare.net/AmazonWebServices/cmp402-amazon-ec2-instances-deep-dive )中推荐了tsc clocksource。 I'm not yet running to production with this, but the situation doesn't seem as bad as implied by packagecloud. 我还没有投入生产,但情况似乎并不像packagecloud所暗示的那样糟糕。

Additional reading: 补充阅读:
Why rdtsc interacts poorly with VMs 为什么rdtsc与VM交互不良
Xen's 4.0 rdtsc changes Xen的4.0 rdtsc更改
Linux kernel timekeeping documentation, discussing the pitfalls of the TSC Linux内核计时文档,讨论了TSC的缺陷

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM