Commit b1f7cc90 authored by Bartosz Podrygajlo's avatar Bartosz Podrygajlo

Add unit testcase for thread-pool.

Extract thread-pool into a separate CMake library.
Extract utils into a separate CMake library.
parent deacf439
......@@ -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
#####################
......
......@@ -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})
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 .)
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
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)
/*
* 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 <sched.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/sysinfo.h>
#include <threadPool/thread-pool.h>
#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;
}
......@@ -33,17 +33,6 @@
#include <sys/sysinfo.h>
#include <threadPool/thread-pool.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);
}
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 <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));
/*
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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment