lte-enb.c 62 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
/*******************************************************************************
    OpenAirInterface
    Copyright(c) 1999 - 2014 Eurecom

    OpenAirInterface is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.


    OpenAirInterface is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with OpenAirInterface.The full GNU General Public License is
    included in this distribution in the file called "COPYING". If not,
    see <http://www.gnu.org/licenses/>.

   Contact Information
   OpenAirInterface Admin: openair_admin@eurecom.fr
   OpenAirInterface Tech : openair_tech@eurecom.fr
   OpenAirInterface Dev  : openair4g-devel@lists.eurecom.fr

   Address      : Eurecom, Campus SophiaTech, 450 Route des Chappes, CS 50193 - 06904 Biot Sophia Antipolis cedex, FRANCE

*******************************************************************************/

/*! \file lte-enb.c
 * \brief Top-level threads for eNodeB
 * \author R. Knopp, F. Kaltenberger, Navid Nikaein
 * \date 2012
 * \version 0.1
 * \company Eurecom
 * \email: knopp@eurecom.fr,florian.kaltenberger@eurecom.fr, navid.nikaein@eurecom.fr
 * \note
 * \warning
 */
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sched.h>
#include <linux/sched.h>
#include <signal.h>
#include <execinfo.h>
#include <getopt.h>
#include <sys/sysinfo.h>
#include "rt_wrapper.h"

#undef MALLOC //there are two conflicting definitions, so we better make sure we don't use it at all

#include "assertions.h"
#include "msc.h"

#include "PHY/types.h"

#include "PHY/defs.h"
#undef MALLOC //there are two conflicting definitions, so we better make sure we don't use it at all
//#undef FRAME_LENGTH_COMPLEX_SAMPLES //there are two conflicting definitions, so we better make sure we don't use it at all

#include "../../ARCH/COMMON/common_lib.h"

//#undef FRAME_LENGTH_COMPLEX_SAMPLES //there are two conflicting definitions, so we better make sure we don't use it at all

71
#include "PHY/LTE_TRANSPORT/if4_tools.h"
72
#include "PHY/LTE_TRANSPORT/if5_tools.h"
73

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
#include "PHY/extern.h"
#include "SCHED/extern.h"
#include "LAYER2/MAC/extern.h"

#include "../../SIMU/USER/init_lte.h"

#include "LAYER2/MAC/defs.h"
#include "LAYER2/MAC/extern.h"
#include "LAYER2/MAC/proto.h"
#include "RRC/LITE/extern.h"
#include "PHY_INTERFACE/extern.h"

#ifdef SMBV
#include "PHY/TOOLS/smbv.h"
unsigned short config_frames[4] = {2,9,11,13};
#endif
#include "UTIL/LOG/log_extern.h"
#include "UTIL/OTG/otg_tx.h"
#include "UTIL/OTG/otg_externs.h"
#include "UTIL/MATH/oml.h"
#include "UTIL/LOG/vcd_signal_dumper.h"
#include "UTIL/OPT/opt.h"
#include "enb_config.h"
//#include "PHY/TOOLS/time_meas.h"

#ifndef OPENAIR2
#include "UTIL/OTG/otg_extern.h"
#endif

#if defined(ENABLE_ITTI)
# if defined(ENABLE_USE_MME)
#   include "s1ap_eNB.h"
#ifdef PDCP_USE_NETLINK
#   include "SIMULATION/ETH_TRANSPORT/proto.h"
#endif
# endif
#endif

//#define DEBUG_THREADS 1

//#define USRP_DEBUG 1
struct timing_info_t {
  //unsigned int frame, hw_slot, last_slot, next_slot;
  RTIME time_min, time_max, time_avg, time_last, time_now;
  //unsigned int mbox0, mbox1, mbox2, mbox_target;
  unsigned int n_samples;
} timing_info;

122 123
// Fix per CC openair rf/if device update
// extern openair0_device openair0;
124 125 126 127 128 129 130 131 132 133 134 135 136

#if defined(ENABLE_ITTI)
extern volatile int             start_eNB;
extern volatile int             start_UE;
#endif
extern volatile int                    oai_exit;

extern openair0_config_t openair0_cfg[MAX_CARDS];

extern pthread_cond_t sync_cond;
extern pthread_mutex_t sync_mutex;
extern int sync_var;

137
//pthread_t                       main_eNB_thread;
138 139 140

time_stats_t softmodem_stats_mt; // main thread
time_stats_t softmodem_stats_hw; //  hw acquisition
knopp's avatar
knopp committed
141
time_stats_t softmodem_stats_rxtx_sf; // total tx time
142
time_stats_t softmodem_stats_rx_sf; // total rx time
143 144 145 146 147 148 149 150 151 152 153 154 155
int32_t **rxdata;
int32_t **txdata;

static int                      time_offset[4] = {0,0,0,0};

/* mutex, cond and variable to serialize phy proc TX calls
 * (this mechanism may be relaxed in the future for better
 * performances)
 */
static struct {
  pthread_mutex_t  mutex_phy_proc_tx;
  pthread_cond_t   cond_phy_proc_tx;
  volatile uint8_t phy_proc_CC_id;
156
} sync_phy_proc;
157 158 159

void exit_fun(const char* s);

160 161
void init_eNB(eNB_func_t node_function[], eNB_timing_t node_timing[],int nb_inst);
void stop_eNB(int nb_inst);
162 163


164 165
void do_OFDM_mod_rt(int subframe,PHY_VARS_eNB *phy_vars_eNB) {
     
166 167
  unsigned int aa,slot_offset, slot_offset_F;
  int dummy_tx_b[7680*4] __attribute__((aligned(32)));
knopp's avatar
knopp committed
168
  int i,j, tx_offset;
169 170
  int slot_sizeF = (phy_vars_eNB->frame_parms.ofdm_symbol_size)*
                   ((phy_vars_eNB->frame_parms.Ncp==1) ? 6 : 7);
knopp's avatar
knopp committed
171 172
  int len,len2;
  int16_t *txdata;
173 174 175

  slot_offset_F = (subframe<<1)*slot_sizeF;

176
  slot_offset = subframe*phy_vars_eNB->frame_parms.samples_per_tti;
177

178 179
  if ((subframe_select(&phy_vars_eNB->frame_parms,subframe)==SF_DL)||
      ((subframe_select(&phy_vars_eNB->frame_parms,subframe)==SF_S))) {
180 181
    //    LOG_D(HW,"Frame %d: Generating slot %d\n",frame,next_slot);

182 183 184
    for (aa=0; aa<phy_vars_eNB->frame_parms.nb_antennas_tx; aa++) {
      if (phy_vars_eNB->frame_parms.Ncp == EXTENDED) {
        PHY_ofdm_mod(&phy_vars_eNB->common_vars.txdataF[0][aa][slot_offset_F],
185
                     dummy_tx_b,
186
                     phy_vars_eNB->frame_parms.ofdm_symbol_size,
187
                     6,
188
                     phy_vars_eNB->frame_parms.nb_prefix_samples,
189
                     CYCLIC_PREFIX);
190 191 192
        PHY_ofdm_mod(&phy_vars_eNB->common_vars.txdataF[0][aa][slot_offset_F+slot_sizeF],
                     dummy_tx_b+(phy_vars_eNB->frame_parms.samples_per_tti>>1),
                     phy_vars_eNB->frame_parms.ofdm_symbol_size,
193
                     6,
194
                     phy_vars_eNB->frame_parms.nb_prefix_samples,
195 196
                     CYCLIC_PREFIX);
      } else {
197
        normal_prefix_mod(&phy_vars_eNB->common_vars.txdataF[0][aa][slot_offset_F],
198 199
                          dummy_tx_b,
                          7,
200
                          &(phy_vars_eNB->frame_parms));
201
	// if S-subframe generate first slot only
202
	if (subframe_select(&phy_vars_eNB->frame_parms,subframe) == SF_DL) 
203 204
	  normal_prefix_mod(&phy_vars_eNB->common_vars.txdataF[0][aa][slot_offset_F+slot_sizeF],
			    dummy_tx_b+(phy_vars_eNB->frame_parms.samples_per_tti>>1),
205
			    7,
206
			    &(phy_vars_eNB->frame_parms));
207 208 209
      }

      // if S-subframe generate first slot only
210 211
      if (subframe_select(&phy_vars_eNB->frame_parms,subframe) == SF_S)
	len = phy_vars_eNB->frame_parms.samples_per_tti>>1;
212
      else
213
	len = phy_vars_eNB->frame_parms.samples_per_tti;
214 215 216 217 218 219 220
      /*
      for (i=0;i<len;i+=4) {
	dummy_tx_b[i] = 0x100;
	dummy_tx_b[i+1] = 0x01000000;
	dummy_tx_b[i+2] = 0xff00;
	dummy_tx_b[i+3] = 0xff000000;
	}*/
knopp's avatar
knopp committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234
      
      if (slot_offset+time_offset[aa]<0) {
	txdata = (int16_t*)&phy_vars_eNB->common_vars.txdata[0][aa][(LTE_NUMBER_OF_SUBFRAMES_PER_FRAME*phy_vars_eNB->frame_parms.samples_per_tti)+tx_offset];
        len2 = -(slot_offset+time_offset[aa]);
	len2 = (len2>len) ? len : len2;
	for (i=0; i<(len2<<1); i++) {
	  txdata[i] = ((int16_t*)dummy_tx_b)[i]<<openair0_cfg[0].iq_txshift;
	}
	if (len2<len) {
	  txdata = (int16_t*)&phy_vars_eNB->common_vars.txdata[0][aa][0];
	  for (j=0; i<(len<<1); i++,j++) {
	    txdata[j++] = ((int16_t*)dummy_tx_b)[i]<<openair0_cfg[0].iq_txshift;
	  }
	}
235
      }  
knopp's avatar
knopp committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
      else if ((slot_offset+time_offset[aa]+len)>(LTE_NUMBER_OF_SUBFRAMES_PER_FRAME*phy_vars_eNB->frame_parms.samples_per_tti)) {
	tx_offset = (int)slot_offset+time_offset[aa];
	txdata = (int16_t*)&phy_vars_eNB->common_vars.txdata[0][aa][tx_offset];
	len2 = -tx_offset+LTE_NUMBER_OF_SUBFRAMES_PER_FRAME*phy_vars_eNB->frame_parms.samples_per_tti;
	for (i=0; i<(len2<<1); i++) {
	  txdata[i] = ((int16_t*)dummy_tx_b)[i]<<openair0_cfg[0].iq_txshift;
	}
	txdata = (int16_t*)&phy_vars_eNB->common_vars.txdata[0][aa][0];
	for (j=0; i<(len<<1); i++,j++) {
	  txdata[j++] = ((int16_t*)dummy_tx_b)[i]<<openair0_cfg[0].iq_txshift;
	}
      }
      else {
	tx_offset = (int)slot_offset+time_offset[aa];
	txdata = (int16_t*)&phy_vars_eNB->common_vars.txdata[0][aa][tx_offset];
251

knopp's avatar
knopp committed
252 253 254 255 256
	for (i=0; i<(len<<1); i++) {
	  txdata[i] = ((int16_t*)dummy_tx_b)[i]<<openair0_cfg[0].iq_txshift;
	}
      }
      
257
     // if S-subframe switch to RX in second subframe
knopp's avatar
knopp committed
258
      /*
259
     if (subframe_select(&phy_vars_eNB->frame_parms,subframe) == SF_S) {
260
       for (i=0; i<len; i++) {
261
	 phy_vars_eNB->common_vars.txdata[0][aa][tx_offset++] = 0x00010001;
262 263
       }
     }
knopp's avatar
knopp committed
264
      */
265
     if ((((phy_vars_eNB->frame_parms.tdd_config==0) ||
266 267 268 269
	   (phy_vars_eNB->frame_parms.tdd_config==1) ||
	   (phy_vars_eNB->frame_parms.tdd_config==2) ||
	   (phy_vars_eNB->frame_parms.tdd_config==6)) && 
	   (subframe==0)) || (subframe==5)) {
270 271 272
       // turn on tx switch N_TA_offset before
       //LOG_D(HW,"subframe %d, time to switch to tx (N_TA_offset %d, slot_offset %d) \n",subframe,phy_vars_eNB->N_TA_offset,slot_offset);
       for (i=0; i<phy_vars_eNB->N_TA_offset; i++) {
273 274 275
         tx_offset = (int)slot_offset+time_offset[aa]+i-phy_vars_eNB->N_TA_offset/2;
         if (tx_offset<0)
           tx_offset += LTE_NUMBER_OF_SUBFRAMES_PER_FRAME*phy_vars_eNB->frame_parms.samples_per_tti;
276
	 
277 278
         if (tx_offset>=(LTE_NUMBER_OF_SUBFRAMES_PER_FRAME*phy_vars_eNB->frame_parms.samples_per_tti))
           tx_offset -= LTE_NUMBER_OF_SUBFRAMES_PER_FRAME*phy_vars_eNB->frame_parms.samples_per_tti;
279
	 
280
         phy_vars_eNB->common_vars.txdata[0][aa][tx_offset] = 0x00000000;
281 282 283 284 285 286
       }
     }
    }
  }
}

