Motr  M0
counter.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2015-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 
30 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_ADDB
31 
32 #include "lib/trace.h"
33 #include "lib/locality.h"
34 #include "lib/time.h"
35 #include "lib/tlist.h"
36 #include "lib/arith.h" /* min64u, max64u, m0_addu64_will_overflow */
37 
38 #include "addb2/counter.h"
39 #include "addb2/internal.h"
40 
41 static const struct m0_addb2_sensor_ops sensor_ops;
44 static void counter_warn(struct m0_addb2_counter *c, uint64_t val);
45 static void counter_dtor(void *area, void *datum);
46 static int counter_ctor(void *area, void *datum);
47 
48 void m0_addb2_counter_add(struct m0_addb2_counter *counter, uint64_t label,
49  int idx)
50 {
51  struct m0_addb2_counter_data *d = &counter->co_val;
52 
53  M0_CASSERT(M0_ADDB2_COUNTER_VALS * sizeof(uint64_t) == sizeof *d);
54 
57  m0_addb2_sensor_add(&counter->co_sensor, label,
59 }
60 
62 {
63  m0_addb2_sensor_del(&counter->co_sensor);
64 }
65 
67 {
69 }
70 
72  int64_t val, uint64_t datum)
73 {
74  struct m0_addb2_counter_data *d = &counter->co_val;
75  uint64_t sq = val * val;
76  int64_t sum = d->cod_sum + val;
77 
78  if (d->cod_nr == ~0ULL)
79  ; /* overflown, do nothing. */
80  else if ((val > 0 && (sum <= d->cod_sum || val > sq)) ||
81  (val < 0 && (sum >= d->cod_sum || -val > sq))) {
83  d->cod_nr = ~0ULL;
84  } else {
85  d->cod_nr ++;
86  d->cod_sum = sum;
87  if (val > d->cod_max) {
88  d->cod_datum = datum;
89  d->cod_max = val;
90  }
91  d->cod_min = min64u(d->cod_min, val);
92  d->cod_ssq += sq;
93  }
94 }
95 
97  struct m0_tl *list, uint64_t label, int idx)
98 {
99  counter->lc_list = list;
100  m0_addb2_sensor_add(&counter->lc_sensor,
101  label, 1, idx, &list_sensor_ops);
102 }
103 
105 {
106  m0_addb2_sensor_del(&counter->lc_sensor);
107 }
108 
109 void m0_addb2_clock_add(struct m0_addb2_sensor *clock, uint64_t label, int idx)
110 {
111  m0_addb2_sensor_add(clock, label, 0, idx, &clock_sensor_ops);
112 }
113 
115 {
116  m0_addb2_sensor_del(clock);
117 }
118 
120  uint64_t id, uint64_t counter)
121 {
122  lc->lc_id = id;
123  lc->lc_key = m0_locality_data_alloc(sizeof(struct m0_addb2_counter),
125  (void *)&counter);
126  return lc->lc_key >= 0 ? 0 : M0_ERR(lc->lc_key);
127 }
128 
130  uint64_t val, uint64_t datum)
131 {
133 }
134 
136  uint64_t *area)
137 {
139  struct m0_addb2_counter_data *d = &counter->co_val;
140 
141  *(struct m0_addb2_counter_data *)area = *d;
143 }
144 
145 static void counter_fini(struct m0_addb2_sensor *s)
146 {;}
147 
148 static const struct m0_addb2_sensor_ops sensor_ops = {
150  .so_fini = &counter_fini
151 };
152 
153 static void counter_warn(struct m0_addb2_counter *c, uint64_t val)
154 {
155  struct m0_addb2_counter_data *d = &c->co_val;
156 
157  M0_LOG(M0_WARN, "addb2 counter overflows: @%"PRIx64
158  " nr: %"PRIx64
159  " sum: %"PRIx64
160  " min: %"PRIx64
161  " max: %"PRIx64
162  " ssq: %"PRIx64
163  " val: %" PRIx64 ".", c->co_sensor.s_id,
164  d->cod_nr, d->cod_sum, d->cod_min, d->cod_max, d->cod_ssq, val);
165 }
166 
168 {
169  M0_SET0(d);
170  d->cod_min = UINT64_MAX;
171 }
172 
173 static void list_counter_snapshot(struct m0_addb2_sensor *s, uint64_t *area)
174 {
176 
177  area[0] = m0_list_length(&counter->lc_list->t_head);
178 }
179 
180 static const struct m0_addb2_sensor_ops list_sensor_ops = {
182  .so_fini = &counter_fini
183 };
184 
185 static void clock_counter_snapshot(struct m0_addb2_sensor *s, uint64_t *area)
186 {
187  /* Do nothing, addb2/addb2.c:sensor_place() already added time-stamp. */
188 }
189 
190 static const struct m0_addb2_sensor_ops clock_sensor_ops = {
192  .so_fini = &counter_fini
193 };
194 
195 static int counter_ctor(void *area, void *datum)
196 {
197  /* See sm_addb2_ctor() for the explanation of 2. */
198  m0_addb2_counter_add(area, *(uint64_t *)datum, 2);
199  return 0;
200 }
201 
202 static void counter_dtor(void *area, void *datum)
203 {
204  m0_addb2_counter_del(area);
205 }
206 
207 
208 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_ADDB
209 
212 /*
213  * Local variables:
214  * c-indentation-style: "K&R"
215  * c-basic-offset: 8
216  * tab-width: 8
217  * fill-column: 80
218  * scroll-step: 1
219  * End:
220  */
221 /*
222  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
223  */
uint64_t id
Definition: cob.h:2380
#define M0_PRE(cond)
static struct m0_list list
Definition: list.c:144
static const struct m0_addb2_sensor_ops clock_sensor_ops
Definition: counter.c:43
Definition: idx_mock.c:52
void m0_addb2_counter_add(struct m0_addb2_counter *counter, uint64_t label, int idx)
Definition: counter.c:48
void * m0_locality_data(int key)
Definition: locality.c:474
static void counter_warn(struct m0_addb2_counter *c, uint64_t val)
Definition: counter.c:153
void m0_addb2_list_counter_del(struct m0_addb2_list_counter *counter)
Definition: counter.c:104
void m0_addb2_counter_mod_with(struct m0_addb2_counter *counter, int64_t val, uint64_t datum)
Definition: counter.c:71
#define M0_LOG(level,...)
Definition: trace.h:167
#define M0_CASSERT(cond)
static int sum
Definition: rwlock.c:53
#define M0_SET0(obj)
Definition: misc.h:64
#define PRIx64
Definition: types.h:61
M0_INTERNAL size_t m0_list_length(const struct m0_list *list)
Definition: list.c:75
static void counter_fini(struct m0_addb2_sensor *s)
Definition: counter.c:145
return M0_ERR(-EOPNOTSUPP)
void m0_addb2_clock_del(struct m0_addb2_sensor *clock)
Definition: counter.c:114
#define M0_AMB(obj, ptr, field)
Definition: misc.h:320
void m0_addb2_counter_del(struct m0_addb2_counter *counter)
Definition: counter.c:61
uint64_t cod_ssq
Definition: counter.h:45
static struct m0_addb2_callback c
Definition: consumer.c:41
static int counter
Definition: mutex.c:32
Definition: tlist.h:251
int m0_addb2_local_counter_init(struct m0_addb2_local_counter *lc, uint64_t id, uint64_t counter)
Definition: counter.c:119
void m0_addb2_sensor_add(struct m0_addb2_sensor *s, uint64_t id, unsigned nr, int idx, const struct m0_addb2_sensor_ops *ops)
Definition: addb2.c:480
static int counter_ctor(void *area, void *datum)
Definition: counter.c:195
#define UINT64_MAX
Definition: types.h:44
static const struct m0_addb2_sensor_ops list_sensor_ops
Definition: counter.c:42
M0_INTERNAL void m0_addb2__counter_data_init(struct m0_addb2_counter_data *d)
Definition: counter.c:167
static void list_counter_snapshot(struct m0_addb2_sensor *s, uint64_t *area)
Definition: counter.c:173
void m0_addb2_clock_add(struct m0_addb2_sensor *clock, uint64_t label, int idx)
Definition: counter.c:109
static uint64_t min64u(uint64_t a, uint64_t b)
Definition: arith.h:66
void m0_addb2_list_counter_add(struct m0_addb2_list_counter *counter, struct m0_tl *list, uint64_t label, int idx)
Definition: counter.c:96
struct m0_addb2_sensor co_sensor
Definition: counter.h:55
#define M0_IS0(obj)
Definition: misc.h:70
static void clock_counter_snapshot(struct m0_addb2_sensor *s, uint64_t *area)
Definition: counter.c:185
M0_INTERNAL void m0_addb2__counter_snapshot(struct m0_addb2_sensor *s, uint64_t *area)
Definition: counter.c:135
void(* so_snapshot)(struct m0_addb2_sensor *s, uint64_t *area)
Definition: addb2.h:298
void m0_addb2_counter_mod(struct m0_addb2_counter *counter, int64_t val)
Definition: counter.c:66
static const struct m0_addb2_sensor_ops sensor_ops
Definition: counter.c:41
uint64_t cod_datum
Definition: counter.h:46
void m0_addb2_local_counter_mod(struct m0_addb2_local_counter *lc, uint64_t val, uint64_t datum)
Definition: counter.c:129
struct m0_addb2_sensor lc_sensor
Definition: counter.h:67
static void counter_dtor(void *area, void *datum)
Definition: counter.c:202
int m0_locality_data_alloc(size_t nob, int(*ctor)(void *, void *), void(*dtor)(void *, void *), void *datum)
Definition: locality.c:442
void m0_addb2_sensor_del(struct m0_addb2_sensor *s)
Definition: addb2.c:504
static struct m0_addb2_source * s
Definition: consumer.c:39
Definition: trace.h:478