blob: f92b22b1a2ecf9fcdb6462e66581a6761245a5b0 [file] [log] [blame]
fei.dengf7a0cd32023-08-29 09:36:37 +00001/*
2 * Copyright (C) 2021 Amlogic Corporation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include <stdlib.h>
17#include <stdio.h>
18#include <sys/time.h>
19#include <stdarg.h>
20#include <sys/types.h>
21#include <limits.h>
22#include <time.h>
23#include "Times.h"
24
25namespace Tls {
26
27#define USE_GETTIMEOFDAY 0
28
29int64_t Times::getSystemTimeNs()
30{
31#if USE_GETTIMEOFDAY
32 // Clock support varies widely across hosts. Mac OS doesn't support
33 // posix clocks, older glibcs don't support CLOCK_BOOTTIME and Windows
34 // is windows.
35 struct timeval t;
36 t.tv_sec = t.tv_usec = 0;
37 gettimeofday(&t, NULL);
38 return (t.tv_sec)*1000000000LL + (t.tv_usec)*1000LL;
39#else
40 static const clockid_t clocks[] = {
41 CLOCK_REALTIME,
42 CLOCK_MONOTONIC,
43 CLOCK_PROCESS_CPUTIME_ID,
44 CLOCK_THREAD_CPUTIME_ID,
45 CLOCK_BOOTTIME
46 };
47 struct timespec t;
48 t.tv_sec = t.tv_nsec = 0;
49 clock_gettime(clocks[1], &t);
50 int64_t mono_ns = int64_t(t.tv_sec)*1000000000LL + t.tv_nsec;
51 return mono_ns;
52#endif
53}
54
55int64_t Times::getSystemTimeMs()
56{
57#if USE_GETTIMEOFDAY
58 struct timeval tv;
59 long long utcCurrentTimeMillis;
60
61 gettimeofday(&tv,0);
62 utcCurrentTimeMillis= tv.tv_sec*1000LL+(tv.tv_usec/1000LL);
63
64 return utcCurrentTimeMillis;
65#else
66 static const clockid_t clocks[] = {
67 CLOCK_REALTIME,
68 CLOCK_MONOTONIC,
69 CLOCK_PROCESS_CPUTIME_ID,
70 CLOCK_THREAD_CPUTIME_ID,
71 CLOCK_BOOTTIME
72 };
73 struct timespec t;
74 t.tv_sec = t.tv_nsec = 0;
75 clock_gettime(clocks[1], &t);
76 int64_t mono_ns = int64_t(t.tv_sec)*1000000000LL + t.tv_nsec;
77 return mono_ns/1000000LL;
78#endif
79}
80
81int64_t Times::getSystemTimeUs()
82{
83#if USE_GETTIMEOFDAY
84 struct timeval tv;
85 long long utcCurrentTimeMillis;
86
87 gettimeofday(&tv,0);
88 utcCurrentTimeMillis= tv.tv_sec*1000LL+(tv.tv_usec/1000LL);
89
90 return utcCurrentTimeMillis;
91#else
92 static const clockid_t clocks[] = {
93 CLOCK_REALTIME,
94 CLOCK_MONOTONIC,
95 CLOCK_PROCESS_CPUTIME_ID,
96 CLOCK_THREAD_CPUTIME_ID,
97 CLOCK_BOOTTIME
98 };
99 struct timespec t;
100 t.tv_sec = t.tv_nsec = 0;
101 clock_gettime(clocks[1], &t);
102 int64_t mono_ns = int64_t(t.tv_sec)*1000000000LL + t.tv_nsec;
103 return mono_ns/1000LL;
104#endif
105}
106}