1 /*
2  * bmon/attr.h		Attributes
3  *
4  * Copyright (c) 2001-2013 Thomas Graf <tgraf@suug.ch>
5  * Copyright (c) 2013 Red Hat, Inc.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  */
25 
26 #ifndef __BMON_ATTR_H_
27 #define __BMON_ATTR_H_
28 
29 #include <bmon/bmon.h>
30 #include <bmon/unit.h>
31 
32 struct element;
33 
34 struct rate
35 {
36 	/* Total value of attribute with eventual overflows accumulated.  */
37 	uint64_t		r_total;
38 
39 	/* Current value of counter */
40 	uint64_t		r_current;
41 
42 	/* Value of r_current at last read */
43 	uint64_t		r_prev;
44 
45 	/* Reset value to substract to emulate statistics reset */
46 	uint64_t		r_reset;
47 
48 	/* Rate per second calculated every `rate_interval' */
49 	float			r_rate;
50 
51 	/* Time of last calculation */
52 	timestamp_t		r_last_calc;
53 };
54 
55 extern uint64_t			rate_get_total(struct rate *);
56 
57 enum {
58 	ATTR_TYPE_UNSPEC,
59 	ATTR_TYPE_COUNTER,
60 	ATTR_TYPE_RATE,
61 	ATTR_TYPE_PERCENT,
62 };
63 
64 struct attr_def {
65 	int			ad_id;
66 	char *			ad_name;
67 	char *			ad_description;
68 	int			ad_type;
69 	int			ad_flags;
70 	struct unit *		ad_unit;
71 
72 	struct list_head	ad_list;
73 };
74 
75 struct attr_map {
76 	const char *	name;
77 	const char *	description;
78 	const char *	unit;
79 	int		attrid,
80 			type,
81 			rxid,
82 			txid,
83 			flags;
84 };
85 
86 extern int			attr_def_add(const char *, const char *,
87 					     struct unit *, int, int);
88 extern struct attr_def *	attr_def_lookup(const char *);
89 extern struct attr_def *	attr_def_lookup_id(int);
90 
91 extern int			attr_map_load(struct attr_map *map, size_t size);
92 
93 #define ATTR_FORCE_HISTORY		0x01	/* collect history */
94 #define ATTR_IGNORE_OVERFLOWS		0x02
95 #define ATTR_TRUE_64BIT			0x04
96 #define ATTR_RX_ENABLED			0x08	/* has RX counter */
97 #define ATTR_TX_ENABLED			0x10	/* has TX counter */
98 #define ATTR_DOING_HISTORY		0x20	/* history collected */
99 
100 struct attr
101 {
102 	struct rate		a_rx_rate,
103 				a_tx_rate;
104 
105 	uint8_t			a_flags;
106 	struct attr_def *	a_def;
107 	timestamp_t		a_last_update;
108 
109 	struct list_head	a_history_list;
110 
111 	struct list_head	a_list;
112 	struct list_head	a_sort_list;
113 };
114 
115 extern struct attr *		attr_lookup(const struct element *, int);
116 extern void			attr_update(struct element *, int,
117 					    uint64_t, uint64_t , int );
118 extern void			attr_notify_update(struct attr *,
119 						   timestamp_t *);
120 extern void			attr_free(struct attr *);
121 
122 extern void			attr_rate2float(struct attr *,
123 						double *, char **, int *,
124 						double *, char **, int *);
125 
126 extern void			attr_calc_usage(struct attr *, float *, float *,
127 						uint64_t, uint64_t);
128 
129 #define ATTR_HASH_SIZE 32
130 
131 #define UPDATE_FLAG_RX			0x01
132 #define UPDATE_FLAG_TX			0x02
133 #define UPDATE_FLAG_64BIT		0x04
134 
135 extern struct attr *		attr_select_first(void);
136 extern struct attr *		attr_select_last(void);
137 extern struct attr *		attr_select_next(void);
138 extern struct attr *		attr_select_prev(void);
139 extern struct attr *		attr_current(void);
140 
141 extern void			attr_start_collecting_history(struct attr *);
142 extern void			attr_reset_counter(struct attr *a);
143 
144 #endif
145