1
1
use std:: sync:: atomic:: * ;
2
2
use std:: sync:: Arc ;
3
3
4
+ #[ derive( Debug , Clone , Default ) ]
5
+ struct AddressStatFields {
6
+ xact_count : Arc < AtomicU64 > ,
7
+ query_count : Arc < AtomicU64 > ,
8
+ bytes_received : Arc < AtomicU64 > ,
9
+ bytes_sent : Arc < AtomicU64 > ,
10
+ xact_time : Arc < AtomicU64 > ,
11
+ query_time : Arc < AtomicU64 > ,
12
+ wait_time : Arc < AtomicU64 > ,
13
+ errors : Arc < AtomicU64 > ,
14
+ }
15
+
4
16
/// Internal address stats
5
17
#[ derive( Debug , Clone , Default ) ]
6
18
pub struct AddressStats {
7
- pub total_xact_count : Arc < AtomicU64 > ,
8
- pub total_query_count : Arc < AtomicU64 > ,
9
- pub total_received : Arc < AtomicU64 > ,
10
- pub total_sent : Arc < AtomicU64 > ,
11
- pub total_xact_time : Arc < AtomicU64 > ,
12
- pub total_query_time : Arc < AtomicU64 > ,
13
- pub total_wait_time : Arc < AtomicU64 > ,
14
- pub total_errors : Arc < AtomicU64 > ,
15
-
16
- pub old_total_xact_count : Arc < AtomicU64 > ,
17
- pub old_total_query_count : Arc < AtomicU64 > ,
18
- pub old_total_received : Arc < AtomicU64 > ,
19
- pub old_total_sent : Arc < AtomicU64 > ,
20
- pub old_total_xact_time : Arc < AtomicU64 > ,
21
- pub old_total_query_time : Arc < AtomicU64 > ,
22
- pub old_total_wait_time : Arc < AtomicU64 > ,
23
- pub old_total_errors : Arc < AtomicU64 > ,
24
-
25
- pub avg_query_count : Arc < AtomicU64 > ,
26
- pub avg_query_time : Arc < AtomicU64 > ,
27
- pub avg_recv : Arc < AtomicU64 > ,
28
- pub avg_sent : Arc < AtomicU64 > ,
29
- pub avg_errors : Arc < AtomicU64 > ,
30
- pub avg_xact_time : Arc < AtomicU64 > ,
31
- pub avg_xact_count : Arc < AtomicU64 > ,
32
- pub avg_wait_time : Arc < AtomicU64 > ,
19
+ total : AddressStatFields ,
20
+
21
+ current : AddressStatFields ,
22
+
23
+ averages : AddressStatFields ,
33
24
34
25
// Determines if the averages have been updated since the last time they were reported
35
26
pub averages_updated : Arc < AtomicBool > ,
@@ -43,133 +34,193 @@ impl IntoIterator for AddressStats {
43
34
vec ! [
44
35
(
45
36
"total_xact_count" . to_string( ) ,
46
- self . total_xact_count . load( Ordering :: Relaxed ) ,
37
+ self . total . xact_count . load( Ordering :: Relaxed ) ,
47
38
) ,
48
39
(
49
40
"total_query_count" . to_string( ) ,
50
- self . total_query_count . load( Ordering :: Relaxed ) ,
41
+ self . total . query_count . load( Ordering :: Relaxed ) ,
51
42
) ,
52
43
(
53
44
"total_received" . to_string( ) ,
54
- self . total_received . load( Ordering :: Relaxed ) ,
45
+ self . total . bytes_received . load( Ordering :: Relaxed ) ,
55
46
) ,
56
47
(
57
48
"total_sent" . to_string( ) ,
58
- self . total_sent . load( Ordering :: Relaxed ) ,
49
+ self . total . bytes_sent . load( Ordering :: Relaxed ) ,
59
50
) ,
60
51
(
61
52
"total_xact_time" . to_string( ) ,
62
- self . total_xact_time . load( Ordering :: Relaxed ) ,
53
+ self . total . xact_time . load( Ordering :: Relaxed ) ,
63
54
) ,
64
55
(
65
56
"total_query_time" . to_string( ) ,
66
- self . total_query_time . load( Ordering :: Relaxed ) ,
57
+ self . total . query_time . load( Ordering :: Relaxed ) ,
67
58
) ,
68
59
(
69
60
"total_wait_time" . to_string( ) ,
70
- self . total_wait_time . load( Ordering :: Relaxed ) ,
61
+ self . total . wait_time . load( Ordering :: Relaxed ) ,
71
62
) ,
72
63
(
73
64
"total_errors" . to_string( ) ,
74
- self . total_errors . load( Ordering :: Relaxed ) ,
65
+ self . total . errors . load( Ordering :: Relaxed ) ,
75
66
) ,
76
67
(
77
68
"avg_xact_count" . to_string( ) ,
78
- self . avg_xact_count . load( Ordering :: Relaxed ) ,
69
+ self . averages . xact_count . load( Ordering :: Relaxed ) ,
79
70
) ,
80
71
(
81
72
"avg_query_count" . to_string( ) ,
82
- self . avg_query_count . load( Ordering :: Relaxed ) ,
73
+ self . averages . query_count . load( Ordering :: Relaxed ) ,
83
74
) ,
84
75
(
85
76
"avg_recv" . to_string( ) ,
86
- self . avg_recv . load( Ordering :: Relaxed ) ,
77
+ self . averages . bytes_received . load( Ordering :: Relaxed ) ,
87
78
) ,
88
79
(
89
80
"avg_sent" . to_string( ) ,
90
- self . avg_sent . load( Ordering :: Relaxed ) ,
81
+ self . averages . bytes_sent . load( Ordering :: Relaxed ) ,
91
82
) ,
92
83
(
93
84
"avg_errors" . to_string( ) ,
94
- self . avg_errors . load( Ordering :: Relaxed ) ,
85
+ self . averages . errors . load( Ordering :: Relaxed ) ,
95
86
) ,
96
87
(
97
88
"avg_xact_time" . to_string( ) ,
98
- self . avg_xact_time . load( Ordering :: Relaxed ) ,
89
+ self . averages . xact_time . load( Ordering :: Relaxed ) ,
99
90
) ,
100
91
(
101
92
"avg_query_time" . to_string( ) ,
102
- self . avg_query_time . load( Ordering :: Relaxed ) ,
93
+ self . averages . query_time . load( Ordering :: Relaxed ) ,
103
94
) ,
104
95
(
105
96
"avg_wait_time" . to_string( ) ,
106
- self . avg_wait_time . load( Ordering :: Relaxed ) ,
97
+ self . averages . wait_time . load( Ordering :: Relaxed ) ,
107
98
) ,
108
99
]
109
100
. into_iter ( )
110
101
}
111
102
}
112
103
113
104
impl AddressStats {
105
+ pub fn xact_count_add ( & self ) {
106
+ self . total . xact_count . fetch_add ( 1 , Ordering :: Relaxed ) ;
107
+ self . current . xact_count . fetch_add ( 1 , Ordering :: Relaxed ) ;
108
+ }
109
+
110
+ pub fn query_count_add ( & self ) {
111
+ self . total . query_count . fetch_add ( 1 , Ordering :: Relaxed ) ;
112
+ self . current . query_count . fetch_add ( 1 , Ordering :: Relaxed ) ;
113
+ }
114
+
115
+ pub fn bytes_received_add ( & self , bytes : u64 ) {
116
+ self . total
117
+ . bytes_received
118
+ . fetch_add ( bytes, Ordering :: Relaxed ) ;
119
+ self . current
120
+ . bytes_received
121
+ . fetch_add ( bytes, Ordering :: Relaxed ) ;
122
+ }
123
+
124
+ pub fn bytes_sent_add ( & self , bytes : u64 ) {
125
+ self . total . bytes_sent . fetch_add ( bytes, Ordering :: Relaxed ) ;
126
+ self . current . bytes_sent . fetch_add ( bytes, Ordering :: Relaxed ) ;
127
+ }
128
+
129
+ pub fn xact_time_add ( & self , time : u64 ) {
130
+ self . total . xact_time . fetch_add ( time, Ordering :: Relaxed ) ;
131
+ self . current . xact_time . fetch_add ( time, Ordering :: Relaxed ) ;
132
+ }
133
+
134
+ pub fn query_time_add ( & self , time : u64 ) {
135
+ self . total . query_time . fetch_add ( time, Ordering :: Relaxed ) ;
136
+ self . current . query_time . fetch_add ( time, Ordering :: Relaxed ) ;
137
+ }
138
+
139
+ pub fn wait_time_add ( & self , time : u64 ) {
140
+ self . total . wait_time . fetch_add ( time, Ordering :: Relaxed ) ;
141
+ self . current . wait_time . fetch_add ( time, Ordering :: Relaxed ) ;
142
+ }
143
+
114
144
pub fn error ( & self ) {
115
- self . total_errors . fetch_add ( 1 , Ordering :: Relaxed ) ;
145
+ self . total . errors . fetch_add ( 1 , Ordering :: Relaxed ) ;
146
+ self . current . errors . fetch_add ( 1 , Ordering :: Relaxed ) ;
116
147
}
117
148
118
149
pub fn update_averages ( & self ) {
119
- let ( totals, averages, old_totals) = self . fields_iterators ( ) ;
120
- for ( total, average, old_total) in itertools:: izip!( totals, averages, old_totals) {
121
- let total_value = total. load ( Ordering :: Relaxed ) ;
122
- let old_total_value = old_total. load ( Ordering :: Relaxed ) ;
123
- average. store (
124
- ( total_value - old_total_value) / ( crate :: stats:: STAT_PERIOD / 1_000 ) ,
125
- Ordering :: Relaxed ,
126
- ) ; // Avg / second
127
- old_total. store ( total_value, Ordering :: Relaxed ) ;
150
+ let stat_period_per_second = crate :: stats:: STAT_PERIOD / 1_000 ;
151
+
152
+ // xact_count
153
+ let current_xact_count = self . current . xact_count . load ( Ordering :: Relaxed ) ;
154
+ let current_xact_time = self . current . xact_time . load ( Ordering :: Relaxed ) ;
155
+ self . averages . xact_count . store (
156
+ current_xact_count / stat_period_per_second,
157
+ Ordering :: Relaxed ,
158
+ ) ;
159
+ if current_xact_count == 0 {
160
+ self . averages . xact_time . store ( 0 , Ordering :: Relaxed ) ;
161
+ } else {
162
+ self . averages
163
+ . xact_time
164
+ . store ( current_xact_time / current_xact_count, Ordering :: Relaxed ) ;
165
+ }
166
+
167
+ // query_count
168
+ let current_query_count = self . current . query_count . load ( Ordering :: Relaxed ) ;
169
+ let current_query_time = self . current . query_time . load ( Ordering :: Relaxed ) ;
170
+ self . averages . query_count . store (
171
+ current_query_count / stat_period_per_second,
172
+ Ordering :: Relaxed ,
173
+ ) ;
174
+ if current_query_count == 0 {
175
+ self . averages . query_time . store ( 0 , Ordering :: Relaxed ) ;
176
+ } else {
177
+ self . averages
178
+ . query_time
179
+ . store ( current_query_time / current_query_count, Ordering :: Relaxed ) ;
128
180
}
181
+
182
+ // bytes_received
183
+ let current_bytes_received = self . current . bytes_received . load ( Ordering :: Relaxed ) ;
184
+ self . averages . bytes_received . store (
185
+ current_bytes_received / stat_period_per_second,
186
+ Ordering :: Relaxed ,
187
+ ) ;
188
+
189
+ // bytes_sent
190
+ let current_bytes_sent = self . current . bytes_sent . load ( Ordering :: Relaxed ) ;
191
+ self . averages . bytes_sent . store (
192
+ current_bytes_sent / stat_period_per_second,
193
+ Ordering :: Relaxed ,
194
+ ) ;
195
+
196
+ // wait_time
197
+ let current_wait_time = self . current . wait_time . load ( Ordering :: Relaxed ) ;
198
+ self . averages . wait_time . store (
199
+ current_wait_time / stat_period_per_second,
200
+ Ordering :: Relaxed ,
201
+ ) ;
202
+
203
+ // errors
204
+ let current_errors = self . current . errors . load ( Ordering :: Relaxed ) ;
205
+ self . averages
206
+ . errors
207
+ . store ( current_errors / stat_period_per_second, Ordering :: Relaxed ) ;
208
+ }
209
+
210
+ pub fn reset_current_counts ( & self ) {
211
+ self . current . xact_count . store ( 0 , Ordering :: Relaxed ) ;
212
+ self . current . xact_time . store ( 0 , Ordering :: Relaxed ) ;
213
+ self . current . query_count . store ( 0 , Ordering :: Relaxed ) ;
214
+ self . current . query_time . store ( 0 , Ordering :: Relaxed ) ;
215
+ self . current . bytes_received . store ( 0 , Ordering :: Relaxed ) ;
216
+ self . current . bytes_sent . store ( 0 , Ordering :: Relaxed ) ;
217
+ self . current . wait_time . store ( 0 , Ordering :: Relaxed ) ;
218
+ self . current . errors . store ( 0 , Ordering :: Relaxed ) ;
129
219
}
130
220
131
221
pub fn populate_row ( & self , row : & mut Vec < String > ) {
132
222
for ( _key, value) in self . clone ( ) {
133
223
row. push ( value. to_string ( ) ) ;
134
224
}
135
225
}
136
-
137
- fn fields_iterators (
138
- & self ,
139
- ) -> (
140
- Vec < Arc < AtomicU64 > > ,
141
- Vec < Arc < AtomicU64 > > ,
142
- Vec < Arc < AtomicU64 > > ,
143
- ) {
144
- let mut totals: Vec < Arc < AtomicU64 > > = Vec :: new ( ) ;
145
- let mut averages: Vec < Arc < AtomicU64 > > = Vec :: new ( ) ;
146
- let mut old_totals: Vec < Arc < AtomicU64 > > = Vec :: new ( ) ;
147
-
148
- totals. push ( self . total_xact_count . clone ( ) ) ;
149
- old_totals. push ( self . old_total_xact_count . clone ( ) ) ;
150
- averages. push ( self . avg_xact_count . clone ( ) ) ;
151
- totals. push ( self . total_query_count . clone ( ) ) ;
152
- old_totals. push ( self . old_total_query_count . clone ( ) ) ;
153
- averages. push ( self . avg_query_count . clone ( ) ) ;
154
- totals. push ( self . total_received . clone ( ) ) ;
155
- old_totals. push ( self . old_total_received . clone ( ) ) ;
156
- averages. push ( self . avg_recv . clone ( ) ) ;
157
- totals. push ( self . total_sent . clone ( ) ) ;
158
- old_totals. push ( self . old_total_sent . clone ( ) ) ;
159
- averages. push ( self . avg_sent . clone ( ) ) ;
160
- totals. push ( self . total_xact_time . clone ( ) ) ;
161
- old_totals. push ( self . old_total_xact_time . clone ( ) ) ;
162
- averages. push ( self . avg_xact_time . clone ( ) ) ;
163
- totals. push ( self . total_query_time . clone ( ) ) ;
164
- old_totals. push ( self . old_total_query_time . clone ( ) ) ;
165
- averages. push ( self . avg_query_time . clone ( ) ) ;
166
- totals. push ( self . total_wait_time . clone ( ) ) ;
167
- old_totals. push ( self . old_total_wait_time . clone ( ) ) ;
168
- averages. push ( self . avg_wait_time . clone ( ) ) ;
169
- totals. push ( self . total_errors . clone ( ) ) ;
170
- old_totals. push ( self . old_total_errors . clone ( ) ) ;
171
- averages. push ( self . avg_errors . clone ( ) ) ;
172
-
173
- ( totals, averages, old_totals)
174
- }
175
226
}
0 commit comments