Motr  M0
histogram.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2020 Seagate Technology LLC and/or its Affiliates
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * For any questions about this software or licensing,
17  * please email opensource@seagate.com or cortx-questions@seagate.com.
18  *
19  */
20 
21 
22 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_UT
23 
24 #include "lib/trace.h"
25 #include "ut/ut.h"
26 #include "addb2/histogram.h"
27 
28 #include "addb2/ut/common.h"
29 
30 static void init_fini(void)
31 {
32  struct m0_addb2_hist h = {};
33 
34  m0_addb2_hist_add(&h, 0, 1, 6, -1);
36  M0_SET0(&h);
37  m0_addb2_hist_add_auto(&h, 3, 6, -1);
39 }
40 
41 static const int64_t minmax[][2] = {
42  { 0, 1 },
43  { 1, 2 },
44  { -1, 0 },
45  { 0, M0_BITS(58) },
46  { -M0_BITS(58), 2 },
47  { -M0_BITS(58), M0_BITS(58) },
48  { 0, 0 }
49 };
50 
51 static const int64_t random_val[] = {
52  3, 5, 7, 9, 10, 11, 13, 15, 17, 1024, 1500, 60, 7776, 0xdeadbeef, 0
53 };
54 
55 static void test_one(struct m0_addb2_hist *h, int64_t val)
56 {
57  uint32_t orig[M0_ADDB2_HIST_BUCKETS];
58  uint32_t *bucket = h->hi_data.hd_bucket;
59  int idx = m0_addb2_hist_bucket(h, val);
60 
61  memcpy(orig, bucket, sizeof orig);
64  bucket[i] == orig[i] + (i == idx)));
65  M0_UT_ASSERT((idx == 0) == (val < h->hi_data.hd_min));
66  M0_UT_ASSERT(ergo(val == h->hi_data.hd_min, idx == 1));
68  idx == M0_ADDB2_HIST_BUCKETS - 1));
69 }
70 static void test_around(struct m0_addb2_hist *h, int64_t val)
71 {
72  test_one(h, val - 1);
73  test_one(h, val);
74  test_one(h, val + 1);
75 }
76 
77 static void test_hist(struct m0_addb2_hist *h)
78 {
79  int i;
80 
81  for (i = 0; i < 60; ++i) {
82  test_around(h, M0_BITS(i));
83  test_around(h, -M0_BITS(i));
84  }
85  test_around(h, 0);
86  test_around(h, h->hi_data.hd_min);
87  test_around(h, h->hi_data.hd_max);
88  for (i = 0; i < ARRAY_SIZE(random_val); ++i)
90 }
91 
92 static void test_bucket(void)
93 {
94  int i;
95 
96  for (i = 0; minmax[i][0] != minmax[i][1]; ++i) {
97  struct m0_addb2_hist h = {};
98 
99  m0_addb2_hist_add(&h, minmax[i][0],minmax[i][1], 68 + i, -1);
100  test_hist(&h);
101  m0_addb2_hist_del(&h);
102  }
103 }
104 
106  .ts_name = "addb2-histogram",
107  .ts_init = NULL,
108  .ts_fini = NULL,
109  .ts_tests = {
110  { "init-fini", &init_fini },
111  { "history-bucket", &test_bucket },
112  { NULL, NULL }
113  }
114 };
115 
116 #undef M0_TRACE_SUBSYSTEM
117 
118 /*
119  * Local variables:
120  * c-indentation-style: "K&R"
121  * c-basic-offset: 8
122  * tab-width: 8
123  * fill-column: 80
124  * scroll-step: 1
125  * End:
126  */
#define NULL
Definition: misc.h:38
Definition: idx_mock.c:52
#define ergo(a, b)
Definition: misc.h:293
struct m0_ut_suite addb2_hist_ut
Definition: histogram.c:105
int m0_addb2_hist_bucket(const struct m0_addb2_hist *hist, int64_t val)
Definition: histogram.c:87
#define M0_BITS(...)
Definition: misc.h:236
void m0_addb2_hist_mod(struct m0_addb2_hist *hist, int64_t val)
Definition: histogram.c:68
#define M0_SET0(obj)
Definition: misc.h:64
Definition: ut.h:77
int i
Definition: dir.c:1033
void m0_addb2_hist_del(struct m0_addb2_hist *hist)
Definition: histogram.c:63
static void test_around(struct m0_addb2_hist *h, int64_t val)
Definition: histogram.c:70
struct m0_addb2_hist_data hi_data
Definition: histogram.h:134
uint32_t hd_bucket[M0_ADDB2_HIST_BUCKETS]
Definition: histogram.h:121
static void test_bucket(void)
Definition: histogram.c:92
static const int64_t minmax[][2]
Definition: histogram.c:41
void m0_addb2_hist_add_auto(struct m0_addb2_hist *hist, int skip, uint64_t label, int idx)
Definition: histogram.c:50
#define m0_forall(var, nr,...)
Definition: misc.h:112
const char * ts_name
Definition: ut.h:99
static const int64_t random_val[]
Definition: histogram.c:51
static void init_fini(void)
Definition: histogram.c:30
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
#define ARRAY_SIZE(a)
Definition: misc.h:45
#define M0_UT_ASSERT(a)
Definition: ut.h:46
static void test_hist(struct m0_addb2_hist *h)
Definition: histogram.c:77
static void test_one(struct m0_addb2_hist *h, int64_t val)
Definition: histogram.c:55