Motr  M0
cookie.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2019-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 
29 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_HA
30 #include "lib/trace.h"
31 
32 #include "ha/cookie.h"
33 
34 #include "lib/misc.h" /* M0_IS0 */
35 #include "lib/thread.h" /* m0_process */
36 #include "lib/types.h" /* UINT64_MAX */
37 #include "lib/uuid.h" /* m0_uuid_generate */
38 
39 
41  .hc_pid = UINT64_MAX,
42  .hc_time_start = UINT64_MAX,
43  .hc_uptime = UINT64_MAX,
44  .hc_uuid = M0_UINT128(UINT64_MAX, UINT64_MAX),
45 };
46 
47 #define HACO_F "(pid=%" PRIu64 " time_start="TIME_F" uptime="TIME_F" " \
48  "uuid="U128X_F")"
49 #define HACO_P(_hc) (_hc)->hc_pid, \
50  TIME_P((_hc)->hc_time_start), \
51  TIME_P((_hc)->hc_uptime), \
52  U128_P(&(_hc)->hc_uuid)
53 
54 
55 M0_INTERNAL void m0_ha_cookie_init(struct m0_ha_cookie *hc)
56 {
57  M0_PRE(M0_IS0(hc));
59  M0_LEAVE("hc="HACO_F, HACO_P(hc));
60 }
61 
62 M0_INTERNAL void m0_ha_cookie_fini(struct m0_ha_cookie *hc)
63 {
64  M0_ENTRY("hc="HACO_F, HACO_P(hc));
65 }
66 
67 M0_INTERNAL void m0_ha_cookie_record(struct m0_ha_cookie *hc)
68 {
70  *hc = (struct m0_ha_cookie){
71  hc->hc_pid = m0_process(),
72  hc->hc_time_start = m0_time_now(),
73  hc->hc_uptime = M0_TIME_NEVER, // XXX FIXME
74  };
76  M0_LEAVE("hc="HACO_F, HACO_P(hc));
77 }
78 
79 M0_INTERNAL bool m0_ha_cookie_is_eq(const struct m0_ha_cookie *a,
80  const struct m0_ha_cookie *b)
81 {
82  M0_LOG(M0_DEBUG, "a="HACO_F, HACO_P(a));
83  M0_LOG(M0_DEBUG, "b="HACO_F, HACO_P(b));
84  return a->hc_pid == b->hc_pid &&
85  a->hc_time_start == b->hc_time_start &&
86  a->hc_uptime == b->hc_uptime &&
87  m0_uint128_eq(&a->hc_uuid, &b->hc_uuid);
88 }
89 
90 M0_INTERNAL void m0_ha_cookie_from_xc(struct m0_ha_cookie *hc,
91  const struct m0_ha_cookie_xc *hc_xc)
92 {
93  *hc = (struct m0_ha_cookie){
94  .hc_pid = hc_xc->hcx_pid,
95  .hc_time_start = hc_xc->hcx_time_start,
96  .hc_uptime = hc_xc->hcx_uptime,
97  .hc_uuid = hc_xc->hcx_uuid,
98  };
99 }
100 
101 M0_INTERNAL void m0_ha_cookie_to_xc(const struct m0_ha_cookie *hc,
102  struct m0_ha_cookie_xc *hc_xc)
103 {
104  *hc_xc = (struct m0_ha_cookie_xc){
105  .hcx_pid = hc->hc_pid,
106  .hcx_time_start = hc->hc_time_start,
107  .hcx_uptime = hc->hc_uptime,
108  .hcx_uuid = hc->hc_uuid,
109  };
110 }
111 
112 #undef HACO_P
113 #undef HACO_F
114 
115 #undef M0_TRACE_SUBSYSTEM
116 
119 /*
120  * Local variables:
121  * c-indentation-style: "K&R"
122  * c-basic-offset: 8
123  * tab-width: 8
124  * fill-column: 80
125  * scroll-step: 1
126  * End:
127  */
128 /*
129  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
130  */
M0_INTERNAL uint64_t m0_process(void)
Definition: kthread.c:295
#define M0_PRE(cond)
M0_INTERNAL void m0_uuid_generate(struct m0_uint128 *u)
Definition: uuid.c:44
const m0_time_t M0_TIME_NEVER
Definition: time.c:108
M0_INTERNAL bool m0_uint128_eq(const struct m0_uint128 *u0, const struct m0_uint128 *u1)
Definition: misc.c:39
#define M0_LOG(level,...)
Definition: trace.h:167
M0_LEAVE()
#define HACO_P(_hc)
Definition: cookie.c:49
M0_INTERNAL void m0_ha_cookie_to_xc(const struct m0_ha_cookie *hc, struct m0_ha_cookie_xc *hc_xc)
Definition: cookie.c:101
M0_INTERNAL void m0_ha_cookie_init(struct m0_ha_cookie *hc)
Definition: cookie.c:55
#define M0_ENTRY(...)
Definition: trace.h:170
M0_INTERNAL void m0_ha_cookie_fini(struct m0_ha_cookie *hc)
Definition: cookie.c:62
M0_INTERNAL bool m0_ha_cookie_is_eq(const struct m0_ha_cookie *a, const struct m0_ha_cookie *b)
Definition: cookie.c:79
const struct m0_ha_cookie m0_ha_cookie_no_record
Definition: cookie.c:40
m0_time_t m0_time_now(void)
Definition: time.c:134
#define UINT64_MAX
Definition: types.h:44
M0_INTERNAL void m0_ha_cookie_record(struct m0_ha_cookie *hc)
Definition: cookie.c:67
#define M0_IS0(obj)
Definition: misc.h:70
#define M0_UINT128(hi, lo)
Definition: types.h:40
M0_INTERNAL void m0_ha_cookie_from_xc(struct m0_ha_cookie *hc, const struct m0_ha_cookie_xc *hc_xc)
Definition: cookie.c:90
#define HACO_F
Definition: cookie.c:47