From ad0ca852291d6cc55e2ff272420b3d7038327498 Mon Sep 17 00:00:00 2001
From: Cedric Roux <cedric.roux@eurecom.fr>
Date: Tue, 27 Aug 2013 10:00:53 +0000
Subject: [PATCH] - Added per-slot based interrupt for accurate computation of
 stats Every 500us an interrupt is raised -> Option -o to enable this feature

git-svn-id: http://svn.eurecom.fr/openair4G/trunk@4108 818b1a75-f10b-46b9-bf7c-635c3b92a50f
---
 targets/SIMU/USER/oaisim.c           |  4 ++
 targets/SIMU/USER/oaisim_config.c    |  3 ++
 targets/SIMU/USER/oaisim_functions.c | 55 +++++++++++++++++++++++++++-
 targets/SIMU/USER/oaisim_functions.h |  8 ++++
 4 files changed, 69 insertions(+), 1 deletion(-)

diff --git a/targets/SIMU/USER/oaisim.c b/targets/SIMU/USER/oaisim.c
index 2ca7dc54f0..2b982a957c 100644
--- a/targets/SIMU/USER/oaisim.c
+++ b/targets/SIMU/USER/oaisim.c
@@ -552,6 +552,8 @@ int
     sinr_dB=-20;
   }
 
+  init_slot_isr();
+
   t = clock();
 
   for (frame=0; frame<oai_emulation.info.n_frames; frame++) {
@@ -606,6 +608,8 @@ int
 
     for (slot=0 ; slot<20 ; slot++) {
 
+      wait_for_slot_isr();
+
       last_slot = (slot - 1)%20;
       if (last_slot <0)
         last_slot+=20;
diff --git a/targets/SIMU/USER/oaisim_config.c b/targets/SIMU/USER/oaisim_config.c
index ece42c4313..c913bcbaaa 100644
--- a/targets/SIMU/USER/oaisim_config.c
+++ b/targets/SIMU/USER/oaisim_config.c
@@ -327,6 +327,9 @@ void init_oai_emulation() {
 
   oai_emulation.info.cli_num_enb= NUMBER_OF_eNB_MAX;
   oai_emulation.info.cli_num_ue= NUMBER_OF_UE_MAX;
+
+  oai_emulation.info.slot_isr = 0;
+  oai_emulation.info.slot_sfd = -1;
  
   //for (i=0; i < oai_emulation.info.cli_num_enb; i++)
   for (i=0; i < NUMBER_OF_eNB_MAX; i++)
diff --git a/targets/SIMU/USER/oaisim_functions.c b/targets/SIMU/USER/oaisim_functions.c
index da517ad5f0..be3d9e1b3a 100644
--- a/targets/SIMU/USER/oaisim_functions.c
+++ b/targets/SIMU/USER/oaisim_functions.c
@@ -1,4 +1,11 @@
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <signal.h>
 #include <execinfo.h>
+#include <time.h>
+
+#include <sys/timerfd.h>
 
 #include "oaisim_functions.h"
 
@@ -102,7 +109,7 @@ void get_simulation_options(int argc, char *argv[]) {
     {NULL, 0, NULL, 0}
   };
 
-  while ((c = getopt_long (argc, argv, "aA:b:B:c:C:D:d:eE:f:FGg:hi:IJ:k:l:m:M:n:N:O:p:P:rR:s:S:t:T:u:U:vVx:y:w:W:X:z:Z:", long_options, &option_index)) != -1) {
+  while ((c = getopt_long (argc, argv, "aA:b:B:c:C:D:d:eE:f:FGg:hi:IJ:k:l:m:M:n:N:oO:p:P:rR:s:S:t:T:u:U:vVx:y:w:W:X:z:Z:", long_options, &option_index)) != -1) {
 
     switch (c) {
     case 0:
@@ -347,6 +354,9 @@ void get_simulation_options(int argc, char *argv[]) {
       printf("You enabled MME mode without MME support...\n");
 #endif
       break;
+    case 'o':
+      oai_emulation.info.slot_isr = 1;
+      break;
     default:
       help ();
       exit (-1);
@@ -853,6 +863,49 @@ void update_otg_UE(int module_id, unsigned int ctime) {
 #endif
 }
 
+int init_slot_isr(void)
+{
+    if (oai_emulation.info.slot_isr) {
+        struct itimerspec its;
+
+        int sfd;
+
+        sfd = timerfd_create(CLOCK_REALTIME, 0);
+        if (sfd == -1) {
+            LOG_E(EMU, "Failed in timerfd_create (%d:%s)\n", errno, strerror(errno));
+            exit(EXIT_FAILURE);
+        }
+
+        /* Start the timer */
+        its.it_value.tv_sec = 0;
+        its.it_value.tv_nsec = 500 * 1000;
+        its.it_interval.tv_sec = its.it_value.tv_sec;
+        its.it_interval.tv_nsec = its.it_value.tv_nsec;
+
+        if (timerfd_settime(sfd, TFD_TIMER_ABSTIME, &its, NULL) == -1) {
+            LOG_E(EMU, "Failed in timer_settime (%d:%s)\n", errno, strerror(errno));
+            exit(EXIT_FAILURE);
+        }
+
+        oai_emulation.info.slot_sfd = sfd;
+    }
+}
+
+void wait_for_slot_isr(void)
+{
+    uint64_t exp;
+    ssize_t res;
+
+    if (oai_emulation.info.slot_sfd > 0) {
+        res = read(oai_emulation.info.slot_sfd, &exp, sizeof(exp));
+
+        if ((res < 0) || (res != sizeof(exp))) {
+            LOG_E(EMU, "Failed in read (%d:%s)\n", errno, strerror(errno));
+            exit(EXIT_FAILURE);
+        }
+    }
+}
+
 void exit_fun(const char* s)
 {
   void *array[10];
diff --git a/targets/SIMU/USER/oaisim_functions.h b/targets/SIMU/USER/oaisim_functions.h
index c28bf480bb..9592f3a58d 100644
--- a/targets/SIMU/USER/oaisim_functions.h
+++ b/targets/SIMU/USER/oaisim_functions.h
@@ -5,6 +5,9 @@
 
 #include "UTIL/FIFO/pad_list.h"
 
+#ifndef OAISIM_FUNCTIONS_H_
+#define OAISIM_FUNCTIONS_H_
+
 void get_simulation_options(int argc, char *argv[]);
 
 void check_and_adjust_params();
@@ -35,3 +38,8 @@ void exit_fun(const char* s);
 
 void init_pad();
 
+int init_slot_isr(void);
+
+void wait_for_slot_isr(void);
+
+#endif /* OAISIM_FUNCTIONS_H_ */
-- 
GitLab