287

288
/*!
knopp's avatar
knopp committed
289
 * \brief The RX UE-specific and TX thread of eNB.
290 291 292
 * \param param is a \ref eNB_proc_t structure which contains the info what to process.
 * \returns a pointer to an int. The storage is not on the heap and must not be freed.
 */
293 294
static void* eNB_thread_rxtx( void* param ) {

knopp's avatar
knopp committed
295
  static int eNB_thread_rxtx_status;
296

knopp's avatar
knopp committed
297
  eNB_rxtx_proc_t *proc = (eNB_rxtx_proc_t*)param;
298 299
  FILE  *tx_time_file = NULL;
  char tx_time_name[101];
300
  void *txp[PHY_vars_eNB_g[0][0]->frame_parms.nb_antennas_tx]; 
Sandeep Kumar's avatar
Sandeep Kumar committed
301 302 303 304
  
  uint16_t packet_type;
  uint32_t symbol_number=0;
  
Sandeep Kumar's avatar
Sandeep Kumar committed
305 306
  uint8_t seqno=0;
  
307
  if (opp_enabled == 1) {
308
    snprintf(tx_time_name, 100,"/tmp/%s_tx_time_thread_sf", "eNB");
309 310 311
    tx_time_file = fopen(tx_time_name,"w");
  }
  // set default return value
knopp's avatar
knopp committed
312
  eNB_thread_rxtx_status = 0;
313 314 315

  MSC_START_USE();

316
#ifdef DEADLINE_SCHEDULER
317
  struct sched_attr attr;
318

319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
  unsigned int flags = 0;
  uint64_t runtime  = 850000 ;  
  uint64_t deadline = 1   *  1000000 ; // each tx thread will finish within 1ms
  uint64_t period   = 1   * 10000000; // each tx thread has a period of 10ms from the starting point

  attr.size = sizeof(attr);
  attr.sched_flags = 0;
  attr.sched_nice = 0;
  attr.sched_priority = 0;

  attr.sched_policy   = SCHED_DEADLINE;
  attr.sched_runtime  = runtime;
  attr.sched_deadline = deadline;
  attr.sched_period   = period; 

  if (sched_setattr(0, &attr, flags) < 0 ) {
    perror("[SCHED] eNB tx thread: sched_setattr failed\n");
knopp's avatar
knopp committed
336
    return &eNB_thread_rxtx_status;
337 338
  }

knopp's avatar
knopp committed
339
  LOG_I( HW, "[SCHED] eNB RXn-TXnp4 deadline thread (TID %ld) started on CPU %d\n", gettid(), sched_getcpu() );
340 341 342 343 344 345

#else //LOW_LATENCY
  int policy, s, j;
  struct sched_param sparam;
  char cpu_affinity[1024];
  cpu_set_t cpuset;
Raymond Knopp's avatar
Raymond Knopp committed
346 347 348 349
  struct timespec wait;

  wait.tv_sec=0;
  wait.tv_nsec=5000000L;
350 351 352

  /* Set affinity mask to include CPUs 1 to MAX_CPUS */
  /* CPU 0 is reserved for UHD threads */
knopp's avatar
knopp committed
353
  /* CPU 1 is reserved for all RX_TX threads */
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
  /* Enable CPU Affinity only if number of CPUs >2 */
  CPU_ZERO(&cpuset);

#ifdef CPU_AFFINITY
  if (get_nprocs() > 2)
  {
    for (j = 1; j < get_nprocs(); j++)
        CPU_SET(j, &cpuset);
    s = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
    if (s != 0)
    {
      perror( "pthread_setaffinity_np");
      exit_fun("Error setting processor affinity");
    }
  }
#endif //CPU_AFFINITY

  /* Check the actual affinity mask assigned to the thread */
  s = pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
373
  if (s != 0) {
374 375 376 377 378
    perror( "pthread_getaffinity_np");
    exit_fun("Error getting processor affinity ");
  }
  memset(cpu_affinity,0,sizeof(cpu_affinity));
  for (j = 0; j < CPU_SETSIZE; j++)
379 380 381 382 383
    if (CPU_ISSET(j, &cpuset)) {  
      char temp[1024];
      sprintf (temp, " CPU_%d", j);
      strcat(cpu_affinity, temp);
    }
384

385
  memset(&sparam, 0, sizeof(sparam));
386 387 388 389
  sparam.sched_priority = sched_get_priority_max(SCHED_FIFO)-1;
  policy = SCHED_FIFO ; 
  
  s = pthread_setschedparam(pthread_self(), policy, &sparam);
390 391 392 393 394
  if (s != 0) {
    perror("pthread_setschedparam : ");
    exit_fun("Error setting thread priority");
  }
  
395
  s = pthread_getschedparam(pthread_self(), &policy, &sparam);
396 397 398 399
  if (s != 0) {
    perror("pthread_getschedparam : ");
    exit_fun("Error getting thread priority");
  }
400

knopp's avatar
knopp committed
401
  LOG_I(HW, "[SCHED][eNB] RXn_TXnp4 thread started on CPU %d TID %ld, sched_policy = %s , priority = %d, CPU Affinity=%s \n",sched_getcpu(),gettid(),
402 403 404 405 406 407 408 409 410 411 412
                   (policy == SCHED_FIFO)  ? "SCHED_FIFO" :
                   (policy == SCHED_RR)    ? "SCHED_RR" :
                   (policy == SCHED_OTHER) ? "SCHED_OTHER" :
                   "???",
                   sparam.sched_priority, cpu_affinity );

#endif //LOW_LATENCY

  mlockall(MCL_CURRENT | MCL_FUTURE);

  while (!oai_exit) {
knopp's avatar
knopp committed
413
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_eNB_PROC_RXTX0+(proc->subframe_rx&1), 0 );
414

Raymond Knopp's avatar
Raymond Knopp committed
415
    if (pthread_mutex_timedlock(&proc->mutex_rxtx,&wait) != 0) {
knopp's avatar
knopp committed
416
      LOG_E( PHY, "[SCHED][eNB] error locking mutex for eNB RXn-TXnp4\n");
417 418 419 420
      exit_fun("nothing to add");
      break;
    }

knopp's avatar
knopp committed
421
    while (proc->instance_cnt_rxtx < 0) {
422
      // most of the time the thread is waiting here
knopp's avatar
knopp committed
423 424
      // proc->instance_cnt_rxtx is -1
      pthread_cond_wait( &proc->cond_rxtx, &proc->mutex_rxtx ); // this unlocks mutex_rxtx while waiting and then locks it again
425 426
    }

knopp's avatar
knopp committed
427
    if (pthread_mutex_unlock(&proc->mutex_rxtx) != 0) {
428
      LOG_E(PHY,"[SCHED][eNB] error unlocking mutex for eNB TX\n");
429 430 431 432
      exit_fun("nothing to add");
      break;
    }

knopp's avatar
knopp committed
433 434
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_eNB_PROC_RXTX0+(proc->subframe_rx&1), 1 );
    start_meas( &softmodem_stats_rxtx_sf );
