Update to odulow per maintenance bronze
[o-du/phy.git] / fhi_lib / lib / src / xran_timer.c
index c77a39e..0b86f8e 100644 (file)
@@ -49,11 +49,17 @@ static struct timespec started_time;
 static struct timespec last_time;
 static struct timespec cur_time;
 
+static uint64_t  curr_tick;
+static uint64_t  last_tick;
+
 static struct timespec* p_cur_time = &cur_time;
 static struct timespec* p_last_time = &last_time;
 
+
 static struct timespec* p_temp_time;
 
+static struct timespec sleeptime = {.tv_nsec = 1E3 }; /* 1 us */
+
 static unsigned long current_second = 0;
 static unsigned long started_second = 0;
 static uint8_t numerlogy = 0;
@@ -109,30 +115,70 @@ int timing_get_debug_stop(void)
     return debugStop;
 }
 
-long poll_next_tick(long interval_ns)
+void timing_adjust_gps_second(struct timespec* p_time)
+{
+    struct xran_device_ctx * p_xran_dev_ctx = xran_dev_get_ctx();
+
+    if (p_time->tv_nsec >= p_xran_dev_ctx->offset_nsec)
+    {
+        p_time->tv_nsec -= p_xran_dev_ctx->offset_nsec;
+        p_time->tv_sec -= p_xran_dev_ctx->offset_sec;
+    }
+    else
+    {
+        p_time->tv_nsec += 1e9 - p_xran_dev_ctx->offset_nsec;
+        p_time->tv_sec -= p_xran_dev_ctx->offset_sec + 1;
+    }
+
+    return;
+}
+uint64_t xran_tick(void)
+{
+    uint32_t hi, lo;
+    __asm volatile ("rdtsc" : "=a"(lo), "=d"(hi));
+    return ( (uint64_t)lo)|( ((uint64_t)hi)<<32 );
+}
+
+unsigned long get_ticks_diff(unsigned long curr_tick, unsigned long last_tick)
 {
+    if (curr_tick >= last_tick)
+        return (unsigned long)(curr_tick - last_tick);
+    else
+        return (unsigned long)(0xFFFFFFFFFFFFFFFF - last_tick + curr_tick);
+}
+
+long poll_next_tick(long interval_ns, unsigned long *used_tick)
+{
+    struct xran_device_ctx * p_xran_dev_ctx = xran_dev_get_ctx();
+    struct xran_common_counters* pCnt = &p_xran_dev_ctx->fh_counters;
+
     long target_time;
     long delta;
     static int counter = 0;
     static long sym_acc = 0;
     static long sym_cnt = 0;
 
-    if(counter){
+    if(counter == 0) {
        clock_gettime(CLOCK_REALTIME, p_last_time);
+       last_tick = MLogTick();
+       if(unlikely(p_xran_dev_ctx->offset_sec || p_xran_dev_ctx->offset_nsec))
+           timing_adjust_gps_second(p_last_time);
        current_second = p_last_time->tv_sec;
        counter = 1;
     }
 
     target_time = (p_last_time->tv_sec * NSEC_PER_SEC + p_last_time->tv_nsec + interval_ns);
 
-    while(1)
-    {
+    while(1) {
         clock_gettime(CLOCK_REALTIME, p_cur_time);
+        curr_tick = MLogTick();
+        if(unlikely(p_xran_dev_ctx->offset_sec || p_xran_dev_ctx->offset_nsec))
+            timing_adjust_gps_second(p_cur_time);
         delta = (p_cur_time->tv_sec * NSEC_PER_SEC + p_cur_time->tv_nsec) - target_time;
         if(delta > 0 || (delta < 0 && abs(delta) < THRESHOLD)) {
-            if (debugStop &&(debugStopCount > 0) && (tx_counter >= debugStopCount)){
+            if (debugStop &&(debugStopCount > 0) && (pCnt->tx_counter >= debugStopCount)){
                 uint64_t t1;
-                printf("STOP:[%ld.%09ld], debugStopCount %d, tx_counter %ld\n", p_cur_time->tv_sec, p_cur_time->tv_nsec, debugStopCount, tx_counter);
+                printf("STOP:[%ld.%09ld], debugStopCount %d, tx_counter %ld\n", p_cur_time->tv_sec, p_cur_time->tv_nsec, debugStopCount, pCnt->tx_counter);
                 t1 = MLogTick();
                 rte_pause();
                 MLogTask(PID_TIME_SYSTIME_STOP, t1, MLogTick());
@@ -140,6 +186,7 @@ long poll_next_tick(long interval_ns)
             }
             if(current_second != p_cur_time->tv_sec){
                 current_second = p_cur_time->tv_sec;
+                xran_updateSfnSecStart();
                 xran_lib_ota_sym_idx = 0;
                 xran_lib_ota_tti = 0;
                 xran_lib_ota_sym = 0;
@@ -171,17 +218,38 @@ long poll_next_tick(long interval_ns)
                 p_cur_time->tv_nsec = sym_acc;
                 sym_cnt++;
             }
+
+#ifdef USE_PTP_TIME
             if(debugStop && delta < interval_ns*10)
                 MLogTask(PID_TIME_SYSTIME_POLL, (p_last_time->tv_sec * NSEC_PER_SEC + p_last_time->tv_nsec), (p_cur_time->tv_sec * NSEC_PER_SEC + p_cur_time->tv_nsec));
+#else
+            MLogTask(PID_TIME_SYSTIME_POLL, last_tick, curr_tick);
+            last_tick = curr_tick;
+#endif
+
+
             p_temp_time = p_last_time;
             p_last_time = p_cur_time;
             p_cur_time  = p_temp_time;
             break;
         } else {
             if( likely(xran_if_current_state == XRAN_RUNNING)){
-                ring_processing_func();
+                uint64_t t1, t2;
+                t1 = xran_tick();
+
+                if(p_xran_dev_ctx->fh_init.io_cfg.pkt_proc_core == 0)
+                    ring_processing_func();
+
                 process_dpdk_io();
+
+                /* work around for some kernel */
+                if(p_xran_dev_ctx->fh_init.io_cfg.io_sleep)
+                    nanosleep(&sleeptime,NULL);
+
+                t2 = xran_tick();
+                *used_tick += get_ticks_diff(t2, t1);
             }
+
         }
   }