@@ -88,50 +88,22 @@ use serde::{Serialize, Serializer};
8888
8989use crate :: logger:: { IncMetric , LatencyAggregateMetrics , SharedIncMetric } ;
9090
91- /// map of network interface id and metrics
92- /// this should be protected by a lock before accessing.
93- #[ derive( Debug ) ]
94- pub struct NetMetricsPerDevice {
95- /// used to access per net device metrics
96- pub metrics : BTreeMap < String , Arc < NetDeviceMetrics > > ,
97- }
98-
99- impl NetMetricsPerDevice {
100- /// Allocate `NetDeviceMetrics` for net device having
101- /// id `iface_id`. Also, allocate only if it doesn't
102- /// exist to avoid overwriting previously allocated data.
103- /// lock is always initialized so it is safe the unwrap
104- /// the lock without a check.
105- pub fn alloc ( iface_id : String ) -> Arc < NetDeviceMetrics > {
106- Arc :: clone (
107- METRICS
108- . write ( )
109- . unwrap ( )
110- . metrics
111- . entry ( iface_id)
112- . or_insert_with ( || Arc :: new ( NetDeviceMetrics :: default ( ) ) ) ,
113- )
114- }
115- }
116-
11791/// Pool of Network-related metrics per device behind a lock to
11892/// keep things thread safe. Since the lock is initialized here
11993/// it is safe to unwrap it without any check.
120- static METRICS : RwLock < NetMetricsPerDevice > = RwLock :: new ( NetMetricsPerDevice {
121- metrics : BTreeMap :: new ( ) ,
122- } ) ;
94+ pub static METRICS : RwLock < BTreeMap < String , Arc < NetDeviceMetrics > > > = RwLock :: new ( BTreeMap :: new ( ) ) ;
12395
12496/// This function facilitates aggregation and serialization of
12597/// per net device metrics.
12698pub fn flush_metrics < S : Serializer > ( serializer : S ) -> Result < S :: Ok , S :: Error > {
12799 let net_metrics = METRICS . read ( ) . unwrap ( ) ;
128- let metrics_len = net_metrics. metrics . len ( ) ;
100+ let metrics_len = net_metrics. len ( ) ;
129101 // +1 to accomodate aggregate net metrics
130102 let mut seq = serializer. serialize_map ( Some ( 1 + metrics_len) ) ?;
131103
132104 let mut net_aggregated: NetDeviceMetrics = NetDeviceMetrics :: default ( ) ;
133105
134- for ( name, metrics) in net_metrics. metrics . iter ( ) {
106+ for ( name, metrics) in net_metrics. iter ( ) {
135107 let devn = format ! ( "net_{}" , name) ;
136108 // serialization will flush the metrics so aggregate before it.
137109 let m: & NetDeviceMetrics = metrics;
@@ -280,27 +252,29 @@ pub mod tests {
280252
281253 for i in 0 ..MAX_NET_DEVICES {
282254 let devn: String = format ! ( "eth{}" , i) ;
283- NetMetricsPerDevice :: alloc ( devn. clone ( ) ) ;
255+
256+ METRICS
257+ . write ( )
258+ . unwrap ( )
259+ . insert ( devn. to_string ( ) , Arc :: new ( NetDeviceMetrics :: default ( ) ) ) ;
260+
284261 METRICS
285262 . read ( )
286263 . unwrap ( )
287- . metrics
288264 . get ( & devn)
289265 . unwrap ( )
290266 . activate_fails
291267 . inc ( ) ;
292268 METRICS
293269 . read ( )
294270 . unwrap ( )
295- . metrics
296271 . get ( & devn)
297272 . unwrap ( )
298273 . rx_bytes_count
299274 . add ( 10 ) ;
300275 METRICS
301276 . read ( )
302277 . unwrap ( )
303- . metrics
304278 . get ( & devn)
305279 . unwrap ( )
306280 . tx_bytes_count
@@ -313,7 +287,6 @@ pub mod tests {
313287 METRICS
314288 . read( )
315289 . unwrap( )
316- . metrics
317290 . get( & devn)
318291 . unwrap( )
319292 . activate_fails
@@ -324,7 +297,6 @@ pub mod tests {
324297 METRICS
325298 . read( )
326299 . unwrap( )
327- . metrics
328300 . get( & devn)
329301 . unwrap( )
330302 . rx_bytes_count
@@ -335,7 +307,6 @@ pub mod tests {
335307 METRICS
336308 . read( )
337309 . unwrap( )
338- . metrics
339310 . get( & devn)
340311 . unwrap( )
341312 . tx_bytes_count
@@ -353,13 +324,14 @@ pub mod tests {
353324 drop ( METRICS . read ( ) . unwrap ( ) ) ;
354325 drop ( METRICS . write ( ) . unwrap ( ) ) ;
355326
356- NetMetricsPerDevice :: alloc ( String :: from ( devn) ) ;
357- METRICS . read ( ) . unwrap ( ) . metrics . get ( devn) . unwrap ( ) ;
327+ METRICS
328+ . write ( )
329+ . unwrap ( )
330+ . insert ( devn. to_string ( ) , Arc :: new ( NetDeviceMetrics :: default ( ) ) ) ;
358331
359332 METRICS
360333 . read ( )
361334 . unwrap ( )
362- . metrics
363335 . get ( devn)
364336 . unwrap ( )
365337 . activate_fails
@@ -368,7 +340,6 @@ pub mod tests {
368340 METRICS
369341 . read( )
370342 . unwrap( )
371- . metrics
372343 . get( devn)
373344 . unwrap( )
374345 . activate_fails
@@ -378,7 +349,6 @@ pub mod tests {
378349 METRICS
379350 . read( )
380351 . unwrap( )
381- . metrics
382352 . get( devn)
383353 . unwrap( )
384354 . activate_fails
@@ -390,7 +360,6 @@ pub mod tests {
390360 METRICS
391361 . read( )
392362 . unwrap( )
393- . metrics
394363 . get( devn)
395364 . unwrap( )
396365 . activate_fails
@@ -400,7 +369,6 @@ pub mod tests {
400369 METRICS
401370 . read( )
402371 . unwrap( )
403- . metrics
404372 . get( devn)
405373 . unwrap( )
406374 . activate_fails
@@ -410,15 +378,13 @@ pub mod tests {
410378 METRICS
411379 . read ( )
412380 . unwrap ( )
413- . metrics
414381 . get ( devn)
415382 . unwrap ( )
416383 . activate_fails
417384 . inc ( ) ;
418385 METRICS
419386 . read ( )
420387 . unwrap ( )
421- . metrics
422388 . get ( devn)
423389 . unwrap ( )
424390 . rx_bytes_count
@@ -427,7 +393,6 @@ pub mod tests {
427393 METRICS
428394 . read( )
429395 . unwrap( )
430- . metrics
431396 . get( devn)
432397 . unwrap( )
433398 . rx_bytes_count
0 commit comments