3232#define SU_ENV_STDERR "SU_STDERR"
3333#define SU_ENV_SIGNO "SU_SIGNO"
3434#define SU_ENV_SYSLOG "SU_SYSLOG"
35+ #define SU_ENV_HUMAN "SU_HUMAN"
3536#define SU_FILL_BYTE 0xcd
3637#define SU_FILL_OFFSET 512
3738#define SU_GROW_MARGIN (256*1024)
@@ -128,6 +129,7 @@ static int su_inited = 0;
128129static int su_log_signo = 0 ;
129130static int su_log_stderr = 0 ;
130131static int su_log_syslog = 0 ;
132+ static int su_log_human = 0 ;
131133static char * su_log_file = NULL ;
132134static struct su_threadinfo_s * threadinfo_head = NULL ;
133135static pthread_mutex_t threadinfo_mx = PTHREAD_MUTEX_INITIALIZER ;
@@ -526,6 +528,19 @@ static void su_thread_init(su_threadtype_t threadtype, pthread_attr_t *rattr,
526528}
527529
528530
531+ static void su_format_size_human (size_t bytes , char * buffer , size_t buflen )
532+ {
533+ if (bytes < 1024 ) {
534+ snprintf (buffer , buflen , "%zu B" , bytes );
535+ } else if (bytes < 1024 * 1024 ) {
536+ snprintf (buffer , buflen , "%.1f KB" , bytes / 1024.0 );
537+ } else if (bytes < 1024 * 1024 * 1024 ) {
538+ snprintf (buffer , buflen , "%.1f MB" , bytes / (1024.0 * 1024.0 ));
539+ } else {
540+ snprintf (buffer , buflen , "%.1f GB" , bytes / (1024.0 * 1024.0 * 1024.0 ));
541+ }
542+ }
543+
529544static void su_log_stack_usage (void )
530545{
531546 struct su_threadinfo_s * threadinfo_it = NULL ;
@@ -539,8 +554,15 @@ static void su_log_stack_usage(void)
539554 tm .tm_year + 1900 , tm .tm_mon + 1 , tm .tm_mday , tm .tm_hour , tm .tm_min , tm .tm_sec );
540555 SU_LOG ("%s log at %s ----------------------------------------\n" ,
541556 su_name , timestamp );
542- SU_LOG (" pid id tid requested actual maxuse max%% dur"
543- " funcP name\n" );
557+
558+ if (su_log_human ) {
559+ SU_LOG (" pid id tid requested actual maxuse max%% dur"
560+ " funcP name\n" );
561+ } else {
562+ SU_LOG (" pid id tid requested actual maxuse max%% dur"
563+ " funcP name\n" );
564+ }
565+
544566 while (threadinfo_it )
545567 {
546568 int usage_percent = 0 ;
@@ -551,18 +573,38 @@ static void su_log_stack_usage(void)
551573 (int ) threadinfo_it -> stack_req_size ;
552574 }
553575
554- SU_LOG ("%5d %3d %5d %9d %9d %9d %3d %5d %18p %s\n" ,
555- getpid (),
556- threadinfo_it -> id ,
557- threadinfo_it -> tid ,
558- (int ) threadinfo_it -> stack_req_size ,
559- (int ) threadinfo_it -> stack_size ,
560- (int ) threadinfo_it -> stack_max_usage ,
561- (int ) usage_percent ,
562- threadinfo_it -> time_duration ,
563- threadinfo_it -> func_ptr ,
564- threadinfo_it -> thread_name
565- );
576+ if (su_log_human ) {
577+ char req_buf [16 ], actual_buf [16 ], max_buf [16 ];
578+ su_format_size_human (threadinfo_it -> stack_req_size , req_buf , sizeof (req_buf ));
579+ su_format_size_human (threadinfo_it -> stack_size , actual_buf , sizeof (actual_buf ));
580+ su_format_size_human (threadinfo_it -> stack_max_usage , max_buf , sizeof (max_buf ));
581+
582+ SU_LOG ("%5d %3d %5d %10s %10s %10s %3d %5d %18p %s\n" ,
583+ getpid (),
584+ threadinfo_it -> id ,
585+ threadinfo_it -> tid ,
586+ req_buf ,
587+ actual_buf ,
588+ max_buf ,
589+ (int ) usage_percent ,
590+ threadinfo_it -> time_duration ,
591+ threadinfo_it -> func_ptr ,
592+ threadinfo_it -> thread_name
593+ );
594+ } else {
595+ SU_LOG ("%5d %3d %5d %9d %9d %9d %3d %5d %18p %s\n" ,
596+ getpid (),
597+ threadinfo_it -> id ,
598+ threadinfo_it -> tid ,
599+ (int ) threadinfo_it -> stack_req_size ,
600+ (int ) threadinfo_it -> stack_size ,
601+ (int ) threadinfo_it -> stack_max_usage ,
602+ (int ) usage_percent ,
603+ threadinfo_it -> time_duration ,
604+ threadinfo_it -> func_ptr ,
605+ threadinfo_it -> thread_name
606+ );
607+ }
566608
567609 threadinfo_it = threadinfo_it -> next ;
568610 }
@@ -678,6 +720,11 @@ static void su_get_env(void)
678720 su_log_signo = strtol (getenv (SU_ENV_SIGNO ), NULL , 10 );
679721 }
680722
723+ if (getenv (SU_ENV_HUMAN ))
724+ {
725+ su_log_human = strtol (getenv (SU_ENV_HUMAN ), NULL , 10 );
726+ }
727+
681728 su_log_file = getenv (SU_ENV_FILE );
682729}
683730
0 commit comments