sched_ulsch.c 7.44 KB
Newer Older
1
/*******************************************************************************
2
    OpenAirInterface
3
    Copyright(c) 1999 - 2014 Eurecom
4

5 6 7 8
    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.
9 10


11 12 13 14
    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.
15

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

21 22 23
   Contact Information
   OpenAirInterface Admin: openair_admin@eurecom.fr
   OpenAirInterface Tech : openair_tech@eurecom.fr
24
   OpenAirInterface Dev  : openair4g-devel@lists.eurecom.fr
25

ghaddab's avatar
ghaddab committed
26
   Address      : Eurecom, Campus SophiaTech, 450 Route des Chappes, CS 50193 - 06904 Biot Sophia Antipolis cedex, FRANCE
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

/*! \file sched_ulsch.c
* \brief ULSCH decoding thread (RTAI)
* \author R. Knopp, F. Kaltenberger
* \date 2011
* \version 0.1
* \company Eurecom
* \email: knopp@eurecom.fr,florian.kaltenberger@eurecom.fr
* \note
* \warning
*/
#include <stdio.h>
#include <stdlib.h>
#include <sched.h>

#include "rt_wrapper.h"

#include <sys/mman.h>

#include "PHY/types.h"
#include "PHY/defs.h"
#include "PHY/extern.h"

#include "SCHED/defs.h"

#include "MAC_INTERFACE/extern.h"

#ifdef CBMIMO1
#include "ARCH/CBMIMO1/DEVICE_DRIVER/cbmimo1_device.h"
#include "ARCH/CBMIMO1/DEVICE_DRIVER/extern.h"
#include "ARCH/CBMIMO1/DEVICE_DRIVER/defs.h"
#endif // CBMIMO1



#define DEBUG_PHY

66
/// Mutex for instance count on ulsch scheduling
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
pthread_mutex_t ulsch_mutex[NUMBER_OF_UE_MAX];
/// Condition variable for ulsch thread
pthread_cond_t ulsch_cond[NUMBER_OF_UE_MAX];

pthread_t ulsch_threads[NUMBER_OF_UE_MAX];
pthread_attr_t attr_ulsch_threads;

// activity indicators for harq_pid's
int ulsch_instance_cnt[NUMBER_OF_UE_MAX];
// process ids for cpu
int ulsch_cpuid[NUMBER_OF_UE_MAX];
// subframe number for each harq_pid (needed to store ack in right place for UL)
int ulsch_subframe[NUMBER_OF_UE_MAX];

extern int oai_exit;

/*
extern int ulsch_errors;
extern int ulsch_received;
extern int ulsch_errors_last;
extern int ulsch_received_last;
extern int ulsch_fer;
extern int current_ulsch_cqi;
*/

/** ULSCH Decoding Thread */
93 94
static void * ulsch_thread(void *param)
{
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

  //unsigned long cpuid;
  unsigned int ulsch_thread_index = (unsigned int)param;

  RTIME time_in,time_out;
#ifdef RTAI
  RT_TASK *task;
  char ulsch_thread_name[64];
#endif

  int eNB_id = 0, UE_id = 0;
  PHY_VARS_eNB *phy_vars_eNB = PHY_vars_eNB_g[eNB_id];

  if ((ulsch_thread_index <0) || (ulsch_thread_index>NUMBER_OF_UE_MAX)) {
    LOG_E(PHY,"[SCHED][ULSCH] Illegal ulsch_thread_index %d!!!!\n",ulsch_thread_index);
    return 0;
  }

#ifdef RTAI
  sprintf(ulsch_thread_name,"ULSCH_THREAD%d",ulsch_thread_index);

  LOG_I(PHY,"[SCHED][ULSCH] starting ulsch_thread %s for process %d\n",
117 118
        ulsch_thread_name,
        ulsch_thread_index);
119 120 121 122 123 124

  task = rt_task_init_schmod(nam2num(ulsch_thread_name), 0, 0, 0, SCHED_FIFO, 0xF);

  if (task==NULL) {
    LOG_E(PHY,"[SCHED][ULSCH] Problem starting ulsch_thread_index %d!!!!\n",ulsch_thread_index);
    return 0;
125
  } else {
126
    LOG_I(PHY,"[SCHED][ULSCH] ulsch_thread for process %d started with id %p\n",
127 128
          ulsch_thread_index,
          task);
129
  }
130

131 132 133 134 135 136 137 138 139 140 141 142 143
#endif

  mlockall(MCL_CURRENT | MCL_FUTURE);

  //rt_set_runnable_on_cpuid(task,1);
  //cpuid = rtai_cpuid();

#ifdef HARD_RT
  rt_make_hard_real_time();
#endif

  //ulsch_cpuid[ulsch_thread_index] = cpuid;

144 145
  while (!oai_exit) {

146 147
    if (pthread_mutex_lock(&ulsch_mutex[ulsch_thread_index]) != 0) {
      LOG_E(PHY,"[SCHED][ULSCH] error locking mutex.\n");
148
    } else {
149 150

      while (ulsch_instance_cnt[ulsch_thread_index] < 0) {
151
        pthread_cond_wait(&ulsch_cond[ulsch_thread_index],&ulsch_mutex[ulsch_thread_index]);
152 153
      }

154 155
      if (pthread_mutex_unlock(&ulsch_mutex[ulsch_thread_index]) != 0) {
        LOG_E(PHY,"[SCHED][ULSCH] error unlocking mutex.\n");
156 157 158 159 160
      }
    }

    if (oai_exit) break;

Raymond Knopp's avatar
 
Raymond Knopp committed
161
    LOG_D(PHY,"[SCHED][ULSCH] Frame %d: Calling ulsch_decoding with ulsch_thread_index = %d\n",phy_vars_eNB->proc[0].frame_tx,ulsch_thread_index);
162 163 164 165

    time_in = rt_get_time_ns();

    ulsch_decoding_procedures(ulsch_subframe[ulsch_thread_index]<<1,ulsch_thread_index,phy_vars_eNB,0);
166

167 168 169 170
    time_out = rt_get_time_ns();

    if (pthread_mutex_lock(&ulsch_mutex[ulsch_thread_index]) != 0) {
      msg("[openair][SCHED][ULSCH] error locking mutex.\n");
171
    } else {
172
      ulsch_instance_cnt[ulsch_thread_index]--;
173 174 175

      if (pthread_mutex_unlock(&ulsch_mutex[ulsch_thread_index]) != 0) {
        msg("[openair][SCHED][ULSCH] error unlocking mutex.\n");
176 177 178 179 180 181 182 183
      }
    }
  }

#ifdef HARD_RT
  rt_make_soft_real_time();
#endif

184
  msg("[openair][SCHED][ULSCH] ULSCH thread %d exiting\n",ulsch_thread_index);
185 186 187 188

  return 0;
}

