snarkos_node_tcp/helpers/stats.rs
1// Copyright (c) 2019-2025 Provable Inc.
2// This file is part of the snarkOS library.
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#[cfg(feature = "locktick")]
17use locktick::parking_lot::RwLock;
18#[cfg(not(feature = "locktick"))]
19use parking_lot::RwLock;
20use std::{
21 sync::atomic::{AtomicU64, Ordering::Relaxed},
22 time::Instant,
23};
24
25/// Contains statistics related to Tcp.
26pub struct Stats {
27 /// The timestamp of the creation (for the node) or connection (to a peer).
28 pub(crate) timestamp: RwLock<Instant>,
29 /// The number of all messages sent.
30 msgs_sent: AtomicU64,
31 /// The number of all messages received.
32 msgs_received: AtomicU64,
33 /// The number of all bytes sent.
34 bytes_sent: AtomicU64,
35 /// The number of all bytes received.
36 bytes_received: AtomicU64,
37 /// The number of failures.
38 failures: AtomicU64,
39}
40
41impl Stats {
42 /// Creates a new instance of the object.
43 pub fn new(timestamp: Instant) -> Self {
44 Self {
45 timestamp: RwLock::new(timestamp),
46 msgs_sent: Default::default(),
47 msgs_received: Default::default(),
48 bytes_sent: Default::default(),
49 bytes_received: Default::default(),
50 failures: Default::default(),
51 }
52 }
53
54 /// Returns the creation or connection timestamp.
55 pub fn timestamp(&self) -> Instant {
56 *self.timestamp.read()
57 }
58
59 /// Returns the number of sent messages and their collective size in bytes.
60 pub fn sent(&self) -> (u64, u64) {
61 let msgs = self.msgs_sent.load(Relaxed);
62 let bytes = self.bytes_sent.load(Relaxed);
63
64 (msgs, bytes)
65 }
66
67 /// Returns the number of received messages and their collective size in bytes.
68 pub fn received(&self) -> (u64, u64) {
69 let msgs = self.msgs_received.load(Relaxed);
70 let bytes = self.bytes_received.load(Relaxed);
71
72 (msgs, bytes)
73 }
74
75 /// Returns the number of failures.
76 pub fn failures(&self) -> u64 {
77 self.failures.load(Relaxed)
78 }
79
80 /// Registers a sent message of the provided `size` in bytes.
81 pub fn register_sent_message(&self, size: usize) {
82 self.msgs_sent.fetch_add(1, Relaxed);
83 self.bytes_sent.fetch_add(size as u64, Relaxed);
84 }
85
86 /// Registers a received message of the provided `size` in bytes.
87 pub fn register_received_message(&self, size: usize) {
88 self.msgs_received.fetch_add(1, Relaxed);
89 self.bytes_received.fetch_add(size as u64, Relaxed);
90 }
91
92 /// Registers a failure.
93 pub fn register_failure(&self) {
94 self.failures.fetch_add(1, Relaxed);
95 }
96}