6 ms·
Measuring Latency in Linux (2014)
- snvzz 7y agocyclictest, from rt-tests. That's the go-to.
- krenel 7y agoOP: Related stuff How Golang[1] implements monotonic clocks — basically it retrieves always both wall and monotonic clocks on "time.Now()" and when doing operations of subtraction it uses the monotonic. When printing, it uses the wall time. Pretty neat. Details on the proposal by Russ Cox [2]. [1] https://golang.org/pkg/time/#hdr-Monotonic_Clocks https://golang.org/pkg/time/#hdr-Monotonic_Clocks [2] https://go.googlesource.com/proposal/+/master/design/12914-monotonic.md https://go.googlesource.com/proposal/+/master/design/12914-m...
- krenel 7y agoIn Python: import time time.get_clock_info(name) where name can be one of those: 'monotonic': time.monotonic() 'perf_counter': time.perf_counter() 'process_time': time.process_time() 'thread_time': time.thread_time() 'time': time.time()
- vlovich123 7y agoHow does scheduling a timer to go off at 3pm this Saturday work?
- krenel 7y agoWell, the parameter of a Timer is of type Duration: A Duration represents the elapsed time between two instants as an int64 nanosecond count. The representation limits the largest representable duration to approximately 290 years. Which is not really good because we have to calculate the time between now() and Saturday. If "wall time" changes, the scheduler will not be triggered when expected. In that case I would not rely on Timer to use it in a cron-like fashion, or trigger a Ticker every second, check the _current_ wall time, and decide if anything needs to be done. You can read more about Timers, Tickers and Sleeps in this (pretty interesting) article[1] [1] https://blog.gopheracademy.com/advent-2016/go-timers/ https://blog.gopheracademy.com/advent-2016/go-timers/
- vlovich123 7y agoOperating systems spend a lot of time designing APIs for you to deal with time correctly. The approach you've described is a limitation in Go, not inherent in general software development. http://man7.org/linux/man-pages/man2/clock_nanosleep.2.html http://man7.org/linux/man-pages/man2/clock_nanosleep.2.html https://developer.apple.com/documentation/dispatch/1420517-dispatch_walltime https://developer.apple.com/documentation/dispatch/1420517-d... OS timers when given a wall-clock expiry will do the right thing when the system wall-clock jumps.
- coder543 7y agoThat’s a hard question regardless of programming language. One commonly referenced YouTube video about time that explains why this is hard: https://youtu.be/-5wpm-gesOY https://youtu.be/-5wpm-gesOY A reasonable first approximation of a solution would be to just check every second (or minute, or hour, depending on requirements) whether the current system time is later than the scheduled time for any pending events. Then you probably want to make sure events are marked as completed so they don’t fire again if the clock moves backwards. Trying to predict how many seconds to sleep between now and 3pm on Saturday is a difficult task, but you can probably use a time library to do that if it’s important enough... but what happens when the government suddenly declares a sudden change to the time zone offset between now and then? The predictive solution would wake up at the wrong time.
- birdyrooster 7y agoPlease put the year in the title (2015)
- jstarks 7y agoThe URL implies it's from 2014.
- birdyrooster 7y agoThank you for the correction.
- cataphract 7y agoI don't think his comment about CLOCK_MONOTONIC_RAW being slow to query applies anymore. It used to be slow because it was not implemented in the vDSO library and so it included the overhead of a syscall. But there was a big vDSO refactoring that landed on 5.3 that I think fixed this problem. Edit: found the patchset. In includes benchmarks for several architectures as well: https://lore.kernel.org/linux-arm-kernel/20190621095252.32307-1-vincenzo.frascino@arm.com/ https://lore.kernel.org/linux-arm-kernel/20190621095252.3230...
- thedance 7y agoIt's a fun fact that on cloud VMs (AWS, etc) vDSO gettime doesn't exist, so if you rely on vDSO to make time measurement free, it's not.
- jstarks 7y agoMaybe this is true for AWS VMs that use Xen. I believe that Linux VMs on Azure do not have this problem, since they use the Hyper-V reference time page, which can be queried from the vDSO.
- robbjuly 7y agoI believe newer generation AWS VMs, like C5, use kvm clock source now and not xen. On the older ones switching to tsc speeds up things.
- xakahnx 7y agoAny idea why this is? I'm even more curious since you've singled out "cloud VMs" from all VMs.
- thedance 7y agoMy understanding is one of the reasons that the virtual time stuff in clouds doesn't work in a straightforward way is that your VM can migrate to another host, where reading TSC could give the appearance of discontinuous time, including jumping backwards in time which would be very bad. This is not a problem unless your VMs are migratory, which I guess is something I associate with GCE. And clouds seem to me to have more variety of hardware than I'd expect in my own private infrastructure, and that variety comes with a great many TSC quirks.
- BeeOnRope 7y agoThe thing about needing cpuid isnt true except perhaps on some older AMD hardware. lfence works as a execution barrier and has an explicit cost of only a few cycles. You can accurately time a region with something like: lfence rdtsc lfence // timed region lfence rdtsc This will give you accurate timing with some offset (i.e. even with an empty region you get a result on the order of 25-40 cycles), which you can mostly subtract out. Carefully done you can get results down to a nanosecond or so. rdtscp has few advantages over lfence + rdtsc, and arguably some disadvantages (you can control where the implied fence goes).
- jasonzemos 7y agoSpecifically, the Intel manual makes the following important points, one involving an `mfence;lfence` combo: * If software requires RDTSC to be executed only after all previous instructions have executed and all previous loads are globally visible, it can execute LFENCE immediately before RDTSC. * If software requires RDTSC to be executed only after all previous instructions have executed and all previous loads and stores are globally visible, it can execute the sequence MFENCE;LFENCE immediately before RDTSC. * If software requires RDTSC to be executed prior to execution of any subsequent instruction (including any memory accesses), it can execute the sequence LFENCE immediately after RDTSC. This instruction was introduced by the Pentium processor. rdtscp is usually a bit more disruptive, and cpuid is probably 100 or 1000 times more disruptive.
- ggm 7y agoHow does VM affect this? How does KVM affect this? How does Docker on KVM affect this? How does Hypervisor affect this? Add "... for a given network driver, e2e, measured RTT.."
- deleted 7y ago[deleted]
- deleted 7y ago[deleted]
- angry_octet 7y agoIf you want finer resolution or multi-machine measurements then look at PTP. You need custom hardware but the improvements are significant.