Program Listing for File UDPStats.h

Return to documentation for file (/home/docs/checkouts/readthedocs.org/user_builds/ska-telescope-ska-pst-recv/checkouts/latest/src/ska/pst/recv/network/UDPStats.h)

/*
 * Copyright 2022 Square Kilometre Array Observatory
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <cstddef>
#include <inttypes.h>

#include "ska/pst/recv/formats/UDPFormat.h"
#include "ska/pst/common/utils/Timer.h"

#ifndef SKA_PST_RECV_NETWORK_UDPStats_h
#define SKA_PST_RECV_NETWORK_UDPStats_h

namespace ska::pst::recv {

  class UDPStats {

    public:

      UDPStats();

      ~UDPStats() = default;

      void configure(UDPFormat * format);

      void increment(uint64_t num_packets=1);

      void increment_bytes(uint64_t nbytes);

      void dropped(uint64_t num_packets=1);

      void dropped_bytes(uint64_t nbytes);

      void ignored(uint64_t num_packets=1);

      void sleeps(uint64_t num_sleeps=1);

      void malformed(uint64_t num_packets=1);

      void misordered(uint64_t num_packets=1);

      void misdirected(uint64_t num_packets=1);

      void discarded(uint64_t num_packets=1);

      void reset();

      uint64_t get_data_transmitted() const { return bytes_transmitted; }

      uint64_t get_packets_transmitted() const;

      uint64_t get_data_dropped() const { return bytes_dropped; }

      uint64_t get_packets_dropped() const;

      uint64_t get_discarded() const { return discarded_packets; }

      uint64_t get_nsleeps() const { return nsleeps; }

      uint64_t get_ignored() const { return ignored_packets; }

      uint64_t get_malformed() const { return malformed_packets; }

      uint64_t get_misdirected() const { return misdirected_packets; }

      uint64_t get_misordered() const { return misordered_packets; }

      void compute_rates();

      double get_data_transmission_rate() const;

      double get_data_drop_rate() const;

      double get_sleep_rate() const;

      void packet_receive_timeout();

      uint64_t get_packet_receive_timeouts() const;

      void set_packet_timeout_threshold(uint64_t threshold);

      void set_initial_packet_timeout_threshold(uint64_t threshold);

      uint64_t get_packet_timeout_threshold();

      uint64_t get_initial_packet_timeout_threshold();


    private:

      unsigned packet_data_size{0};

      unsigned packet_size{0};

      uint64_t bytes_transmitted{0};

      uint64_t bytes_dropped{0};

      uint64_t nsleeps{0};

      uint64_t ignored_packets{0};

      uint64_t discarded_packets{0};

      uint64_t malformed_packets{0};

      uint64_t misordered_packets{0};

      uint64_t misdirected_packets{0};

      uint64_t packet_receive_timeouts{0};

      uint64_t initial_packet_timeout_threshold = 10;

      uint64_t packet_timeout_threshold = 1;

      std::mutex compute_rates_mutex;

      ska::pst::common::Timer timer{};

      ska::pst::common::Timer timeout_timer{};

      uint64_t prev_bytes_transmitted{0};

      uint64_t prev_bytes_dropped{0};

      uint64_t prev_nsleeps{0};

      double bytes_transmitted_per_second{0};

      double bytes_dropped_per_second{0};

      double nsleeps_per_second{0};

      double minimum_elapsed{0.001};
  };

} // namespace ska::pst::recv

#endif // SKA_PST_RECV_NETWORK_UDPStats_h