32
32
#define SU_ENV_STDERR "SU_STDERR"
33
33
#define SU_ENV_SIGNO "SU_SIGNO"
34
34
#define SU_ENV_SYSLOG "SU_SYSLOG"
35
+ #define SU_ENV_HUMAN "SU_HUMAN"
35
36
#define SU_FILL_BYTE 0xcd
36
37
#define SU_FILL_OFFSET 512
37
38
#define SU_GROW_MARGIN (256*1024)
@@ -128,6 +129,7 @@ static int su_inited = 0;
128
129
static int su_log_signo = 0 ;
129
130
static int su_log_stderr = 0 ;
130
131
static int su_log_syslog = 0 ;
132
+ static int su_log_human = 0 ;
131
133
static char * su_log_file = NULL ;
132
134
static struct su_threadinfo_s * threadinfo_head = NULL ;
133
135
static 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,
526
528
}
527
529
528
530
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
+
529
544
static void su_log_stack_usage (void )
530
545
{
531
546
struct su_threadinfo_s * threadinfo_it = NULL ;
@@ -539,8 +554,15 @@ static void su_log_stack_usage(void)
539
554
tm .tm_year + 1900 , tm .tm_mon + 1 , tm .tm_mday , tm .tm_hour , tm .tm_min , tm .tm_sec );
540
555
SU_LOG ("%s log at %s ----------------------------------------\n" ,
541
556
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
+
544
566
while (threadinfo_it )
545
567
{
546
568
int usage_percent = 0 ;
@@ -551,18 +573,38 @@ static void su_log_stack_usage(void)
551
573
(int ) threadinfo_it -> stack_req_size ;
552
574
}
553
575
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
+ }
566
608
567
609
threadinfo_it = threadinfo_it -> next ;
568
610
}
@@ -678,6 +720,11 @@ static void su_get_env(void)
678
720
su_log_signo = strtol (getenv (SU_ENV_SIGNO ), NULL , 10 );
679
721
}
680
722
723
+ if (getenv (SU_ENV_HUMAN ))
724
+ {
725
+ su_log_human = strtol (getenv (SU_ENV_HUMAN ), NULL , 10 );
726
+ }
727
+
681
728
su_log_file = getenv (SU_ENV_FILE );
682
729
}
683
730
0 commit comments