Revise wifi statistics routine 96/251596/2 accepted/tizen/6.0/unified/20210225.040157 submit/tizen_6.0/20210224.083922
authorJaehyun Kim <jeik01.kim@samsung.com>
Fri, 15 Jan 2021 10:38:58 +0000 (19:38 +0900)
committerJaehyun Kim <jeik01.kim@samsung.com>
Fri, 15 Jan 2021 10:58:51 +0000 (19:58 +0900)
Net-config updates the DB every 2 or 10 seconds for the wifi usage statistics,
which can cause shorten flash storage lifetime.
So the update cycle and method have been changed.

Change-Id: I0ed5053a48771d569320211c31890d3aec7cb55d
Signed-off-by: Jaehyun Kim <jeik01.kim@samsung.com>
include/network-statistics.h
src/network-statistics.c
src/wifi-indicator.c

index 8bcffef..626033d 100755 (executable)
@@ -30,7 +30,7 @@ extern "C" {
 
 gboolean netconfig_wifi_get_bytes_statistics(guint64 *tx, guint64 *rx, gboolean update);
 void netconfig_wifi_get_bytes_default_iface(guint64 *tx, guint64 *rx);
-void netconfig_wifi_set_bytes_pkt_vconf(guint64 tx_diff, guint64 rx_diff);
+void netconfig_wifi_set_bytes_pkt_vconf(guint64 tx_diff, guint64 rx_diff, gboolean update);
 void netconfig_wifi_reset_last_bytes(void);
 
 void statistics_object_create_and_init(void);
index b440449..840ce8e 100755 (executable)
 #include "generated-code.h"
 
 #define NETCONFIG_PROCDEV                                      "/proc/net/dev"
+#define WIFI_VCONF_UPDATE_INTERVAL                     3600
 
+static struct {
+       guint64 tx_diff;
+       guint64 rx_diff;
+       guint64 last_tx_offset;
+       guint64 last_rx_offset;
+} stat_count;
+
+static guint wifi_vconf_update_timer = 0;
 static Network_statistics *netconfigstatistics = NULL;
 
 static wifi_device_data_s *__find_wifi_interface_name(const char *interface_name)
@@ -142,37 +151,53 @@ void netconfig_wifi_get_bytes_default_iface(guint64 *tx, guint64 *rx)
        *rx = device_data->rx_diff;
 }
 
-void netconfig_wifi_set_bytes_pkt_vconf(guint64 tx_diff, guint64 rx_diff)
+void netconfig_wifi_set_bytes_pkt_vconf(guint64 tx_diff, guint64 rx_diff, gboolean update)
 {
        int val = 0;
        guint64 last_tx = 0, last_rx = 0;
        guint64 total_tx = 0, total_rx = 0;
 
-       /* LAST */
-       netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_SNT, &val);
-       last_tx = (guint64)val + tx_diff;
+       stat_count.tx_diff += tx_diff;
+       stat_count.rx_diff += rx_diff;
 
-       netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_RCV, &val);
-       last_rx = (guint64)val + rx_diff;
+       if (update) {
+               /* LAST */
+               netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_SNT, &val);
+               last_tx = (guint64)val + stat_count.tx_diff - stat_count.last_tx_offset;
 
-       netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_SNT, (int)last_tx, FALSE);
-       netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_RCV, (int)last_rx, FALSE);
+               netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_RCV, &val);
+               last_rx = (guint64)val + stat_count.rx_diff - stat_count.last_rx_offset;
 
-       /* TOTAL */
-       netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_SNT, &val);
-       total_tx = (guint64)val + tx_diff;
+               netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_SNT, (int)last_tx, FALSE);
+               netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_RCV, (int)last_rx, FALSE);
 
-       netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_RCV, &val);
-       total_rx = (guint64)val + rx_diff;
+               stat_count.last_tx_offset = 0;
+               stat_count.last_rx_offset = 0;
 