435
  
436 437
    if (oai_exit) break;

438 439 440
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_FRAME_NUMBER_RX0_ENB+(proc->subframe_rx&1), proc->frame_rx );
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_SUBFRAME_NUMBER_RX0_ENB+(proc->subframe_rx&1), proc->subframe_rx );

441 442 443
    // Common procedures
    phy_procedures_eNB_common_RX(PHY_vars_eNB_g[0][proc->CC_id], 0);

knopp's avatar
knopp committed
444
    // UE-specific RX processing for subframe n
445 446 447
    if ((PHY_vars_eNB_g[0][proc->CC_id]->node_function == eNodeB_3GPP) ||
        (PHY_vars_eNB_g[0][proc->CC_id]->node_function == eNodeB_3GPP_BBU) ||
        (PHY_vars_eNB_g[0][proc->CC_id]->node_function == NGFI_RCC_IF4)) {
448

knopp's avatar
knopp committed
449 450 451 452 453
      // this is the ue-specific processing for the subframe and can be multi-threaded later
      phy_procedures_eNB_uespec_RX(PHY_vars_eNB_g[0][proc->CC_id], proc, 0, no_relay );
    }

    // TX processing for subframe n+4
454 455 456 457
    if (((PHY_vars_eNB_g[0][proc->CC_id]->frame_parms.frame_type == TDD) &&
         ((subframe_select(&PHY_vars_eNB_g[0][proc->CC_id]->frame_parms,proc->subframe_tx) == SF_DL) ||
          (subframe_select(&PHY_vars_eNB_g[0][proc->CC_id]->frame_parms,proc->subframe_tx) == SF_S))) ||
        (PHY_vars_eNB_g[0][proc->CC_id]->frame_parms.frame_type == FDD)) {
458 459 460
      /* run PHY TX procedures the one after the other for all CCs to avoid race conditions
       * (may be relaxed in the future for performance reasons)
       */
knopp's avatar
knopp committed
461
      
Raymond Knopp's avatar
Raymond Knopp committed
462
      if (pthread_mutex_timedlock(&sync_phy_proc.mutex_phy_proc_tx,&wait) != 0) {
463
        LOG_E(PHY, "[SCHED][eNB] error locking PHY proc mutex for eNB TX\n");
464 465 466
        exit_fun("nothing to add");
        break;
      }
467
      
knopp's avatar
knopp committed
468
      // wait for our turn or oai_exit
469 470 471
      while (sync_phy_proc.phy_proc_CC_id != proc->CC_id && !oai_exit) {
        pthread_cond_wait(&sync_phy_proc.cond_phy_proc_tx,
                          &sync_phy_proc.mutex_phy_proc_tx);
472 473
      }

474 475
      if (pthread_mutex_unlock(&sync_phy_proc.mutex_phy_proc_tx) != 0) {
        LOG_E(PHY, "[SCHED][eNB] error unlocking PHY proc mutex for eNB TX\n");
476 477 478
        exit_fun("nothing to add");
      }

479 480
      VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_FRAME_NUMBER_TX0_ENB+(proc->subframe_tx&1), proc->frame_tx );
      VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_SUBFRAME_NUMBER_TX0_ENB+(proc->subframe_tx&1), proc->subframe_tx );
481 482 483
      
      if (oai_exit) break;
      
484 485 486
      if ((PHY_vars_eNB_g[0][proc->CC_id]->node_function == eNodeB_3GPP) ||
          (PHY_vars_eNB_g[0][proc->CC_id]->node_function == eNodeB_3GPP_BBU) ||
          (PHY_vars_eNB_g[0][proc->CC_id]->node_function == NGFI_RCC_IF4)) { 
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
        phy_procedures_eNB_TX(PHY_vars_eNB_g[0][proc->CC_id], proc, 0, no_relay, NULL );

        /* we're done, let the next one proceed */
        if (pthread_mutex_lock(&sync_phy_proc.mutex_phy_proc_tx) != 0) {
          LOG_E(PHY, "[SCHED][eNB] error locking PHY proc mutex for eNB TX proc\n");
          exit_fun("nothing to add");
          break;
        }	
        sync_phy_proc.phy_proc_CC_id++;
        sync_phy_proc.phy_proc_CC_id %= MAX_NUM_CCs;
        pthread_cond_broadcast(&sync_phy_proc.cond_phy_proc_tx);
        if (pthread_mutex_unlock(&sync_phy_proc.mutex_phy_proc_tx) != 0) {
          LOG_E(PHY, "[SCHED][eNB] error unlocking PHY proc mutex for eNB TX proc\n");
          exit_fun("nothing to add");
          break;
Sandeep Kumar's avatar
Sandeep Kumar committed
502
        }
503
      } else if (PHY_vars_eNB_g[0][proc->CC_id]->node_function == NGFI_RRU_IF4) {
504
        /// **** recv_IF4 of txdataF from RCC **** ///             
Sandeep Kumar's avatar
Sandeep Kumar committed
505
        VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_RECV_IF4, 1 );  
Sandeep Kumar's avatar
Sandeep Kumar committed
506
        do { 
507
          recv_IF4(PHY_vars_eNB_g[0][proc->CC_id], &proc->frame_tx, &proc->subframe_tx, &packet_type, &symbol_number);
Sandeep Kumar's avatar
Sandeep Kumar committed
508
        } while (symbol_number < PHY_vars_eNB_g[0][proc->CC_id]->frame_parms.symbols_per_tti-1); 
Sandeep Kumar's avatar
Sandeep Kumar committed
509
        
510
        VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_RECV_IF4, 0 );   
511
        
512 513 514
      } else if (PHY_vars_eNB_g[0][proc->CC_id]->node_function == NGFI_RRU_IF5) {
        /// **** recv_IF5 of txdata from BBU **** ///       
        VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_RECV_IF5, 1 );  
Sandeep Kumar's avatar
Sandeep Kumar committed
515
        recv_IF5(PHY_vars_eNB_g[0][proc->CC_id], &proc->timestamp_tx, proc->subframe_tx, IF5_RRH_GW_DL);
516 517
        VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_RECV_IF5, 0 );  

518 519 520
      }
    }

521
    // eNodeB_3GPP, _BBU and RRU create txdata
522 523 524
    if ((PHY_vars_eNB_g[0][proc->CC_id]->node_function == eNodeB_3GPP) ||
        (PHY_vars_eNB_g[0][proc->CC_id]->node_function == eNodeB_3GPP_BBU) ||
        (PHY_vars_eNB_g[0][proc->CC_id]->node_function == NGFI_RRU_IF4)) {
525
      VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_SFGEN , 1 );
526
      do_OFDM_mod_rt( proc->subframe_tx, PHY_vars_eNB_g[0][proc->CC_id] );
527
      VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_SFGEN , 0 );
528 529
    }

530 531 532 533 534 535 536 537 538 539 540 541
      /*
        short *txdata = (short*)&PHY_vars_eNB_g[0][proc->CC_id]->common_vars.txdata[0][0][proc->subframe_tx*PHY_vars_eNB_g[0][proc->CC_id]->frame_parms.samples_per_tti];
        int i;
        for (i=0;i<PHY_vars_eNB_g[0][proc->CC_id]->frame_parms.samples_per_tti*2;i+=8) {
        txdata[i] = 2047;
        txdata[i+1] = 0;
        txdata[i+2] = 0;
        txdata[i+3] = 2047;
        txdata[i+4] = -2047;
        txdata[i+5] = 0;
        txdata[i+6] = 0;
        txdata[i+7] = -2047;      }
542 543
      */      
          
544

545
    // eNodeB_3GPP, RRU write to RF device    
