From d4cf8a17827b19f4fcea688991720996ae418130 Mon Sep 17 00:00:00 2001 From: fanasina Date: Wed, 2 Jul 2025 22:11:46 +0200 Subject: [PATCH] trying to find why too much cpu load! --- y_socket_t/src/y_socket_t/y_socket_t.c | 1 + y_socket_t/test/is_good.c | 2 +- y_worker_t/include/y_worker_t/y_worker_t.h | 2 + y_worker_t/src/y_worker_t/y_task_t.c | 14 +++--- y_worker_t/src/y_worker_t/y_worker_t.c | 58 +++++++++++++++------- y_worker_t/test/is_good.c | 27 +++++++--- 6 files changed, 71 insertions(+), 33 deletions(-) diff --git a/y_socket_t/src/y_socket_t/y_socket_t.c b/y_socket_t/src/y_socket_t/y_socket_t.c index f804768..9cf1013 100644 --- a/y_socket_t/src/y_socket_t/y_socket_t.c +++ b/y_socket_t/src/y_socket_t/y_socket_t.c @@ -331,6 +331,7 @@ void* y_socket_handler_(void *arg){ // printf("debug: kill_all\n"); } } + return NULL; } void *y_socket_poll_fds(void *arg){ struct y_socket_t * argSock = (struct y_socket_t*)arg; diff --git a/y_socket_t/test/is_good.c b/y_socket_t/test/is_good.c index 336ffa4..8ee9c28 100644 --- a/y_socket_t/test/is_good.c +++ b/y_socket_t/test/is_good.c @@ -164,7 +164,7 @@ TEST(searchNode){ TEST(pollThread){ - struct y_socket_t *argS=y_socket_create("1600", 2, 3); + struct y_socket_t *argS=y_socket_create("1600", 2, 1); pthread_t pollTh; pthread_create(&pollTh, NULL, y_socket_poll_fds, (void*)argS); diff --git a/y_worker_t/include/y_worker_t/y_worker_t.h b/y_worker_t/include/y_worker_t/y_worker_t.h index fd82fe9..fa92c79 100644 --- a/y_worker_t/include/y_worker_t/y_worker_t.h +++ b/y_worker_t/include/y_worker_t/y_worker_t.h @@ -19,6 +19,8 @@ struct argWorker; typedef struct y_worker_t{ int exec; + pthread_mutex_t *mut_exec; + pthread_cond_t *cond_exec; int status; pthread_mutex_t *mut_worker; pthread_cond_t *cond_worker; diff --git a/y_worker_t/src/y_worker_t/y_task_t.c b/y_worker_t/src/y_worker_t/y_task_t.c index a01759e..ff6cb34 100644 --- a/y_worker_t/src/y_worker_t/y_task_t.c +++ b/y_worker_t/src/y_worker_t/y_task_t.c @@ -26,21 +26,21 @@ void free_y_tasQ(struct y_tasQ * tasQ){ } void push_tasQ(struct y_tasQ *tasQ, struct y_task_t task){ - //printf("debug: push_tasQ debut\n"); + printf("debug: push_tasQ debut\n"); pthread_mutex_lock(tasQ->mut_tasQ); push_back_list_y_TASK_T(tasQ->list_tasQ, task); pthread_mutex_unlock(tasQ->mut_tasQ); pthread_cond_signal(tasQ->cond_tasQ); - //printf("debug: push_tasQ fin\n"); + printf("debug: push_tasQ fin\n"); } struct list_y_TASK_T* pull_tasQ(struct y_tasQ *tasQ){ - //printf("debug: pull_tasQ debut id_th:%ld\n",pthread_self()); + printf("debug: pull_tasQ debut id_th:%ld\n",pthread_self()); struct list_y_TASK_T *valueRet = NULL; pthread_mutex_lock(tasQ->mut_tasQ); while(tasQ->list_tasQ->end_list == NULL){ pthread_cond_wait(tasQ->cond_tasQ, tasQ->mut_tasQ); } - //printf("debug: call pull_begin_from_list_y_TASK_T debut\n"); + printf("debug: call pull_begin_from_list_y_TASK_T debut\n"); valueRet = pull_begin_from_list_y_TASK_T(tasQ->list_tasQ); // valueRet = pull_end_from_list_y_TASK_T(tasQ->list_tasQ); //printf("debug: call pull_begin_from_list_y_TASK_T fin, is tasQ NULL? : %d\nis tasQ->list_tasQ NULL?:%d\n", tasQ==NULL, tasQ->list_tasQ == NULL); @@ -78,9 +78,10 @@ void * execute_task(void *arg){ struct y_tasQ *historytasQ = argx->historytasQ; struct list_y_TASK_T *l_task=NULL; while(check_go_on_tasQ(argx)){ + printf("\n\ndebug: -------------------> task begin\n\n \n"); l_task = pull_tasQ(tasQ); - //printf("debug: is l_task NULL? = %d\n", l_task==NULL); + printf("debug: is l_task NULL? = %d\n", l_task==NULL); if(l_task){ if((l_task->value.status != TASK_DONE) && (l_task->value.func!=NULL)) l_task->value.ret = l_task->value.func(l_task->value.arg); @@ -91,7 +92,8 @@ void * execute_task(void *arg){ if(l_task) append_list_y_TASK_T(historytasQ->list_tasQ, l_task); pthread_mutex_unlock(historytasQ->mut_tasQ); - + printf("\n\ndebug: -------------------> task done\n\n \n"); + } //printf("debug: -------------------> exit task exec \n"); // usleep(1000); diff --git a/y_worker_t/src/y_worker_t/y_worker_t.c b/y_worker_t/src/y_worker_t/y_worker_t.c index 3d0e1fc..fed3171 100644 --- a/y_worker_t/src/y_worker_t/y_worker_t.c +++ b/y_worker_t/src/y_worker_t/y_worker_t.c @@ -18,6 +18,10 @@ ptr_y_WORKER_T create_ptr_y_WORKER_T(struct main_list_ptr_y_WORKER_T * workers, struct argExecTasQ *argx, int exec, int id ){ ptr_y_WORKER_T pworker = malloc(sizeof(y_WORKER_T)); pworker->exec=exec; + pworker->mut_exec = malloc(sizeof(pthread_mutex_t)); + pthread_mutex_init(pworker->mut_exec, NULL); + pworker->cond_exec = malloc(sizeof(pthread_cond_t)); + pthread_cond_init(pworker->cond_exec, NULL); pworker->status=WORKER_OFF; pworker->mut_worker = malloc(sizeof(pthread_mutex_t)); pthread_mutex_init(pworker->mut_worker, NULL); @@ -39,13 +43,18 @@ ptr_y_WORKER_T create_ptr_y_WORKER_T(struct main_list_ptr_y_WORKER_T * workers, push_back_list_ptr_y_WORKER_T(workers, pworker); pthread_mutex_unlock(mut_workers); - pthread_create(pworker->thread,NULL,execute_work,(void*)(pworker->arg)); + //pthread_create(pworker->thread,NULL,execute_work,(void*)(pworker->arg)); + pthread_create(pworker->thread,NULL,execute_task,(void*)(pworker->arg->argx)); return pworker; } void __free_ptr_y_WORKER_T(void* p_worker){ ptr_y_WORKER_T pworker = (struct y_worker_t*)p_worker; + pthread_mutex_destroy(pworker->mut_exec); + free(pworker->mut_exec); + pthread_cond_destroy(pworker->cond_exec); + free(pworker->cond_exec); pthread_mutex_destroy(pworker->mut_worker); free(pworker->mut_worker); pthread_cond_destroy(pworker->cond_worker); @@ -77,25 +86,28 @@ void* execute_work(void* arg){ struct argExecTasQ *argx=argw->argx; // pthread_mutex_t *mut_workers=argw->mut_workers; struct y_worker_t * pworker = argw->pworker; - - size_t id_thread=pthread_self(); int exec; + size_t id_thread=pthread_self(); pthread_mutex_lock(pworker->mut_worker); - exec=pworker->exec; pworker->status=WORKER_ON; pworker->id_thread=id_thread; pthread_mutex_unlock(pworker->mut_worker); pthread_cond_signal(pworker->cond_worker); //printf("debug: ############################ execute_task call : thread_id:%ld, self=%ld \n",pworker->id,id_thread); - do{ +// do{ //printf("debug: execute_task call : thread_id:%ld, self=%ld \n",pworker->id,id_thread); execute_task((void*)argx); //printf("debug: <<<<>>>> execute_task end, worker exec=%d id:%ld self:%ld \n",exec,pworker->id, pworker->id_thread); - pthread_mutex_lock(pworker->mut_worker); - exec=pworker->exec; - pthread_mutex_unlock(pworker->mut_worker); - //printf("debug: execute_task end, worker exec=%d id:%ld self:%ld \n",exec,pworker->id, pworker->id_thread); - }while(exec); +/* + pthread_mutex_lock(pworker->mut_exec); + while(pworker->exec == 1){ + pthread_cond_wait(pworker->cond_exec, pworker->mut_exec); + } + exec = pworker->exec; + pthread_mutex_unlock(pworker->mut_exec); +*/ + printf("debug: execute_task end, worker exec=%d id:%ld self:%ld \n",exec,pworker->id, pworker->id_thread); +// }while(exec); pthread_mutex_lock(pworker->mut_worker); @@ -108,8 +120,11 @@ void* execute_work(void* arg){ } void wait_workers(struct main_list_ptr_y_WORKER_T *workers){ - for(move_current_to_begin_list_ptr_y_WORKER_T(workers); workers->current_list;increment_list_ptr_y_WORKER_T(workers)){ - pthread_join(*(workers->current_list->value->thread) ,NULL); + // for(move_current_to_begin_list_ptr_y_WORKER_T(workers); workers->current_list;increment_list_ptr_y_WORKER_T(workers)){ + // pthread_join(*(workers->current_list->value->thread) ,NULL); + // } + for(struct list_ptr_y_WORKER_T * loc_current_list = workers->begin_list; loc_current_list; loc_current_list = loc_current_list->next ){ + pthread_join(*(loc_current_list->value->thread) ,NULL); } } @@ -124,6 +139,10 @@ int check_worker_status(struct y_worker_t * pworker ){ GEN_FUNC_PTR_LIST_FREE(ptr_y_WORKER_T){ ptr_y_WORKER_T pworker = (struct y_worker_t*)arg; + pthread_mutex_destroy(pworker->mut_exec); + free(pworker->mut_exec); + pthread_cond_destroy(pworker->cond_exec); + free(pworker->cond_exec); pthread_mutex_destroy(pworker->mut_worker); free(pworker->mut_worker); pthread_cond_destroy(pworker->cond_worker); @@ -137,17 +156,20 @@ void kill_all_workers ( struct argWorker *argw){ struct main_list_ptr_y_WORKER_T * workers = argw->workers; struct argExecTasQ *argx = argw->argx; - for(move_current_to_begin_list_ptr_y_WORKER_T(workers); workers->current_list;increment_list_ptr_y_WORKER_T(workers)){ - pthread_mutex_lock(workers->current_list->value->mut_worker); - workers->current_list->value->exec=KILL_WORKER; - pthread_mutex_unlock(workers->current_list->value->mut_worker); +// for(move_current_to_begin_list_ptr_y_WORKER_T(workers); workers->current_list;increment_list_ptr_y_WORKER_T(workers)){} + for(struct list_ptr_y_WORKER_T * loc_current_list = workers->begin_list; loc_current_list; loc_current_list = loc_current_list->next ){ + pthread_mutex_lock(loc_current_list->value->mut_exec); + loc_current_list->value->exec=KILL_WORKER; + pthread_mutex_unlock(loc_current_list->value->mut_exec); + pthread_cond_signal(loc_current_list->value->cond_exec); } pthread_mutex_lock(argx->tasQ->mut_tasQ); (argx->go_on)=0; pthread_mutex_unlock(argx->tasQ->mut_tasQ); - for(move_current_to_begin_list_ptr_y_WORKER_T(workers); workers->current_list;increment_list_ptr_y_WORKER_T(workers)){ +// for(move_current_to_begin_list_ptr_y_WORKER_T(workers); workers->current_list;increment_list_ptr_y_WORKER_T(workers)){} + for(struct list_ptr_y_WORKER_T * loc_current_list = workers->begin_list; loc_current_list; loc_current_list = loc_current_list->next ){ /// if(check_worker_status(workers->current_list->value) == WORKER_ON) { struct y_task_t task = { @@ -159,7 +181,7 @@ void kill_all_workers ( struct argWorker *argw){ } } ///} - + wait_workers(workers); diff --git a/y_worker_t/test/is_good.c b/y_worker_t/test/is_good.c index e3ce3bd..7ccd6ad 100644 --- a/y_worker_t/test/is_good.c +++ b/y_worker_t/test/is_good.c @@ -4,6 +4,8 @@ #include +#include + // for sleep ! #ifdef __linux__ #include @@ -57,7 +59,7 @@ void* funcPrintSelf(void* arg){ // struct argExecTasQ *argx=(struct argExecTasQ*)arg; size_t thread_id=pthread_self(); LOG("func: number=%d; threadid=%ld\n", *count, thread_id); - usleep(200000); + usleep(2000); LOG("func: end func %d; in thread=%ld\n",*count,thread_id); // free(count); return NULL; @@ -69,7 +71,7 @@ void* funcEnd(void* arg){ static long int count = 0; //randomm=rand()%1000; size_t thread_id=pthread_self(); LOG("funcEnd: my status=%d, func number=%ld; threadid=%ld\n",(argx->go_on), count, thread_id); - usleep(20000); + usleep(2000); LOG("funcEnd: end func %ld; in thread=%ld\n",count,thread_id); ++count; return NULL; @@ -80,7 +82,7 @@ void* funcDepRel(void* arg){ struct dependency_task *dep = (struct dependency_task*)arg; size_t thread_id=pthread_self(); LOG("func: threadid=%ld\n", thread_id); - usleep(300000); + usleep(3000); release_dependancy_task(dep); LOG("==============-------------------> func: end func release in thread=%ld\n",thread_id); return NULL; @@ -116,7 +118,7 @@ int nb_worker=4; LOG(" - / -----  -- / --- %d workers %ld created\n",nb_worker,(pw->arg->pworker->id)); //usleep(500); } -usleep(50000); +usleep(500); int nb_task=59; for(int i=0; ibegin_list->value->arg); @@ -192,9 +194,9 @@ int nb_worker=4; LOG(" - / -----  -- / --- %d workers %ld created\n",nb_worker,(pw->arg->pworker->id)); //usleep(500); } -usleep(50000); +usleep(500); -int nb_task=59; +int nb_task=9; for(int i=0; ibegin_list->value->arg);