File Coverage

bson/bson-clock.c
Criterion Covered Total %
statement 0 5 0.0
branch n/a
condition n/a
subroutine n/a
pod n/a
total 0 5 0.0


line stmt bran cond sub pod time code
1             /*
2             * Copyright 2013 MongoDB, Inc.
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              
18             #ifdef __APPLE__
19             # include
20             # include
21             # include
22             # include
23             #endif
24              
25              
26             #include "bson-config.h"
27             #include "bson-compat.h"
28              
29              
30             #if defined(BSON_HAVE_CLOCK_GETTIME)
31             # include
32             # include
33             #endif
34              
35             #include "bson-clock.h"
36              
37              
38             /*
39             *--------------------------------------------------------------------------
40             *
41             * bson_gettimeofday --
42             *
43             * A wrapper around gettimeofday() with fallback support for Windows.
44             *
45             * Returns:
46             * 0 if successful.
47             *
48             * Side effects:
49             * @tv is set.
50             *
51             *--------------------------------------------------------------------------
52             */
53              
54             int
55 0           bson_gettimeofday (struct timeval *tv) /* OUT */
56             {
57             #if defined(_WIN32)
58             # if defined(_MSC_VER)
59             # define DELTA_EPOCH_IN_MICROSEC 11644473600000000Ui64
60             # else
61             # define DELTA_EPOCH_IN_MICROSEC 11644473600000000ULL
62             # endif
63             FILETIME ft;
64             uint64_t tmp = 0;
65              
66             /*
67             * The const value is shamelessy stolen from
68             * http://www.boost.org/doc/libs/1_55_0/boost/chrono/detail/inlined/win/chrono.hpp
69             *
70             * File times are the number of 100 nanosecond intervals elapsed since
71             * 12:00 am Jan 1, 1601 UTC. I haven't check the math particularly hard
72             *
73             * ... good luck
74             */
75              
76             if (tv) {
77             GetSystemTimeAsFileTime (&ft);
78              
79             /* pull out of the filetime into a 64 bit uint */
80             tmp |= ft.dwHighDateTime;
81             tmp <<= 32;
82             tmp |= ft.dwLowDateTime;
83              
84             /* convert from 100's of nanosecs to microsecs */
85             tmp /= 10;
86              
87             /* adjust to unix epoch */
88             tmp -= DELTA_EPOCH_IN_MICROSEC;
89              
90             tv->tv_sec = (long)(tmp / 1000000UL);
91             tv->tv_usec = (long)(tmp % 1000000UL);
92             }
93              
94             return 0;
95             #else
96 0           return gettimeofday (tv, NULL);
97             #endif
98             }
99              
100              
101             /*
102             *--------------------------------------------------------------------------
103             *
104             * bson_get_monotonic_time --
105             *
106             * Returns the monotonic system time, if available. A best effort is
107             * made to use the monotonic clock. However, some systems may not
108             * support such a feature.
109             *
110             * Returns:
111             * The monotonic clock in microseconds.
112             *
113             * Side effects:
114             * None.
115             *
116             *--------------------------------------------------------------------------
117             */
118              
119             int64_t
120 0           bson_get_monotonic_time (void)
121             {
122             #if defined(BSON_HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
123             struct timespec ts;
124 0           clock_gettime (CLOCK_MONOTONIC, &ts);
125 0           return ((ts.tv_sec * 1000000UL) + (ts.tv_nsec / 1000UL));
126             #elif defined(__APPLE__)
127             static mach_timebase_info_data_t info = { 0 };
128             static double ratio = 0.0;
129              
130             if (!info.denom) {
131             // the value from mach_absolute_time () * info.numer / info.denom
132             // is in nano seconds. So we have to divid by 1000.0 to get micro seconds
133             mach_timebase_info (&info);
134             ratio = (double)info.numer / (double)info.denom / 1000.0;
135             }
136              
137             return mach_absolute_time () * ratio;
138             #elif defined(_WIN32)
139             /* Despite it's name, this is in milliseconds! */
140             int64_t ticks = GetTickCount64 ();
141             return (ticks * 1000L);
142             #else
143             # warning "Monotonic clock is not yet supported on your platform."
144             struct timeval tv;
145              
146             bson_gettimeofday (&tv);
147             return (tv.tv_sec * 1000000UL) + tv.tv_usec;
148             #endif
149             }