50 #include <stdint.h>
51 #include <time.h>
52
53 #elif PLATFORM(EFL)
54 #include <Ecore.h>
55 #else
56 #include <sys/time.h>
57 #endif
58
59 #if USE(GLIB) && !PLATFORM(EFL)
60 #include <glib.h>
61 #endif
62
63 #if PLATFORM(JAVA)
64 #include <WebCore/platform/java/JavaEnv.h>
65 #endif
66
67
68 namespace WTF {
69
70 #if OS(WINDOWS)
71
72 // Number of 100 nanosecond between January 1, 1601 and January 1, 1970.
73 static const ULONGLONG epochBias = 116444736000000000ULL;
74 static const double hundredsOfNanosecondsPerMillisecond = 10000;
75
76 static double lowResUTCTime()
77 {
78 FILETIME fileTime;
79
80 GetSystemTimeAsFileTime(&fileTime);
81
82 // As per Windows documentation for FILETIME, copy the resulting FILETIME structure to a
83 // ULARGE_INTEGER structure using memcpy (using memcpy instead of direct assignment can
84 // prevent alignment faults on 64-bit Windows).
85
86 ULARGE_INTEGER dateTime;
87 memcpy(&dateTime, &fileTime, sizeof(dateTime));
88
89 // Windows file times are in 100s of nanoseconds.
295 #elif OS(DARWIN)
296
297 double monotonicallyIncreasingTime()
298 {
299 // Based on listing #2 from Apple QA 1398, but modified to be thread-safe.
300 static mach_timebase_info_data_t timebaseInfo;
301 static std::once_flag initializeTimerOnceFlag;
302 std::call_once(initializeTimerOnceFlag, [] {
303 kern_return_t kr = mach_timebase_info(&timebaseInfo);
304 ASSERT_UNUSED(kr, kr == KERN_SUCCESS);
305 ASSERT(timebaseInfo.denom);
306 });
307
308 return (mach_absolute_time() * timebaseInfo.numer) / (1.0e9 * timebaseInfo.denom);
309 }
310
311 #else
312
313 double monotonicallyIncreasingTime()
314 {
315 static double lastTime = 0;
316 double currentTimeNow = currentTime();
317 if (currentTimeNow < lastTime)
318 return lastTime;
319 lastTime = currentTimeNow;
320 return currentTimeNow;
321 }
322
323 #endif
324
325 std::chrono::microseconds currentCPUTime()
326 {
327 #if OS(DARWIN)
328 mach_msg_type_number_t infoCount = THREAD_BASIC_INFO_COUNT;
329 thread_basic_info_data_t info;
330
331 // Get thread information
332 mach_port_t threadPort = mach_thread_self();
333 thread_info(threadPort, THREAD_BASIC_INFO, reinterpret_cast<thread_info_t>(&info), &infoCount);
334 mach_port_deallocate(mach_task_self(), threadPort);
335
336 return std::chrono::seconds(info.user_time.seconds + info.system_time.seconds) + std::chrono::microseconds(info.user_time.microseconds + info.system_time.microseconds);
337 #elif OS(WINDOWS)
338 union {
339 FILETIME fileTime;
340 unsigned long long fileTimeAsLong;
|
50 #include <stdint.h>
51 #include <time.h>
52
53 #elif PLATFORM(EFL)
54 #include <Ecore.h>
55 #else
56 #include <sys/time.h>
57 #endif
58
59 #if USE(GLIB) && !PLATFORM(EFL)
60 #include <glib.h>
61 #endif
62
63 #if PLATFORM(JAVA)
64 #include <WebCore/platform/java/JavaEnv.h>
65 #endif
66
67
68 namespace WTF {
69
70 #if PLATFORM(JAVA) && OS(LINUX)
71
72 static const double hundredsOfNSecPerMillisecond = 10000;
73
74 double clockTimeNow()
75 {
76 auto now = std::chrono::steady_clock::now();
77 return static_cast<double>(std::chrono::duration_cast<std::chrono::microseconds>(
78 now.time_since_epoch()).count()) /
79 static_cast<double>(hundredsOfNSecPerMillisecond);
80 }
81
82 #endif
83
84 #if OS(WINDOWS)
85
86 // Number of 100 nanosecond between January 1, 1601 and January 1, 1970.
87 static const ULONGLONG epochBias = 116444736000000000ULL;
88 static const double hundredsOfNanosecondsPerMillisecond = 10000;
89
90 static double lowResUTCTime()
91 {
92 FILETIME fileTime;
93
94 GetSystemTimeAsFileTime(&fileTime);
95
96 // As per Windows documentation for FILETIME, copy the resulting FILETIME structure to a
97 // ULARGE_INTEGER structure using memcpy (using memcpy instead of direct assignment can
98 // prevent alignment faults on 64-bit Windows).
99
100 ULARGE_INTEGER dateTime;
101 memcpy(&dateTime, &fileTime, sizeof(dateTime));
102
103 // Windows file times are in 100s of nanoseconds.
309 #elif OS(DARWIN)
310
311 double monotonicallyIncreasingTime()
312 {
313 // Based on listing #2 from Apple QA 1398, but modified to be thread-safe.
314 static mach_timebase_info_data_t timebaseInfo;
315 static std::once_flag initializeTimerOnceFlag;
316 std::call_once(initializeTimerOnceFlag, [] {
317 kern_return_t kr = mach_timebase_info(&timebaseInfo);
318 ASSERT_UNUSED(kr, kr == KERN_SUCCESS);
319 ASSERT(timebaseInfo.denom);
320 });
321
322 return (mach_absolute_time() * timebaseInfo.numer) / (1.0e9 * timebaseInfo.denom);
323 }
324
325 #else
326
327 double monotonicallyIncreasingTime()
328 {
329 return clockTimeNow();
330 }
331
332 #endif
333
334 std::chrono::microseconds currentCPUTime()
335 {
336 #if OS(DARWIN)
337 mach_msg_type_number_t infoCount = THREAD_BASIC_INFO_COUNT;
338 thread_basic_info_data_t info;
339
340 // Get thread information
341 mach_port_t threadPort = mach_thread_self();
342 thread_info(threadPort, THREAD_BASIC_INFO, reinterpret_cast<thread_info_t>(&info), &infoCount);
343 mach_port_deallocate(mach_task_self(), threadPort);
344
345 return std::chrono::seconds(info.user_time.seconds + info.system_time.seconds) + std::chrono::microseconds(info.user_time.microseconds + info.system_time.microseconds);
346 #elif OS(WINDOWS)
347 union {
348 FILETIME fileTime;
349 unsigned long long fileTimeAsLong;
|