Add debug instrumentation; fix pholder freeing logic

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@1599 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo 2009-03-13 22:35:23 +00:00
parent b72d773051
commit e1412760e1
4 changed files with 110 additions and 1 deletions

View file

@ -155,6 +155,10 @@ static void check_test_memory_usage(void)
#else
memcpy(&g_mmprevious, &g_mmafter, sizeof(struct mallinfo));
#endif
/* If so enabled, show the use of priority inheritance resources */
dump_nfreeholders("user_main:");
}
#else
# define check_test_memory_usage()

View file

@ -71,6 +71,14 @@
# define CONFIG_EXAMPLES_OSTEST_NBARRIER_THREADS 8
#endif
/* Priority inheritance */
#if defined(CONFIG_DEBUG) && defined(CONFIG_PRIORITY_INHERITANCE) && defined(CONFIG_SEM_PHDEBUG)
# define dump_nfreeholders(s) printf(s " nfreeholders: %d\n", sem_nfreeholders())
#else
# define dump_nfreeholders(s)
#endif
/****************************************************************************
* Public Types
****************************************************************************/
@ -139,4 +147,16 @@ extern void barrier_test(void);
extern void priority_inheritance(void);
/* APIs exported (conditionally) by the OS specifically for testing of
* priority inheritance
*/
#if defined(CONFIG_DEBUG) && defined(CONFIG_PRIORITY_INHERITANCE) && defined(CONFIG_SEM_PHDEBUG)
extern void sem_enumholders(FAR sem_t *sem);
extern int sem_nfreeholders(void);
#else
# define sem_enumholders(sem)
# define sem_nfreeholders()
#endif
#endif /* __OSTEST_H */

View file

@ -269,6 +269,7 @@ static void *lowpri_thread(void *parameter)
printf(" g_highstate[%d]: %d\n", i, (int)g_highstate[i]);
}
printf(" I still have a count on the semaphore\n");
sem_enumholders(&g_sem);
fflush(stdout);
sleep(1);
}
@ -308,6 +309,7 @@ static void *lowpri_thread(void *parameter)
}
ret = pthread_getschedparam(pthread_self(), &policy, &sparam);
sem_enumholders(&g_sem);
sem_post(&g_sem);
if (ret != 0)
{
@ -352,6 +354,7 @@ static void *lowpri_thread(void *parameter)
printf(" ERROR should have been %d\n", g_lowpri);
}
}
sem_enumholders(&g_sem);
printf("lowpri_thread-%d: Okay... I'm done!\n", threadno);
fflush(stdout);
@ -396,6 +399,7 @@ void priority_inheritance(void)
g_medpri = my_pri - 1;
sem_init(&g_sem, 0, NLOWPRI_THREADS);
dump_nfreeholders("priority_inheritance:");
/* Start the low priority threads */
@ -429,6 +433,7 @@ void priority_inheritance(void)
}
printf("priority_inheritance: Waiting...\n");
sleep(2);
dump_nfreeholders("priority_inheritance:");
/* Start the medium priority thread */
@ -458,6 +463,7 @@ void priority_inheritance(void)
}
printf("priority_inheritance: Waiting...\n");
sleep(1);
dump_nfreeholders("priority_inheritance:");
/* Start the high priority threads */
@ -491,6 +497,8 @@ void priority_inheritance(void)
printf("priority_inheritance: pthread_create failed, status=%d\n", status);
}
}
dump_nfreeholders("priority_inheritance:");
fflush(stdout);
/* Wait for all thread instances to complete */
@ -499,19 +507,23 @@ void priority_inheritance(void)
printf("priority_inheritance: Waiting for highpri_thread-%d to complete\n", i+1);
fflush(stdout);
(void)pthread_join(highpri[i], &result);
dump_nfreeholders("priority_inheritance:");
}
printf("priority_inheritance: Waiting for medpri_thread to complete\n");
fflush(stdout);
(void)pthread_join(medpri, &result);
dump_nfreeholders("priority_inheritance:");
for (i = 0; i < NLOWPRI_THREADS; i++)
{
printf("priority_inheritance: Waiting for lowpri_thread-%d to complete\n", i+1);
fflush(stdout);
(void)pthread_join(lowpri[i], &result);
dump_nfreeholders("priority_inheritance:");
}
printf("priority_inheritance: Finished\n");
sem_destroy(&g_sem);
dump_nfreeholders("priority_inheritance:");
fflush(stdout);
#endif /* CONFIG_PRIORITY_INHERITANCE && !CONFIG_DISABLE_SIGNALS && !CONFIG_DISABLE_PTHREAD */
}

View file

@ -251,6 +251,7 @@ static int sem_foreachholder(FAR sem_t *sem, holderhandler_t handler, FAR void *
static int sem_recoverholders(struct semholder_s *pholder, FAR sem_t *sem, FAR void *arg)
{
sem_freeholder(sem, pholder);
return 0;
}
/****************************************************************************
@ -376,6 +377,23 @@ static int sem_verifyholder(struct semholder_s *pholder, FAR sem_t *sem, FAR voi
}
#endif
/****************************************************************************
* Name: sem_dumpholder
****************************************************************************/
#if defined(CONFIG_DEBUG) && defined(CONFIG_SEM_PHDEBUG)
static int sem_dumpholder(struct semholder_s *pholder, FAR sem_t *sem, FAR void *arg)
{
#if CONFIG_SEM_PREALLOCHOLDERS > 0
dbg(" %08x: %08x %08x %04x\n",
pholder, pholder->flink, pholder->holder, pholder->counts);
#else
dbg(" %08x: %08x %04x\n", pholder, pholder->holder, pholder->counts);
#endif
return 0;
}
#endif
/****************************************************************************
* Name: sem_restoreholderprio
****************************************************************************/
@ -759,7 +777,7 @@ void sem_restorebaseprio(FAR _TCB *stcb, FAR sem_t *sem)
* Description:
* Called from sem_post() after a thread that was waiting for a semaphore
* count was awakened because of a signal and the semaphore wait has been
* canceld.
* canceled.
*
* Parameters:
* sem - A reference to the semaphore no longer being waited for
@ -784,4 +802,59 @@ void sem_canceled(FAR sem_t *sem)
(void)sem_foreachholder(sem, sem_restoreholderprio, rtcb);
}
/****************************************************************************
* Function: sem_enumholders
*
* Description:
* Show information about threads currently waiting on this semaphore
*
* Parameters:
* sem - A reference to the semaphore
*
* Return Value:
* None
*
* Assumptions:
*
****************************************************************************/
#if defined(CONFIG_DEBUG) && defined(CONFIG_SEM_PHDEBUG)
void sem_enumholders(FAR sem_t *sem)
{
(void)sem_foreachholder(sem, sem_dumpholder, NULL);
}
#endif
/****************************************************************************
* Function: sem_nfreeholders
*
* Description:
* Return the number of available holder containers. This is a good way
* to find out which threads are not calling sem_destroy.
*
* Parameters:
* sem - A reference to the semaphore
*
* Return Value:
* The number of available holder containers
*
* Assumptions:
*
****************************************************************************/
#if defined(CONFIG_DEBUG) && defined(CONFIG_SEM_PHDEBUG)
int sem_nfreeholders(void)
{
#if CONFIG_SEM_PREALLOCHOLDERS > 0
FAR struct semholder_s *pholder;
int n;
for (pholder = g_freeholders, n = 0; pholder; pholder = pholder->flink) n++;
return n;
#else
return 0;
#endif
}
#endif
#endif /* CONFIG_PRIORITY_INHERITANCE */