546 547 548
    if ((PHY_vars_eNB_g[0][proc->CC_id]->node_function == eNodeB_3GPP) ||
        (PHY_vars_eNB_g[0][proc->CC_id]->node_function == NGFI_RRU_IF4) ||
        (PHY_vars_eNB_g[0][proc->CC_id]->node_function == NGFI_RRU_IF5)) {
549
      // Transmit TX buffer based on timestamp from RX  
550 551 552 553
      VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_TRX_WRITE, 1 );
      // prepare tx buffer pointers
      int i;
      for (i=0; i<PHY_vars_eNB_g[0][0]->frame_parms.nb_antennas_tx; i++)
554 555
        txp[i] = (void*)&PHY_vars_eNB_g[0][0]->common_vars.txdata[0][i][proc->subframe_tx*PHY_vars_eNB_g[0][0]->frame_parms.samples_per_tti];
    
556
      // if symb_written < spp ==> error 
557 558 559 560 561 562 563
      PHY_vars_eNB_g[0][proc->CC_id]->rfdevice.trx_write_func(&PHY_vars_eNB_g[0][proc->CC_id]->rfdevice,
            (proc->timestamp_tx-openair0_cfg[0].tx_sample_advance),
            txp,
            PHY_vars_eNB_g[0][0]->frame_parms.samples_per_tti,
            PHY_vars_eNB_g[0][0]->frame_parms.nb_antennas_tx,
            1);
	      
564
      VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_TRX_WRITE, 0 );
565
 
566 567
      VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_TRX_TST, (proc->timestamp_tx-openair0_cfg[0].tx_sample_advance)&0xffffffff );

568
    } else if (PHY_vars_eNB_g[0][proc->CC_id]->node_function == eNodeB_3GPP_BBU) {
569 570
      /// **** send_IF5 of txdata to RRH **** ///       
      VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_SEND_IF5, 1 );  
Sandeep Kumar's avatar
Sandeep Kumar committed
571
      send_IF5(PHY_vars_eNB_g[0][proc->CC_id], proc->timestamp_tx, proc->subframe_tx, &seqno, IF5_RRH_GW_DL);
572
      VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_SEND_IF5, 0 );  
Sandeep Kumar's avatar
Sandeep Kumar committed
573

574
    } else { 
575
      /// **** send_IF4 of txdataF to RRU **** ///       
576
      VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_SEND_IF4, 1 );   
577
      send_IF4(PHY_vars_eNB_g[0][proc->CC_id], proc->frame_tx, proc->subframe_tx, IF4_PDLFFT, 0);
578
      VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_SEND_IF4, 0 );
Sandeep Kumar's avatar
Sandeep Kumar committed
579
      
580 581
    }

knopp's avatar
knopp committed
582
    if (pthread_mutex_lock(&proc->mutex_rxtx) != 0) {
583
      LOG_E( PHY, "[SCHED][eNB] error locking mutex for eNB TX proc\n");
584 585 586 587
      exit_fun("nothing to add");
      break;
    }

knopp's avatar
knopp committed
588
    proc->instance_cnt_rxtx--;
589

knopp's avatar
knopp committed
590
    if (pthread_mutex_unlock(&proc->mutex_rxtx) != 0) {
591
      LOG_E( PHY, "[SCHED][eNB] error unlocking mutex for eNB TX proc\n");
592 593 594 595
      exit_fun("nothing to add");
      break;
    }

knopp's avatar
knopp committed
596
    stop_meas( &softmodem_stats_rxtx_sf );
597

598
#ifdef DEADLINE_SCHEDULER
599
    if (opp_enabled){
knopp's avatar
knopp committed
600 601
      if(softmodem_stats_rxtx_sf.diff_now/(cpuf) > attr.sched_runtime){
	VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_RUNTIME_TX_ENB, (softmodem_stats_rxtx_sf.diff_now/cpuf - attr.sched_runtime)/1000000.0);
602 603 604
    }
#endif 

605
    print_meas_now(&softmodem_stats_rxtx_sf,"eNB_TX_SF",tx_time_file);
606 607
  }

knopp's avatar
knopp committed
608
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_eNB_PROC_RXTX0+(proc->subframe_rx&1), 0 );
609

Raymond Knopp's avatar
Raymond Knopp committed
610
  printf( "Exiting eNB thread RXn_TXnp4\n");
611

knopp's avatar
knopp committed
612 613
  eNB_thread_rxtx_status = 0;
  return &eNB_thread_rxtx_status;
614 615
}

616
#if defined(ENABLE_ITTI)
617 618 619
/* Wait for eNB application initialization to be complete (eNB registration to MME) */
static void wait_system_ready (char *message, volatile int *start_flag) {
  
620
  static char *indicator[] = {".    ", "..   ", "...  ", ".... ", ".....",
621
			      " ....", "  ...", "   ..", "    .", "     "};
622 623 624 625 626 627 628
  int i = 0;
  
  while ((!oai_exit) && (*start_flag == 0)) {
    LOG_N(EMU, message, indicator[i]);
    fflush(stdout);
    i = (i + 1) % (sizeof(indicator) / sizeof(indicator[0]));
    usleep(200000);
629
  }
630 631
  
  LOG_D(EMU,"\n");
632 633
}
#endif
634

635 636 637 638 639 640 641 642 643 644 645 646 647
/*!
 * \brief The Asynchronous RX FH thread of RAU/RCC/eNB.
 * This handles the RX FH for an asynchronous RRU/UE
 * \param param is a \ref eNB_proc_t structure which contains the info what to process.
 * \returns a pointer to an int. The storage is not on the heap and must not be freed.
 */
static void* eNB_thread_asynch_rx( void* param ) {

  eNB_proc_t *proc = (eNB_proc_t*)param;
  PHY_VARS_eNB *eNB = PHY_vars_eNB_g[0][proc->CC_id];
  LTE_DL_FRAME_PARMS *fp = &eNB->frame_parms;
  openair0_timestamp timestamp_rx;
  int frame_rx,subframe_rx;
knopp's avatar
knopp committed
648
  static int first_rx = 1;
649 650 651 652
  uint16_t packet_type;
  uint32_t symbol_number=0;
  uint32_t symbol_mask, symbol_mask_full;
  int prach_rx;
653 654
  int dummy_rx[fp->nb_antennas_rx][fp->samples_per_tti]; 
  int rxs;
655

knopp's avatar
knopp committed
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
#ifdef DEADLINE_SCHEDULER
  struct sched_attr attr;
  unsigned int flags = 0;
  uint64_t runtime  = 870000 ;
  uint64_t deadline = 1   *  1000000;
  uint64_t period   = 1   * 10000000; // each rx thread has a period of 10ms from the starting point
 
  attr.size = sizeof(attr);
  attr.sched_flags = 0;
  attr.sched_nice = 0;
  attr.sched_priority = 0;

  attr.sched_policy = SCHED_DEADLINE;
  attr.sched_runtime  = runtime;
  attr.sched_deadline = deadline;
  attr.sched_period   = period; 

  if (sched_setattr(0, &attr, flags) < 0 ) {
    perror("[SCHED] eNB FH sched_setattr failed\n");
    return &eNB_thread_FH_status;
  }

  LOG_I( HW, "[SCHED] eNB asynch RX deadline thread (TID %ld) started on CPU %d\n", gettid(), sched_getcpu() );
#else // LOW_LATENCY
  int policy, s, j;
  struct sched_param sparam;
  char cpu_affinity[1024];
  cpu_set_t cpuset;

  /* Set affinity mask to include CPUs 1 to MAX_CPUS */
  /* CPU 0 is reserved for UHD */
  /* CPU 1 is reserved for all TX threads */
  /* CPU 2..MAX_CPUS is reserved for all RX threads */
  /* Set CPU Affinity only if number of CPUs >2 */
  CPU_ZERO(&cpuset);
#ifdef CPU_AFFINITY
  if (get_nprocs() >2) {
    for (j = 1; j < get_nprocs(); j++)
      CPU_SET(j, &cpuset);
  
    s = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
    if (s != 0) {
      perror( "pthread_setaffinity_np");  
      exit_fun (" Error setting processor affinity :");
    }
  }
#endif //CPU_AFFINITY
  /* Check the actual affinity mask assigned to the thread */

  s = pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
  if (s != 0) {
    perror ("pthread_getaffinity_np");
    exit_fun (" Error getting processor affinity :");
  }
  memset(cpu_affinity,0, sizeof(cpu_affinity));

  for (j = 0; j < CPU_SETSIZE; j++)
    if (CPU_ISSET(j, &cpuset)) {  
      char temp[1024];
      sprintf (temp, " CPU_%d", j);
      strcat(cpu_affinity, temp);
    }

  memset(&sparam, 0 , sizeof (sparam)); 
  sparam.sched_priority = sched_get_priority_max(SCHED_FIFO);

  policy = SCHED_FIFO ; 
  s = pthread_setschedparam(pthread_self(), policy, &sparam);
  if (s != 0) {
    perror("pthread_setschedparam : ");
    exit_fun("Error setting thread priority");     
  }

  memset(&sparam, 0 , sizeof (sparam));

  s = pthread_getschedparam(pthread_self(), &policy, &sparam);
  if (s != 0) {
    perror("pthread_getschedparam");
    exit_fun("Error getting thread priority");
  }

  LOG_I(HW, "[SCHED][eNB] eNB asynch RX thread started on CPU %d TID %ld, sched_policy = %s, priority = %d, CPU Affinity = %s\n", sched_getcpu(),gettid(),
	 (policy == SCHED_FIFO)  ? "SCHED_FIFO" :
	 (policy == SCHED_RR)    ? "SCHED_RR" :
	 (policy == SCHED_OTHER) ? "SCHED_OTHER" :
	 "???",
	 sparam.sched_priority, cpu_affinity);
  
  
#endif // DEADLINE_SCHEDULER

747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774

  mlockall(MCL_CURRENT | MCL_FUTURE);

  // wait for top-level synchronization and do one acquisition to get timestamp for setting frame/subframe
  printf( "waiting for sync (eNB_thread_asynch_rx)\n");
  pthread_mutex_lock( &sync_mutex );

  while (sync_var<0)
    pthread_cond_wait( &sync_cond, &sync_mutex );
 
  pthread_mutex_unlock(&sync_mutex);
 
  printf( "got sync (eNB_thread_asynch_rx)\n" );

  if (eNB->node_function == eNodeB_3GPP) { // acquisition from RF

    if (eNB->rfdevice.trx_read_func)
      rxs = eNB->rfdevice.trx_read_func(&eNB->rfdevice,
					&proc->timestamp_rx,
					(void**)dummy_rx,
					fp->samples_per_tti,
					fp->nb_antennas_rx);
    else {
      printf("eNB asynch RX\n");
      sleep(1);
    }
  }
  else if (eNB->node_function == eNodeB_3GPP_BBU) { // acquisition from IF
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
      /// **** recv_IF5 of rxdata from RRH **** ///       
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_RECV_IF5, 1 );  
    recv_IF5(eNB, &timestamp_rx, subframe_rx++, IF5_RRH_GW_UL); 
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_RECV_IF5, 0 );  
    if (first_rx == 1) {
      first_rx = 0;
      subframe_rx = (timestamp_rx/fp->samples_per_tti)%10;
    }
    else {
    // check timestamp
      if ((timestamp_rx - proc->timestamp_rx) < (2*fp->samples_per_tti))
	printf("RX overflow ...\n");

    }
  } // eNodeB_3GPP_BBU 
  
  else if (eNB->node_function == NGFI_RCC_IF4) {
      /// **** recv_IF4 of rxdataF from RRU **** ///
      /// **** recv_IF4 of rxsigF from RRU **** ///
      // get frame/subframe information from IF4 interface
      // timed loop (200 us)
      
      symbol_number = 0;
      symbol_mask = 0;
      symbol_mask_full = (1<<fp->symbols_per_tti)-1;
      prach_rx = 0;
         
      do {   // Blocking, we need a timeout on this !!!!!!!!!!!!!!!!!!!!!!!
        VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_RECV_IF4, 1 );   
        recv_IF4(eNB, &frame_rx, &subframe_rx, &packet_type, &symbol_number);
        VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_RECV_IF4, 0 );   

        if (packet_type == IF4_PULFFT) {
          symbol_mask = symbol_mask | (1<<symbol_number);     
                       
        } else if (packet_type == IF4_PRACH) {
          // wake up thread for PRACH RX
          prach_rx = 1;
Sandeep Kumar's avatar
Sandeep Kumar committed
813
        }
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
      } while( (symbol_mask != symbol_mask_full) && (prach_rx == 0));    

      if (proc->first_rx == 0) {
        if (subframe_rx < proc->subframe_rx+2){
          LOG_E(PHY,"RX overflow (proc->subframe_rx %d, subframe_rx %d)\n",proc->subframe_rx,subframe_rx);
        }
      } else {
        proc->first_rx = 0;
      }
    } // node_timing == synch_to_externs, node_function = NGFI_IF4
    else { // should not get here
      AssertFatal(1==0, "Unknown eNB->node_function %d",eNB->node_function);
    }

}
829

