c77a39eaa2c44059fb8e7a2ce27e1731b042d4c7
[o-du/phy.git] / fhi_lib / lib / src / xran_timer.c
1 /******************************************************************************
2 *
3 *   Copyright (c) 2019 Intel.
4 *
5 *   Licensed under the Apache License, Version 2.0 (the "License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 *
17 *******************************************************************************/
18
19 /**
20  * @brief This file provides implementation to Timing for XRAN.
21  *
22  * @file xran_timer.c
23  * @ingroup group_lte_source_xran
24  * @author Intel Corporation
25  *
26  **/
27
28 #include <time.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32
33 #include "xran_timer.h"
34 #include "xran_printf.h"
35 #include "xran_mlog_lnx.h"
36 #include "xran_lib_mlog_tasks_id.h"
37 #include "ethdi.h"
38 #include "xran_fh_o_du.h"
39 #include "xran_common.h"
40
41 #define NSEC_PER_SEC  1000000000L
42 #define NSEC_PER_USEC 1000L
43 #define THRESHOLD        35  /**< the avg cost of clock_gettime() in ns */
44 #define TIMECOMPENSATION 2   /**< time compensation in us, avg latency of clock_nanosleep */
45
46 #define SEC_MOD_STOP (60)
47
48 static struct timespec started_time;
49 static struct timespec last_time;
50 static struct timespec cur_time;
51
52 static struct timespec* p_cur_time = &cur_time;
53 static struct timespec* p_last_time = &last_time;
54
55 static struct timespec* p_temp_time;
56
57 static unsigned long current_second = 0;
58 static unsigned long started_second = 0;
59 static uint8_t numerlogy = 0;
60 extern uint32_t xran_lib_ota_sym;
61 extern uint32_t xran_lib_ota_tti;
62 extern uint32_t xran_lib_ota_sym_idx;
63
64 static int debugStop = 0;
65 static int debugStopCount = 0;
66
67 static long fine_tuning[5][2] =
68 {
69     {71428L, 71429L},  /* mu = 0 */
70     {35714L, 35715L},  /* mu = 1 */
71     {0, 0},            /* mu = 2 not supported */
72     {8928L, 8929L},    /* mu = 3 */
73     {0,0  }            /* mu = 4 not supported */
74 };
75
76 static uint8_t slots_per_subframe[4] =
77 {
78     1,  /* mu = 0 */
79     2,  /* mu = 1 */
80     4,  /* mu = 2 */
81     8,  /* mu = 3 */
82 };
83
84 uint64_t timing_get_current_second(void)
85 {
86     return current_second;
87 }
88
89 int timing_set_numerology(uint8_t value)
90 {
91     numerlogy = value;
92     return numerlogy;
93 }
94
95 int timing_set_debug_stop(int value, int count)
96 {
97     debugStop = value;
98     debugStopCount = count;
99
100     if(debugStop){
101         clock_gettime(CLOCK_REALTIME, &started_time);
102         started_second =started_time.tv_sec;
103     }
104     return debugStop;
105 }
106
107 int timing_get_debug_stop(void)
108 {
109     return debugStop;
110 }
111
112 long poll_next_tick(long interval_ns)
113 {
114     long target_time;
115     long delta;
116     static int counter = 0;
117     static long sym_acc = 0;
118     static long sym_cnt = 0;
119
120     if(counter){
121        clock_gettime(CLOCK_REALTIME, p_last_time);
122        current_second = p_last_time->tv_sec;
123        counter = 1;
124     }
125
126     target_time = (p_last_time->tv_sec * NSEC_PER_SEC + p_last_time->tv_nsec + interval_ns);
127
128     while(1)
129     {
130         clock_gettime(CLOCK_REALTIME, p_cur_time);
131         delta = (p_cur_time->tv_sec * NSEC_PER_SEC + p_cur_time->tv_nsec) - target_time;
132         if(delta > 0 || (delta < 0 && abs(delta) < THRESHOLD)) {
133             if (debugStop &&(debugStopCount > 0) && (tx_counter >= debugStopCount)){
134                 uint64_t t1;
135                 printf("STOP:[%ld.%09ld], debugStopCount %d, tx_counter %ld\n", p_cur_time->tv_sec, p_cur_time->tv_nsec, debugStopCount, tx_counter);
136                 t1 = MLogTick();
137                 rte_pause();
138                 MLogTask(PID_TIME_SYSTIME_STOP, t1, MLogTick());
139                 xran_if_current_state = XRAN_STOPPED;
140             }
141             if(current_second != p_cur_time->tv_sec){
142                 current_second = p_cur_time->tv_sec;
143                 xran_lib_ota_sym_idx = 0;
144                 xran_lib_ota_tti = 0;
145                 xran_lib_ota_sym = 0;
146                 sym_cnt = 0;
147                 sym_acc = 0;
148                 print_dbg("ToS:C Sync timestamp: [%ld.%09ld]\n", p_cur_time->tv_sec, p_cur_time->tv_nsec);
149                 if(debugStop){
150                     if(p_cur_time->tv_sec > started_second && ((p_cur_time->tv_sec % SEC_MOD_STOP) == 0)){
151                         uint64_t t1;
152                         printf("STOP:[%ld.%09ld]\n", p_cur_time->tv_sec, p_cur_time->tv_nsec);
153                         t1 = MLogTick();
154                         rte_pause();
155                         MLogTask(PID_TIME_SYSTIME_STOP, t1, MLogTick());
156                         xran_if_current_state = XRAN_STOPPED;
157                     }
158                 }
159                 p_cur_time->tv_nsec = 0; // adjust to 1pps
160             } else {
161                 xran_lib_ota_sym_idx = XranIncrementSymIdx(xran_lib_ota_sym_idx, XRAN_NUM_OF_SYMBOL_PER_SLOT*slots_per_subframe[numerlogy]);
162                 /* adjust to sym boundary */
163                 if(sym_cnt & 1)
164                     sym_acc +=  fine_tuning[numerlogy][0];
165                 else
166                     sym_acc +=  fine_tuning[numerlogy][1];
167                 /* fine tune to second boundary */
168                 if(sym_cnt % 13 == 0)
169                     sym_acc += 1;
170
171                 p_cur_time->tv_nsec = sym_acc;
172                 sym_cnt++;
173             }
174             if(debugStop && delta < interval_ns*10)
175                 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));
176             p_temp_time = p_last_time;
177             p_last_time = p_cur_time;
178             p_cur_time  = p_temp_time;
179             break;
180         } else {
181             if( likely(xran_if_current_state == XRAN_RUNNING)){
182                 ring_processing_func();
183                 process_dpdk_io();
184             }
185         }
186   }
187
188   return delta;
189 }
190
191 long sleep_next_tick(long interval)
192 {
193    struct timespec start_time;
194    struct timespec cur_time;
195    //struct timespec target_time_convert;
196    struct timespec sleep_target_time_convert;
197    long target_time;
198    long sleep_target_time;
199    long delta;
200
201    clock_gettime(CLOCK_REALTIME, &start_time);
202    target_time = (start_time.tv_sec * NSEC_PER_SEC + start_time.tv_nsec + interval * NSEC_PER_USEC) / (interval * NSEC_PER_USEC) * interval;
203    //printf("target: %ld, current: %ld, %ld\n", target_time, start_time.tv_sec, start_time.tv_nsec);
204    sleep_target_time = target_time - TIMECOMPENSATION;
205    sleep_target_time_convert.tv_sec = sleep_target_time * NSEC_PER_USEC / NSEC_PER_SEC;
206    sleep_target_time_convert.tv_nsec = (sleep_target_time * NSEC_PER_USEC) % NSEC_PER_SEC;
207
208    //target_time_convert.tv_sec = target_time * NSEC_PER_USEC / NSEC_PER_SEC;
209    //target_time_convert.tv_nsec = (target_time * NSEC_PER_USEC) % NSEC_PER_SEC;
210
211    clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &sleep_target_time_convert, NULL);
212
213    clock_gettime(CLOCK_REALTIME, &cur_time);
214
215    delta = (cur_time.tv_sec * NSEC_PER_SEC + cur_time.tv_nsec) - target_time * NSEC_PER_USEC;
216
217    return delta;
218 }
219
220
221