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