-       netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_SNT, (int)total_tx, FALSE);
-       netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_RCV, (int)total_rx, FALSE);
+               /* TOTAL */
+               netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_SNT, &val);
+               total_tx = (guint64)val + stat_count.tx_diff;
+
+               netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_RCV, &val);
+               total_rx = (guint64)val + stat_count.rx_diff;
+
+               netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_SNT, (int)total_tx, FALSE);
+               netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_RCV, (int)total_rx, FALSE);
+
+               stat_count.rx_diff = 0;
+               stat_count.tx_diff = 0;
+       }
 }
 
 void netconfig_wifi_reset_last_bytes(void)
 {
-       netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_SNT, 0, FALSE);
-       netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_RCV, 0, FALSE);
+       int val = 0;
+
+       netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_SNT, &val);
+       stat_count.last_tx_offset = (guint64)val + stat_count.tx_diff;
+
+       netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_RCV, &val);
+       stat_count.last_rx_offset = (guint64)val + stat_count.rx_diff;
 }
 
 static gboolean handle_get_wifi_total_tx_bytes(
@@ -188,7 +213,7 @@ static gboolean handle_get_wifi_total_tx_bytes(
        netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
 
        netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_SNT, &val);
-       tx_bytes = (guint64)val;
+       tx_bytes = (guint64)val + stat_count.tx_diff;
 
        if (wifi_state != VCONFKEY_WIFI_CONNECTED) {
                total_bytes = tx_bytes;
@@ -196,10 +221,13 @@ static gboolean handle_get_wifi_total_tx_bytes(
                return TRUE;
        }
 
-       if (netconfig_wifi_get_bytes_statistics(&tx, &rx, FALSE) == TRUE)
+       if (netconfig_wifi_get_bytes_statistics(&tx, &rx, FALSE) == TRUE) {
                total_bytes = tx + tx_bytes;
-       else
+               stat_count.tx_diff += tx;
+               stat_count.rx_diff += rx;
+       } else {
                total_bytes = tx_bytes;
+       }
 
        network_statistics_complete_get_wifi_total_tx_bytes(object, context, total_bytes);
        return TRUE;
@@ -218,7 +246,7 @@ static gboolean handle_get_wifi_total_rx_bytes(
        netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
 
        netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_RCV, &val);
-       rx_bytes = (guint64)val;
+       rx_bytes = (guint64)val + stat_count.rx_diff;
 
        if (wifi_state != VCONFKEY_WIFI_CONNECTED) {
                total_bytes = rx_bytes;
@@ -226,10 +254,13 @@ static gboolean handle_get_wifi_total_rx_bytes(
                return TRUE;
        }
 
-       if (netconfig_wifi_get_bytes_statistics(&tx, &rx, FALSE) == TRUE)
+       if (netconfig_wifi_get_bytes_statistics(&tx, &rx, FALSE) == TRUE) {
                total_bytes = rx + rx_bytes;
-       else
+               stat_count.tx_diff += tx;
+               stat_count.rx_diff += rx;
+       } else {
                total_bytes = rx_bytes;
+       }
 
        network_statistics_complete_get_wifi_total_rx_bytes(object, context, total_bytes);
        return TRUE;
@@ -248,7 +279,7 @@ static gboolean handle_get_wifi_last_tx_bytes(
        netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
 
        netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_SNT, &val);
-       tx_bytes = (guint64)val;
+       tx_bytes = (guint64)val + stat_count.tx_diff - stat_count.last_tx_offset;
 
        if (wifi_state != VCONFKEY_WIFI_CONNECTED) {
                last_bytes = tx_bytes;
@@ -256,10 +287,13 @@ static gboolean handle_get_wifi_last_tx_bytes(
                return TRUE;
        }
 
-       if (netconfig_wifi_get_bytes_statistics(&tx, &rx, FALSE) == TRUE)
+       if (netconfig_wifi_get_bytes_statistics(&tx, &rx, FALSE) == TRUE) {
                last_bytes = tx + tx_bytes;
-       else
+               stat_count.tx_diff += tx;
+               stat_count.rx_diff += rx;
+       } else {
                last_bytes = tx_bytes;
+       }
 
        network_statistics_complete_get_wifi_last_tx_bytes(object, context, last_bytes);
        return TRUE;
@@ -278,7 +312,7 @@ static gboolean handle_get_wifi_last_rx_bytes(
        netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
 
        netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_RCV, &val);
-       rx_bytes = (guint64)val;
+       rx_bytes = (guint64)val + stat_count.rx_diff - stat_count.last_rx_offset;
 
        if (wifi_state != VCONFKEY_WIFI_CONNECTED) {
                last_bytes = rx_bytes;
@@ -286,10 +320,13 @@ static gboolean handle_get_wifi_last_rx_bytes(
                return TRUE;
        }
 
-       if (netconfig_wifi_get_bytes_statistics(&tx, &rx, FALSE) == TRUE)
+       if (netconfig_wifi_get_bytes_statistics(&tx, &rx, FALSE) == TRUE) {
                last_bytes = rx + rx_bytes;
-       else
+               stat_count.tx_diff += tx;
+               stat_count.rx_diff += rx;
+       } else {
                last_bytes = rx_bytes;
+       }
 
        network_statistics_complete_get_wifi_last_rx_bytes(object, context, last_bytes);
        return TRUE;
@@ -335,6 +372,16 @@ static gboolean handle_reset_wifi_total_tx_bytes(
                Network_statistics *object,
                GDBusMethodInvocation *context)
 {
+       int val = 0;
+       guint64 last_tx = 0;
+
+       netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_SNT, &val);
+       last_tx = (guint64)val + stat_count.tx_diff - stat_count.last_tx_offset;
+
+       netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_SNT, (int)last_tx, FALSE);
+       stat_count.last_tx_offset = 0;
+       stat_count.tx_diff = 0;
+
        netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_SNT, 0, TRUE);
        network_statistics_complete_reset_wifi_total_tx_bytes(object, context);
        return TRUE;
@@ -344,6 +391,16 @@ static gboolean handle_reset_wifi_total_rx_bytes(
                Network_statistics *object,
                GDBusMethodInvocation *context)
 {
+       int val = 0;
+       guint64 last_rx = 0;
+
+       netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_RCV, &val);
+       last_rx = (guint64)val + stat_count.rx_diff - stat_count.last_rx_offset;
+
+       netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_RCV, (int)last_rx, FALSE);
+       stat_count.last_rx_offset = 0;
+       stat_count.rx_diff = 0;
+
        netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_RCV, 0, TRUE);
        network_statistics_complete_reset_wifi_total_rx_bytes(object, context);
        return TRUE;
@@ -353,7 +410,17 @@ static gboolean handle_reset_wifi_last_tx_bytes(
                Network_statistics *object,
                GDBusMethodInvocation *context)
 {
+       int val = 0;
+       guint64 total_tx = 0;
+
+       netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_SNT, &val);
+       total_tx = (guint64)val + stat_count.tx_diff;
+
+       netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_SNT, (int)total_tx, FALSE);
+       stat_count.tx_diff = 0;
+
        netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_SNT, 0, TRUE);