830
/*!
831 832 833
 * \brief The Fronthaul thread of RRU/RAU/RCC/eNB
 * In the case of RRU/eNB, handles interface with external RF
 * In the case of RAU/RCC, handles fronthaul interface with RRU/RAU
834 835 836
 * \param param is a \ref eNB_proc_t structure which contains the info what to process.
 * \returns a pointer to an int. The storage is not on the heap and must not be freed.
 */
837
static void* eNB_thread_FH( void* param ) {
838
  
839
  static int eNB_thread_FH_status;
840 841

  eNB_proc_t *proc = (eNB_proc_t*)param;
842 843
  PHY_VARS_eNB *eNB = PHY_vars_eNB_g[0][proc->CC_id];
  LTE_DL_FRAME_PARMS *fp = &eNB->frame_parms;
844 845
  void *rxp[fp->nb_antennas_rx]; 
  unsigned int rxs;
846 847
  FILE  *rx_time_file = NULL;
  char rx_time_name[101];
Raymond Knopp's avatar
Raymond Knopp committed
848
  struct timespec wait;
849 850 851 852 853 854 855
  int i;
  int prach_rx;

  uint16_t packet_type;
  uint32_t symbol_number=0;
  uint32_t symbol_mask, symbol_mask_full;

Sandeep Kumar's avatar
Sandeep Kumar committed
856 857
  int subframe=0, frame=0; 
  
Raymond Knopp's avatar
Raymond Knopp committed
858 859
  wait.tv_sec=0;
  wait.tv_nsec=5000000L;
860

861
  if (opp_enabled == 1) {
862
    snprintf(rx_time_name, 100,"/tmp/%s_rx_time_thread_sf", "eNB");
863 864 865
    rx_time_file = fopen(rx_time_name,"w");
  }
  // set default return value
866
  eNB_thread_FH_status = 0;
867 868 869

  MSC_START_USE();

870
#ifdef DEADLINE_SCHEDULER
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
  struct sched_attr attr;
  unsigned int flags = 0;
  uint64_t runtime  = 870000 ;
  uint64_t deadline = 1   *  1000000;
  uint64_t period   = 1   * 10000000; // each rx thread has a period of 10ms from the starting point
 
  attr.size = sizeof(attr);
  attr.sched_flags = 0;
  attr.sched_nice = 0;
  attr.sched_priority = 0;

  attr.sched_policy = SCHED_DEADLINE;
  attr.sched_runtime  = runtime;
  attr.sched_deadline = deadline;
  attr.sched_period   = period; 

  if (sched_setattr(0, &attr, flags) < 0 ) {
knopp's avatar
knopp committed
888
    perror("[SCHED] eNB FH sched_setattr failed\n");
889
    return &eNB_thread_FH_status;
890 891
  }

knopp's avatar
knopp committed
892
  LOG_I( HW, "[SCHED] eNB FH deadline thread (TID %ld) started on CPU %d\n", gettid(), sched_getcpu() );
893 894 895 896 897 898 899 900 901 902 903 904 905
#else // LOW_LATENCY
  int policy, s, j;
  struct sched_param sparam;
  char cpu_affinity[1024];
  cpu_set_t cpuset;

  /* Set affinity mask to include CPUs 1 to MAX_CPUS */
  /* CPU 0 is reserved for UHD */
  /* CPU 1 is reserved for all TX threads */
  /* CPU 2..MAX_CPUS is reserved for all RX threads */
  /* Set CPU Affinity only if number of CPUs >2 */
  CPU_ZERO(&cpuset);
#ifdef CPU_AFFINITY
906
  if (get_nprocs() >2) {
907
    for (j = 1; j < get_nprocs(); j++)
908
      CPU_SET(j, &cpuset);
909 910
  
    s = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
911
    if (s != 0) {
912 913 914 915 916 917 918 919
      perror( "pthread_setaffinity_np");  
      exit_fun (" Error setting processor affinity :");
    }
  }
#endif //CPU_AFFINITY
  /* Check the actual affinity mask assigned to the thread */

  s = pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
920 921 922
  if (s != 0) {
    perror ("pthread_getaffinity_np");
    exit_fun (" Error getting processor affinity :");
923 924 925 926
  }
  memset(cpu_affinity,0, sizeof(cpu_affinity));

  for (j = 0; j < CPU_SETSIZE; j++)
927 928 929 930 931
    if (CPU_ISSET(j, &cpuset)) {  
      char temp[1024];
      sprintf (temp, " CPU_%d", j);
      strcat(cpu_affinity, temp);
    }
932 933

  memset(&sparam, 0 , sizeof (sparam)); 
knopp's avatar
knopp committed
934
  sparam.sched_priority = sched_get_priority_max(SCHED_FIFO);
935 936 937

  policy = SCHED_FIFO ; 
  s = pthread_setschedparam(pthread_self(), policy, &sparam);
938 939 940 941
  if (s != 0) {
    perror("pthread_setschedparam : ");
    exit_fun("Error setting thread priority");     
  }
942 943 944 945

  memset(&sparam, 0 , sizeof (sparam));

  s = pthread_getschedparam(pthread_self(), &policy, &sparam);
946 947 948 949
  if (s != 0) {
    perror("pthread_getschedparam");
    exit_fun("Error getting thread priority");
  }
950

knopp's avatar
knopp committed
951
  LOG_I(HW, "[SCHED][eNB] FH thread started on CPU %d TID %ld, sched_policy = %s, priority = %d, CPU Affinity = %s\n", sched_getcpu(),gettid(),
952 953 954 955 956 957 958
	 (policy == SCHED_FIFO)  ? "SCHED_FIFO" :
	 (policy == SCHED_RR)    ? "SCHED_RR" :
	 (policy == SCHED_OTHER) ? "SCHED_OTHER" :
	 "???",
	 sparam.sched_priority, cpu_affinity);
  
  
959
#endif // DEADLINE_SCHEDULER
960 961


962
  mlockall(MCL_CURRENT | MCL_FUTURE);
963

knopp's avatar
knopp committed
964 965
  // wait for top-level synchronization and do one acquisition to get timestamp for setting frame/subframe
  printf( "waiting for sync (eNB_thread_FH)\n");
966
  pthread_mutex_lock( &sync_mutex );
967

968 969
  while (sync_var<0)
    pthread_cond_wait( &sync_cond, &sync_mutex );
970
 
971
  pthread_mutex_unlock(&sync_mutex);
972
 
973
  printf( "got sync (eNB_thread FH)\n" );
974 975 976 977
 
#if defined(ENABLE_ITTI)
  wait_system_ready ("Waiting for eNB application to be ready %s\r", &start_eNB);
#endif 
978

979 980 981 982 983 984 985 986 987 988 989 990
  // Create buffer for IF device and free when stopping
  if (eNB->node_function == NGFI_RCC_IF4 || eNB->node_function == NGFI_RRU_IF4) {
    malloc_IF4_buffer(eNB);
    
  } else if (eNB->node_function == NGFI_RRU_IF5 || eNB->node_function == eNodeB_3GPP_BBU) {
    //malloc_IF5_buffer(eNB);

  } else {
    eNB->ifbuffer.tx = NULL;
    eNB->ifbuffer.rx = NULL;
  }

991
  // Start IF device for this CC
992
  if (eNB->node_function != eNodeB_3GPP) {
993 994 995 996 997
    if (eNB->ifdevice.trx_start_func(&eNB->ifdevice) != 0 ) 
      LOG_E(HW,"Could not start the IF device\n");
  }
  
  // Start RF device for this CC
998 999 1000
  if ((eNB->node_function == eNodeB_3GPP) || 
      (eNB->node_function == NGFI_RRU_IF4) || 
      (eNB->node_function == NGFI_RRU_IF5)) {
1001 1002 1003
    if (eNB->rfdevice.trx_start_func(&eNB->rfdevice) != 0 ) 
      LOG_E(HW,"Could not start the RF device\n");
  }
1004
      
1005 1006
  // This is a forever while loop, it loops over subframes which are scheduled by incoming samples from HW devices
  while (!oai_exit) {
1007
   
Sandeep Kumar's avatar
Sandeep Kumar committed
1008
    if (oai_exit) break;   
1009

1010 1011 1012 1013 1014 1015 1016 1017
    if (subframe==9) { 
      subframe=0;
      frame++;
      frame&=1023;
    } else {
      subframe++;
    }      

Sandeep Kumar's avatar
Sandeep Kumar committed
1018
    // This case is for synchronization to another thread
1019
    if ((eNB->node_timing == synch_to_other) &&
Sandeep Kumar's avatar
Sandeep Kumar committed
1020 1021
       ((eNB->node_function == NGFI_RCC_IF4) ||
        (eNB->node_function == eNodeB_3GPP_BBU))) {   
1022 1023 1024 1025 1026
      //wait for event

      // how long should we wait here, for MOBIPASS this could be long
      //      if (pthread_mutex_timedlock(&proc->mutex_FH,&wait) != 0) {
      if (pthread_mutex_lock(&proc->mutex_FH) != 0) {
Sandeep Kumar's avatar
Sandeep Kumar committed
1027 1028 1029
        LOG_E( PHY, "[SCHED][eNB] error locking mutex for FH\n");
        exit_fun( "error locking mutex" );
        break;
1030 1031 1032
      }
      
      while (proc->instance_cnt_FH < 0) {
Sandeep Kumar's avatar
Sandeep Kumar committed
1033 1034 1035 1036
        // most of the time the thread is waiting here
        // proc->instance_cnt_prach is -1
        pthread_cond_wait( &proc->cond_FH,&proc->mutex_FH ); // this unlocks mutex_rxtx while waiting and then locks it again
      }      
1037 1038
      proc->instance_cnt_FH++;

1039
    }
1040 1041
    // Remaining cases are all for synchronization on FH interface
    else if ((eNB->node_timing == synch_to_ext_device) &&
Sandeep Kumar's avatar
Sandeep Kumar committed
1042 1043 1044
             ((eNB->node_function == NGFI_RRU_IF4) ||
              (eNB->node_function == NGFI_RRU_IF5) || 
              (eNB->node_function == eNodeB_3GPP))) { // acquisition from RF
1045 1046
      
      for (i=0; i<fp->nb_antennas_rx; i++)
Sandeep Kumar's avatar
Sandeep Kumar committed
1047
        rxp[i] = (void*)&eNB->common_vars.rxdata[0][i][subframe*fp->samples_per_tti];
1048 1049
      
      VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_TRX_READ, 1 );
1050

1051

1052

1053
      rxs = eNB->rfdevice.trx_read_func(&eNB->rfdevice,
Sandeep Kumar's avatar
Sandeep Kumar committed
1054 1055 1056 1057 1058
                                        &proc->timestamp_rx,
                                        rxp,
                                        fp->samples_per_tti,
                                        fp->nb_antennas_rx);

1059 1060 1061 1062
      proc->frame_rx    = (proc->timestamp_rx / (fp->samples_per_tti*10))&1023;
      proc->subframe_rx = (proc->timestamp_rx / fp->samples_per_tti)%10;
      
      if (proc->first_rx == 0) {
Sandeep Kumar's avatar
Sandeep Kumar committed
1063
        if (proc->subframe_rx != subframe){
1064 1065
          LOG_E(PHY,"Received Timestamp doesn't correspond to the time we think it is (proc->subframe_rx %d, subframe %d)\n",proc->subframe_rx,subframe);
          exit_fun("Exiting");
Sandeep Kumar's avatar
Sandeep Kumar committed
1066 1067
        }
        
1068
        if (proc->frame_rx != frame) {
Sandeep Kumar's avatar
Sandeep Kumar committed
1069 1070 1071
          LOG_E(PHY,"Received Timestamp doesn't correspond to the time we think it is (proc->frame_rx %d frame %d)\n",proc->frame_rx,frame);
          exit_fun("Exiting");
        }
1072 1073
      } else {
        proc->first_rx = 0;
Sandeep Kumar's avatar
Sandeep Kumar committed
1074 1075
        frame = proc->frame_rx;
        subframe = proc->subframe_rx;        
1076 1077
      }
      
Sandeep Kumar's avatar
Sandeep Kumar committed
1078
      //printf("timestamp_rx %lu, frame %d(%d), subframe %d(%d)\n",proc->timestamp_rx,proc->frame_rx,frame,proc->subframe_rx,subframe);
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
      
      VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_TRX_TS, proc->timestamp_rx&0xffffffff );
      
      if (rxs != fp->samples_per_tti)
        exit_fun( "problem receiving samples" );
      
      VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_TRX_READ, 0 );
      
    }  // node_timing==synch_to_ext_device && node_function == RRU || eNodeB
    else if ((eNB->node_timing == synch_to_ext_device) &&
Sandeep Kumar's avatar
Sandeep Kumar committed
1089
             (eNB->node_function == eNodeB_3GPP_BBU)) { // acquisition from IF
1090 1091 1092 1093 1094 1095 1096
      /// **** recv_IF5 of rxdata from RRH **** ///       
      VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_RECV_IF5, 1 );  
      recv_IF5(eNB, &proc->timestamp_rx, proc->subframe_rx, IF5_RRH_GW_UL); 
      VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_RECV_IF5, 0 );  
      
      proc->frame_rx    = (proc->timestamp_rx / (fp->samples_per_tti*10))&1023;
      proc->subframe_rx = (proc->timestamp_rx / fp->samples_per_tti)%10;
