Motr  M0
clk_src.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2021 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 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_DTM0
23 #include "lib/trace.h"
24 
25 #include "dtm0/clk_src.h"
26 #include "lib/assert.h" /* M0_PRE */
27 
28 
30  int (*cso_cmp)(const struct m0_dtm0_ts *left,
31  const struct m0_dtm0_ts *right);
32  void (*cso_now)(struct m0_dtm0_clk_src *cs, struct m0_dtm0_ts *ts);
33 };
34 
35 /* See M0_DTM0_CS_PHYS for details */
36 static const struct m0_dtm0_clk_src_ops cs_phys_ops;
37 
38 M0_INTERNAL void m0_dtm0_clk_src_init(struct m0_dtm0_clk_src *cs,
40 {
41  M0_ENTRY();
42  M0_PRE(M0_IN(type, (M0_DTM0_CS_PHYS)));
44  cs->cs_last = M0_DTM0_TS_MIN;
45  cs->cs_ops = &cs_phys_ops;
46  M0_LEAVE();
47 }
48 
49 M0_INTERNAL void m0_dtm0_clk_src_fini(struct m0_dtm0_clk_src *cs)
50 {
51  M0_ENTRY();
52  M0_PRE(cs);
53  M0_PRE(M0_IN(cs->cs_ops, (&cs_phys_ops)));
56  cs->cs_ops = NULL;
57  M0_LEAVE();
58 }
59 
60 M0_INTERNAL
62  const struct m0_dtm0_ts *left,
63  const struct m0_dtm0_ts *right)
64 {
65  M0_PRE(cs);
68  return cs->cs_ops->cso_cmp(left, right);
69 }
70 
71 M0_INTERNAL void m0_dtm0_clk_src_now(struct m0_dtm0_clk_src *cs,
72  struct m0_dtm0_ts *now)
73 {
74  M0_PRE(cs != NULL);
75  cs->cs_ops->cso_now(cs, now);
77 }
78 
79 M0_INTERNAL bool m0_dtm0_ts__invariant(const struct m0_dtm0_ts *ts)
80 {
81  const struct m0_dtm0_ts min_ts = M0_DTM0_TS_MIN;
82  const struct m0_dtm0_ts max_ts = M0_DTM0_TS_MAX;
83 
84  return _0C(ts->dts_phys >= min_ts.dts_phys) &&
85  _0C(ts->dts_phys <= max_ts.dts_phys);
86 }
87 
88 /* Phys clock implementation */
89 
90 static enum m0_dtm0_ts_ord cs_phys_cmp(const struct m0_dtm0_ts *left,
91  const struct m0_dtm0_ts *right)
92 {
93  return M0_3WAY(left->dts_phys, right->dts_phys);
94 }
95 
96 static void cs_phys_now(struct m0_dtm0_clk_src *cs, struct m0_dtm0_ts *now)
97 {
98  M0_PRE(M0_IN(cs->cs_ops, (&cs_phys_ops)));
99 
101 
102  now->dts_phys = m0_time_now();
103  if (now->dts_phys > cs->cs_last.dts_phys) {
104  cs->cs_last = *now;
105  } else {
106  cs->cs_last.dts_phys++;
107  *now = cs->cs_last;
108  }
109 
111 }
112 
113 static const struct m0_dtm0_clk_src_ops cs_phys_ops = {
114  .cso_cmp = cs_phys_cmp,
115  .cso_now = cs_phys_now,
116 };
117 
118 M0_INTERNAL bool m0_dtm0_ts_is_none(const struct m0_dtm0_ts *ts)
119 {
120  return ts->dts_phys == 0;
121 }
122 
123 #undef M0_TRACE_SUBSYSTEM
124 
125 /*
126  * Local variables:
127  * c-indentation-style: "K&R"
128  * c-basic-offset: 8
129  * tab-width: 8
130  * fill-column: 80
131  * scroll-step: 1
132  * End:
133  */
134 /*
135  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
136  */
M0_INTERNAL enum m0_dtm0_ts_ord m0_dtm0_ts_cmp(const struct m0_dtm0_clk_src *cs, const struct m0_dtm0_ts *left, const struct m0_dtm0_ts *right)
Definition: clk_src.c:61
#define M0_PRE(cond)
M0_INTERNAL void m0_mutex_unlock(struct m0_mutex *mutex)
Definition: mutex.c:66
#define NULL
Definition: misc.h:38
struct m0_dtm0_ts cs_last
Definition: clk_src.h:126
#define M0_3WAY(v0, v1)
Definition: arith.h:199
static void cs_phys_now(struct m0_dtm0_clk_src *cs, struct m0_dtm0_ts *now)
Definition: clk_src.c:96
M0_LEAVE()
#define M0_DTM0_TS_MAX
Definition: clk_src.h:99
static int left
Definition: locality.c:280
M0_INTERNAL void m0_mutex_lock(struct m0_mutex *mutex)
Definition: mutex.c:49
#define M0_ENTRY(...)
Definition: trace.h:170
m0_time_t dts_phys
Definition: clk_src.h:93
M0_INTERNAL bool m0_dtm0_ts_is_none(const struct m0_dtm0_ts *ts)
Definition: clk_src.c:118
M0_INTERNAL void m0_dtm0_clk_src_now(struct m0_dtm0_clk_src *cs, struct m0_dtm0_ts *now)
Definition: clk_src.c:71
m0_time_t m0_time_now(void)
Definition: time.c:134
m0_dtm0_cs_types
Definition: clk_src.h:74
int(* cso_cmp)(const struct m0_dtm0_ts *left, const struct m0_dtm0_ts *right)
Definition: clk_src.c:30
M0_INTERNAL void m0_mutex_init(struct m0_mutex *mutex)
Definition: mutex.c:35
#define M0_POST(cond)
const struct m0_dtm0_clk_src_ops * cs_ops
Definition: clk_src.h:125
struct m0_mutex cs_phys_lock
Definition: clk_src.h:127
M0_INTERNAL void m0_dtm0_clk_src_fini(struct m0_dtm0_clk_src *cs)
Definition: clk_src.c:49
#define _0C(exp)
Definition: assert.h:311
M0_INTERNAL void m0_mutex_fini(struct m0_mutex *mutex)
Definition: mutex.c:42
void(* cso_now)(struct m0_dtm0_clk_src *cs, struct m0_dtm0_ts *ts)
Definition: clk_src.c:32
static const struct m0_dtm0_clk_src_ops cs_phys_ops
Definition: clk_src.c:36
M0_INTERNAL bool m0_dtm0_ts__invariant(const struct m0_dtm0_ts *ts)
Definition: clk_src.c:79
m0_dtm0_ts_ord
Definition: clk_src.h:112
#define M0_DTM0_TS_MIN
Definition: clk_src.h:97
int type
Definition: dir.c:1031
static enum m0_dtm0_ts_ord cs_phys_cmp(const struct m0_dtm0_ts *left, const struct m0_dtm0_ts *right)
Definition: clk_src.c:90
M0_INTERNAL void m0_dtm0_clk_src_init(struct m0_dtm0_clk_src *cs, enum m0_dtm0_cs_types type)
Definition: clk_src.c:38
#define M0_DTM0_TS_INIT
Definition: clk_src.h:101