From bcbf2b38ee58e4c74a9450cb82bb1c247fe52f60 Mon Sep 17 00:00:00 2001
From: Robert Schmidt <robert.schmidt@eurecom.fr>
Date: Wed, 29 Jun 2022 11:46:24 +0200
Subject: [PATCH] Add more MAC stats

---
 openair2/LAYER2/NR_MAC_gNB/gNB_scheduler.c       |  3 +++
 openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_dlsch.c |  9 +++++++++
 openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_ulsch.c | 11 +++++++++++
 openair2/LAYER2/NR_MAC_gNB/nr_mac_gNB.h          |  8 ++++++++
 4 files changed, 31 insertions(+)

diff --git a/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler.c b/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler.c
index c87aa68f75..46ffb0b210 100644
--- a/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler.c
+++ b/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler.c
@@ -133,6 +133,9 @@ void gNB_dlsch_ulsch_scheduler(module_id_t module_idP,
       gNB->tdd_beam_association[i] = -1;
   }
 
+  gNB->frame = frame;
+  gNB->slot = slot;
+
   start_meas(&RC.nrmac[module_idP]->eNB_scheduler);
   VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_gNB_DLSCH_ULSCH_SCHEDULER,VCD_FUNCTION_IN);
 
diff --git a/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_dlsch.c b/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_dlsch.c
index 6aee81fa67..617327d750 100644
--- a/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_dlsch.c
+++ b/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_dlsch.c
@@ -888,6 +888,7 @@ void nr_schedule_ue_spec(module_id_t module_id,
 
     NR_sched_pdsch_t *sched_pdsch = &sched_ctrl->sched_pdsch;
     UE->mac_stats.dl.current_bytes = 0;
+    UE->mac_stats.dl.current_rbs = 0;
     NR_CellGroupConfig_t *cg = UE->CellGroup;
 
     /* update TA and set ta_apply every 10 frames.
@@ -1185,6 +1186,7 @@ void nr_schedule_ue_spec(module_id_t module_id,
                   current_harq_pid);
       T(T_GNB_MAC_RETRANSMISSION_DL_PDU_WITH_DATA, T_INT(module_id), T_INT(CC_id), T_INT(rnti),
         T_INT(frame), T_INT(slot), T_INT(current_harq_pid), T_INT(harq->round), T_BUFFER(harq->transportBlock, TBS));
+      UE->mac_stats.dl.total_rbs_retx += sched_pdsch->rbSize;
     } else { /* initial transmission */
       LOG_D(NR_MAC, "[%s] Initial HARQ transmission in %d.%d\n", __FUNCTION__, frame, slot);
       uint8_t *buf = (uint8_t *) harq->transportBlock;
@@ -1200,6 +1202,7 @@ void nr_schedule_ue_spec(module_id_t module_id,
       int dlsch_total_bytes = 0;
       /* next, get RLC data */
       start_meas(&gNB_mac->rlc_data_req);
+      int sdus = 0;
 
       if (sched_ctrl->num_total_bytes > 0) {
         /* loop over all activated logical channels */
@@ -1251,6 +1254,7 @@ void nr_schedule_ue_spec(module_id_t module_id,
             buf += len+sizeof(NR_MAC_SUBHEADER_LONG);
             dlsch_total_bytes += len;
             lcid_bytes += len;
+            sdus += 1;
           }
 
           UE->mac_stats.dl.lc_bytes[lcid] += lcid_bytes;
@@ -1278,6 +1282,7 @@ void nr_schedule_ue_spec(module_id_t module_id,
           }
           for (; buf < bufEnd; buf++)
             *buf = lrand48() & 0xff;
+          sdus +=1;
         }
       }
 
@@ -1295,6 +1300,10 @@ void nr_schedule_ue_spec(module_id_t module_id,
 
       UE->mac_stats.dl.total_bytes += TBS;
       UE->mac_stats.dl.current_bytes = TBS;
+      UE->mac_stats.dl.total_rbs += sched_pdsch->rbSize;
+      UE->mac_stats.dl.num_mac_sdu += sdus;
+      UE->mac_stats.dl.current_rbs = sched_pdsch->rbSize;
+
       /* save retransmission information */
       harq->sched_pdsch = *sched_pdsch;
       /* save which time allocation has been used, to be used on
diff --git a/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_ulsch.c b/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_ulsch.c
index 43e88eb9a9..7b80303ba9 100644
--- a/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_ulsch.c
+++ b/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_ulsch.c
@@ -88,6 +88,7 @@ int nr_process_mac_pdu( instance_t module_idP,
 
 
     uint8_t done = 0;
+    int sdus = 0;
 
     NR_UE_sched_ctrl_t *sched_ctrl = &UE->UE_sched_ctrl;
 
@@ -349,6 +350,7 @@ int nr_process_mac_pdu( instance_t module_idP,
                              1,
                              NULL);
 
+            sdus += 1;
             /* Updated estimated buffer when receiving data */
             if (sched_ctrl->estimated_ul_buffer >= mac_len) {
               sched_ctrl->estimated_ul_buffer -= mac_len;
@@ -357,6 +359,7 @@ int nr_process_mac_pdu( instance_t module_idP,
             }
 
             break;
+            sdus += 1;
 
         default:
           LOG_E(NR_MAC, "Received unknown MAC header (LCID = 0x%02x)\n", rx_lcid);
@@ -388,6 +391,9 @@ int nr_process_mac_pdu( instance_t module_idP,
           return 0;
         }
     }
+
+  UE->mac_stats.ul.num_mac_sdu += sdus;
+
   return 0;
 }
 
@@ -1494,6 +1500,7 @@ void nr_schedule_ulsch(module_id_t module_id, frame_t frame, sub_frame_t slot)
     NR_UE_UL_BWP_t *current_BWP = &UE->current_UL_BWP;
 
     UE->mac_stats.ul.current_bytes = 0;
+    UE->mac_stats.ul.current_rbs = 0;
 
     /* dynamic PUSCH values (RB alloc, MCS, hence R, Qm, TBS) that change in
      * every TTI are pre-populated by the preprocessor and used below */
@@ -1548,6 +1555,8 @@ void nr_schedule_ulsch(module_id_t module_id, frame_t frame, sub_frame_t slot)
        * retransmissions */
       cur_harq->sched_pusch.time_domain_allocation = ps->time_domain_allocation;
       sched_ctrl->sched_ul_bytes += sched_pusch->tb_size;
+      UE->mac_stats.ul.total_rbs += sched_pusch->rbSize;
+
     } else {
       LOG_D(NR_MAC,
             "%d.%2d UL retransmission RNTI %04x sched %d.%2d HARQ PID %d round %d NDI %d\n",
@@ -1559,8 +1568,10 @@ void nr_schedule_ulsch(module_id_t module_id, frame_t frame, sub_frame_t slot)
             harq_id,
             cur_harq->round,
             cur_harq->ndi);
+      UE->mac_stats.ul.total_rbs_retx += sched_pusch->rbSize;
     }
     UE->mac_stats.ul.current_bytes = sched_pusch->tb_size;
