1 /*
2  * Copyright(c) 2019 Intel Corporation
3  * SPDX - License - Identifier: BSD - 2 - Clause - Patent
4  */
5 
6 #ifdef _WIN32
7 #include <sys/timeb.h>
8 #include <windows.h>
9 #elif !defined(__USE_POSIX199309)
10 #define __USE_POSIX199309
11 #endif
12 
13 #include <time.h>
14 
15 #if !defined(CLOCK_MONOTONIC) && !defined(_WIN32)
16 #include <sys/time.h>
17 #endif
18 
19 #include "EbAppTime.h"
20 
app_svt_vp9_sleep(const unsigned milliseconds)21 void app_svt_vp9_sleep(const unsigned milliseconds) {
22     if (!milliseconds) return;
23 #ifdef _WIN32
24     Sleep(milliseconds);
25 #else
26     nanosleep(&(struct timespec){milliseconds / 1000,
27                                  (milliseconds % 1000) * 1000000},
28               NULL);
29 #endif
30 }
31 
app_svt_vp9_compute_overall_elapsed_time(const uint64_t start_seconds,const uint64_t start_useconds,const uint64_t finish_seconds,const uint64_t finish_useconds)32 double app_svt_vp9_compute_overall_elapsed_time(
33     const uint64_t start_seconds, const uint64_t start_useconds,
34     const uint64_t finish_seconds, const uint64_t finish_useconds) {
35     const int64_t s_diff = (int64_t)finish_seconds - (int64_t)start_seconds,
36                   u_diff = (int64_t)finish_useconds - (int64_t)start_useconds;
37     return ((double)s_diff * 1000.0 + (double)u_diff / 1000.0 + 0.5) / 1000.0;
38 }
39 
app_svt_vp9_get_time(uint64_t * const seconds,uint64_t * const useconds)40 void app_svt_vp9_get_time(uint64_t *const seconds, uint64_t *const useconds) {
41 #ifdef _WIN32
42     struct _timeb curr_time;
43     _ftime_s(&curr_time);
44     *seconds = curr_time.time;
45     *useconds = curr_time.millitm;
46 #elif defined(CLOCK_MONOTONIC)
47     struct timespec curr_time;
48     clock_gettime(CLOCK_MONOTONIC, &curr_time);
49     *seconds = (uint64_t)curr_time.tv_sec;
50     *useconds = (uint64_t)curr_time.tv_nsec / 1000UL;
51 #else
52     struct timeval curr_time;
53     gettimeofday(&curr_time, NULL);
54     *seconds = curr_time.tv_sec;
55     *useconds = curr_time.tv_usec;
56 #endif
57 }
58