Sandeep Kumar's avatar
Sandeep Kumar committed
1097

1098
      if (proc->first_rx == 0) {
Sandeep Kumar's avatar
Sandeep Kumar committed
1099 1100 1101 1102 1103 1104 1105 1106 1107
        if (proc->subframe_rx != subframe){
          LOG_E(PHY,"Received Timestamp doesn't correspond to the time we think it is (proc->subframe_rx %d, subframe %d)\n",proc->subframe_rx,subframe);
          exit_fun("Exiting");
        }
        
        if (proc->frame_rx != frame) {
          LOG_E(PHY,"Received Timestamp doesn't correspond to the time we think it is (proc->frame_rx %d frame %d)\n",proc->frame_rx,frame);
          exit_fun("Exiting");
        }
1108
      } else {
Sandeep Kumar's avatar
Sandeep Kumar committed
1109 1110
        proc->first_rx = 0;
      }      
1111 1112
      
      VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_TRX_TS, proc->timestamp_rx&0xffffffff );
Sandeep Kumar's avatar
Sandeep Kumar committed
1113
      
1114 1115
    } // eNodeB_3GPP_BBU && node_timing == synch_to_ext_device
    else if ((eNB->node_timing == synch_to_ext_device) &&
Sandeep Kumar's avatar
Sandeep Kumar committed
1116
             (eNB->node_function == NGFI_RCC_IF4)) {
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
      /// **** recv_IF4 of rxdataF from RRU **** ///
      /// **** recv_IF4 of rxsigF from RRU **** ///
      // timed loop (200 us)
      
      symbol_number = 0;
      symbol_mask = 0;
      symbol_mask_full = (1<<fp->symbols_per_tti)-1;
      prach_rx = 0;
         
      do {   // Blocking, we need a timeout on this !!!!!!!!!!!!!!!!!!!!!!!
        VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_RECV_IF4, 1 );   
        recv_IF4(eNB, &proc->frame_rx, &proc->subframe_rx, &packet_type, &symbol_number);
        VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_RECV_IF4, 0 );   

        if (packet_type == IF4_PULFFT) {
          symbol_mask = symbol_mask | (1<<symbol_number);     
                       
        } else if (packet_type == IF4_PRACH) {
          // wake up thread for PRACH RX
          prach_rx = 1;
Sandeep Kumar's avatar
Sandeep Kumar committed
1137
	    }
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
      } while( (symbol_mask != symbol_mask_full) && (prach_rx == 0));    

      if (proc->first_rx == 0) {
        if (proc->subframe_rx != subframe){
          LOG_E(PHY,"Received Timestamp doesn't correspond to the time we think it is (proc->subframe_rx %d, subframe %d)\n",proc->subframe_rx,subframe);
          //exit_fun("Exiting");
        }
        if (proc->frame_rx != frame) {
          LOG_E(PHY,"Received Timestamp doesn't correspond to the time we think it is (proc->frame_rx %d frame %d)\n",proc->frame_rx,frame);
          //exit_fun("Exiting");
        }
      } else {
        proc->first_rx = 0;
      }
