Motr  M0
time.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2012-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_LIB
24 #include "lib/trace.h"
25 
26 #include "lib/time.h"
27 #include "lib/time_internal.h" /* m0_clock_gettime_wrapper */
28 #include "lib/misc.h" /* M0_EXPORTED */
29 
41 m0_time_t m0_time(uint64_t secs, long ns)
42 {
43  return M0_MKTIME(secs, ns);
44 }
45 M0_EXPORTED(m0_time);
46 
48 {
49  m0_time_t res;
50 
53 
54  if (t1 == M0_TIME_NEVER || t2 == M0_TIME_NEVER)
56  else
57  res = t1 + t2;
58 
59  M0_POST(res >= t1);
60  M0_POST(res >= t2);
61  return res;
62 }
63 M0_EXPORTED(m0_time_add);
64 
66 {
67  m0_time_t res;
70  M0_ASSERT_INFO(t1 >= t2,
71  "t1="TIME_F" t2="TIME_F, TIME_P(t1), TIME_P(t2));
72 
73  if (t1 == M0_TIME_NEVER)
75  else
76  res = t1 - t2;
77 
78  M0_POST(t1 >= res);
79  return res;
80 }
81 M0_EXPORTED(m0_time_sub);
82 
83 uint64_t m0_time_seconds(const m0_time_t time)
84 {
85  return time / M0_TIME_ONE_SECOND;
86 }
87 M0_EXPORTED(m0_time_seconds);
88 
89 uint64_t m0_time_nanoseconds(const m0_time_t time)
90 {
91 
92  return time % M0_TIME_ONE_SECOND;
93 }
94 M0_EXPORTED(m0_time_nanoseconds);
95 
96 m0_time_t m0_time_from_now(uint64_t secs, long ns)
97 {
98  return m0_time_now() + m0_time(secs, ns);
99 }
100 M0_EXPORTED(m0_time_from_now);
101 
103 {
104  return t < m0_time_now();
105 }
106 
108 const m0_time_t M0_TIME_NEVER = ~0ULL;
109 M0_EXPORTED(M0_TIME_IMMEDIATELY);
110 M0_EXPORTED(M0_TIME_NEVER);
111 
114 
115 M0_INTERNAL int m0_time_init(void)
116 {
117  m0_time_t realtime;
118  m0_time_t monotonic;
119 
123  m0_time_monotonic_offset = realtime - monotonic;
124  if (m0_time_monotonic_offset == 0)
126  }
127  return 0;
128 } M0_EXPORTED(m0_time_init);
129 
130 M0_INTERNAL void m0_time_fini(void)
131 {
132 } M0_EXPORTED(m0_time_fini);
133 
135 {
136  m0_time_t result;
137 
138  switch (M0_CLOCK_SOURCE) {
143  break;
146  break;
151  break;
152  default:
153  M0_IMPOSSIBLE("Unknown clock source");
154  result = M0_TIME_NEVER;
155  };
156  return result;
157 }
158 M0_EXPORTED(m0_time_now);
159 
161 {
162  m0_time_t source_time;
163  m0_time_t realtime;
164  m0_time_t monotonic;
165 
166  if (abs_time != M0_TIME_NEVER && abs_time != 0) {
167  switch (M0_CLOCK_SOURCE) {
171  realtime =
173  abs_time += realtime - source_time;
174  break;
176  monotonic =
178  realtime =
180  /* get monotonic time */
181  abs_time -= m0_time_monotonic_offset;
182  /* add offset for realtime */
183  abs_time += realtime - monotonic;
184  /* It will mitigate time jumps between call
185  * to m0_time_now() and call to this function. */
186  break;
189  break;
190  default:
191  M0_IMPOSSIBLE("Unknown clock source");
192  abs_time = 0;
193  }
194  }
195  return abs_time;
196 } M0_EXPORTED(m0_time_to_realtime);
197 
200 /*
201  * Local variables:
202  * c-indentation-style: "K&R"
203  * c-basic-offset: 8
204  * tab-width: 8
205  * fill-column: 80
206  * scroll-step: 1
207  * End:
208  */
#define M0_PRE(cond)
m0_time_t m0_time_monotonic_offset
Definition: time.c:113
M0_INTERNAL m0_time_t m0_clock_gettimeofday_wrapper(void)
Definition: ktime.c:62
CLOCK_SOURCES
Definition: time.h:36
M0_INTERNAL void m0_time_fini(void)
Definition: time.c:130
const m0_time_t M0_TIME_NEVER
Definition: time.c:108
uint64_t m0_time_t
Definition: time.h:37
uint64_t m0_time_nanoseconds(const m0_time_t time)
Definition: time.c:89
enum CLOCK_SOURCES M0_CLOCK_SOURCE
Definition: time.c:112
#define TIME_P(t)
Definition: time.h:45
m0_time_t m0_time(uint64_t secs, long ns)
Definition: time.c:41
M0_INTERNAL m0_time_t m0_clock_gettime_wrapper(enum CLOCK_SOURCES clock_id)
Definition: ktime.c:40
static void t2(int n)
Definition: thread.c:48
#define TIME_F
Definition: time.h:44
bool m0_time_is_in_past(m0_time_t t)
Definition: time.c:102
m0_time_t m0_time_now(void)
Definition: time.c:134
static struct m0_thread t[8]
Definition: service_ut.c:1230
#define M0_POST(cond)
M0_INTERNAL int m0_time_init(void)
Definition: time.c:115
m0_time_t m0_time_add(const m0_time_t t1, const m0_time_t t2)
Definition: time.c:47
uint64_t m0_time_seconds(const m0_time_t time)
Definition: time.c:83
m0_time_t m0_time_from_now(uint64_t secs, long ns)
Definition: time.c:96
m0_time_t m0_time_sub(const m0_time_t t1, const m0_time_t t2)
Definition: time.c:65
static void t1(int n)
Definition: mutex.c:48
#define M0_MKTIME(secs, ns)
Definition: time.h:86
#define M0_ASSERT_INFO(cond, fmt,...)
M0_INTERNAL m0_time_t m0_time_to_realtime(m0_time_t abs_time)
Definition: time.c:160
const m0_time_t M0_TIME_IMMEDIATELY
Definition: time.c:107
#define M0_IMPOSSIBLE(fmt,...)