zbus/fdo/
stats.rs

1//! D-Bus standard interfaces.
2//!
3//! The D-Bus specification defines the message bus messages and some standard interfaces that may
4//! be useful across various D-Bus applications. This module provides their proxy.
5
6use serde::{Deserialize, Serialize};
7use std::collections::HashMap;
8use zbus_names::{BusName, OwnedUniqueName};
9use zvariant::{as_value::optional, OwnedValue, Type};
10
11use super::Result;
12use crate::proxy;
13
14/// Proxy for the [`org.freedesktop.DBus.Debug.Stats`][link] interface.
15///
16/// [link]: https://dbus.freedesktop.org/doc/dbus-specification.html#message-bus-debug-stats-interface
17#[proxy(
18    interface = "org.freedesktop.DBus.Debug.Stats",
19    default_service = "org.freedesktop.DBus",
20    default_path = "/org/freedesktop/DBus"
21)]
22pub trait Stats {
23    /// Get statistics about the message bus itself.
24    fn get_stats(&self) -> Result<Stats>;
25
26    /// Get statistics about a connection, identified by its unique connection name or by any
27    /// well-known bus name for which it is the primary owner. This method is not meaningful for
28    /// the message bus `org.freedesktop.DBus` itself.
29    fn get_connection_stats(&self, name: BusName<'_>) -> Result<ConnectionStats>;
30
31    /// List all of the match rules that are active on this message bus. The keys in the result
32    /// dictionary are unique connection names. The values are lists of match rules registered by
33    /// that connection, in an unspecified order. If a connection has registered the same match rule
34    /// more than once, it is unspecified whether duplicate entries appear in the list.
35    fn get_all_match_rules(&self) -> Result<HashMap<OwnedUniqueName, Vec<crate::OwnedMatchRule>>>;
36}
37
38/// The stats returned by the [`StatsProxy::get_stats`] method.
39#[derive(Debug, Default, Deserialize, PartialEq, Serialize, Type)]
40#[zvariant(signature = "a{sv}", rename_all = "PascalCase")]
41#[serde(default, rename_all = "PascalCase")]
42pub struct Stats {
43    #[serde(with = "optional", skip_serializing_if = "Option::is_none")]
44    pub(crate) serial: Option<u32>,
45    #[serde(with = "optional", skip_serializing_if = "Option::is_none")]
46    pub(crate) active_connections: Option<u32>,
47    #[serde(with = "optional", skip_serializing_if = "Option::is_none")]
48    pub(crate) incomplete_connections: Option<u32>,
49    #[serde(with = "optional", skip_serializing_if = "Option::is_none")]
50    pub(crate) match_rules: Option<u32>,
51    #[serde(with = "optional", skip_serializing_if = "Option::is_none")]
52    pub(crate) peak_match_rules: Option<u32>,
53    #[serde(with = "optional", skip_serializing_if = "Option::is_none")]
54    pub(crate) peak_match_rules_per_connection: Option<u32>,
55    #[serde(with = "optional", skip_serializing_if = "Option::is_none")]
56    pub(crate) bus_names: Option<u32>,
57    #[serde(with = "optional", skip_serializing_if = "Option::is_none")]
58    pub(crate) peak_bus_names: Option<u32>,
59    #[serde(with = "optional", skip_serializing_if = "Option::is_none")]
60    pub(crate) peak_bus_names_per_connection: Option<u32>,
61    #[serde(flatten)]
62    pub(crate) rest: HashMap<String, OwnedValue>,
63}
64
65impl Stats {
66    /// A serial number which is incremented with each call to the GetStats method.
67    pub fn serial(&self) -> Option<u32> {
68        self.serial
69    }
70
71    /// The number of active connections currently handled by this message bus. The exact meaning of
72    /// an active connection is implementation-defined: in the reference dbus-daemon, a connection
73    /// is considered to be active after it has successfully called the Hello method.
74    pub fn active_connections(&self) -> Option<u32> {
75        self.active_connections
76    }
77
78    /// The number of incomplete connections currently handled by this message bus. The exact
79    /// meaning of an incomplete connection is implementation-defined: in the reference dbus-daemon,
80    /// a connection is considered to be incomplete if it is still carrying out the SASL handshake
81    /// or if it has not yet successfully called the `Hello` method.
82    pub fn incomplete_connections(&self) -> Option<u32> {
83        self.incomplete_connections
84    }
85
86    /// The total number of match rules that are currently in use.
87    pub fn match_rules(&self) -> Option<u32> {
88        self.match_rules
89    }
90
91    /// The largest total number of match rules that have been in use at any one time.
92    pub fn peak_match_rules(&self) -> Option<u32> {
93        self.peak_match_rules
94    }
95
96    /// The largest number of match rules that have been in use by a single connection at any one
97    /// time.
98    pub fn peak_match_rules_per_connection(&self) -> Option<u32> {
99        self.peak_match_rules_per_connection
100    }
101
102    /// The total number of bus names that are currently in use.
103    pub fn bus_names(&self) -> Option<u32> {
104        self.bus_names
105    }
106
107    /// The largest total number of bus names that have been in use at any one time.
108    pub fn peak_bus_names(&self) -> Option<u32> {
109        self.peak_bus_names
110    }
111
112    /// The largest number of bus names that have been in use by a single connection at any one
113    /// time.
114    pub fn peak_bus_names_per_connection(&self) -> Option<u32> {
115        self.peak_bus_names_per_connection
116    }
117
118    /// The rest of the statistics, that are not defined by the D-Bus specificiation and hence
119    /// specific to individual D-Bus broker implementations.
120    pub fn rest(&self) -> &HashMap<String, OwnedValue> {
121        &self.rest
122    }
123}
124
125/// The stats returned by the [`StatsProxy::get_connection_stats`] method.
126#[derive(Debug, Default, Deserialize, PartialEq, Serialize, Type)]
127#[zvariant(signature = "a{sv}")]
128#[serde(default, rename_all = "PascalCase")]
129pub struct ConnectionStats {
130    #[serde(with = "optional", skip_serializing_if = "Option::is_none")]
131    pub(crate) serial: Option<u32>,
132    #[serde(with = "optional", skip_serializing_if = "Option::is_none")]
133    pub(crate) unique_name: Option<OwnedUniqueName>,
134    #[serde(with = "optional", skip_serializing_if = "Option::is_none")]
135    pub(crate) match_rules: Option<u32>,
136    #[serde(with = "optional", skip_serializing_if = "Option::is_none")]
137    pub(crate) peak_match_rules: Option<u32>,
138    #[serde(with = "optional", skip_serializing_if = "Option::is_none")]
139    pub(crate) bus_names: Option<u32>,
140    #[serde(with = "optional", skip_serializing_if = "Option::is_none")]
141    pub(crate) peak_bus_names: Option<u32>,
142    #[serde(with = "optional", skip_serializing_if = "Option::is_none")]
143    pub(crate) incoming_messages: Option<u32>,
144    #[serde(with = "optional", skip_serializing_if = "Option::is_none")]
145    pub(crate) outgoing_messages: Option<u32>,
146    #[serde(with = "optional", skip_serializing_if = "Option::is_none")]
147    pub(crate) incoming_bytes: Option<u32>,
148    #[serde(with = "optional", skip_serializing_if = "Option::is_none")]
149    pub(crate) outgoing_bytes: Option<u32>,
150    #[serde(with = "optional", skip_serializing_if = "Option::is_none")]
151    pub(crate) incoming_fds: Option<u32>,
152    #[serde(with = "optional", skip_serializing_if = "Option::is_none")]
153    pub(crate) outgoing_fds: Option<u32>,
154    #[serde(with = "optional", skip_serializing_if = "Option::is_none")]
155    pub(crate) peak_incoming_messages: Option<u32>,
156    #[serde(with = "optional", skip_serializing_if = "Option::is_none")]
157    pub(crate) peak_outgoing_messages: Option<u32>,
158    #[serde(with = "optional", skip_serializing_if = "Option::is_none")]
159    pub(crate) peak_incoming_bytes: Option<u32>,
160    #[serde(with = "optional", skip_serializing_if = "Option::is_none")]
161    pub(crate) peak_outgoing_bytes: Option<u32>,
162    #[serde(with = "optional", skip_serializing_if = "Option::is_none")]
163    pub(crate) peak_incoming_fds: Option<u32>,
164    #[serde(with = "optional", skip_serializing_if = "Option::is_none")]
165    pub(crate) peak_outgoing_fds: Option<u32>,
166    #[serde(flatten)]
167    pub(crate) rest: HashMap<String, OwnedValue>,
168}
169
170impl ConnectionStats {
171    /// A serial number which is incremented with each call to the GetConnectionStats method.
172    pub fn serial(&self) -> Option<u32> {
173        self.serial
174    }
175
176    /// The unique name of the connection.
177    pub fn unique_name(&self) -> Option<&OwnedUniqueName> {
178        self.unique_name.as_ref()
179    }
180
181    /// The total number of match rules that are currently in use by this connection.
182    pub fn match_rules(&self) -> Option<u32> {
183        self.match_rules
184    }
185
186    /// The largest total number of match rules that have been in use by this connection at any one
187    /// time.
188    pub fn peak_match_rules(&self) -> Option<u32> {
189        self.peak_match_rules
190    }
191
192    /// The total number of bus names that are currently in use by this connection.
193    pub fn bus_names(&self) -> Option<u32> {
194        self.bus_names
195    }
196
197    /// The largest total number of bus names that have been in use by this connection at any one
198    /// time.
199    pub fn peak_bus_names(&self) -> Option<u32> {
200        self.peak_bus_names
201    }
202
203    /// The total number of messages received by this connection.
204    pub fn incoming_messages(&self) -> Option<u32> {
205        self.incoming_messages
206    }
207
208    /// The total number of messages sent by this connection.
209    pub fn outgoing_messages(&self) -> Option<u32> {
210        self.outgoing_messages
211    }
212
213    /// The total number of bytes received by this connection.
214    pub fn incoming_bytes(&self) -> Option<u32> {
215        self.incoming_bytes
216    }
217
218    /// The total number of bytes sent by this connection.
219    pub fn outgoing_bytes(&self) -> Option<u32> {
220        self.outgoing_bytes
221    }
222
223    /// The total number of file descriptors received by this connection.
224    pub fn incoming_fds(&self) -> Option<u32> {
225        self.incoming_fds
226    }
227
228    /// The total number of file descriptors sent by this connection.
229    pub fn outgoing_fds(&self) -> Option<u32> {
230        self.outgoing_fds
231    }
232
233    /// The largest total number of messages that have been in use by this connection at any one
234    /// time.
235    pub fn peak_incoming_messages(&self) -> Option<u32> {
236        self.peak_incoming_messages
237    }
238
239    /// The largest total number of messages that have been in use by this connection at any one
240    /// time.
241    pub fn peak_outgoing_messages(&self) -> Option<u32> {
242        self.peak_outgoing_messages
243    }
244
245    /// The largest total number of bytes that have been in use by this connection at any one time.
246    pub fn peak_incoming_bytes(&self) -> Option<u32> {
247        self.peak_incoming_bytes
248    }
249
250    /// The largest total number of bytes that have been in use by this connection at any one time.
251    pub fn peak_outgoing_bytes(&self) -> Option<u32> {
252        self.peak_outgoing_bytes
253    }
254
255    /// The largest total number of file descriptors that have been in use by this connection at any
256    /// one time.
257    pub fn peak_incoming_fds(&self) -> Option<u32> {
258        self.peak_incoming_fds
259    }
260
261    /// The largest total number of file descriptors that have been in use by this connection at any
262    /// one time.
263    pub fn peak_outgoing_fds(&self) -> Option<u32> {
264        self.peak_outgoing_fds
265    }
266
267    /// The rest of the statistics, that are not defined by the D-Bus specificiation and hence
268    /// specific to individual D-Bus broker implementations.
269    pub fn rest(&self) -> &HashMap<String, OwnedValue> {
270        &self.rest
271    }
272}