Motr  M0
histogram.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2016-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_ADDB
30 #include "lib/trace.h"
31 #include "addb2/histogram.h"
32 #include "addb2/internal.h" /* m0_addb2__counter_snapshot */
33 
34 static const struct m0_addb2_sensor_ops hist_ops;
35 
36 void m0_addb2_hist_add(struct m0_addb2_hist *hist, int64_t min, int64_t max,
37  uint64_t label, int idx)
38 {
39  struct m0_addb2_counter *c = &hist->hi_counter;
40 
41  M0_PRE(M0_IS0(hist));
42  M0_PRE(max > min);
43 
45  hist->hi_data.hd_min = min;
46  hist->hi_data.hd_max = max;
47  m0_addb2_sensor_add(&c->co_sensor, label, VALUE_MAX_NR, idx, &hist_ops);
48 }
49 
51  uint64_t label, int idx)
52 {
53  struct m0_addb2_counter *c = &hist->hi_counter;
54 
55  M0_PRE(M0_IS0(hist));
56  M0_PRE(skip > 0);
57 
59  hist->hi_skip = skip;
60  m0_addb2_sensor_add(&c->co_sensor, label, VALUE_MAX_NR, idx, &hist_ops);
61 }
62 
64 {
65  m0_addb2_sensor_del(&hist->hi_counter.co_sensor);
66 }
67 
68 void m0_addb2_hist_mod(struct m0_addb2_hist *hist, int64_t val)
69 {
71 }
72 
74  int64_t val, uint64_t datum)
75 {
76  struct m0_addb2_hist_data *hd = &hist->hi_data;
77 
78  if (hist->hi_skip > 0) {
79  hd->hd_min = min64(hd->hd_min, val);
80  hd->hd_max = max64(hd->hd_max, val);
81  hist->hi_skip--;
82  } else
83  hist->hi_data.hd_bucket[m0_addb2_hist_bucket(hist, val)]++;
84  m0_addb2_counter_mod_with(&hist->hi_counter, val, datum);
85 }
86 
87 int m0_addb2_hist_bucket(const struct m0_addb2_hist *hist, int64_t val)
88 {
89  const struct m0_addb2_hist_data *hd = &hist->hi_data;
90  int idx;
91 
92  if (val < hd->hd_min)
93  idx = 0;
94  else if (val >= hd->hd_max)
95  idx = M0_ADDB2_HIST_BUCKETS - 1;
96  else
97  idx = (val - hd->hd_min) * (M0_ADDB2_HIST_BUCKETS - 2) /
98  (hd->hd_max - hd->hd_min) + 1;
99  M0_POST(0 <= idx && idx < M0_ADDB2_HIST_BUCKETS);
100  return idx;
101 }
102 
103 static void hist_snapshot(struct m0_addb2_sensor *s, uint64_t *area)
104 {
106  struct m0_addb2_hist_data *hd = &hist->hi_data;
107  int i;
108 
110  area += M0_ADDB2_COUNTER_VALS;
111  *(struct m0_addb2_hist_data *)area = *hd;
112  for (i = 0; i < ARRAY_SIZE(hd->hd_bucket); ++i)
113  hd->hd_bucket[i] = 0;
114 }
115 
116 static void hist_fini(struct m0_addb2_sensor *s)
117 {;}
118 
119 static const struct m0_addb2_sensor_ops hist_ops = {
121  .so_fini = &hist_fini
122 };
123 
124 #undef M0_TRACE_SUBSYSTEM
125 
128 /*
129  * Local variables:
130  * c-indentation-style: "K&R"
131  * c-basic-offset: 8
132  * tab-width: 8
133  * fill-column: 80
134  * scroll-step: 1
135  * End:
136  */
137 /*
138  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
139  */
#define M0_PRE(cond)
Definition: idx_mock.c:52
void m0_addb2_counter_mod_with(struct m0_addb2_counter *counter, int64_t val, uint64_t datum)
Definition: counter.c:71
int m0_addb2_hist_bucket(const struct m0_addb2_hist *hist, int64_t val)
Definition: histogram.c:87
void m0_addb2_hist_mod(struct m0_addb2_hist *hist, int64_t val)
Definition: histogram.c:68
static const struct m0_addb2_sensor_ops hist_ops
Definition: histogram.c:34
int i
Definition: dir.c:1033
void m0_addb2_hist_del(struct m0_addb2_hist *hist)
Definition: histogram.c:63
static int64_t max64(int64_t a, int64_t b)
Definition: arith.h:51
#define M0_AMB(obj, ptr, field)
Definition: misc.h:320
static struct m0_addb2_callback c
Definition: consumer.c:41
static void hist_snapshot(struct m0_addb2_sensor *s, uint64_t *area)
Definition: histogram.c:103
uint32_t hd_bucket[M0_ADDB2_HIST_BUCKETS]
Definition: histogram.h:121
static long long max(long long a, long long b)
Definition: crate.c:196
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
#define M0_POST(cond)
M0_INTERNAL void m0_addb2__counter_data_init(struct m0_addb2_counter_data *d)
Definition: counter.c:167
void m0_addb2_hist_mod_with(struct m0_addb2_hist *hist, int64_t val, uint64_t datum)
Definition: histogram.c:73
void m0_addb2_hist_add_auto(struct m0_addb2_hist *hist, int skip, uint64_t label, int idx)
Definition: histogram.c:50
struct m0_addb2_sensor co_sensor
Definition: counter.h:55
struct m0_addb2_counter hi_counter
Definition: histogram.h:131
static long long min(long long a, long long b)
Definition: crate.c:191
#define M0_IS0(obj)
Definition: misc.h:70
static int64_t min64(int64_t a, int64_t b)
Definition: arith.h:46
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
static void skip(struct m0_addb2__context *ctx, const uint64_t *v, char *buf)
Definition: dump.c:465
void m0_addb2_sensor_del(struct m0_addb2_sensor *s)
Definition: addb2.c:504
void m0_addb2_hist_add(struct m0_addb2_hist *hist, int64_t min, int64_t max, uint64_t label, int idx)
Definition: histogram.c:36
static void hist_fini(struct m0_addb2_sensor *s)
Definition: histogram.c:116
static struct m0_addb2_source * s
Definition: consumer.c:39
#define ARRAY_SIZE(a)
Definition: misc.h:45
Definition: hist.py:1