Motr  M0
cnt.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-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 #include <string.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <err.h>
26 #include <math.h>
27 
28 #include "motr/magic.h"
29 #include "desim/sim.h"
30 #include "desim/cnt.h"
31 
37 M0_TL_DESCR_DEFINE(cnts, "counters", static, struct cnt,
40 M0_TL_DEFINE(cnts, static, struct cnt);
41 
42 static struct m0_tl cnts;
43 
44 M0_INTERNAL void cnt_init(struct cnt *cnt, struct cnt *parent,
45  const char *format, ...)
46 {
47  va_list valist;
48 
49  memset(cnt, 0, sizeof *cnt);
50  cnt->c_min = ~0ULL;
51  cnt->c_max = 0;
52  va_start(valist, format);
53  sim_name_vaset(&cnt->c_name, format, valist);
54  va_end(valist);
55  cnt->c_parent = parent;
56  cnts_tlink_init_at_tail(cnt, &cnts);
57 }
58 
59 M0_INTERNAL void cnt_dump(struct cnt *cnt)
60 {
61  cnt_t avg;
62  double sig;
63 
64  if (cnt->c_nr != 0) {
65  avg = cnt->c_sum / cnt->c_nr;
66  sig = sqrt(cnt->c_sq/cnt->c_nr - avg*avg);
67  sim_log(NULL, SLL_INFO, "[%s: %llu (%llu) %llu %llu %f]\n",
68  cnt->c_name, avg, cnt->c_nr,
69  cnt->c_min, cnt->c_max, sig);
70  } else
71  sim_log(NULL, SLL_INFO, "[%s: empty]\n", cnt->c_name);
72 }
73 
74 M0_INTERNAL void cnt_dump_all(void)
75 {
76  struct cnt *scan;
77 
79  cnt_dump(scan);
81 }
82 
83 M0_INTERNAL void cnt_fini(struct cnt *cnt)
84 {
85  if (cnt->c_name != NULL)
86  free(cnt->c_name);
87  cnts_tlink_del_fini(cnt);
88  cnt->c_magic = 0;
89 }
90 
91 
92 M0_INTERNAL void cnt_mod(struct cnt *cnt, cnt_t val)
93 {
94  do {
95  cnt->c_sum += val;
96  cnt->c_nr++;
97  cnt->c_sq += val*val;
98  if (val > cnt->c_max)
99  cnt->c_max = val;
100  if (val < cnt->c_min)
101  cnt->c_min = val;
102  } while ((cnt = cnt->c_parent) != NULL);
103 }
104 
105 M0_INTERNAL void cnt_global_init(void)
106 {
107  cnts_tlist_init(&cnts);
108 }
109 
110 M0_INTERNAL void cnt_global_fini(void)
111 {
112  struct cnt *scan;
113 
114  m0_tl_for(cnts, &cnts, scan)
115  cnt_fini(scan);
116  m0_tl_endfor;
117 
118  cnts_tlist_fini(&cnts);
119 }
120 
123 /*
124  * Local variables:
125  * c-indentation-style: "K&R"
126  * c-basic-offset: 8
127  * tab-width: 8
128  * fill-column: 80
129  * scroll-step: 1
130  * End:
131  */
cnt_t c_min
Definition: cnt.h:38
#define NULL
Definition: misc.h:38
Definition: idx_mock.c:52
cnt_t c_nr
Definition: cnt.h:40
M0_INTERNAL void sim_log(struct sim *s, enum sim_log_level level, const char *format,...)
Definition: sim.c:527
struct m0_tlink c_linkage
Definition: cnt.h:43
M0_INTERNAL void cnt_init(struct cnt *cnt, struct cnt *parent, const char *format,...)
Definition: cnt.c:44
M0_INTERNAL void cnt_global_fini(void)
Definition: cnt.c:110
#define m0_tl_endfor
Definition: tlist.h:700
M0_TL_DEFINE(cnts, static, struct cnt)
M0_INTERNAL void cnt_mod(struct cnt *cnt, cnt_t val)
Definition: cnt.c:92
static struct m0_tl cnts
Definition: cnt.c:42
Definition: cnt.h:36
cnt_t c_max
Definition: cnt.h:39
M0_TL_DESCR_DEFINE(cnts, "counters", static, struct cnt, c_linkage, c_magic, M0_DESIM_CNT_MAGIC, M0_DESIM_CNTS_HEAD_MAGIC)
Definition: tlist.h:251
char * c_name
Definition: cnt.h:42
struct cnt * c_parent
Definition: cnt.h:44
format
Definition: hist.py:128
M0_INTERNAL void sim_name_vaset(char **name, const char *format, va_list valist)
Definition: sim.c:513
M0_INTERNAL void cnt_global_init(void)
Definition: cnt.c:105
M0_INTERNAL void cnt_dump_all(void)
Definition: cnt.c:74
M0_INTERNAL void cnt_dump(struct cnt *cnt)
Definition: cnt.c:59
uint64_t c_magic
Definition: cnt.h:45
double c_sq
Definition: cnt.h:41
Definition: sim.h:286
unsigned long long cnt_t
Definition: cnt.h:34
cnt_t c_sum
Definition: cnt.h:37
static int scan(struct scanner *s)
Definition: beck.c:963
#define m0_tl_for(name, head, obj)
Definition: tlist.h:695
M0_INTERNAL void cnt_fini(struct cnt *cnt)
Definition: cnt.c:83