api,bearer: add 'total-rx-bytes', 'total-tx-bytes' and 'total-duration' stats

Compiling the amount of bytes transferred and received during all
tracked connection attempts, as well as the total duration of all the
connections.
This commit is contained in:
Aleksander Morgado
2020-03-27 18:01:36 +01:00
parent f325ef5902
commit 79daa3099e
8 changed files with 248 additions and 11 deletions

View File

@@ -250,6 +250,9 @@ print_bearer_info (MMBearer *bearer)
gchar *bytes_tx = NULL;
gchar *attempts = NULL;
gchar *failed_attempts = NULL;
gchar *total_duration = NULL;
gchar *total_bytes_rx = NULL;
gchar *total_bytes_tx = NULL;
if (stats) {
guint64 val;
@@ -269,6 +272,15 @@ print_bearer_info (MMBearer *bearer)
val = mm_bearer_stats_get_failed_attempts (stats);
if (val)
failed_attempts = g_strdup_printf ("%" G_GUINT64_FORMAT, val);
val = mm_bearer_stats_get_total_duration (stats);
if (val)
total_duration = g_strdup_printf ("%" G_GUINT64_FORMAT, val);
val = mm_bearer_stats_get_total_rx_bytes (stats);
if (val)
total_bytes_rx = g_strdup_printf ("%" G_GUINT64_FORMAT, val);
val = mm_bearer_stats_get_total_tx_bytes (stats);
if (val)
total_bytes_tx = g_strdup_printf ("%" G_GUINT64_FORMAT, val);
}
mmcli_output_string_take (MMC_F_BEARER_STATS_DURATION, duration);
@@ -276,6 +288,9 @@ print_bearer_info (MMBearer *bearer)
mmcli_output_string_take (MMC_F_BEARER_STATS_BYTES_TX, bytes_tx);
mmcli_output_string_take (MMC_F_BEARER_STATS_ATTEMPTS, attempts);
mmcli_output_string_take (MMC_F_BEARER_STATS_FAILED_ATTEMPTS, failed_attempts);
mmcli_output_string_take (MMC_F_BEARER_STATS_TOTAL_DURATION, total_duration);
mmcli_output_string_take (MMC_F_BEARER_STATS_TOTAL_BYTES_RX, total_bytes_rx);
mmcli_output_string_take (MMC_F_BEARER_STATS_TOTAL_BYTES_TX, total_bytes_tx);
}
mmcli_output_dump ();

View File

