From ff9f690c4e2ed607e39b0336047e09cd430f83c4 Mon Sep 17 00:00:00 2001 From: Daniel Burns Date: Fri, 3 Jul 2020 15:59:10 +0000 Subject: [PATCH] Fix #532, Set pthread names to match CFE tasks names This change allows underlying OS tools to view thread names for platforms that support the pthread_setname_np function. --- src/bsp/pc-linux/config/osconfig.h | 6 +++++ src/os/posix/os-posix.h | 5 +---- src/os/posix/osapi.c | 36 +++++++++++++++++++++++++++--- src/os/posix/ostimer.c | 11 +++++++-- 4 files changed, 49 insertions(+), 9 deletions(-) diff --git a/src/bsp/pc-linux/config/osconfig.h b/src/bsp/pc-linux/config/osconfig.h index ec0a26d9d..1320468ca 100644 --- a/src/bsp/pc-linux/config/osconfig.h +++ b/src/bsp/pc-linux/config/osconfig.h @@ -196,4 +196,10 @@ */ #define OSAL_DEBUG_PERMISSIVE_MODE +/* + * If OS_HAVE_PTHREAD_SETNAME_NP is defined, this will allow underlying pthread names + * to match CFE task names + */ +#define OS_HAVE_PTHREAD_SETNAME_NP + #endif diff --git a/src/os/posix/os-posix.h b/src/os/posix/os-posix.h index 0fbeed7a2..0c4a425d2 100644 --- a/src/os/posix/os-posix.h +++ b/src/os/posix/os-posix.h @@ -107,7 +107,4 @@ int32 OS_Posix_StreamAPI_Impl_Init(void); int32 OS_Posix_DirAPI_Impl_Init(void); int32 OS_Posix_FileSysAPI_Impl_Init(void); -int32 OS_Posix_InternalTaskCreate_Impl (pthread_t *thr, uint32 priority, size_t stacksz, PthreadFuncPtr_t Entry, void *entry_arg); - - - +int32 OS_Posix_InternalTaskCreate_Impl (pthread_t *thr, const char *thread_name, uint32 priority, size_t stacksz, PthreadFuncPtr_t Entry, void *entry_arg); diff --git a/src/os/posix/osapi.c b/src/os/posix/osapi.c index 64d17119f..f8e3d5ebe 100644 --- a/src/os/posix/osapi.c +++ b/src/os/posix/osapi.c @@ -163,6 +163,10 @@ const OS_ErrorTable_Entry_t OS_IMPL_ERROR_NAME_TABLE[] = { { 0, NULL } }; static void OS_CompAbsDelayTime( uint32 milli_second , struct timespec * tm); static int OS_PriorityRemap(uint32 InputPri); +#ifdef OS_HAVE_PTHREAD_SETNAME_NP +extern int pthread_setname_np(pthread_t thread, const char *name); +#endif + /*---------------------------------------------------------------- * @@ -734,7 +738,9 @@ int32 OS_Posix_TaskAPI_Impl_Init(void) * Purpose: Local helper routine, not part of OSAL API. * *-----------------------------------------------------------------*/ -int32 OS_Posix_InternalTaskCreate_Impl(pthread_t *pthr, uint32 priority, size_t stacksz, PthreadFuncPtr_t entry, void *entry_arg) +int32 OS_Posix_InternalTaskCreate_Impl( + pthread_t *pthr, const char* thread_name, uint32 priority, size_t stacksz, + PthreadFuncPtr_t entry, void *entry_arg) { int return_code = 0; pthread_attr_t custom_attr; @@ -830,6 +836,20 @@ int32 OS_Posix_InternalTaskCreate_Impl(pthread_t *pthr, uint32 priority, size_t return(OS_ERROR); } + /* + ** Set the thread name through the POSIX API to allow for visibility in OS + ** utilities + */ + +#ifdef OS_HAVE_PTHREAD_SETNAME_NP + return_code = pthread_setname_np(*pthr, thread_name); + + if (return_code != 0) + { + OS_DEBUG("pthread_setname_np error in OS_TaskCreate: %s\n",strerror(return_code)); + } +#endif + /* ** Free the resources that are no longer needed ** Since the task is now running - pthread_create() was successful - @@ -870,6 +890,7 @@ int32 OS_TaskCreate_Impl (uint32 task_id, uint32 flags) return_code = OS_Posix_InternalTaskCreate_Impl( &OS_impl_task_table[task_id].id, + OS_task_table[task_id].task_name, OS_task_table[task_id].priority, OS_task_table[task_id].stack_size, OS_PthreadTaskEntry, @@ -2470,6 +2491,8 @@ int32 OS_ConsoleCreate_Impl(uint32 local_id) OS_impl_console_internal_record_t *local = &OS_impl_console_table[local_id]; pthread_t consoletask; int32 return_code; + char console_name[OS_MAX_API_NAME]; + OS_U32ValueWrapper_t local_arg = { 0 }; if (local_id == 0) @@ -2487,8 +2510,15 @@ int32 OS_ConsoleCreate_Impl(uint32 local_id) else { local_arg.value = local_id; - return_code = OS_Posix_InternalTaskCreate_Impl(&consoletask, OS_CONSOLE_TASK_PRIORITY, 0, - OS_ConsoleTask_Entry, local_arg.opaque_arg); + + /* + ** Construct the console task name: + ** The name will consist of "console.{console local id}" + */ + snprintf(console_name, sizeof(console_name), "console.%d", local_id); + + return_code = OS_Posix_InternalTaskCreate_Impl(&consoletask, console_name, + OS_CONSOLE_TASK_PRIORITY, 0, OS_ConsoleTask_Entry, local_arg.opaque_arg); if (return_code != OS_SUCCESS) { diff --git a/src/os/posix/ostimer.c b/src/os/posix/ostimer.c index d3d5ae4e0..2ea4e7859 100644 --- a/src/os/posix/ostimer.c +++ b/src/os/posix/ostimer.c @@ -351,7 +351,7 @@ int32 OS_TimeBaseCreate_Impl(uint32 timer_id) OS_impl_timebase_internal_record_t *local; OS_common_record_t *global; OS_U32ValueWrapper_t arg; - + char timer_name[OS_MAX_API_NAME]; local = &OS_impl_timebase_table[timer_id]; global = &OS_global_timebase_table[timer_id]; @@ -368,7 +368,14 @@ int32 OS_TimeBaseCreate_Impl(uint32 timer_id) */ arg.opaque_arg = NULL; arg.value = global->active_id; - return_code = OS_Posix_InternalTaskCreate_Impl(&local->handler_thread, 0, 0, OS_TimeBasePthreadEntry, arg.opaque_arg); + + /* + ** Construct the timer thread name: + ** The name will consist of "timer.{timer id}" + */ + snprintf(timer_name, sizeof(timer_name), "timer.%d", timer_id); + + return_code = OS_Posix_InternalTaskCreate_Impl(&local->handler_thread, timer_name, 0, 0, OS_TimeBasePthreadEntry, arg.opaque_arg); if (return_code != OS_SUCCESS) { return return_code;