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 <inttypes.h>
18 #include <sstream>
19 
20 #include "time_utils.h"
21 
22 #include "base/logging.h"
23 #include "base/stringprintf.h"
24 
25 #if defined(__APPLE__)
26 #include <sys/time.h>
27 #endif
28 
29 namespace art {
30 
PrettyDuration(uint64_t nano_duration,size_t max_fraction_digits)31 std::string PrettyDuration(uint64_t nano_duration, size_t max_fraction_digits) {
32   if (nano_duration == 0) {
33     return "0";
34   } else {
35     return FormatDuration(nano_duration, GetAppropriateTimeUnit(nano_duration),
36                           max_fraction_digits);
37   }
38 }
39 
GetAppropriateTimeUnit(uint64_t nano_duration)40 TimeUnit GetAppropriateTimeUnit(uint64_t nano_duration) {
41   const uint64_t one_sec = 1000 * 1000 * 1000;
42   const uint64_t one_ms  = 1000 * 1000;
43   const uint64_t one_us  = 1000;
44   if (nano_duration >= one_sec) {
45     return kTimeUnitSecond;
46   } else if (nano_duration >= one_ms) {
47     return kTimeUnitMillisecond;
48   } else if (nano_duration >= one_us) {
49     return kTimeUnitMicrosecond;
50   } else {
51     return kTimeUnitNanosecond;
52   }
53 }
54 
GetNsToTimeUnitDivisor(TimeUnit time_unit)55 uint64_t GetNsToTimeUnitDivisor(TimeUnit time_unit) {
56   const uint64_t one_sec = 1000 * 1000 * 1000;
57   const uint64_t one_ms  = 1000 * 1000;
58   const uint64_t one_us  = 1000;
59 
60   switch (time_unit) {
61     case kTimeUnitSecond:
62       return one_sec;
63     case kTimeUnitMillisecond:
64       return one_ms;
65     case kTimeUnitMicrosecond:
66       return one_us;
67     case kTimeUnitNanosecond:
68       return 1;
69   }
70   return 0;
71 }
72 
FormatDuration(uint64_t nano_duration,TimeUnit time_unit,size_t max_fraction_digits)73 std::string FormatDuration(uint64_t nano_duration, TimeUnit time_unit,
74                            size_t max_fraction_digits) {
75   const char* unit = nullptr;
76   uint64_t divisor = GetNsToTimeUnitDivisor(time_unit);
77   switch (time_unit) {
78     case kTimeUnitSecond:
79       unit = "s";
80       break;
81     case kTimeUnitMillisecond:
82       unit = "ms";
83       break;
84     case kTimeUnitMicrosecond:
85       unit = "us";
86       break;
87     case kTimeUnitNanosecond:
88       unit = "ns";
89       break;
90   }
91   const uint64_t whole_part = nano_duration / divisor;
92   uint64_t fractional_part = nano_duration % divisor;
93   if (fractional_part == 0) {
94     return StringPrintf("%" PRIu64 "%s", whole_part, unit);
95   } else {
96     static constexpr size_t kMaxDigits = 30;
97     size_t avail_digits = kMaxDigits;
98     char fraction_buffer[kMaxDigits];
99     char* ptr = fraction_buffer;
100     uint64_t multiplier = 10;
101     // This infinite loops if fractional part is 0.
102     while (avail_digits > 1 && fractional_part * multiplier < divisor) {
103       multiplier *= 10;
104       *ptr++ = '0';
105       avail_digits--;
106     }
107     snprintf(ptr, avail_digits, "%" PRIu64, fractional_part);
108     fraction_buffer[std::min(kMaxDigits - 1, max_fraction_digits)] = '\0';
109     return StringPrintf("%" PRIu64 ".%s%s", whole_part, fraction_buffer, unit);
110   }
111 }
112 
GetIsoDate()113 std::string GetIsoDate() {
114   time_t now = time(nullptr);
115   tm tmbuf;
116   tm* ptm = localtime_r(&now, &tmbuf);
117   return StringPrintf("%04d-%02d-%02d %02d:%02d:%02d",
118       ptm->tm_year + 1900, ptm->tm_mon+1, ptm->tm_mday,
119       ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
120 }
121 
MilliTime()122 uint64_t MilliTime() {
123 #if defined(__linux__)
124   timespec now;
125   clock_gettime(CLOCK_MONOTONIC, &now);
126   return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000) + now.tv_nsec / UINT64_C(1000000);
127 #else  // __APPLE__
128   timeval now;
129   gettimeofday(&now, nullptr);
130   return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000) + now.tv_usec / UINT64_C(1000);
131 #endif
132 }
133 
MicroTime()134 uint64_t MicroTime() {
135 #if defined(__linux__)
136   timespec now;
137   clock_gettime(CLOCK_MONOTONIC, &now);
138   return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000) + now.tv_nsec / UINT64_C(1000);
139 #else  // __APPLE__
140   timeval now;
141   gettimeofday(&now, nullptr);
142   return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000) + now.tv_usec;
143 #endif
144 }
145 
NanoTime()146 uint64_t NanoTime() {
147 #if defined(__linux__)
148   timespec now;
149   clock_gettime(CLOCK_MONOTONIC, &now);
150   return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000000) + now.tv_nsec;
151 #else  // __APPLE__
152   timeval now;
153   gettimeofday(&now, nullptr);
154   return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000000) + now.tv_usec * UINT64_C(1000);
155 #endif
156 }
157 
ThreadCpuNanoTime()158 uint64_t ThreadCpuNanoTime() {
159 #if defined(__linux__)
160   timespec now;
161   clock_gettime(CLOCK_THREAD_CPUTIME_ID, &now);
162   return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000000) + now.tv_nsec;
163 #else  // __APPLE__
164   UNIMPLEMENTED(WARNING);
165   return -1;
166 #endif
167 }
168 
NanoSleep(uint64_t ns)169 void NanoSleep(uint64_t ns) {
170   timespec tm;
171   tm.tv_sec = ns / MsToNs(1000);
172   tm.tv_nsec = ns - static_cast<uint64_t>(tm.tv_sec) * MsToNs(1000);
173   nanosleep(&tm, nullptr);
174 }
175 
InitTimeSpec(bool absolute,int clock,int64_t ms,int32_t ns,timespec * ts)176 void InitTimeSpec(bool absolute, int clock, int64_t ms, int32_t ns, timespec* ts) {
177   int64_t endSec;
178 
179   if (absolute) {
180 #if !defined(__APPLE__)
181     clock_gettime(clock, ts);
182 #else
183     UNUSED(clock);
184     timeval tv;
185     gettimeofday(&tv, nullptr);
186     ts->tv_sec = tv.tv_sec;
187     ts->tv_nsec = tv.tv_usec * 1000;
188 #endif
189   } else {
190     ts->tv_sec = 0;
191     ts->tv_nsec = 0;
192   }
193   endSec = ts->tv_sec + ms / 1000;
194   if (UNLIKELY(endSec >= 0x7fffffff)) {
195     std::ostringstream ss;
196     LOG(INFO) << "Note: end time exceeds epoch: " << ss.str();
197     endSec = 0x7ffffffe;
198   }
199   ts->tv_sec = endSec;
200   ts->tv_nsec = (ts->tv_nsec + (ms % 1000) * 1000000) + ns;
201 
202   // Catch rollover.
203   if (ts->tv_nsec >= 1000000000L) {
204     ts->tv_sec++;
205     ts->tv_nsec -= 1000000000L;
206   }
207 }
208 
209 }  // namespace art
210