From d895b6b0b1ce163b3fb26ff9564092087eb141b4 Mon Sep 17 00:00:00 2001
From: Jonathan Hanks <jonathan.hanks@ligo.org>
Date: Wed, 4 Nov 2020 12:22:34 -0800
Subject: [PATCH] Adding {prefix}TOTAL_CRC_SUM to cps_recv.

---
 src/pub_sub_stream/dc_stats.cc            | 24 +++++++++++++++++++++++
 src/pub_sub_stream/dc_stats.hh            | 12 ++++++++++++
 src/pub_sub_stream/tests/test_dc_stats.cc |  3 +++
 3 files changed, 39 insertions(+)

diff --git a/src/pub_sub_stream/dc_stats.cc b/src/pub_sub_stream/dc_stats.cc
index 5de580416..7458cb44f 100644
--- a/src/pub_sub_stream/dc_stats.cc
+++ b/src/pub_sub_stream/dc_stats.cc
@@ -356,6 +356,15 @@ DCStats::DCStats( std::vector< SimplePV >& pvs,
         std::numeric_limits< int >::max( ),
         -1,
     } );
+    pvs.emplace_back( SimplePV{
+        "TOTAL_CRC_SUM",
+        SIMPLE_PV_INT,
+        reinterpret_cast< void* >( &total_crc_count_ ),
+        1,
+        1,
+        -1,
+        -1,
+    } );
 
     valid_ = true;
 }
@@ -574,6 +583,12 @@ entry_was_processed( DCUStats& cur )
     return cur.processed;
 }
 
+static std::int64_t
+total_crc_sum( std::int64_t val, const DCUStats& cur )
+{
+    return val + static_cast< std::int64_t >( cur.crc_sum );
+}
+
 dc_queue::value_type
 DCStats::get_message( simple_pv_handle epics_server )
 {
@@ -602,6 +617,7 @@ DCStats::run( simple_pv_handle epics_server )
     std::vector< dcuid_crc_pair > data_block_crcs;
     data_block_crcs.reserve( DCU_COUNT );
 
+    int           prev_total_crc_count{ 0 };
     std::uint64_t tp_data{ 0 };
     std::uint64_t model_data{ 0 };
     std::uint64_t total_data{ 0 };
@@ -694,6 +710,13 @@ DCStats::run( simple_pv_handle epics_server )
         for_each( dcu_status_, mark_dcu_if_skipped_this_cycle );
         for_each( dcu_status_, clear_seen_last_cycle_if_skipped );
 
+        uint64_t crc_sum = boost::accumulate( dcu_status_, 0, total_crc_sum );
+        if ( crc_sum > prev_total_crc_count )
+        {
+            total_crc_count_ += crc_sum - prev_total_crc_count;
+        }
+        prev_total_crc_count = crc_sum;
+
         bool alternating_cycle = cycles % 2 == 0;
         bool one_sec_update = cycles % 16 == 0;
 
@@ -739,6 +762,7 @@ DCStats::run( simple_pv_handle epics_server )
             {
                 for_each( dcu_status_, clear_crc );
                 request_clear_crc_ = false;
+                prev_total_crc_count = 0;
             }
         }
         else if ( alternating_cycle )
diff --git a/src/pub_sub_stream/dc_stats.hh b/src/pub_sub_stream/dc_stats.hh
index 35e342773..449f13916 100644
--- a/src/pub_sub_stream/dc_stats.hh
+++ b/src/pub_sub_stream/dc_stats.hh
@@ -108,6 +108,17 @@ public:
         return dcu_status_[ dcuid ];
     }
 
+    /*!
+     * @brief Return the total crc count for the system, a debugging/testing
+     * aid.
+     * @return the sum of the crc count for the lifetime of this object.
+     */
+    int
+    get_total_crcs( ) const
+    {
+        return total_crc_count_;
+    }
+
     /*!
      * @brief get a readonly reference to the channel list
      * @return the channel list
@@ -145,6 +156,7 @@ private:
     unsigned int tp_data_kb_per_s_{ 0 };
     unsigned int model_data_kb_per_s_{ 0 };
     unsigned int total_data_kb_per_s_{ 0 };
+    int          total_crc_count_{ 0 };
 
     std::atomic< bool > request_clear_crc_{ false };
     std::atomic< bool > request_stop_{ false };
diff --git a/src/pub_sub_stream/tests/test_dc_stats.cc b/src/pub_sub_stream/tests/test_dc_stats.cc
index e7b839ea2..c775f0dc3 100644
--- a/src/pub_sub_stream/tests/test_dc_stats.cc
+++ b/src/pub_sub_stream/tests/test_dc_stats.cc
@@ -142,6 +142,7 @@ TEST_CASE(
     REQUIRE( dc_stats.peek_stats( 2 ).processed );
     REQUIRE( dc_stats.peek_stats( 2 ).crc_sum == 0 );
     REQUIRE( dc_stats.peek_stats( 2 ).status == 0 );
+    REQUIRE( dc_stats.get_total_crcs( ) == 0 );
     {
         auto data = make_unique_ptr< daq_dc_data_t >( );
         memset(
@@ -158,6 +159,7 @@ TEST_CASE(
     REQUIRE( dc_stats.peek_stats( 2 ).processed );
     REQUIRE( dc_stats.peek_stats( 2 ).crc_sum == 0 );
     REQUIRE( dc_stats.peek_stats( 2 ).status == 0 );
+    REQUIRE( dc_stats.get_total_crcs( ) == 1 );
     {
         auto data = make_unique_ptr< daq_dc_data_t >( );
         memset(
@@ -174,6 +176,7 @@ TEST_CASE(
     REQUIRE( dc_stats.peek_stats( 2 ).processed );
     REQUIRE( dc_stats.peek_stats( 2 ).crc_sum == 0 );
     REQUIRE( dc_stats.peek_stats( 2 ).status == 0 );
+    REQUIRE( dc_stats.get_total_crcs( ) == 1 );
     dc_stats.stop( );
 
     th.join( );
-- 
GitLab