Git Product home page Git Product logo

tscns's Introduction

TSCNS 2.0

What's the problem with clock_gettime/gettimeofday/std::chrono::XXX_clock?

Although current Linux systems are using VDSO to implement clock_gettime/gettimeofday/std::chrono::XXX_clock, they still have a nonnegligible overhead with latency from 20 to 100 ns. The problem is even worse on Windows as the latency is more unstable and could be as high as 1 us, also on Windows, the high resolution clock is at only 100 ns precison.

These problems are not good for time-critical tasks where high precison timestamp is required and latency of getting timestamp itself should be minimized.

How is TSCNS different?

TSCNS uses rdtsc instruction and simple arithmatic operations to implement a thread-safe clock with 1 ns precision, and is much faster and stable in terms of latency in less than 10 ns, comprising latency of rdtsc(4 ~ 7 ns depending on platforms) plus calculations in less than 1 ns.

Also it can be closely synchronized with the system clock, which makes it a good alternative of standard system clocks. However, real-time synchronization requires the clock to be calibrated at a proper interval, but it's a easy and cheap job to do.

Usage

Initialization:

TSCNS tscns;

tscns.init();

Getting nanosecond timestamp in a single step:

int64_t ns = tscns.rdns();

Or just recording a tsc in some time-critical tasks and converting it to ns in jobs that can be delayed:

// in time-critical task
int64_t tsc = tscns.rdtsc();
...
// in logging task
int64_t ns = tscns.tsc2ns(tsc);

Calibration with some interval in the background:

while(running) {
  tscns.calibrate();
  std::this_thread::sleep_for(std::chrono::seconds(1));
}

More about calibration

Actually the init function has two optional parameters: void init(int64_t init_calibrate_ns, int64_t calibrate_interval_ns): the initial calibration wait time and afterwards calibration interval. The initial calibration is used to find a proper tsc frequency to start with, and it's blocking in tscns.init(), so the default wait time is set to a small value: 20 ms. User can choose to wait a longer time for a more precise initial calibration, e.g. 1 second.

calibrate_interval_ns sets the minimum calibration interval to keep tscns synced with system clock, the default value is 3 seconds. Also user need to call calibrate() function to trigger calibration in an interval no larger than calibrate_interval_ns. The calibrate() function is non-blocking and cheap to call but not thread-safe, so user should have only one thread calling it. The calibrations will adjust tsc frequency in the library to trace that of the system clock and keep timestamp divergence in a minimum level. During calibration, rdns() results in other threads is guaranteed to be continuous: there won't be jump in values and especially timestamp won't go backwards. Below picture shows how these routine calibrations suppress timestamp error caused by the initial coarse calibration and system clock speed correction. Also user can choose not to calibrate after initialization: just don't call the calibrate() function and tscns will always go with the initial tsc frequency.

tscns

Differences with TSCNS 1.0

  • TSCNS 2.0 supports routine calibrations in addition to only initial calibration in 1.0, so time drifting awaying from system clock can be radically eliminated. Also tsc_ghz can't be set by the user any more and the cheat method in 1.0 are also obsolete. In 2.0, tsc2ns() added a sequence lock to protect from parameters change caused by calibrations, the added performance cost is less than 0.5 ns.
  • Windows is supported now. We believe Windows applications will benefit much more from TSCNS because of the drawbacks of the system clock we mentioned at the beginning.

tscns's People

Contributors

mengrao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tscns's Issues

精度不高的一个简单解决方案

思路是利用线程缓存

int64_t steady_time_now()
{
    using namespace std::chrono;
    static thread_local int64_t tsc = 0;
    static thread_local int64_t now = 0;

    int64_t new_tsc = __builtin_ia32_rdtsc();
    if (new_tsc - tsc > 1000 * 1000)  // according to the precision
    {
        tsc = new_tsc;
        now = duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count();
    }

    return now;
}

Concurrency safe?

Hi ! I wanted to know if this library was safe to use in a concurrent environment. I have multiple applications running which send messages to each other, so I wanted to know if this library could be used to calculate time taken by each thread to finish a task after receiving some data from another thread. also would it possible to use tscns for calculating startup and shutdown time for the whole application (including all the threads) ?

请教误差范围

在我的服务器上,无论是程序自己calibrate还是cheat,误差都会在一分钟之内增大到微秒级甚至毫秒级
cpu是双路志强8255C,是支持constant_tsc的

另外在家里的电脑8700K上测试结果也是一样,系统都是Ubuntu 20.04.1 LTS

这里的误差是 rdtsc -> clock_gettime -> rdtsc,然后两次rdtsc结果平均值和clock_gettime结果的差

关于sleep影响计时准确性的问题

注释掉 std::this_thread::sleep_for(std::chrono::seconds(1));
结果完全不一样,我的机器上(gcc 4.5.8, centos 7.4)rdsysns_latency基本在35ns左右,偶尔也会彪到100+
看上去调用频率影响了函数执行时间?

do it like the kernel

What do you think of this to overcome NTP issues:

A process which opens a shared memory segment and recalibrates periodically using syncTime and writes the base_ns and base_tsc and ghz to the shared memory segment. A process can then use rdtsc and read the base_ns and base_tsc and GHZ from the shared memory to compute the timestamp. This in fact replicates the kernel VDSO setup but we can use a finer grained ghz value. It has the advantage that we are more closely tracking wall clock so we don't drift over time due to NTP slewing. My problem is the NTP drift as this tscns currently does it and I don't want to recalibrate in my fast path.
Any flaws in this approach?

Need mfence after builtin_ia32_rtscp

According to intel_doc
and rdscp, need to call mfence after builtin_ia32——rtscp incase of cpu reordering.
Using rtsc without cpuid is very bad, the timestamp is almost useless because instruction before and after it can get reordered in cpu.

TSC计时在超频服务器上的问题

请教一下,在超频机器上计算出的CPU主频不对呢,比如CPU主频3.0GHZ,超频到5.0GHZ,但计时器计算得到的CPU当前主频仍然是3.0GHZ,是TSC计时有什么问题么?CPU支持invariant tsc。
[xtrader@XXXX ~]$ dmesg | grep "tsc: Refined TSC clocksource calibration"
[ 1.851706] tsc: Refined TSC clocksource calibration: 3000.001 MHz

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.