YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/gutil/walltime.h
Line
Count
Source
1
// Copyright 2012 Google Inc. All Rights Reserved.
2
//
3
// The following only applies to changes made to this file as part of YugaByte development.
4
//
5
// Portions Copyright (c) YugaByte, Inc.
6
//
7
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8
// in compliance with the License.  You may obtain a copy of the License at
9
//
10
// http://www.apache.org/licenses/LICENSE-2.0
11
//
12
// Unless required by applicable law or agreed to in writing, software distributed under the License
13
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14
// or implied.  See the License for the specific language governing permissions and limitations
15
// under the License.
16
//
17
// Licensed to the Apache Software Foundation (ASF) under one
18
// or more contributor license agreements.  See the NOTICE file
19
// distributed with this work for additional information
20
// regarding copyright ownership.  The ASF licenses this file
21
// to you under the Apache License, Version 2.0 (the
22
// "License"); you may not use this file except in compliance
23
// with the License.  You may obtain a copy of the License at
24
//
25
//   http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing,
28
// software distributed under the License is distributed on an
29
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
30
// KIND, either express or implied.  See the License for the
31
// specific language governing permissions and limitations
32
// under the License.
33
#ifndef YB_GUTIL_WALLTIME_H
34
#define YB_GUTIL_WALLTIME_H
35
36
#include <sys/resource.h>
37
#include <sys/time.h>
38
39
#include <string>
40
41
#if defined(__APPLE__)
42
#include <mach/clock.h>
43
#include <mach/mach.h>
44
#include <mach/mach_time.h>
45
46
#include "yb/gutil/once.h"
47
#else
48
#include <time.h>
49
#endif  // defined(__APPLE__)
50
51
#include "yb/gutil/integral_types.h"
52
53
using std::string;
54
55
typedef double WallTime;
56
57
// Append result to a supplied string.
58
// If an error occurs during conversion 'dst' is not modified.
59
void StringAppendStrftime(std::string* dst,
60
                          const char* format,
61
                          time_t when,
62
                          bool local);
63
64
void StringAppendStrftime(string* dst,
65
                          const char* format,
66
                          const struct tm* tm);
67
68
// Return the local time as a string suitable for user display.
69
std::string LocalTimeAsString();
70
71
// Similar to the WallTime_Parse, but it takes a boolean flag local as
72
// argument specifying if the time_spec is in local time or UTC
73
// time. If local is set to true, the same exact result as
74
// WallTime_Parse is returned.
75
bool WallTime_Parse_Timezone(const char* time_spec,
76
                             const char* format,
77
                             const struct tm* default_time,
78
                             bool local,
79
                             WallTime* result);
80
81
// Return current time in seconds as a WallTime.
82
WallTime WallTime_Now();
83
84
typedef int64 MicrosecondsInt64;
85
86
// Get User & Sys CPU time of current thread since an arbitrary epoch.
87
void GetThreadUserAndSysCpuTimeMicros(MicrosecondsInt64 *user, MicrosecondsInt64 *sys);
88
89
namespace walltime_internal {
90
91
#if defined(__APPLE__)
92
93
extern GoogleOnceType timebase_info_once;
94
extern mach_timebase_info_data_t timebase_info;
95
extern void InitializeTimebaseInfo();
96
97
147M
inline void GetCurrentTime(mach_timespec_t* ts) {
98
147M
  clock_serv_t cclock;
99
147M
  host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
100
147M
  clock_get_time(cclock, ts);
101
147M
  mach_port_deallocate(mach_task_self(), cclock);
102
147M
}
103
104
108M
inline MicrosecondsInt64 GetCurrentTimeMicros() {
105
108M
  mach_timespec_t ts;
106
108M
  GetCurrentTime(&ts);
107
108M
  return ts.tv_sec * 1e6 + ts.tv_nsec / 1e3;
108
108M
}
109
110
103M
inline int64_t GetMonoTimeNanos() {
111
  // See Apple Technical Q&A QA1398 for further detail on mono time in OS X.
112
103M
  GoogleOnceInit(&timebase_info_once, &InitializeTimebaseInfo);
113
114
103M
  uint64_t time = mach_absolute_time();
115
116
  // mach_absolute_time returns ticks, which need to be scaled by the timebase
117
  // info to get nanoseconds.
118
103M
  return time * timebase_info.numer / timebase_info.denom;
119
103M
}
120
121
103M
inline MicrosecondsInt64 GetMonoTimeMicros() {
122
103M
  return GetMonoTimeNanos() / 1e3;
123
103M
}
124
125
359k
inline MicrosecondsInt64 GetThreadCpuTimeMicros() {
126
359k
  MicrosecondsInt64 thread_user_cpu = 0;
127
359k
  GetThreadUserAndSysCpuTimeMicros(&thread_user_cpu, nullptr);
128
359k
  return thread_user_cpu;
129
359k
}
130
131
#else
132
133
inline MicrosecondsInt64 GetClockTimeMicros(clockid_t clock) {
134
  timespec ts;
135
  clock_gettime(clock, &ts);
136
  return ts.tv_sec * 1e6 + ts.tv_nsec / 1e3;
137
}
138
139
#endif // defined(__APPLE__)
140
141
} // namespace walltime_internal
142
143
// Returns the time since the Epoch measured in microseconds.
144
108M
inline MicrosecondsInt64 GetCurrentTimeMicros() {
145
108M
#if defined(__APPLE__)
146
108M
  return walltime_internal::GetCurrentTimeMicros();
147
#else
148
  return walltime_internal::GetClockTimeMicros(CLOCK_REALTIME);
149
#endif  // defined(__APPLE__)
150
108M
}
151
152
// Returns the time since some arbitrary reference point, measured in microseconds.
153
// Guaranteed to be monotonic (and therefore useful for measuring intervals)
154
103M
inline MicrosecondsInt64 GetMonoTimeMicros() {
155
103M
#if defined(__APPLE__)
156
103M
  return walltime_internal::GetMonoTimeMicros();
157
#else
158
  return walltime_internal::GetClockTimeMicros(CLOCK_MONOTONIC);
159
#endif  // defined(__APPLE__)
160
103M
}
161
162
// Returns the time spent in user CPU on the current thread, measured in microseconds.
163
360k
inline MicrosecondsInt64 GetThreadCpuTimeMicros() {
164
360k
#if defined(__APPLE__)
165
360k
  return walltime_internal::GetThreadCpuTimeMicros();
166
#else
167
  return walltime_internal::GetClockTimeMicros(CLOCK_THREAD_CPUTIME_ID);
168
#endif  // defined(__APPLE__)
169
360k
}
170
171
// A CycleClock yields the value of a cycle counter that increments at a rate
172
// that is approximately constant.
173
class CycleClock {
174
 public:
175
  // Return the value of the counter.
176
  static inline int64 Now();
177
178
 private:
179
  CycleClock();
180
};
181
182
#include "yb/gutil/cycleclock-inl.h"  // inline method bodies
183
#endif  // YB_GUTIL_WALLTIME_H