mengrao / tscns Goto Github PK
View Code? Open in Web Editor NEWA low overhead nanosecond clock based on x86 TSC
License: MIT License
A low overhead nanosecond clock based on x86 TSC
License: MIT License
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) ?
注释掉 std::this_thread::sleep_for(std::chrono::seconds(1));
结果完全不一样,我的机器上(gcc 4.5.8, centos 7.4)rdsysns_latency基本在35ns左右,偶尔也会彪到100+
看上去调用频率影响了函数执行时间?
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?
我没理解错的话rdtsc不是应该需要加一个lfence或者mfence吗
Hello,
Will this work on arm64 architectures like RPI?
Best Regards.
C.A.
请教一下,在超频机器上计算出的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
alignas(64) std::atomic<uint32_t> param_seq_ = 0;
这里的初始化语句试图调用被deleted的拷贝构造函数,使代码无法通过编译。
可考虑改为:
alignas(64) std::atomic<uint32_t> param_seq_{0};
思路是利用线程缓存
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;
}
在我的服务器上,无论是程序自己calibrate还是cheat,误差都会在一分钟之内增大到微秒级甚至毫秒级
cpu是双路志强8255C,是支持constant_tsc的
另外在家里的电脑8700K上测试结果也是一样,系统都是Ubuntu 20.04.1 LTS
这里的误差是 rdtsc -> clock_gettime -> rdtsc,然后两次rdtsc结果平均值和clock_gettime结果的差
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.