+       stat_count.last_tx_offset = 0;
        network_statistics_complete_reset_wifi_last_tx_bytes(object, context);
        return TRUE;
 }
@@ -362,7 +429,17 @@ static gboolean handle_reset_wifi_last_rx_bytes(
                Network_statistics *object,
                GDBusMethodInvocation *context)
 {
+       int val = 0;
+       guint64 total_rx = 0;
+
+       netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_RCV, &val);
+       total_rx = (guint64)val + stat_count.rx_diff;
+
+       netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_RCV, (int)total_rx, FALSE);
+       stat_count.rx_diff = 0;
+
        netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_RCV, 0, TRUE);
+       stat_count.last_rx_offset = 0;
        network_statistics_complete_reset_wifi_last_rx_bytes(object, context);
        return TRUE;
 }
@@ -374,6 +451,17 @@ static void wifi_statistics_update_state(void)
        netconfig_wifi_get_bytes_statistics(&tx, &rx, TRUE);
 }
 
+static gboolean __statistics_update_wifi_vconf(gpointer data)
+{
+       if (stat_count.tx_diff ||
+                       stat_count.rx_diff ||
+                       stat_count.last_tx_offset ||
+                       stat_count.last_rx_offset)
+               netconfig_wifi_set_bytes_pkt_vconf(0, 0, TRUE);
+
+       return TRUE;
+}
+
 void statistics_object_create_and_init(void)
 {
        DBG("Creating statistics object");
@@ -421,11 +509,15 @@ void statistics_object_create_and_init(void)
        }
 
        wifi_statistics_update_state();
+       netconfig_start_timer_seconds(WIFI_VCONF_UPDATE_INTERVAL,
+                       __statistics_update_wifi_vconf, NULL, &wifi_vconf_update_timer);
 
        return;
 }
 
 void statistics_object_deinit(void)
 {
+       netconfig_wifi_set_bytes_pkt_vconf(0, 0, TRUE);
+       netconfig_stop_timer(&wifi_vconf_update_timer);
        g_object_unref(netconfigstatistics);
 }
index db16d2e..06ac9da 100755 (executable)
@@ -300,7 +300,7 @@ static void __netconfig_wifi_get_statistics(void)
        int transfer_state;
 
        if (netconfig_wifi_get_bytes_statistics(&tx, &rx, TRUE))
-               netconfig_wifi_set_bytes_pkt_vconf(tx, rx);
+               netconfig_wifi_set_bytes_pkt_vconf(tx, rx, FALSE);
 
        netconfig_wifi_get_bytes_default_iface(&tx, &rx);
 
@@ -425,7 +425,7 @@ void netconfig_wifi_indicator_stop(const char *interface_name)
 
        if (wifi_state != VCONFKEY_WIFI_CONNECTED) {
                if (netconfig_wifi_get_bytes_statistics(&tx, &rx, TRUE))
-                       netconfig_wifi_set_bytes_pkt_vconf(tx, rx);
+                       netconfig_wifi_set_bytes_pkt_vconf(tx, rx, FALSE);
 
                netconfig_stop_timer(&netconfig_wifi_statistics_timer);
        }