@@ -228,6 +228,9 @@ static FieldInfo field_infos[] = {
[MMC_F_BEARER_STATS_BYTES_TX] = { "bearer.stats.bytes-tx", "bytes tx", MMC_S_BEARER_STATS, },
[MMC_F_BEARER_STATS_ATTEMPTS] = { "bearer.stats.attempts", "attempts", MMC_S_BEARER_STATS, },
[MMC_F_BEARER_STATS_FAILED_ATTEMPTS] = { "bearer.stats.failed-attempts", "attempts", MMC_S_BEARER_STATS, },
[MMC_F_BEARER_STATS_TOTAL_DURATION] = { "bearer.stats.total-duration", "total-duration", MMC_S_BEARER_STATS, },
[MMC_F_BEARER_STATS_TOTAL_BYTES_RX] = { "bearer.stats.total-bytes-rx", "total-bytes rx", MMC_S_BEARER_STATS, },
[MMC_F_BEARER_STATS_TOTAL_BYTES_TX] = { "bearer.stats.total-bytes-tx", "total-bytes tx", MMC_S_BEARER_STATS, },
[MMC_F_CALL_GENERAL_DBUS_PATH] = { "call.dbus-path", "dbus path", MMC_S_CALL_GENERAL, },
[MMC_F_CALL_PROPERTIES_NUMBER] = { "call.properties.number", "number", MMC_S_CALL_PROPERTIES, },
[MMC_F_CALL_PROPERTIES_DIRECTION] = { "call.properties.direction", "direction", MMC_S_CALL_PROPERTIES, },

View File

@@ -245,6 +245,9 @@ typedef enum {
MMC_F_BEARER_STATS_BYTES_TX,
MMC_F_BEARER_STATS_ATTEMPTS,
MMC_F_BEARER_STATS_FAILED_ATTEMPTS,
MMC_F_BEARER_STATS_TOTAL_DURATION,
MMC_F_BEARER_STATS_TOTAL_BYTES_RX,
MMC_F_BEARER_STATS_TOTAL_BYTES_TX,
MMC_F_CALL_GENERAL_DBUS_PATH,
MMC_F_CALL_PROPERTIES_NUMBER,
MMC_F_CALL_PROPERTIES_DIRECTION,

View File

@@ -1130,6 +1130,9 @@ mm_bearer_stats_get_rx_bytes
mm_bearer_stats_get_tx_bytes
mm_bearer_stats_get_attempts
mm_bearer_stats_get_failed_attempts
mm_bearer_stats_get_total_duration
mm_bearer_stats_get_total_rx_bytes
mm_bearer_stats_get_total_tx_bytes
<SUBSECTION Private>
mm_bearer_stats_get_dictionary
mm_bearer_stats_new
@@ -1139,6 +1142,9 @@ mm_bearer_stats_set_rx_bytes
mm_bearer_stats_set_tx_bytes
mm_bearer_stats_set_attempts
mm_bearer_stats_set_failed_attempts
mm_bearer_stats_set_total_duration
mm_bearer_stats_set_total_rx_bytes
mm_bearer_stats_set_total_tx_bytes
<SUBSECTION Standard>
MMBearerStatsClass
MMBearerStatsPrivate

View File

@@ -266,8 +266,8 @@
</varlistentry>
<varlistentry><term><literal>"tx-bytes"</literal></term>
<listitem>
Number bytes transmitted without error in the ongoing connection,
given as an unsigned 64-bit integer value (signature
Number of bytes transmitted without error in the ongoing
connection, given as an unsigned 64-bit integer value (signature
<literal>"t"</literal>).
</listitem>
</varlistentry>
@@ -290,6 +290,27 @@
<literal>"u"</literal>).
</listitem>
</varlistentry>
<varlistentry><term><literal>"total-rx-bytes"</literal></term>
<listitem>
Total number of bytes received without error in all the successful
connection establishments, given as an unsigned 64-bit integer
value (signature <literal>"t"</literal>).
</listitem>
</varlistentry>
<varlistentry><term><literal>"total-tx-bytes"</literal></term>
<listitem>
Total number of bytes transmitted without error in all the
successful connection establishments, given as an unsigned 64-bit
integer value (signature <literal>"t"</literal>).
</listitem>
</varlistentry>
<varlistentry><term><literal>"total-duration"</literal></term>
<listitem>
Total duration of all the successful connection establishments, in
seconds, given as an unsigned integer value (signature
<literal>"u"</literal>).
</listitem>
</varlistentry>
</variablelist>
-->
<property name="Stats" type="a{sv}" access="read" />

View File

@@ -38,6 +38,9 @@ G_DEFINE_TYPE (MMBearerStats, mm_bearer_stats, G_TYPE_OBJECT)
#define PROPERTY_TX_BYTES "tx-bytes"
#define PROPERTY_ATTEMPTS "attempts"
#define PROPERTY_FAILED_ATTEMPTS "failed-attempts"
#define PROPERTY_TOTAL_DURATION "total-duration"
#define PROPERTY_TOTAL_RX_BYTES "total-rx-bytes"
#define PROPERTY_TOTAL_TX_BYTES "total-tx-bytes"
struct _MMBearerStatsPrivate {
guint duration;
@@ -45,6 +48,9 @@ struct _MMBearerStatsPrivate {
guint64 tx_bytes;
guint attempts;
guint failed_attempts;
guint total_duration;
guint64 total_rx_bytes;
guint64 total_tx_bytes;
};
/*****************************************************************************/
@@ -209,6 +215,104 @@ mm_bearer_stats_set_failed_attempts (MMBearerStats *self,
/*****************************************************************************/
/**
* mm_bearer_stats_get_total_duration:
* @self: a #MMBearerStats.
*
* Gets the total duration of all the connections of this bearer.
*
* Returns: a #guint.
*
* Since: 1.14
*/
guint
mm_bearer_stats_get_total_duration (MMBearerStats *self)
{
g_return_val_if_fail (MM_IS_BEARER_STATS (self), 0);
return self->priv->total_duration;
}
/**
* mm_bearer_stats_set_total_duration: (skip)
*/
void
mm_bearer_stats_set_total_duration (MMBearerStats *self,
guint total_duration)
{
g_return_if_fail (MM_IS_BEARER_STATS (self));
self->priv->total_duration = total_duration;
}
/*****************************************************************************/
/**
* mm_bearer_stats_get_total_rx_bytes:
* @self: a #MMBearerStats.
*
* Gets the total number of bytes received without error during all the
* connections of this bearer.
*
* Returns: a #guint64.
*
* Since: 1.14
*/
guint64
mm_bearer_stats_get_total_rx_bytes (MMBearerStats *self)
{
g_return_val_if_fail (MM_IS_BEARER_STATS (self), 0);
return self->priv->total_rx_bytes;
}
/**
* mm_bearer_stats_set_total_rx_bytes: (skip)
*/
void
mm_bearer_stats_set_total_rx_bytes (MMBearerStats *self,
guint64 total_bytes)
{
g_return_if_fail (MM_IS_BEARER_STATS (self));
self->priv->total_rx_bytes = total_bytes;
}
/*****************************************************************************/
/**
* mm_bearer_stats_get_total_tx_bytes:
* @self: a #MMBearerStats.
*
* Gets the total number of bytes transmitted without error during all the
* connections of this bearer.
*
* Returns: a #guint64.
*
* Since: 1.14
*/
guint64
mm_bearer_stats_get_total_tx_bytes (MMBearerStats *self)
{
g_return_val_if_fail (MM_IS_BEARER_STATS (self), 0);
return self->priv->total_tx_bytes;
}
/**
* mm_bearer_stats_set_total_tx_bytes: (skip)
*/
void
mm_bearer_stats_set_total_tx_bytes (MMBearerStats *self,
guint64 total_bytes)
{
g_return_if_fail (MM_IS_BEARER_STATS (self));
self->priv->total_tx_bytes = total_bytes;
}
/*****************************************************************************/
/**
* mm_bearer_stats_get_dictionary: (skip)
*/
@@ -242,6 +346,18 @@ mm_bearer_stats_get_dictionary (MMBearerStats *self)
"{sv}",
PROPERTY_FAILED_ATTEMPTS,
g_variant_new_uint32 (self->priv->failed_attempts));
g_variant_builder_add (&builder,
"{sv}",
PROPERTY_TOTAL_DURATION,
g_variant_new_uint32 (self->priv->total_duration));
g_variant_builder_add (&builder,
"{sv}",
PROPERTY_TOTAL_RX_BYTES,
g_variant_new_uint64 (self->priv->total_rx_bytes));
g_variant_builder_add (&builder,
"{sv}",
PROPERTY_TOTAL_TX_BYTES,
g_variant_new_uint64 (self->priv->total_tx_bytes));
return g_variant_builder_end (&builder);
}
@@ -295,7 +411,20 @@ mm_bearer_stats_new_from_dictionary (GVariant *dictionary,
mm_bearer_stats_set_failed_attempts (
self,
g_variant_get_uint32 (value));
} else if (g_str_equal (key, PROPERTY_TOTAL_DURATION)) {
mm_bearer_stats_set_total_duration (
self,
g_variant_get_uint32 (value));
} else if (g_str_equal (key, PROPERTY_TOTAL_RX_BYTES)) {
mm_bearer_stats_set_total_rx_bytes (
self,
g_variant_get_uint64 (value));
} else if (g_str_equal (key, PROPERTY_TOTAL_TX_BYTES)) {
mm_bearer_stats_set_total_tx_bytes (
self,
g_variant_get_uint64 (value));
}
g_free (key);
g_variant_unref (value);
}

View File

@@ -63,6 +63,9 @@ guint64 mm_bearer_stats_get_rx_bytes (MMBearerStats *self);
guint64 mm_bearer_stats_get_tx_bytes (MMBearerStats *self);
guint mm_bearer_stats_get_attempts (MMBearerStats *self);
guint mm_bearer_stats_get_failed_attempts (MMBearerStats *self);
guint mm_bearer_stats_get_total_duration (MMBearerStats *self);
guint64 mm_bearer_stats_get_total_rx_bytes (MMBearerStats *self);
guint64 mm_bearer_stats_get_total_tx_bytes (MMBearerStats *self);
/*****************************************************************************/
/* ModemManager/libmm-glib/mmcli specific methods */
@@ -80,6 +83,9 @@ void mm_bearer_stats_set_rx_bytes (MMBearerStats *self, guint64 rx_b
void mm_bearer_stats_set_tx_bytes (MMBearerStats *self, guint64 tx_bytes);
void mm_bearer_stats_set_attempts (MMBearerStats *self, guint attempts);
void mm_bearer_stats_set_failed_attempts (MMBearerStats *self, guint failed_attempts);
void mm_bearer_stats_set_total_duration (MMBearerStats *self, guint duration);
void mm_bearer_stats_set_total_rx_bytes (MMBearerStats *self, guint64 rx_bytes);
void mm_bearer_stats_set_total_tx_bytes (MMBearerStats *self, guint64 tx_bytes);
GVariant *mm_bearer_stats_get_dictionary (MMBearerStats *self);

View File

@@ -262,11 +262,65 @@ bearer_reset_ongoing_interface_stats (MMBaseBearer *self)
bearer_update_interface_stats (self);
}
static void
bearer_set_ongoing_interface_stats (MMBaseBearer *self,
guint duration,
guint64 rx_bytes,
guint64 tx_bytes)
{
guint n_updates = 0;
/* Make sure we don't reset to 0 these values if we had ever set them
* before. Just ignore the update if we're reported 0 */
if (duration) {
gint delta_duration;
delta_duration = duration - mm_bearer_stats_get_duration (self->priv->stats);
if (delta_duration > 0) {
mm_bearer_stats_set_duration (self->priv->stats, duration);
mm_bearer_stats_set_total_duration (self->priv->stats,
mm_bearer_stats_get_total_duration (self->priv->stats) + delta_duration);
n_updates++;
}
}
if (rx_bytes) {
gint64 delta_rx_bytes;
delta_rx_bytes = rx_bytes - mm_bearer_stats_get_rx_bytes (self->priv->stats);
if (delta_rx_bytes > 0) {
mm_bearer_stats_set_rx_bytes (self->priv->stats, rx_bytes);
mm_bearer_stats_set_total_rx_bytes (self->priv->stats,
mm_bearer_stats_get_total_rx_bytes (self->priv->stats) + delta_rx_bytes);
n_updates++;
}
}
if (tx_bytes) {
gint64 delta_tx_bytes;
delta_tx_bytes = tx_bytes - mm_bearer_stats_get_tx_bytes (self->priv->stats);
if (delta_tx_bytes > 0) {
mm_bearer_stats_set_tx_bytes (self->priv->stats, tx_bytes);
mm_bearer_stats_set_total_tx_bytes (self->priv->stats,
mm_bearer_stats_get_total_tx_bytes (self->priv->stats) + delta_tx_bytes);
n_updates++;
}
}
if (n_updates)
bearer_update_interface_stats (self);
}
static void
bearer_stats_stop (MMBaseBearer *self)
{
if (self->priv->duration_timer) {
mm_bearer_stats_set_duration (self->priv->stats, (guint64) g_timer_elapsed (self->priv->duration_timer, NULL));
bearer_set_ongoing_interface_stats (self,
(guint64) g_timer_elapsed (self->priv->duration_timer, NULL),
0,
0);
g_timer_destroy (self->priv->duration_timer);
self->priv->duration_timer = NULL;
}
@@ -303,10 +357,10 @@ reload_stats_ready (MMBaseBearer *self,
}
/* We only update stats if they were retrieved properly */
mm_bearer_stats_set_duration (self->priv->stats, (guint32) g_timer_elapsed (self->priv->duration_timer, NULL));
mm_bearer_stats_set_tx_bytes (self->priv->stats, tx_bytes);
mm_bearer_stats_set_rx_bytes (self->priv->stats, rx_bytes);
bearer_update_interface_stats (self);
bearer_set_ongoing_interface_stats (self,
(guint32) g_timer_elapsed (self->priv->duration_timer, NULL),
rx_bytes,
tx_bytes);
}
static gboolean
@@ -328,10 +382,10 @@ stats_update_cb (MMBaseBearer *self)
}
/* Otherwise, just update duration and we're done */
mm_bearer_stats_set_duration (self->priv->stats, (guint32) g_timer_elapsed (self->priv->duration_timer, NULL));
mm_bearer_stats_set_tx_bytes (self->priv->stats, 0);
mm_bearer_stats_set_rx_bytes (self->priv->stats, 0);
bearer_update_interface_stats (self);
bearer_set_ongoing_interface_stats (self,
(guint32) g_timer_elapsed (self->priv->duration_timer, NULL),
0,
0);
return G_SOURCE_CONTINUE;
}