From caef8c6e568f585d3a8b453a89a47addc50dfc52 Mon Sep 17 00:00:00 2001
From: Wang Tsu-Han <wangts@eurecom.fr>
Date: Fri, 3 Nov 2017 12:27:39 +0100
Subject: [PATCH] fixing the sleep delay problem for tx

---
 openair1/PHY/LTE_TRANSPORT/dlsch_coding.c |  6 +++++
 openair1/SCHED/ru_procedures.c            | 11 +++++++++
 openair2/UTIL/LOG/vcd_signal_dumper.c     |  3 +++
 openair2/UTIL/LOG/vcd_signal_dumper.h     |  3 +++
 targets/RT/USER/lte-enb.c                 | 13 ++++++----
 targets/RT/USER/lte-ru.c                  | 30 +++++++++++++++++------
 targets/RT/USER/lte-softmodem.c           |  2 +-
 7 files changed, 55 insertions(+), 13 deletions(-)

diff --git a/openair1/PHY/LTE_TRANSPORT/dlsch_coding.c b/openair1/PHY/LTE_TRANSPORT/dlsch_coding.c
index 9b5a898aac..e407f2c4bf 100644
--- a/openair1/PHY/LTE_TRANSPORT/dlsch_coding.c
+++ b/openair1/PHY/LTE_TRANSPORT/dlsch_coding.c
@@ -354,9 +354,15 @@ int dlsch_encoding_2threads0(te_params *tep) {
 
 extern int oai_exit;
 void *te_thread(void *param) {
+  cpu_set_t cpuset;
+  CPU_ZERO(&cpuset);
+
   pthread_setname_np( pthread_self(),"te processing");
   LOG_I(PHY,"thread te created id=%ld", syscall(__NR_gettid));
 
+  CPU_SET(4, &cpuset);
+  pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
+
   eNB_proc_t *proc = &((te_params *)param)->eNB->proc;
   while (!oai_exit) {
 
diff --git a/openair1/SCHED/ru_procedures.c b/openair1/SCHED/ru_procedures.c
index e927fcdbc2..f60f066ef5 100644
--- a/openair1/SCHED/ru_procedures.c
+++ b/openair1/SCHED/ru_procedures.c
@@ -140,6 +140,12 @@ static void *feptx_thread(void *param) {
 
   RU_t *ru = (RU_t *)param;
   RU_proc_t *proc  = &ru->proc;
+  cpu_set_t cpuset;
+  CPU_ZERO(&cpuset);
+
+  CPU_SET(6, &cpuset);
+  pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
+
 
   thread_top_init("feptx_thread",0,870000,1000000,1000000);
 
@@ -418,6 +424,11 @@ static void *fep_thread(void *param) {
 
   thread_top_init("fep_thread",0,870000,1000000,1000000);
 
+  cpu_set_t cpuset;
+  CPU_ZERO(&cpuset);
+  CPU_SET(2, &cpuset);
+  pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
+
   while (!oai_exit) {
 
     if (wait_on_condition(&proc->mutex_fep,&proc->cond_fep,&proc->instance_cnt_fep,"fep thread")<0) break; 
diff --git a/openair2/UTIL/LOG/vcd_signal_dumper.c b/openair2/UTIL/LOG/vcd_signal_dumper.c
index 665e5b4384..dfd48c77f7 100644
--- a/openair2/UTIL/LOG/vcd_signal_dumper.c
+++ b/openair2/UTIL/LOG/vcd_signal_dumper.c
@@ -195,6 +195,9 @@ const char* eurecomVariablesNames[] = {
   "ue0_trx_write_ns",
   "ue0_trx_read_ns_missing",
   "ue0_trx_write_ns_missing",
+  "enb_thread_rxtx_CPUID",
+  "ru_thread_CPUID",
+  "ru_thread_tx_CPUID"
 };
 
 const char* eurecomFunctionsNames[] = {
diff --git a/openair2/UTIL/LOG/vcd_signal_dumper.h b/openair2/UTIL/LOG/vcd_signal_dumper.h
index d777236b34..e13ee7cfdc 100644
--- a/openair2/UTIL/LOG/vcd_signal_dumper.h
+++ b/openair2/UTIL/LOG/vcd_signal_dumper.h
@@ -167,6 +167,9 @@ typedef enum {
   VCD_SIGNAL_DUMPER_VARIABLES_UE0_TRX_WRITE_NS,
   VCD_SIGNAL_DUMPER_VARIABLES_UE0_TRX_READ_NS_MISSING,
   VCD_SIGNAL_DUMPER_VARIABLES_UE0_TRX_WRITE_NS_MISSING,
+  VCD_SIGNAL_DUMPER_VARIABLES_CPUID_ENB_THREAD_RXTX,
+  VCD_SIGNAL_DUMPER_VARIABLES_CPUID_RU_THREAD,
+  VCD_SIGNAL_DUMPER_VARIABLES_CPUID_RU_THREAD_TX,
   VCD_SIGNAL_DUMPER_VARIABLES_END
 } vcd_signal_dump_variables;
 
diff --git a/targets/RT/USER/lte-enb.c b/targets/RT/USER/lte-enb.c
index 4a35a817fb..90a096285e 100644
--- a/targets/RT/USER/lte-enb.c
+++ b/targets/RT/USER/lte-enb.c
@@ -219,6 +219,8 @@ static void* eNB_thread_rxtx( void* param ) {
 
   char thread_name[100];
 
+  cpu_set_t cpuset;
+  CPU_ZERO(&cpuset);
 
   // set default return value
   eNB_thread_rxtx_status = 0;
@@ -227,11 +229,15 @@ static void* eNB_thread_rxtx( void* param ) {
   sprintf(thread_name,"RXn_TXnp4_%d\n",&eNB->proc.proc_rxtx[0] == proc ? 0 : 1);
   thread_top_init(thread_name,1,850000L,1000000L,2000000L);
 
+  CPU_SET(3, &cpuset);
+  pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
+
   while (!oai_exit) {
     VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_eNB_PROC_RXTX0+(proc->subframe_rx&1), 0 );
 
     if (wait_on_condition(&proc->mutex_rxtx,&proc->cond_rxtx,&proc->instance_cnt_rxtx,thread_name)<0) break;
 
+    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_CPUID_ENB_THREAD_RXTX,sched_getcpu());
     VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_eNB_PROC_RXTX0+(proc->subframe_rx&1), 1 );
     //ru_proc = eNB_proc->ru_proc;
    
@@ -356,12 +362,9 @@ int wakeup_rxtx(PHY_VARS_eNB *eNB,RU_t *ru) {
   wait.tv_nsec=5000000L;
 
   /* accept some delay in processing - up to 5ms */
-  for (i = 0; i < 10 && proc_rxtx->instance_cnt_rxtx == 0; i++) {
-    LOG_W( PHY,"[eNB] Frame %d, eNB RXn-TXnp4 thread busy!! (cnt_rxtx %i)\n", proc_rxtx->frame_tx, proc_rxtx->instance_cnt_rxtx);
-    usleep(500);
-  }
+
   if (proc_rxtx->instance_cnt_rxtx == 0) {
-    exit_fun( "TX thread busy" );
+    LOG_E(PHY,"Frame %d, subframe %d: RXTX thread busy, dropping",proc_rxtx->frame_rx,proc_rxtx->subframe_rx);
     return(-1);
   }
 
diff --git a/targets/RT/USER/lte-ru.c b/targets/RT/USER/lte-ru.c
index 1dc3a8780c..a106012815 100644
--- a/targets/RT/USER/lte-ru.c
+++ b/targets/RT/USER/lte-ru.c
@@ -1126,12 +1126,12 @@ void wakeup_eNBs(RU_t *ru) {
     ru->eNB_top(eNB_list[0],ru->proc.frame_rx,ru->proc.subframe_rx,string,ru);
   }
   else {
-
+    
     for (i=0;i<ru->num_eNB;i++)
-	{
-      if (ru->wakeup_rxtx(eNB_list[i],ru) < 0)
-	    LOG_E(PHY,"could not wakeup eNB rxtx process for subframe %d\n", ru->proc.subframe_rx);
-	}
+      {
+	if (ru->wakeup_rxtx(eNB_list[i],ru) < 0)
+	  LOG_E(PHY,"could not wakeup eNB rxtx process for subframe %d\n", ru->proc.subframe_rx);
+      }
   }
 }
 
@@ -1367,14 +1367,21 @@ static void* ru_thread_tx( void* param ) {
   RU_t *ru         = (RU_t*)param;
   RU_proc_t *proc  = &ru->proc;
   int subframe=0, frame=0; 
+  cpu_set_t cpuset;
+  CPU_ZERO(&cpuset);
+
 
   thread_top_init("ru_thread_tx",1,870000L,1000000L,1000000L);
 
+  CPU_SET(5, &cpuset);
+  pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
+
   wait_on_condition(&proc->mutex_FH1,&proc->cond_FH1,&proc->instance_cnt_FH1,"ru_thread_tx");
 
   printf( "ru_thread_tx ready\n");
   while (!oai_exit) { 
-   
+
+    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_CPUID_RU_THREAD_TX,sched_getcpu());   
     if (oai_exit) break;   
 
     if (subframe==9) { 
@@ -1416,6 +1423,9 @@ static void* ru_thread( void* param ) {
   int                ret;
   int                subframe =9;
   int                frame    =1023; 
+  cpu_set_t cpuset;
+  CPU_ZERO(&cpuset);
+
 
   // set default return value
   ru_thread_status = 0;
@@ -1424,6 +1434,9 @@ static void* ru_thread( void* param ) {
   // set default return value
   thread_top_init("ru_thread",0,870000,1000000,1000000);
 
+  CPU_SET(1, &cpuset);
+  pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
+
   LOG_I(PHY,"Starting RU %d (%s,%s),\n",ru->idx,eNB_functions[ru->function],eNB_timing[ru->if_timing]);
 
 
@@ -1491,6 +1504,8 @@ static void* ru_thread( void* param ) {
   // This is a forever while loop, it loops over subframes which are scheduled by incoming samples from HW devices
   while (!oai_exit) {
 
+    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_CPUID_RU_THREAD,sched_getcpu());
+
     // these are local subframe/frame counters to check that we are in synch with the fronthaul timing.
     // They are set on the first rx/tx in the underly FH routines.
     if (subframe==9) { 
@@ -1537,6 +1552,7 @@ static void* ru_thread( void* param ) {
     // wakeup all eNB processes waiting for this RU
     if (ru->num_eNB>0) wakeup_eNBs(ru);
 
+    /*
 	if(fh_two_thread == 0)
 	{
 		// wait until eNBs are finished subframe RX n and TX n+4
@@ -1553,7 +1569,7 @@ static void* ru_thread( void* param ) {
 	
 		if (ru->fh_north_out) ru->fh_north_out(ru);
 	}
-
+    */
   }
   
 
diff --git a/targets/RT/USER/lte-softmodem.c b/targets/RT/USER/lte-softmodem.c
index e21fd12428..b126e70a25 100644
--- a/targets/RT/USER/lte-softmodem.c
+++ b/targets/RT/USER/lte-softmodem.c
@@ -206,7 +206,7 @@ extern void reset_opp_meas(void);
 extern void print_opp_meas(void);
 
 int transmission_mode=1;
-int numerology = 0;
+int numerology = 1;
 int fh_two_thread = 1;
 
 
-- 
GitLab