Update to odulow per maintenance bronze
[o-du/phy.git] / fapi_5g / source / utils / nr5g_fapi_stats.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  * @file This file defines all the functions used for statistics. 
21  *
22  **/
23 #include "nr5g_fapi_std.h"
24 #include "nr5g_fapi_stats.h"
25 #include "nr5g_fapi_log.h"
26
27 #define NR5G_FAPI_STATS_FNAME_LEN   64
28
29 void nr5g_fapi_print_phy_instance_stats(
30     p_nr5g_fapi_phy_instance_t p_phy_instance)
31 {
32     int fd = 0, ret_val;
33     FILE *fp = NULL;
34     enum { FILE_MODE = 0666 };
35     nr5g_fapi_stats_t *p_stats;
36     char *fname = NULL;
37 #ifdef DEBUG_MODE
38     char stats_fname[NR5G_FAPI_STATS_FNAME_LEN];
39     uint32_t test_num;
40     uint32_t test_type;
41     char test_type_str[][8] = { {"DL"}, {"UL"}, {"FD"} };
42 #endif
43
44     if (NULL == p_phy_instance) {
45         NR5G_FAPI_LOG(ERROR_LOG, ("[NR5G_FAPI][STATS] Invalid "
46                 "phy instance"));
47         return;
48     }
49 #ifdef DEBUG_MODE
50     if (p_phy_instance->shutdown_test_type) {
51         test_num = p_phy_instance->shutdown_test_type & 0xFFFFFFF;
52         test_type = p_phy_instance->shutdown_test_type >> 28;
53         snprintf(stats_fname, NR5G_FAPI_STATS_FNAME_LEN,
54             "FapiStats_%s_%d.txt", test_type_str[test_type], test_num);
55         fname = stats_fname;
56     }
57 #endif
58
59     if (!fname)
60         fname = NR5G_FAPI_STATS_FNAME;
61
62     ret_val = remove(fname);
63     if ((-1 == ret_val) && (ENOENT != errno)) {
64         NR5G_FAPI_LOG(ERROR_LOG, ("File %s delete not successful errno %d",
65                 fname, errno));
66         perror("[Error] ");
67         return;
68     }
69
70     fd = open(fname, O_RDWR | O_CREAT | O_EXCL);
71     if (-1 == fd) {
72         NR5G_FAPI_LOG(ERROR_LOG, ("Failed to open the file %s\n", fname));
73         perror("[Error] ");
74         return;
75     }
76
77     fp = fdopen(fd, "w");
78     if (NULL == fp) {
79         NR5G_FAPI_LOG(ERROR_LOG,
80             ("Failed to open the file %s from file descriptor\n", fname));
81         perror("[Error] ");
82
83         if (-1 == close(fd)) {
84             NR5G_FAPI_LOG(ERROR_LOG,
85                 ("Failed to close the descriptor for file %s\n", fname));
86             perror("[Error] ");
87         }
88         return;
89     }
90
91     p_stats = &p_phy_instance->stats;
92     fprintf(fp, "5GNR FAPI instance statistics PhyId: %u\n",
93         p_phy_instance->phy_id);
94     fprintf(fp, "%*s: %u\t\t\t%*s: %u\n", 14, "ParamReq",
95         p_stats->fapi_stats.fapi_param_req, 14, "ConfigReq",
96         p_stats->fapi_stats.fapi_config_req);
97     fprintf(fp, "%*s: %u\t\t\t%*s: %u\n", 14, "StartReq",
98         p_stats->fapi_stats.fapi_start_req, 14, "StopReq",
99         p_stats->fapi_stats.fapi_stop_req);
100     fprintf(fp, "%*s: %u\t\t\t%*s: %u\n", 14, "ParamResp",
101         p_stats->fapi_stats.fapi_param_res, 14, "ConfigResp",
102         p_stats->fapi_stats.fapi_config_res);
103     fprintf(fp, "%*s: %u\t\t\t%*s: %u\n", 14, "stopInd",
104         p_stats->fapi_stats.fapi_stop_ind, 14, "vendorMsg",
105         p_stats->fapi_stats.fapi_vendor_msg);
106     fprintf(fp, "%*s: %lu\t\t\t%*s: %lu\n", 14, "DLTTIReq",
107         p_stats->fapi_stats.fapi_dl_tti_req, 14, "ULTTIReq",
108         p_stats->fapi_stats.fapi_ul_tti_req);
109     fprintf(fp, "%*s: %lu\t\t\t%*s: %lu\n", 14, "TxDataReq",
110         p_stats->fapi_stats.fapi_tx_data_req, 14, "ErrorInd",
111         p_stats->fapi_stats.fapi_error_ind);
112     fprintf(fp, "%*s: %lu\t\t\t%*s: %lu\n", 14, "ULDciReq",
113         p_stats->fapi_stats.fapi_ul_dci_req, 14, "SlotInd",
114         p_stats->fapi_stats.fapi_slot_ind);
115     fprintf(fp, "%*s: %lu\t\t\t%*s: %lu\n", 14, "CrcInd",
116         p_stats->fapi_stats.fapi_crc_ind, 14, "RxDataInd",
117         p_stats->fapi_stats.fapi_rx_data_ind);
118     fprintf(fp, "%*s: %lu\t\t\t%*s: %lu\n", 14, "UciInd",
119         p_stats->fapi_stats.fapi_uci_ind, 14, "SrsInd",
120         p_stats->fapi_stats.fapi_srs_ind);
121     fprintf(fp, "%*s: %lu\t\t\t%*s: %u\n", 14, "RachInd",
122         p_stats->fapi_stats.fapi_rach_ind, 14, "ShutdownReq",
123         p_stats->fapi_stats.fapi_vext_shutdown_req);
124     fprintf(fp, "%*s: %u\t\t\t%*s: %lu\n", 14, "ShutdownRes",
125         p_stats->fapi_stats.fapi_vext_shutdown_res, 14, "ULTTIReqPdus",
126         p_stats->fapi_stats.fapi_ul_tti_pdus);
127     fprintf(fp, "%*s: %lu\t\t\t%*s: %lu\n", 14, "ULTTIReqPrach",
128         p_stats->fapi_stats.fapi_ul_tti_prach_pdus, 14, "ULTTIReqPusch",
129         p_stats->fapi_stats.fapi_ul_tti_pusch_pdus);
130     fprintf(fp, "%*s: %lu\t\t\t%*s: %lu\n", 14, "ULTTIReqPucch",
131         p_stats->fapi_stats.fapi_ul_tti_pucch_pdus, 14, "ULTTIReqSrs",
132         p_stats->fapi_stats.fapi_ul_tti_srs_pdus);
133     fprintf(fp, "%*s: %lu\t\t\t%*s: %lu\n", 14, "DLTTIReqPdus",
134         p_stats->fapi_stats.fapi_dl_tti_pdus, 14, "DLTTIReqPdsch",
135         p_stats->fapi_stats.fapi_dl_tti_pdsch_pdus);
136     fprintf(fp, "%*s: %lu\t\t\t%*s: %lu\n", 14, "DLTTIReqPdcch",
137         p_stats->fapi_stats.fapi_dl_tti_pdcch_pdus, 14, "DLTTIReqSsb",
138         p_stats->fapi_stats.fapi_dl_tti_ssb_pdus);
139     fprintf(fp, "%*s: %lu\t\t\t%*s: %lu\n", 14, "DLTTIReqCsiRs",
140         p_stats->fapi_stats.fapi_dl_tti_csi_rs_pdus, 14, "ULDCIReqPdus",
141         p_stats->fapi_stats.fapi_ul_dci_pdus);
142     fprintf(fp, "%*s: %lu\t\t\t%*s: %lu\n", 14, "CrcIndPdus",
143         p_stats->fapi_stats.fapi_crc_ind_pdus, 14, "RxDataPdus",
144         p_stats->fapi_stats.fapi_rx_data_ind_pdus);
145     fprintf(fp, "%*s: %lu\t\t\t%*s: %lu\n", 14, "UciIndPdus",
146         p_stats->fapi_stats.fapi_uci_ind_pdus, 14, "SrsIndPdus",
147         p_stats->fapi_stats.fapi_srs_ind_pdus);
148     fprintf(fp, "%*s: %lu\t\t\t\n", 14, "RachPdus",
149         p_stats->fapi_stats.fapi_rach_ind_pdus);
150     fprintf(fp, "\n\n");
151
152     fprintf(fp, "5GNR L1 instance statistics PhyId: %u\n",
153         p_phy_instance->phy_id);
154     fprintf(fp, "%*s: %u\t\t\t%*s: %u\n", 14, "ParamReq",
155         p_stats->iapi_stats.iapi_param_req, 14, "ConfigReq",
156         p_stats->iapi_stats.iapi_config_req);
157     fprintf(fp, "%*s: %u\t\t\t%*s: %u\n", 14, "StartReq",
158         p_stats->iapi_stats.iapi_start_req, 14, "StopReq",
159         p_stats->iapi_stats.iapi_stop_req);
160     fprintf(fp, "%*s: %u\t\t\t%*s: %u\n", 14, "StartRes",
161         p_stats->iapi_stats.iapi_start_res, 14, "StopInd",
162         p_stats->iapi_stats.iapi_stop_ind);
163     fprintf(fp, "%*s: %lu\t\t\t%*s: %lu\n", 14, "DlConfigReq",
164         p_stats->iapi_stats.iapi_dl_config_req, 14, "UlConfigReq",
165         p_stats->iapi_stats.iapi_ul_config_req);
166     fprintf(fp, "%*s: %lu\t\t\t%*s: %lu\n", 14, "TxReq",
167         p_stats->iapi_stats.iapi_tx_req, 14, "ErrorInd",
168         p_stats->iapi_stats.iapi_error_ind);
169     fprintf(fp, "%*s: %lu\t\t\t%*s: %lu\n", 14, "UlDciReq",
170         p_stats->iapi_stats.iapi_ul_dci_req, 14, "SlotInd",
171         p_stats->iapi_stats.iapi_slot_ind);
172     fprintf(fp, "%*s: %lu\t\t\t%*s: %lu\n", 14, "CrcInd",
173         p_stats->iapi_stats.iapi_crc_ind, 14, "RxDataInd",
174         p_stats->iapi_stats.iapi_rx_data_ind);
175     fprintf(fp, "%*s: %lu\t\t\t%*s: %lu\n", 14, "UciInd",
176         p_stats->iapi_stats.iapi_uci_ind, 14, "SrsInd",
177         p_stats->iapi_stats.iapi_srs_ind);
178     fprintf(fp, "%*s: %lu\t\t\t%*s: %u\n", 14, "RACHInd",
179         p_stats->iapi_stats.iapi_rach_ind, 14, "ShutdownReq",
180         p_stats->iapi_stats.iapi_shutdown_req);
181     fprintf(fp, "%*s: %u\t\t\t%*s: %lu\n", 14, "ShutdownRes",
182         p_stats->iapi_stats.iapi_shutdown_res, 14, "ULTTIReqPdus",
183         p_stats->iapi_stats.iapi_ul_tti_pdus);
184     fprintf(fp, "%*s: %lu\t\t\t%*s: %lu\n", 14, "ULTTIReqPrach",
185         p_stats->iapi_stats.iapi_ul_tti_prach_pdus, 14, "ULTTIReqPusch",
186         p_stats->iapi_stats.iapi_ul_tti_pusch_pdus);
187     fprintf(fp, "%*s: %lu\t\t\t%*s: %lu\n", 14, "ULTTIReqPucch",
188         p_stats->iapi_stats.iapi_ul_tti_pucch_pdus, 14, "ULTTIReqSrs",
189         p_stats->iapi_stats.iapi_ul_tti_srs_pdus);
190     fprintf(fp, "%*s: %lu\t\t\t%*s: %lu\n", 14, "DLTTIReqPdus",
191         p_stats->iapi_stats.iapi_dl_tti_pdus, 14, "DLTTIReqPdsch",
192         p_stats->iapi_stats.iapi_dl_tti_pdsch_pdus);
193     fprintf(fp, "%*s: %lu\t\t\t%*s: %lu\n", 14, "DLTTIReqPdcch",
194         p_stats->iapi_stats.iapi_dl_tti_pdcch_pdus, 14, "DLTTIReqSsb",
195         p_stats->iapi_stats.iapi_dl_tti_ssb_pdus);
196     fprintf(fp, "%*s: %lu\t\t\t%*s: %lu\n", 14, "DLTTIReqCsiRs",
197         p_stats->iapi_stats.iapi_dl_tti_csi_rs_pdus, 14, "ULDCIReqPdus",
198         p_stats->iapi_stats.iapi_ul_dci_pdus);
199     fprintf(fp, "%*s: %lu\t\t\t%*s: %lu\n", 14, "CrcIndPdus",
200         p_stats->iapi_stats.iapi_crc_ind_pdus, 14, "RxDataPdus",
201         p_stats->iapi_stats.iapi_rx_data_ind_pdus);
202     fprintf(fp, "%*s: %lu\t\t\t%*s: %lu\n", 14, "UciIndPdus",
203         p_stats->iapi_stats.iapi_uci_ind_pdus, 14, "SrsIndPdus",
204         p_stats->iapi_stats.iapi_srs_ind_pdus);
205     fprintf(fp, "%*s: %lu\t\t\t\n", 14, "RachPreambles",
206         p_stats->iapi_stats.iapi_rach_preambles);
207     fprintf(fp, "\n");
208     if (EOF == fclose(fp)) {
209         NR5G_FAPI_LOG(ERROR_LOG, ("Unable to close the file\n"));
210     }
211     close(fd);
212 }