+    UE->mac_stats.ul.current_rbs = sched_pusch->rbSize;
     sched_ctrl->last_ul_frame = sched_pusch->frame;
     sched_ctrl->last_ul_slot = sched_pusch->slot;
 
diff --git a/openair2/LAYER2/NR_MAC_gNB/nr_mac_gNB.h b/openair2/LAYER2/NR_MAC_gNB/nr_mac_gNB.h
index 09f3193790..8cf3b75801 100644
--- a/openair2/LAYER2/NR_MAC_gNB/nr_mac_gNB.h
+++ b/openair2/LAYER2/NR_MAC_gNB/nr_mac_gNB.h
@@ -667,6 +667,10 @@ typedef struct NR_mac_dir_stats {
   uint64_t errors;
   uint64_t total_bytes;
   uint32_t current_bytes;
+  uint32_t total_rbs;
+  uint32_t total_rbs_retx;
+  uint32_t num_mac_sdu;
+  uint32_t current_rbs;
 } NR_mac_dir_stats_t;
 
 typedef struct NR_mac_stats {
@@ -849,6 +853,10 @@ typedef struct gNB_MAC_INST_s {
   uint8_t min_grant_mcs;
 
   nr_mac_rrc_ul_if_t mac_rrc;
+
+  int16_t frame;
+  int16_t slot;
+
 } gNB_MAC_INST;
 
 #endif /*__LAYER2_NR_MAC_GNB_H__ */
-- 
2.26.2