knopp's avatar
knopp committed
1152

1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
      VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_TRX_TS, proc->timestamp_rx&0xffffffff );
		  
    } // node_timing == synch_to_externs, node_function = NGFI_IF4
    else { // should not get here
      AssertFatal(1==0, "Unknown eNB->node_function %d",eNB->node_function);
    }

    // At this point, all information for subframe has been received on FH interface
    // If this proc is to provide synchronization, do so
    for (i=0;i<proc->num_slaves;i++) {
      eNB_proc_t *slave_proc = proc->slave_proc[i];
      // wake up slave FH thread
      // lock the FH mutex and make sure the thread is ready
      if (pthread_mutex_timedlock(&slave_proc->mutex_FH,&wait) != 0) {
Sandeep Kumar's avatar
Sandeep Kumar committed
1167 1168 1169
        LOG_E( PHY, "[eNB] ERROR pthread_mutex_lock for eNB CCid %d slave CCid %d (IC %d)\n",proc->CC_id,slave_proc->CC_id);
        exit_fun( "error locking mutex_rxtx" );
        break;
1170
      }
Sandeep Kumar's avatar
Sandeep Kumar committed
1171

1172 1173 1174 1175 1176 1177 1178 1179
      int cnt_slave            = ++slave_proc->instance_cnt_FH;
      slave_proc->frame_rx     = proc->frame_rx;
      slave_proc->subframe_rx  = proc->subframe_rx;
      slave_proc->timestamp_rx = proc->timestamp_rx;
 
      pthread_mutex_unlock( &slave_proc->mutex_FH );
      
      if (cnt_slave == 0) {
Sandeep Kumar's avatar
Sandeep Kumar committed
1180 1181 1182 1183 1184 1185
      // the thread was presumably waiting where it should and can now be woken up
        if (pthread_cond_signal(&slave_proc->cond_FH) != 0) {
          LOG_E( PHY, "[eNB] ERROR pthread_cond_signal for eNB CCid %d, slave CCid %d\n",proc->CC_id,slave_proc->CC_id);
          exit_fun( "ERROR pthread_cond_signal" );
	        break;
        }
1186
      } else {
knopp's avatar
knopp committed
1187 1188
	LOG_W( PHY,"[eNB] Frame %d, FH CC_id %d thread busy!! (cnt_FH %i)\n",slave_proc->frame_rx,slave_proc->CC_id, cnt_slave);
	exit_fun( "FH thread busy" );
1189 1190 1191 1192 1193
	break;
      }             
    }
      
    // wake up RXn_TXnp4 thread for the subframe
1194 1195 1196 1197 1198
    // choose even or odd thread for RXn-TXnp4 processing 
    eNB_rxtx_proc_t *proc_rxtx = &proc->proc_rxtx[proc->subframe_rx&1];

    // wake up TX for subframe n+4
    // lock the TX mutex and make sure the thread is ready
Raymond Knopp's avatar
Raymond Knopp committed
1199
    if (pthread_mutex_timedlock(&proc_rxtx->mutex_rxtx,&wait) != 0) {
1200
      LOG_E( PHY, "[eNB] ERROR pthread_mutex_lock for eNB RXTX thread %d (IC %d)\n", proc_rxtx->subframe_rx&1,proc_rxtx->instance_cnt_rxtx );
1201 1202 1203 1204
      exit_fun( "error locking mutex_rxtx" );
      break;
    }
    int cnt_rxtx = ++proc_rxtx->instance_cnt_rxtx;
1205
    
1206 1207 1208
    // We have just received and processed the common part of a subframe, say n. 
    // TS_rx is the last received timestamp (start of 1st slot), TS_tx is the desired 
    // transmitted timestamp of the next TX slot (first).
Sandeep Kumar's avatar
Sandeep Kumar committed
1209
    // The last (TS_rx mod samples_per_frame) was n*samples_per_tti, 
1210 1211 1212 1213 1214 1215 1216
    // we want to generate subframe (n+3), so TS_tx = TX_rx+3*samples_per_tti,
    // and proc->subframe_tx = proc->subframe_rx+3
    proc_rxtx->timestamp_tx = proc->timestamp_rx + (4*fp->samples_per_tti);
    proc_rxtx->frame_rx     = proc->frame_rx;
    proc_rxtx->subframe_rx  = proc->subframe_rx;
    proc_rxtx->frame_tx     = (proc_rxtx->subframe_rx > 5) ? (proc_rxtx->frame_rx+1)&1023 : proc_rxtx->frame_rx;
    proc_rxtx->subframe_tx  = (proc_rxtx->subframe_rx + 4)%10;
knopp's avatar
knopp committed
1217
   
1218
    pthread_mutex_unlock( &proc_rxtx->mutex_rxtx );
1219
   
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
    if (cnt_rxtx == 0) {
      // the thread was presumably waiting where it should and can now be woken up
      if (pthread_cond_signal(&proc_rxtx->cond_rxtx) != 0) {
        LOG_E( PHY, "[eNB] ERROR pthread_cond_signal for eNB RXn-TXnp4 thread\n");
        exit_fun( "ERROR pthread_cond_signal" );
        break;
      }
    } else {
      LOG_W( PHY,"[eNB] Frame %d, eNB RXn-TXnp4 thread busy!! (cnt_rxtx %i)\n", proc_rxtx->frame_tx, cnt_rxtx );
      exit_fun( "TX thread busy" );
      break;
Sandeep Kumar's avatar
Sandeep Kumar committed
1231
    }          
1232

1233
    stop_meas( &softmodem_stats_rxtx_sf );
1234
#ifdef DEADLINE_SCHEDULER
1235
    if (opp_enabled){
Sandeep Kumar's avatar
Sandeep Kumar committed
1236 1237
      if(softmodem_stats_rxtx_sf.diff_now/(cpuf) > attr.sched_runtime) {
        VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_RUNTIME_RXTX_ENB, (softmodem_stats_rxtx_sf.diff_now/cpuf - attr.sched_runtime)/1000000.0);
1238 1239
      }
    }
1240
#endif // DEADLINE_SCHEDULER  
1241 1242 1243
    print_meas_now(&softmodem_stats_rx_sf,"eNB_RX_SF", rx_time_file);
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_eNB_PROC_RXTX0+(proc->subframe_rx&1), 0 );
    
1244 1245 1246
    print_meas_now(&softmodem_stats_rx_sf,"eNB_RX_SF", rx_time_file);
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_eNB_PROC_RXTX0+(proc->subframe_rx&1), 0 );
  }
knopp's avatar
knopp committed
1247
 
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
  if (eNB->node_timing == synch_to_ext_device) {
    proc->instance_cnt_FH--;
    
    if (pthread_mutex_unlock(&proc->mutex_FH) != 0) {
      LOG_E( PHY, "[SCHED][eNB] error unlocking mutex for FH\n");
      exit_fun( "error unlocking mutex" );
    }
  }
    
  printf( "Exiting FH thread \n");
knopp's avatar
knopp committed
1258
 
1259 1260
  eNB_thread_FH_status = 0;
  return &eNB_thread_FH_status;
1261 1262 1263 1264 1265 1266 1267 1268
}


/*!
 * \brief The prach receive thread of eNB.
 * \param param is a \ref eNB_proc_t structure which contains the info what to process.
 * \returns a pointer to an int. The storage is not on the heap and must not be freed.
 */
