-/* "Instant" stats.
- *
- * Some information in the database must be kept as up-to-date as possible to
- * allow controllers to respond rapidly to network outages. We call these
- * statistics "instant" stats.
- *
- * We wish to update these statistics every INSTANT_INTERVAL_MSEC milliseconds,
- * assuming that they've changed. The only means we have to determine whether
- * they have changed are:
- *
- * - Try to commit changes to the database. If nothing changed, then
- * ovsdb_idl_txn_commit() returns TXN_UNCHANGED, otherwise some other
- * value.
- *
- * - instant_stats_run() is called late in the run loop, after anything that
- * might change any of the instant stats.
- *
- * We use these two facts together to avoid waking the process up every
- * INSTANT_INTERVAL_MSEC whether there is any change or not.
- */
-
-/* Minimum interval between writing updates to the instant stats to the
- * database. */
-#define INSTANT_INTERVAL_MSEC 100
-
-/* Current instant stats database transaction, NULL if there is no ongoing
- * transaction. */
-static struct ovsdb_idl_txn *instant_txn;
-
-/* Next time (in msec on monotonic clock) at which we will update the instant
- * stats. */
-static long long int instant_next_txn = LLONG_MIN;
-
-/* True if the run loop has run since we last saw that the instant stats were
- * unchanged, that is, this is true if we need to wake up at 'instant_next_txn'
- * to refresh the instant stats. */
-static bool instant_stats_could_have_changed;
-
-static void
-instant_stats_run(void)
-{
- enum ovsdb_idl_txn_status status;
-
- instant_stats_could_have_changed = true;
-
- if (!instant_txn) {
- struct bridge *br;
- uint64_t seq;
-
- if (time_msec() < instant_next_txn) {
- return;
- }
- instant_next_txn = time_msec() + INSTANT_INTERVAL_MSEC;
-
- seq = seq_read(connectivity_seq_get());
- if (seq == connectivity_seqno) {
- return;
- }
- connectivity_seqno = seq;
-
- instant_txn = ovsdb_idl_txn_create(idl);
- HMAP_FOR_EACH (br, node, &all_bridges) {
- struct iface *iface;
- struct port *port;
-
- br_refresh_stp_status(br);
-
- HMAP_FOR_EACH (port, hmap_node, &br->ports) {
- port_refresh_stp_status(port);
- }
-
- HMAP_FOR_EACH (iface, name_node, &br->iface_by_name) {
- enum netdev_flags flags;
- struct smap smap;
- const char *link_state;
- int64_t link_resets;
- int current, error;
-
- if (iface_is_synthetic(iface)) {
- continue;
- }
-
- current = ofproto_port_is_lacp_current(br->ofproto,
- iface->ofp_port);
- if (current >= 0) {
- bool bl = current;
- ovsrec_interface_set_lacp_current(iface->cfg, &bl, 1);
- } else {
- ovsrec_interface_set_lacp_current(iface->cfg, NULL, 0);
- }
-
- error = netdev_get_flags(iface->netdev, &flags);
- if (!error) {
- const char *state = flags & NETDEV_UP ? "up" : "down";
- ovsrec_interface_set_admin_state(iface->cfg, state);
- } else {
- ovsrec_interface_set_admin_state(iface->cfg, NULL);
- }
-
- link_state = netdev_get_carrier(iface->netdev) ? "up" : "down";
- ovsrec_interface_set_link_state(iface->cfg, link_state);
-
- link_resets = netdev_get_carrier_resets(iface->netdev);
- ovsrec_interface_set_link_resets(iface->cfg, &link_resets, 1);
-
- iface_refresh_cfm_stats(iface);
-
- smap_init(&smap);
- ofproto_port_get_bfd_status(br->ofproto, iface->ofp_port,
- &smap);
- ovsrec_interface_set_bfd_status(iface->cfg, &smap);
- smap_destroy(&smap);
- }
- }
- }
-
- status = ovsdb_idl_txn_commit(instant_txn);
- if (status != TXN_INCOMPLETE) {
- ovsdb_idl_txn_destroy(instant_txn);
- instant_txn = NULL;
- }
- if (status == TXN_UNCHANGED) {
- instant_stats_could_have_changed = false;
- }
-}
-
-static void
-instant_stats_wait(void)
-{
- if (instant_txn) {
- ovsdb_idl_txn_wait(instant_txn);
- } else if (instant_stats_could_have_changed) {
- poll_timer_wait_until(instant_next_txn);
- }
-}
-\f