Skip to content

Commit 3b60c0c

Browse files
dpelled99kris
authored andcommitted
Report thread names in stackusage output (#6)
stackusage reports the function pointer of each thread. It can be unclear to users what each thread really is. This change makes stackusage also report the thread name. Applications can set a thread name using POSIX API pthread_setname_np(). When not set, the thread name is the program name. Example of output (notice the "name" last column which is added by this change): stackusage log at 2019-10-31 11:12:41 ---------------------------------------- pid id tid requested actual maxuse max% dur funcP name ... 2254 15 22272 8388608 8388608 74676 0 14 0x7f9228afe890 Fts ... 22254 19 22273 8388608 8388608 15328 0 1 0x7f9228afe890 FtsGetRemoteCat ... 22254 42 22313 8388608 8388608 10664 0 1 0x7f9228afe890 FtsAsyncInitial 22254 43 22314 8388608 8388608 46584 0 1 0x7f9228afe890 FtsLocalSearchA I've only checked on Linux x86_64. I don't have access to other platforms.
1 parent 2d03ce8 commit 3b60c0c

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

src/sumain.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ typedef struct su_threadinfo_s
8181
time_t time_stop;
8282
int time_duration;
8383
void *func_ptr;
84+
char thread_name[16];
8485
struct su_threadinfo_s *next;
8586
} su_threadinfo_t;
8687

@@ -147,7 +148,7 @@ void __attribute__ ((destructor)) su_fini(void)
147148
{
148149
if(su_inited == 1)
149150
{
150-
/* Unegister main thread */
151+
/* Unregister main thread */
151152
su_thread_fini(NULL);
152153

153154
/* Log stack usage in process */
@@ -364,6 +365,8 @@ static void su_thread_init(su_threadtype_t threadtype, pthread_attr_t *rattr,
364365
threadinfo->threadtype = threadtype;
365366
threadinfo->pthread = pthread_self();
366367
threadinfo->func_ptr = func_ptr;
368+
threadinfo->thread_name[0] = '\0';
369+
367370
#ifdef __linux__
368371
threadinfo->tid = syscall(SYS_gettid);
369372
#endif
@@ -513,7 +516,7 @@ static void su_log_stack_usage(void)
513516
SU_LOG("%s log at %s ----------------------------------------\n",
514517
su_name, timestamp);
515518
SU_LOG(" pid id tid requested actual maxuse max%% dur"
516-
" funcP\n");
519+
" funcP name\n");
517520
while(threadinfo_it)
518521
{
519522
int usage_percent = 0;
@@ -524,7 +527,7 @@ static void su_log_stack_usage(void)
524527
(int) threadinfo_it->stack_req_size;
525528
}
526529

527-
SU_LOG("%5d %3d %5d %9d %9d %9d %3d %5d %p\n",
530+
SU_LOG("%5d %3d %5d %9d %9d %9d %3d %5d %18p %s\n",
528531
getpid(),
529532
threadinfo_it->id,
530533
threadinfo_it->tid,
@@ -533,7 +536,8 @@ static void su_log_stack_usage(void)
533536
(int) threadinfo_it->stack_max_usage,
534537
(int) usage_percent,
535538
threadinfo_it->time_duration,
536-
threadinfo_it->func_ptr
539+
threadinfo_it->func_ptr,
540+
threadinfo_it->thread_name
537541
);
538542

539543
threadinfo_it = threadinfo_it->next;
@@ -608,6 +612,9 @@ static void su_thread_fini(void *key)
608612
/* Update its stack usage info */
609613
su_get_stack_usage(threadinfo);
610614

615+
/* Record the thread name */
616+
pthread_getname_np(threadinfo->pthread, threadinfo->thread_name, 16);
617+
611618
/* Store stop time and calculate duration */
612619
if(time(&(threadinfo->time_stop)) != ((time_t)-1))
613620
{

0 commit comments

Comments
 (0)