1269
static void* eNB_thread_prach( void* param ) {
1270 1271 1272 1273
  static int eNB_thread_prach_status;

  eNB_proc_t *proc = (eNB_proc_t*)param;
  PHY_VARS_eNB *eNB= PHY_vars_eNB_g[0][proc->CC_id];
Raymond Knopp's avatar
Raymond Knopp committed
1274 1275 1276 1277 1278 1279

  struct timespec wait;

  wait.tv_sec=0;
  wait.tv_nsec=5000000L;

1280 1281 1282 1283 1284
  // set default return value
  eNB_thread_prach_status = 0;

  MSC_START_USE();
    
1285
#ifdef DEADLINE_SCHEDULER
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
  struct sched_attr attr;
  unsigned int flags = 0;
  uint64_t runtime  = 870000 ;
  uint64_t deadline = 1   *  1000000;
  uint64_t period   = 1   * 10000000; // each prach thread has a period of 10ms from the starting point
 
  attr.size = sizeof(attr);
  attr.sched_flags = 0;
  attr.sched_nice = 0;
  attr.sched_priority = 0;

  attr.sched_policy = SCHED_DEADLINE;
  attr.sched_runtime  = runtime;
  attr.sched_deadline = deadline;
  attr.sched_period   = period; 

  if (sched_setattr(0, &attr, flags) < 0 ) {
    perror("[SCHED] eNB PRACH sched_setattr failed\n");
    return &eNB_thread_prach_status;
  }

  LOG_I( HW, "[SCHED] eNB PRACH deadline thread (TID %ld) started on CPU %d\n", 0, gettid(), sched_getcpu() );
#else // LOW_LATENCY
  int policy, s, j;
  struct sched_param sparam;
  char cpu_affinity[1024];
  cpu_set_t cpuset;

  /* Set affinity mask to include CPUs 1 to MAX_CPUS */
  /* CPU 0 is reserved for UHD */
  /* CPU 1 is reserved for all TX threads */
  /* CPU 2..MAX_CPUS is reserved for all RX threads */
  /* Set CPU Affinity only if number of CPUs >2 */
  CPU_ZERO(&cpuset);
#ifdef CPU_AFFINITY
1321
  if (get_nprocs() >2) {
1322
    for (j = 1; j < get_nprocs(); j++)
1323
      CPU_SET(j, &cpuset);
1324 1325
  
    s = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
1326
    if (s != 0) {
1327 1328 1329 1330 1331 1332
      perror( "pthread_setaffinity_np");  
      exit_fun (" Error setting processor affinity :");
    }
  }
#endif //CPU_AFFINITY

1333
  /* Check the actual affinity mask assigned to the thread */
1334
  s = pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
1335 1336 1337
  if (s != 0) {
    perror ("pthread_getaffinity_np");
    exit_fun (" Error getting processor affinity :");
1338 1339 1340 1341
  }
  memset(cpu_affinity,0, sizeof(cpu_affinity));

  for (j = 0; j < CPU_SETSIZE; j++)
1342 1343 1344 1345 1346
    if (CPU_ISSET(j, &cpuset)) {  
      char temp[1024];
      sprintf (temp, " CPU_%d", j);
      strcat(cpu_affinity, temp);
    }
1347 1348

  memset(&sparam, 0 , sizeof (sparam)); 
knopp's avatar
knopp committed
1349
  sparam.sched_priority = sched_get_priority_max(SCHED_FIFO)-2;
1350 1351 1352

  policy = SCHED_FIFO ; 
  s = pthread_setschedparam(pthread_self(), policy, &sparam);
1353 1354 1355 1356
  if (s != 0) {
    perror("pthread_setschedparam : ");
    exit_fun("Error setting thread priority");
  }
1357 1358 1359 1360

  memset(&sparam, 0 , sizeof (sparam));

  s = pthread_getschedparam(pthread_self(), &policy, &sparam);
1361 1362 1363 1364
  if (s != 0) {
    perror("pthread_getschedparam");
    exit_fun("Error getting thread priority");
  }
1365

Raymond Knopp's avatar
Raymond Knopp committed
1366
  LOG_I(HW, "[SCHED][eNB] PRACH thread started on CPU %d TID %ld, IC %d, sched_policy = %s, priority = %d, CPU Affinity = %s\n", sched_getcpu(),gettid(),proc->instance_cnt_prach,
1367 1368 1369 1370 1371 1372
	 (policy == SCHED_FIFO)  ? "SCHED_FIFO" :
	 (policy == SCHED_RR)    ? "SCHED_RR" :
	 (policy == SCHED_OTHER) ? "SCHED_OTHER" :
	 "???",
	 sparam.sched_priority, cpu_affinity);
  
1373
#endif // DEADLINE_SCHEDULER
1374

1375
  mlockall(MCL_CURRENT | MCL_FUTURE);
1376

1377 1378 1379
  while (!oai_exit) {
    
    if (oai_exit) break;
1380
        
Raymond Knopp's avatar
Raymond Knopp committed
1381
    if (pthread_mutex_timedlock(&proc->mutex_prach,&wait) != 0) {
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
      LOG_E( PHY, "[SCHED][eNB] error locking mutex for eNB PRACH\n");
      exit_fun( "error locking mutex" );
      break;
    }

    while (proc->instance_cnt_prach < 0) {
      // most of the time the thread is waiting here
      // proc->instance_cnt_prach is -1
      pthread_cond_wait( &proc->cond_prach, &proc->mutex_prach ); // this unlocks mutex_rxtx while waiting and then locks it again
    }

    if (pthread_mutex_unlock(&proc->mutex_prach) != 0) {
      LOG_E( PHY, "[SCHED][eNB] error unlocking mutex for eNB PRACH\n");
      exit_fun( "error unlocking mutex" );
      break;
    }
1398
   
1399 1400 1401
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_PRACH_RX,1);
    prach_procedures(eNB,0);
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_PRACH_RX,0);
1402
    
Raymond Knopp's avatar
Raymond Knopp committed
1403
    if (pthread_mutex_timedlock(&proc->mutex_prach,&wait) != 0) {
1404 1405 1406 1407
      LOG_E( PHY, "[SCHED][eNB] error locking mutex for eNB PRACH proc %d\n");
      exit_fun( "error locking mutex" );
      break;
    }
1408
   
1409 1410 1411 1412 1413 1414 1415 1416
    proc->instance_cnt_prach--;
    
    if (pthread_mutex_unlock(&proc->mutex_prach) != 0) {
      LOG_E( PHY, "[SCHED][eNB] error unlocking mutex for eNB RX proc %d\n");
      exit_fun( "error unlocking mutex" );
      break;
    } 
  }
1417

1418
  printf( "Exiting eNB thread PRACH\n");
Raymond Knopp's avatar
Raymond Knopp committed
1419

1420 1421
  eNB_thread_prach_status = 0;
  return &eNB_thread_prach_status;
1422 1423 1424
}


1425
void init_eNB_proc(int inst) {
1426
  
1427 1428
  int i;
  int CC_id;
1429 1430
  PHY_VARS_eNB *eNB;
  eNB_proc_t *proc;
knopp's avatar
knopp committed
1431
  eNB_rxtx_proc_t *proc_rxtx;
1432 1433 1434 1435 1436 1437
  
  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
    eNB = PHY_vars_eNB_g[inst][CC_id];
    LOG_I(PHY,"Initializing eNB %d CC_id %d (%s,%s),\n",inst,CC_id,eNB_functions[eNB->node_function],eNB_timing[eNB->node_timing]);
    proc = &eNB->proc;
    proc_rxtx = proc->proc_rxtx;
1438
#ifndef DEADLINE_SCHEDULER 
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464
    /*  
	pthread_attr_init( &attr_eNB_proc_tx[CC_id][i] );
	if (pthread_attr_setstacksize( &attr_eNB_proc_tx[CC_id][i], 64 *PTHREAD_STACK_MIN ) != 0)
	perror("[ENB_PROC_TX] setting thread stack size failed\n");
	
	pthread_attr_init( &attr_eNB_proc_rx[CC_id][i] );
	if (pthread_attr_setstacksize( &attr_eNB_proc_rx[CC_id][i], 64 * PTHREAD_STACK_MIN ) != 0)
	perror("[ENB_PROC_RX] setting thread stack size failed\n");
    */
    // set the kernel scheduling policy and priority
    proc_rxtx[0].sched_param_rxtx.sched_priority = sched_get_priority_max(SCHED_FIFO)-1; //OPENAIR_THREAD_PRIORITY;
    pthread_attr_setschedparam  (&proc_rxtx[0].attr_rxtx, &proc_rxtx[0].sched_param_rxtx);
    pthread_attr_setschedpolicy (&proc_rxtx[0].attr_rxtx, SCHED_FIFO);
    proc_rxtx[1].sched_param_rxtx.sched_priority = sched_get_priority_max(SCHED_FIFO)-1; //OPENAIR_THREAD_PRIORITY;
    pthread_attr_setschedparam  (&proc_rxtx[1].attr_rxtx, &proc_rxtx[1].sched_param_rxtx);
    pthread_attr_setschedpolicy (&proc_rxtx[1].attr_rxtx, SCHED_FIFO);
    
    proc->sched_param_FH.sched_priority = sched_get_priority_max(SCHED_FIFO); //OPENAIR_THREAD_PRIORITY;
    pthread_attr_setschedparam  (&proc->attr_FH, &proc->sched_param_FH);
    pthread_attr_setschedpolicy (&proc->attr_FH, SCHED_FIFO);
    
    proc->sched_param_prach.sched_priority = sched_get_priority_max(SCHED_FIFO)-1; //OPENAIR_THREAD_PRIORITY;
    pthread_attr_setschedparam  (&proc->attr_prach, &proc