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