From b1f7cc90256aeedc9256a2a830a26208cf5282c3 Mon Sep 17 00:00:00 2001 From: Bartosz Podrygajlo Date: Mon, 6 May 2024 11:49:26 -0400 Subject: [PATCH] Add unit testcase for thread-pool. Extract thread-pool into a separate CMake library. Extract utils into a separate CMake library. --- CMakeLists.txt | 10 +- common/utils/CMakeLists.txt | 4 + common/utils/threadPool/CMakeLists.txt | 6 + common/utils/threadPool/Makefile | 3 - common/utils/threadPool/test/CMakeLists.txt | 6 + .../utils/threadPool/test/test_thread-pool.c | 133 ++++++++++++++++++ common/utils/threadPool/thread-pool.c | 129 ----------------- 7 files changed, 151 insertions(+), 140 deletions(-) create mode 100644 common/utils/threadPool/CMakeLists.txt delete mode 100644 common/utils/threadPool/Makefile create mode 100644 common/utils/threadPool/test/CMakeLists.txt create mode 100644 common/utils/threadPool/test/test_thread-pool.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 4363719cc2..78933f5577 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -651,13 +651,7 @@ include_directories(${OPENAIR_DIR}/common/utils/hashtable) add_library(UTIL ${OPENAIR_DIR}/common/utils/LOG/vcd_signal_dumper.c - ${OPENAIR2_DIR}/UTIL/MATH/oml.c ${OPENAIR2_DIR}/UTIL/OPT/probe.c - ${OPENAIR_DIR}/common/utils/threadPool/thread-pool.c - ${OPENAIR_DIR}/common/utils/utils.c - ${OPENAIR_DIR}/common/utils/system.c - ${OPENAIR_DIR}/common/utils/time_meas.c - ${OPENAIR_DIR}/common/utils/time_stat.c ) if (ENABLE_LTTNG) find_package(LTTngUST 2.3.8 EXACT REQUIRED) @@ -671,7 +665,7 @@ if (cap_FOUND) target_link_libraries(UTIL PRIVATE cap) target_compile_definitions(UTIL PRIVATE HAVE_LIB_CAP) endif() -target_link_libraries(UTIL PUBLIC ${T_LIB} pthread LOG) +target_link_libraries(UTIL PUBLIC ${T_LIB} pthread LOG thread-pool utils) set(SECURITY_SRC ${OPENAIR3_DIR}/SECU/secu_defs.c @@ -1206,7 +1200,7 @@ set(PHY_MEX_UE ${OPENAIR1_DIR}/PHY/LTE_ESTIMATION/lte_ue_measurements.c ) add_library(PHY_MEX ${PHY_MEX_UE}) -target_link_libraries(PHY_MEX PRIVATE asn1_lte_rrc_hdrs LOG) +target_link_libraries(PHY_MEX PRIVATE asn1_lte_rrc_hdrs UTIL) #Layer 2 library ##################### diff --git a/common/utils/CMakeLists.txt b/common/utils/CMakeLists.txt index 9d882a4272..932f458a77 100644 --- a/common/utils/CMakeLists.txt +++ b/common/utils/CMakeLists.txt @@ -12,3 +12,7 @@ endif() add_subdirectory(T) add_subdirectory(nr) add_subdirectory(LOG) +add_subdirectory(threadPool) +add_library(utils utils.c system.c time_meas.c time_stat.c) +target_include_directories(utils PUBLIC .) +target_link_libraries(utils PRIVATE ${T_LIB}) diff --git a/common/utils/threadPool/CMakeLists.txt b/common/utils/threadPool/CMakeLists.txt new file mode 100644 index 0000000000..eb48e39a94 --- /dev/null +++ b/common/utils/threadPool/CMakeLists.txt @@ -0,0 +1,6 @@ +if (ENABLE_TESTS) + add_subdirectory(test) +endif() +add_library(thread-pool thread-pool.c) +target_link_libraries(thread-pool PRIVATE utils) +target_include_directories(thread-pool PUBLIC .) diff --git a/common/utils/threadPool/Makefile b/common/utils/threadPool/Makefile deleted file mode 100644 index 8913edb1f4..0000000000 --- a/common/utils/threadPool/Makefile +++ /dev/null @@ -1,3 +0,0 @@ -thread-pool-test: thread-pool.c thread-pool.h - gcc -g -O3 thread-pool.c -I ${OPENAIR_DIR}/nfapi/open-nFAPI/nfapi/public_inc -I ${OPENAIR_DIR}/ -I ${OPENAIR_DIR}/common/utils/ -I. -I ${OPENAIR_DIR}/openair2/COMMON ${OPENAIR_DIR}/common/utils/system.c ${OPENAIR_DIR}/common/utils/LOG/log.c ${OPENAIR_DIR}/common/config/config_userapi.c ${OPENAIR_DIR}/common/config/config_load_configmodule.c ${OPENAIR_DIR}/common/config/config_cmdline.c -lpthread -ldl -D TEST_THREAD_POOL -DMAX_NUM_CCs=1 -I../LOG -I../../utils/T -o thread-pool-test - diff --git a/common/utils/threadPool/test/CMakeLists.txt b/common/utils/threadPool/test/CMakeLists.txt new file mode 100644 index 0000000000..c756ba874a --- /dev/null +++ b/common/utils/threadPool/test/CMakeLists.txt @@ -0,0 +1,6 @@ +add_executable(test_thread-pool + test_thread-pool.c) +add_dependencies(tests test_thread-pool) +target_link_libraries(test_thread-pool PRIVATE thread-pool pthread LOG minimal_lib) +add_test(NAME test_thread-pool + COMMAND ./test_thread-pool) diff --git a/common/utils/threadPool/test/test_thread-pool.c b/common/utils/threadPool/test/test_thread-pool.c new file mode 100644 index 0000000000..0d6defd5cb --- /dev/null +++ b/common/utils/threadPool/test/test_thread-pool.c @@ -0,0 +1,133 @@ +/* + * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The OpenAirInterface Software Alliance licenses this file to You under + * the OAI Public License, Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.openairinterface.org/?page_id=698 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *------------------------------------------------------------------------------- + * For more information about the OpenAirInterface (OAI) Software Alliance: + * contact@openairinterface.org + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "log.h" + +void displayList(notifiedFIFO_t *nf) +{ + int n = 0; + notifiedFIFO_elt_t *ptr = nf->outF; + + while (ptr) { + printf("element: %d, key: %lu\n", ++n, ptr->key); + ptr = ptr->next; + } + + printf("End of list: %d elements\n", n); +} + +struct testData { + int id; + int sleepTime; + char txt[30]; +}; + +void processing(void *arg) +{ + struct testData *in = (struct testData *)arg; + // printf("doing: %d, %s, in thr %ld\n",in->id, in->txt,pthread_self() ); + sprintf(in->txt, "Done by %ld, job %d", pthread_self(), in->id); + in->sleepTime = rand() % 1000; + usleep(in->sleepTime); + // printf("done: %d, %s, in thr %ld\n",in->id, in->txt,pthread_self() ); +} + +int main() +{ + logInit(); + notifiedFIFO_t myFifo; + initNotifiedFIFO(&myFifo); + pushNotifiedFIFO(&myFifo, newNotifiedFIFO_elt(sizeof(struct testData), 1234, NULL, NULL)); + + for (int i = 10; i > 1; i--) { + pushNotifiedFIFO(&myFifo, newNotifiedFIFO_elt(sizeof(struct testData), 1000 + i, NULL, NULL)); + } + + displayList(&myFifo); + notifiedFIFO_elt_t *tmp = pullNotifiedFIFO(&myFifo); + printf("pulled: %lu\n", tmp->key); + displayList(&myFifo); + tmp = pullNotifiedFIFO(&myFifo); + printf("pulled: %lu\n", tmp->key); + displayList(&myFifo); + pushNotifiedFIFO(&myFifo, newNotifiedFIFO_elt(sizeof(struct testData), 12345678, NULL, NULL)); + displayList(&myFifo); + + do { + tmp = pollNotifiedFIFO(&myFifo); + + if (tmp) { + printf("pulled: %lu\n", tmp->key); + displayList(&myFifo); + } else + printf("Empty list \n"); + } while (tmp); + + tpool_t pool; + char params[] = "1,2,3,4,5"; + initTpool(params, &pool, true); + notifiedFIFO_t worker_back; + initNotifiedFIFO(&worker_back); + + //sleep(1); + int cumulProcessTime = 0; + struct timespec st, end; + clock_gettime(CLOCK_MONOTONIC, &st); + int nb_jobs = 4; + for (int i = 0; i < 1000; i++) { + int parall = nb_jobs; + for (int j = 0; j < parall; j++) { + notifiedFIFO_elt_t *work = newNotifiedFIFO_elt(sizeof(struct testData), i, &worker_back, processing); + struct testData *x = (struct testData *)NotifiedFifoData(work); + x->id = i; + pushTpool(&pool, work); + } + int sleepmax = 0; + while (parall) { + tmp = pullTpool(&worker_back, &pool); + if (tmp) { + parall--; + struct testData *dd = NotifiedFifoData(tmp); + if (dd->sleepTime > sleepmax) + sleepmax = dd->sleepTime; + delNotifiedFIFO_elt(tmp); + } + } + cumulProcessTime += sleepmax; + } + clock_gettime(CLOCK_MONOTONIC, &end); + long long dur = (end.tv_sec - st.tv_sec) * 1000 * 1000 + (end.tv_nsec - st.tv_nsec) / 1000; + printf("In µs, Total time per group of %d job:%lld, work time per job %d, overhead per job %lld\n", + nb_jobs, + dur / 1000, + cumulProcessTime / 1000, + (dur - cumulProcessTime) / (1000 * nb_jobs)); + return 0; +} diff --git a/common/utils/threadPool/thread-pool.c b/common/utils/threadPool/thread-pool.c index 6410f7f4b9..64341501cb 100644 --- a/common/utils/threadPool/thread-pool.c +++ b/common/utils/threadPool/thread-pool.c @@ -33,17 +33,6 @@ #include #include -void displayList(notifiedFIFO_t *nf) { - int n=0; - notifiedFIFO_elt_t *ptr=nf->outF; - - while(ptr) { - printf("element: %d, key: %lu\n",++n,ptr->key); - ptr=ptr->next; - } - - printf("End of list: %d elements\n",n); -} static inline notifiedFIFO_elt_t *pullNotifiedFifoRemember( notifiedFIFO_t *nf, struct one_thread *thr) { mutexlock(nf->lockF); @@ -176,121 +165,3 @@ void initFloatingCoresTpool(int nbThreads,tpool_t *pool, bool performanceMeas, c threads[sizeof(threads)-1]=0; initNamedTpool(threads, pool, performanceMeas, name); } - -#ifdef TEST_THREAD_POOL -int oai_exit = 0; - -void exit_function(const char *file, const char *function, const int line, const char *s, const int assert) -{ - if (assert) { - abort(); - } else { - exit(EXIT_SUCCESS); - } -} - -struct testData { - int id; - int sleepTime; - char txt[30]; -}; - -void processing(void *arg) { - struct testData *in=(struct testData *)arg; - //printf("doing: %d, %s, in thr %ld\n",in->id, in->txt,pthread_self() ); - sprintf(in->txt,"Done by %ld, job %d", pthread_self(), in->id); - in->sleepTime=rand()%1000; - usleep(in->sleepTime); - //printf("done: %d, %s, in thr %ld\n",in->id, in->txt,pthread_self() ); -} - -int main() { - notifiedFIFO_t myFifo; - initNotifiedFIFO(&myFifo); - pushNotifiedFIFO(&myFifo,newNotifiedFIFO_elt(sizeof(struct testData), 1234,NULL,NULL)); - - for(int i=10; i>1; i--) { - pushNotifiedFIFO(&myFifo,newNotifiedFIFO_elt(sizeof(struct testData), 1000+i,NULL,NULL)); - } - - displayList(&myFifo); - notifiedFIFO_elt_t *tmp=pullNotifiedFIFO(&myFifo); - printf("pulled: %lu\n", tmp->key); - displayList(&myFifo); - tmp=pullNotifiedFIFO(&myFifo); - printf("pulled: %lu\n", tmp->key); - displayList(&myFifo); - pushNotifiedFIFO(&myFifo,newNotifiedFIFO_elt(sizeof(struct testData), 12345678, NULL, NULL)); - displayList(&myFifo); - - do { - tmp=pollNotifiedFIFO(&myFifo); - - if (tmp) { - printf("pulled: %lu\n", tmp->key); - displayList(&myFifo); - } else - printf("Empty list \n"); - } while(tmp); - - tpool_t pool; - char params[]="1,2,3,4,5"; - initTpool(params,&pool, true); - notifiedFIFO_t worker_back; - initNotifiedFIFO(&worker_back); - - sleep(1); - int cumulProcessTime=0, cumulTime=0; - struct timespec st,end; - clock_gettime(CLOCK_MONOTONIC, &st); - int nb_jobs=4; - for (int i=0; i <1000 ; i++) { - int parall=nb_jobs; - for (int j=0; j id=i; - pushTpool(&pool, work); - } - int sleepmax=0; - while (parall) { - tmp=pullTpool(&worker_back,&pool); - if (tmp) { - parall--; - struct testData *dd=NotifiedFifoData(tmp); - if (dd->sleepTime > sleepmax) - sleepmax=dd->sleepTime; - delNotifiedFIFO_elt(tmp); - } - } - cumulProcessTime+=sleepmax; - } - clock_gettime(CLOCK_MONOTONIC, &end); - long long dur=(end.tv_sec-st.tv_sec)*1000*1000+(end.tv_nsec-st.tv_nsec)/1000; - printf("In µs, Total time per group of %d job:%lld, work time per job %d, overhead per job %lld\n", - nb_jobs, dur/1000, cumulProcessTime/1000, (dur-cumulProcessTime)/(1000*nb_jobs)); - - /* - for (int i=0; i <1000 ; i++) { - notifiedFIFO_elt_t *work=newNotifiedFIFO_elt(sizeof(struct testData), i, &worker_back, processing); - struct testData *x=(struct testData *)NotifiedFifoData(work); - x->id=i; - pushTpool(&pool, work); - } - - do { - tmp=pullTpool(&worker_back,&pool); - - if (tmp) { - struct testData *dd=NotifiedFifoData(tmp); - printf("Result: %s\n",dd->txt); - delNotifiedFIFO_elt(tmp); - } else - printf("Empty list \n"); - - abortTpoolJob(&pool,510); - } while(tmp); - */ - return 0; -} -#endif