31 void msecsleep(
unsigned int ms) noexcept {
33 XTime t0(XTime::now());
35 unsigned int elapsed_ms = XTime::now().diff_msec(t0);
38 if(ms - elapsed_ms >= 30) {
39 QThread::msleep(ms - elapsed_ms);
42 XTime t1(XTime::now());
43 QThread::yieldCurrentThread();
44 if(XTime::now().diff_msec(t1) > 1)
45 QThread::msleep(std::min(10u, ms - elapsed_ms));
55 XTime t0(XTime::now());
60 req.tv_sec = (int)(t0 - t1);
61 req.tv_nsec = lrint((t0 - t1 - req.tv_sec) * 1e9);
62 if( !nanosleep(&req, NULL))
75 timestamp_t timeStamp() noexcept {
76 #if defined __i386__ || defined __i486__ || defined __i586__ || defined __i686__ || defined __x86_64__
82 asm volatile(
"rdtsc" :
"=A" (r));
87 XTime time(XTime::now());
88 return (time.usec() + time.sec() * 1000000uL);
95 DECLSPEC_KAME timestamp_t timeStampCountsPerMilliSec() noexcept {
96 if( !s_time_stamp_cnt_per_ms) {
98 timestamp_t time_stamp_start = timeStamp();
99 s_time_stamp_calc = time_stamp_start;
100 XTime time_start(XTime::now());
101 timestamp_t time_stamp_start2 = timeStamp();
103 timestamp_t dt = timeStamp() - time_stamp_start2;
104 unsigned int msec = XTime::now().diff_msec(time_start);
105 timestamp_t dt2 = timeStamp() - time_stamp_start;
106 s_time_stamp_cnt_per_ms = (dt+dt2)/2 / msec;
108 if((
double)dt2 / dt < 1.2)
112 return s_time_stamp_cnt_per_ms;
117 XTime::now() noexcept {
119 qint64 x = QDateTime::currentMSecsSinceEpoch();
120 return XTime(x / 1000LL, (x % 1000LL) * 1000l);
123 gettimeofday(&tv, NULL);
124 return XTime(tv.tv_sec, tv.tv_usec);
129 XTime::getTimeStr(
bool subsecond)
const {
133 __time32_t t32 = tv_sec;
134 __time64_t t64 = tv_sec;
135 if(
sizeof(tv_sec) == 4)
136 _ctime32_s(str,
sizeof(str) - 1, &t32);
138 _ctime64_s(str,
sizeof(str) - 1, &t64);
140 ctime_r( &tv_sec, str);
142 str[strlen(str) - 1] =
'\0';
144 sprintf(str + strlen(str),
" +%.3dms", (
int)tv_usec/1000);
152 XTime::getTimeFmtStr(
const char *fmt,
bool subsecond)
const {
156 __time32_t t32 = tv_sec;
157 __time64_t t64 = tv_sec;
158 if(
sizeof(tv_sec) == 4)
159 _localtime32_s( &time, &t32);
161 _localtime64_s( &time, &t64);
163 localtime_r( &tv_sec, &time);
166 strftime(str, 100, fmt, &time);
168 sprintf(str + strlen(str),
" +%.3f", 1e-6 * tv_usec);