From 9e4d52293771f2b10c5f7c48c8eb2d0c7fc31c33 Mon Sep 17 00:00:00 2001 From: Sakthivel Velumani Date: Wed, 1 Mar 2023 15:31:58 -0500 Subject: [PATCH] Make kpi variables atomic Mutex for phy scope: Repeating this change again. My earlier change in commit 06ef311975d was erased by someone during merge of QT scope. --- CMakeLists.txt | 4 +++- openair1/PHY/TOOLS/nr_phy_qt_scope.cpp | 1 + openair1/PHY/TOOLS/nr_phy_scope.c | 4 +--- openair1/PHY/TOOLS/phy_scope_interface.c | 18 ++++++++++++++---- openair1/PHY/TOOLS/phy_scope_interface.h | 17 ++++++++++++----- openair1/PHY/defs_nr_UE.h | 4 ++++ 6 files changed, 35 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 691cb4aaa8..113197f673 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2016,7 +2016,9 @@ add_boolean_option(ENABLE_NRQTSCOPE OFF "Build the Qt-Scope" OFF) if (ENABLE_NRQTSCOPE) find_package(Qt5 REQUIRED COMPONENTS Widgets Charts) message ("Qt5 Widgets and Charts found for nrqtscope") - set(QTSCOPE_SOURCE_NR ${OPENAIR1_DIR}/PHY/TOOLS/nr_phy_qt_scope.cpp) + set(QTSCOPE_SOURCE_NR + ${OPENAIR1_DIR}/PHY/TOOLS/nr_phy_qt_scope.cpp + ${OPENAIR1_DIR}/PHY/TOOLS/phy_scope_interface.c) # Creates rules for calling the Meta-Object Compiler (moc) on the given source files qt5_wrap_cpp(QTSCOPE_SOURCE_NR ${OPENAIR1_DIR}/PHY/TOOLS/nr_phy_qt_scope.h) add_library(nrqtscope MODULE ${QTSCOPE_SOURCE_NR}) diff --git a/openair1/PHY/TOOLS/nr_phy_qt_scope.cpp b/openair1/PHY/TOOLS/nr_phy_qt_scope.cpp index fecfefd131..f1da2dce36 100644 --- a/openair1/PHY/TOOLS/nr_phy_qt_scope.cpp +++ b/openair1/PHY/TOOLS/nr_phy_qt_scope.cpp @@ -1147,6 +1147,7 @@ void nrUEinitQtScope(PHY_VARS_NR_UE *ue) scope->liveData = calloc(sizeof(scopeGraphData_t *), UEdataTypeNumberOfItems); scope->copyData = UEcopyData; + UEcopyDataMutexInit(); ue->scopeData = scope; diff --git a/openair1/PHY/TOOLS/nr_phy_scope.c b/openair1/PHY/TOOLS/nr_phy_scope.c index 206ed490e5..17141c7158 100644 --- a/openair1/PHY/TOOLS/nr_phy_scope.c +++ b/openair1/PHY/TOOLS/nr_phy_scope.c @@ -1137,8 +1137,6 @@ static void *nrUEscopeThread(void *arg) { } #endif -pthread_mutex_t UEcopyDataMutex; - STATICFORXSCOPE void nrUEinitScope(PHY_VARS_NR_UE *ue) { AssertFatal(ue->scopeData=malloc(sizeof(scopeData_t)),""); @@ -1148,8 +1146,8 @@ STATICFORXSCOPE void nrUEinitScope(PHY_VARS_NR_UE *ue) #ifndef WEBSRVSCOPE pthread_t forms_thread; threadCreate(&forms_thread, nrUEscopeThread, ue, "scope", -1, OAI_PRIORITY_RT_LOW); + UEcopyDataMutexInit(); #endif - pthread_mutex_init(&UEcopyDataMutex, NULL); } void nrscope_autoinit(void *dataptr) { diff --git a/openair1/PHY/TOOLS/phy_scope_interface.c b/openair1/PHY/TOOLS/phy_scope_interface.c index b567e96b03..6dfcc36c75 100644 --- a/openair1/PHY/TOOLS/phy_scope_interface.c +++ b/openair1/PHY/TOOLS/phy_scope_interface.c @@ -35,9 +35,16 @@ #include "phy_scope_interface.h" #define SOFTSCOPE_ENDFUNC_IDX 0 +#define THREAD_MEM 4 static loader_shlibfunc_t scope_fdesc[]= {{"end_forms",NULL}}; +pthread_mutex_t UEcopyDataMutex; + +int UEcopyDataMutexInit(void) { + return pthread_mutex_init(&UEcopyDataMutex, NULL); +} + int load_softscope(char *exectype, void *initarg) { char libname[64]; sprintf(libname,"%.10sscope",exectype); @@ -56,16 +63,19 @@ int end_forms(void) { void UEcopyData(PHY_VARS_NR_UE *ue, enum UEdataType type, void *dataIn, int elementSz, int colSz, int lineSz) { // Local static copy of the scope data bufs // The active data buf is alterned to avoid interference between the Scope thread (display) and the Rx thread (data input) - // Index of "2" could be set to the number of Rx threads + 1 - static scopeGraphData_t *copyDataBufs[UEdataTypeNumberOfItems][2] = {0}; + // Index of THREAD_MEM could be set to the number of Rx threads + 1. Rx slots could run asynchronous to each other. + // THREAD_MEM = 4 slot process running in parallel is an assumption. THREAD_MEM can be increased if scope appears inconsistent. + static scopeGraphData_t *copyDataBufs[UEdataTypeNumberOfItems][THREAD_MEM] = {0}; static int copyDataBufsIdx[UEdataTypeNumberOfItems] = {0}; scopeData_t *tmp = (scopeData_t *)ue->scopeData; if (tmp) { - // Begin of critical zone between UE Rx threads that might copy new data at the same time: might require a mutex - int newCopyDataIdx = (copyDataBufsIdx[type]==0)?1:0; + // Begin of critical zone between UE Rx threads that might copy new data at the same time: + pthread_mutex_lock(&UEcopyDataMutex); + int newCopyDataIdx = (copyDataBufsIdx[type]<(THREAD_MEM-1))?copyDataBufsIdx[type]+1:0; copyDataBufsIdx[type] = newCopyDataIdx; + pthread_mutex_unlock(&UEcopyDataMutex); // End of critical zone between UE Rx threads // New data will be copied in a different buffer than the live one diff --git a/openair1/PHY/TOOLS/phy_scope_interface.h b/openair1/PHY/TOOLS/phy_scope_interface.h index b6d4480762..19395891ad 100644 --- a/openair1/PHY/TOOLS/phy_scope_interface.h +++ b/openair1/PHY/TOOLS/phy_scope_interface.h @@ -31,15 +31,21 @@ */ #ifndef __PHY_SCOPE_INTERFACE_H__ #define __PHY_SCOPE_INTERFACE_H__ + +#ifdef __cplusplus +#include +#define _Atomic(X) std::atomic< X > +#endif + #include #include typedef struct { - uint32_t nb_total; - uint32_t nb_nack; - uint32_t blockSize; // block size, to be used for throughput calculation - uint16_t nofRBs; - uint8_t dl_mcs; + _Atomic(uint32_t) nb_total; + _Atomic(uint32_t) nb_nack; + _Atomic(uint32_t) blockSize; // block size, to be used for throughput calculation + _Atomic(uint16_t) nofRBs; + _Atomic(uint8_t ) dl_mcs; } extended_kpi_ue; typedef struct { @@ -80,6 +86,7 @@ typedef struct { int load_softscope(char *exectype, void *initarg); int end_forms(void) ; +int UEcopyDataMutexInit(void); void UEcopyData(PHY_VARS_NR_UE *ue, enum UEdataType type, void *dataIn, int elementSz, int colSz, int lineSz); #define UEscopeCopy(ue, type, ...) if(ue->scopeData) ((scopeData_t*)ue->scopeData)->copyData(ue, type, ##__VA_ARGS__); diff --git a/openair1/PHY/defs_nr_UE.h b/openair1/PHY/defs_nr_UE.h index 2276def8cd..ac5379fe42 100644 --- a/openair1/PHY/defs_nr_UE.h +++ b/openair1/PHY/defs_nr_UE.h @@ -32,6 +32,10 @@ #ifndef __PHY_DEFS_NR_UE__H__ #define __PHY_DEFS_NR_UE__H__ +#ifdef __cplusplus +#include +#define _Atomic(X) std::atomic< X > +#endif #include "defs_nr_common.h" #include "CODING/nrPolar_tools/nr_polar_pbch_defs.h"