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