• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
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 
17 #define _POSIX_THREAD_SAFE_FUNCTIONS  // For mingw localtime_r().
18 
19 #include "time_utils.h"
20 
21 #include <inttypes.h>
22 #include <stdio.h>
23 
24 #include <limits>
25 #include <sstream>
26 
27 #include "android-base/stringprintf.h"
28 
29 #include "logging.h"
30 
31 #if defined(__APPLE__)
32 #include <sys/time.h>
33 #endif
34 
35 namespace art {
36 
37 namespace {
38 
39 #if !defined(__linux__)
GetTimeOfDay(struct timeval * tv,struct timezone * tz)40 int GetTimeOfDay(struct timeval* tv, struct timezone* tz) {
41 #ifdef _WIN32
42   return mingw_gettimeofday(tv, tz);
43 #else
44   return gettimeofday(tv, tz);
45 #endif
46 }
47 #endif
48 
49 }  // namespace
50 
51 using android::base::StringPrintf;
52 
PrettyDuration(uint64_t nano_duration,size_t max_fraction_digits)53 std::string PrettyDuration(uint64_t nano_duration, size_t max_fraction_digits) {
54   if (nano_duration == 0) {
55     return "0";
56   } else {
57     return FormatDuration(nano_duration, GetAppropriateTimeUnit(nano_duration),
58                           max_fraction_digits);
59   }
60 }
61 
GetAppropriateTimeUnit(uint64_t nano_duration)62 TimeUnit GetAppropriateTimeUnit(uint64_t nano_duration) {
63   const uint64_t one_sec = 1000 * 1000 * 1000;
64   const uint64_t one_ms  = 1000 * 1000;
65   const uint64_t one_us  = 1000;
66   if (nano_duration >= one_sec) {
67     return kTimeUnitSecond;
68   } else if (nano_duration >= one_ms) {
69     return kTimeUnitMillisecond;
70   } else if (nano_duration >= one_us) {
71     return kTimeUnitMicrosecond;
72   } else {
73     return kTimeUnitNanosecond;
74   }
75 }
76 
GetNsToTimeUnitDivisor(TimeUnit time_unit)77 uint64_t GetNsToTimeUnitDivisor(TimeUnit time_unit) {
78   const uint64_t one_sec = 1000 * 1000 * 1000;
79   const uint64_t one_ms  = 1000 * 1000;
80   const uint64_t one_us  = 1000;
81 
82   switch (time_unit) {
83     case kTimeUnitSecond:
84       return one_sec;
85     case kTimeUnitMillisecond:
86       return one_ms;
87     case kTimeUnitMicrosecond:
88       return one_us;
89     case kTimeUnitNanosecond:
90       return 1;
91   }
92   return 0;
93 }
94 
FormatDuration(uint64_t nano_duration,TimeUnit time_unit,size_t max_fraction_digits)95 std::string FormatDuration(uint64_t nano_duration, TimeUnit time_unit,
96                            size_t max_fraction_digits) {
97   const char* unit = nullptr;
98   uint64_t divisor = GetNsToTimeUnitDivisor(time_unit);
99   switch (time_unit) {
100     case kTimeUnitSecond:
101       unit = "s";
102       break;
103     case kTimeUnitMillisecond:
104       unit = "ms";
105       break;
106     case kTimeUnitMicrosecond:
107       unit = "us";
108       break;
109     case kTimeUnitNanosecond:
110       unit = "ns";
111       break;
112   }
113   const uint64_t whole_part = nano_duration / divisor;
114   uint64_t fractional_part = nano_duration % divisor;
115   if (fractional_part == 0) {
116     return StringPrintf("%" PRIu64 "%s", whole_part, unit);
117   } else {
118     static constexpr size_t kMaxDigits = 30;
119     size_t avail_digits = kMaxDigits;
120     char fraction_buffer[kMaxDigits];
121     char* ptr = fraction_buffer;
122     uint64_t multiplier = 10;
123     // This infinite loops if fractional part is 0.
124     while (avail_digits > 1 && fractional_part * multiplier < divisor) {
125       multiplier *= 10;
126       *ptr++ = '0';
127       avail_digits--;
128     }
129     snprintf(ptr, avail_digits, "%" PRIu64, fractional_part);
130     fraction_buffer[std::min(kMaxDigits - 1, max_fraction_digits)] = '\0';
131     return StringPrintf("%" PRIu64 ".%s%s", whole_part, fraction_buffer, unit);
132   }
133 }
134 
GetIsoDate()135 std::string GetIsoDate() {
136   tm tmbuf;
137   int ns;
138   if (__builtin_available(macOS 10.12, *)) {
139     timespec now;
140     clock_gettime(CLOCK_REALTIME, &now);
141     localtime_r(&now.tv_sec, &tmbuf);
142     ns = now.tv_nsec;
143   } else {
144     time_t now = time(nullptr);
145     localtime_r(&now, &tmbuf);
146     ns = 0;
147   }
148   char zone[16] = {};
149   strftime(zone, sizeof(zone), "%z", &tmbuf);
150   return StringPrintf("%04d-%02d-%02d %02d:%02d:%02d.%09d%s",
151       tmbuf.tm_year + 1900, tmbuf.tm_mon+1, tmbuf.tm_mday,
152       tmbuf.tm_hour, tmbuf.tm_min, tmbuf.tm_sec, ns, zone);
153 }
154 
MilliTime()155 uint64_t MilliTime() {
156 #if defined(__linux__)
157   timespec now;
158   clock_gettime(CLOCK_MONOTONIC, &now);
159   return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000) + now.tv_nsec / UINT64_C(1000000);
160 #else
161   timeval now;
162   GetTimeOfDay(&now, nullptr);
163   return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000) + now.tv_usec / UINT64_C(1000);
164 #endif
165 }
166 
MicroTime()167 uint64_t MicroTime() {
168 #if defined(__linux__)
169   timespec now;
170   clock_gettime(CLOCK_MONOTONIC, &now);
171   return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000) + now.tv_nsec / UINT64_C(1000);
172 #else
173   timeval now;
174   GetTimeOfDay(&now, nullptr);
175   return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000) + now.tv_usec;
176 #endif
177 }
178 
NanoTime()179 uint64_t NanoTime() {
180 #if defined(__linux__)
181   timespec now;
182   clock_gettime(CLOCK_MONOTONIC, &now);
183   return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000000) + now.tv_nsec;
184 #else
185   timeval now;
186   GetTimeOfDay(&now, nullptr);
187   return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000000) + now.tv_usec * UINT64_C(1000);
188 #endif
189 }
190 
ThreadCpuNanoTime()191 uint64_t ThreadCpuNanoTime() {
192 #if defined(__linux__)
193   timespec now;
194   clock_gettime(CLOCK_THREAD_CPUTIME_ID, &now);
195   return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000000) + now.tv_nsec;
196 #else
197   UNIMPLEMENTED(WARNING);
198   return -1;
199 #endif
200 }
201 
ProcessCpuNanoTime()202 uint64_t ProcessCpuNanoTime() {
203 #if defined(__linux__)
204   timespec now;
205   clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &now);
206   return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000000) + now.tv_nsec;
207 #else
208   // We cannot use clock_gettime() here. Return the process wall clock time
209   // (using art::NanoTime, which relies on gettimeofday()) as approximation of
210   // the process CPU time instead.
211   //
212   // Note: clock_gettime() is available from macOS 10.12 (Darwin 16), but we try
213   // to keep things simple here.
214   return NanoTime();
215 #endif
216 }
217 
NanoSleep(uint64_t ns)218 void NanoSleep(uint64_t ns) {
219   timespec tm;
220   tm.tv_sec = SaturatedTimeT(ns / MsToNs(1000));
221   tm.tv_nsec = ns - static_cast<uint64_t>(tm.tv_sec) * MsToNs(1000);
222   nanosleep(&tm, nullptr);
223 }
224 
InitTimeSpec(bool absolute,int clock,int64_t ms,int32_t ns,timespec * ts)225 void InitTimeSpec(bool absolute, int clock, int64_t ms, int32_t ns, timespec* ts) {
226   if (absolute) {
227 #if defined(__linux__)
228     clock_gettime(clock, ts);
229 #else
230     UNUSED(clock);
231     timeval tv;
232     GetTimeOfDay(&tv, nullptr);
233     ts->tv_sec = tv.tv_sec;
234     ts->tv_nsec = tv.tv_usec * 1000;
235 #endif
236   } else {
237     ts->tv_sec = 0;
238     ts->tv_nsec = 0;
239   }
240 
241   int64_t end_sec = ts->tv_sec + ms / 1000;
242   constexpr int32_t int32_max = std::numeric_limits<int32_t>::max();
243   if (UNLIKELY(end_sec >= int32_max)) {
244     // Either ms was intended to denote an infinite timeout, or we have a
245     // problem. The former generally uses the largest possible millisecond
246     // or nanosecond value.  Log only in the latter case.
247     constexpr int64_t int64_max = std::numeric_limits<int64_t>::max();
248     if (ms != int64_max && ms != int64_max / (1000 * 1000)) {
249       LOG(INFO) << "Note: end time exceeds INT32_MAX: " << end_sec;
250     }
251     end_sec = int32_max - 1;  // Allow for increment below.
252   }
253   ts->tv_sec = end_sec;
254   ts->tv_nsec = (ts->tv_nsec + (ms % 1000) * 1000000) + ns;
255 
256   // Catch rollover.
257   if (ts->tv_nsec >= 1000000000L) {
258     ts->tv_sec++;
259     ts->tv_nsec -= 1000000000L;
260   }
261 }
262 
263 }  // namespace art
264