189 190 191
int init_ulsch_threads(void)
{

192 193 194 195 196
  int error_code, return_code=0;
  struct sched_param p;
  int ulsch_thread_index;

  // later loop on all harq_pids, do 0 for now
197
  for (ulsch_thread_index=0; ulsch_thread_index<NUMBER_OF_UE_MAX; ulsch_thread_index++) {
198 199

    pthread_mutex_init(&ulsch_mutex[ulsch_thread_index],NULL);
200

201 202 203 204
    pthread_cond_init(&ulsch_cond[ulsch_thread_index],NULL);

    pthread_attr_init (&attr_ulsch_threads);
    pthread_attr_setstacksize(&attr_ulsch_threads,OPENAIR_THREAD_STACK_SIZE);
205

206 207 208 209 210 211 212
    //attr_ulsch_threads.priority = 1;

    p.sched_priority = OPENAIR_THREAD_PRIORITY;
    pthread_attr_setschedparam  (&attr_ulsch_threads, &p);
#ifndef RTAI_ISNT_POSIX
    pthread_attr_setschedpolicy (&attr_ulsch_threads, SCHED_FIFO);
#endif
213

214 215 216
    ulsch_instance_cnt[ulsch_thread_index] = -1;
    rt_printk("[openair][SCHED][ULSCH][INIT] Allocating ULSCH thread for ulsch_thread_index %d\n",ulsch_thread_index);
    error_code = pthread_create(&ulsch_threads[ulsch_thread_index],
217 218 219
                                &attr_ulsch_threads,
                                ulsch_thread,
                                (void *)ulsch_thread_index);
220 221 222 223 224

    if (error_code!= 0) {
      rt_printk("[openair][SCHED][ULSCH][INIT] Could not allocate ulsch_thread %d, error %d\n",ulsch_thread_index,error_code);
      return_code+=error_code;
      //return(error_code);
225
    } else {
226 227 228 229
      rt_printk("[openair][SCHED][ULSCH][INIT] Allocate ulsch_thread %d successful\n",ulsch_thread_index);
      //return(0);
    }
  }
230

231 232 233
  return(return_code);
}

234 235
void cleanup_ulsch_threads(void)
{
236 237 238

  int ulsch_thread_index;

239
  for (ulsch_thread_index=0; ulsch_thread_index<NUMBER_OF_UE_MAX; ulsch_thread_index++) {
240 241 242 243 244

    //  pthread_exit(&ulsch_threads[ulsch_thread_index]);
    rt_printk("[openair][SCHED][ULSCH] Scheduling ulsch_thread %d to exit\n",ulsch_thread_index);

    ulsch_instance_cnt[ulsch_thread_index] = 0;
245

246 247 248 249
    if (pthread_cond_signal(&ulsch_cond[ulsch_thread_index]) != 0)
      rt_printk("[openair][SCHED][ULSCH] ERROR pthread_cond_signal\n");
    else
      rt_printk("[openair][SCHED][ULSCH] Signalled ulsch_thread %d to exit\n",ulsch_thread_index);
250

251 252 253 254 255
    rt_printk("[openair][SCHED][ULSCH] Exiting ...\n");
    pthread_cond_destroy(&ulsch_cond[ulsch_thread_index]);
    pthread_mutex_destroy(&ulsch_mutex[ulsch_thread_index]);
  }
}