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