Skip to content

Commit 08b703f

Browse files
authored
Merge pull request #16 from charmitro/human-readable
stackusage: add human-readable output format option
2 parents c30c9d8 + 2b6c713 commit 08b703f

File tree

2 files changed

+70
-16
lines changed

2 files changed

+70
-16
lines changed

src/stackusage

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ showusage()
1414
echo "designed to work in resource-constrained environments, such as "
1515
echo "embedded systems."
1616
echo ""
17-
echo "Usage: stackusage [-d] [-o PATH] [-s SIG] PROG [ARGS..]"
17+
echo "Usage: stackusage [-d] [-h] [-o PATH] [-s SIG] PROG [ARGS..]"
1818
echo " or: stackusage --help"
1919
echo " or: stackusage --version"
2020
echo ""
2121
echo "Options:"
2222
echo " -d debug mode, running program through debugger"
23+
echo " -h human-readable output (show sizes in KB/MB instead of bytes)"
2324
echo " -o <PATH> write output to specified file path, instead of stderr"
2425
echo " -s <SIG> enable on-demand logging when signalled SIG signal"
2526
echo " PROG program to run and analyze"
@@ -85,7 +86,8 @@ fi
8586
DEBUG="0"
8687
SIGNO=""
8788
OUTFILE=""
88-
while getopts "?do:s:" OPT; do
89+
HUMAN="0"
90+
while getopts "?dho:s:" OPT; do
8991
case "${OPT}" in
9092
\?)
9193
showusage
@@ -94,6 +96,9 @@ while getopts "?do:s:" OPT; do
9496
d)
9597
DEBUG="1"
9698
;;
99+
h)
100+
HUMAN="1"
101+
;;
97102
o)
98103
OUTFILE="${OPTARG}"
99104
;;
@@ -180,13 +185,15 @@ while [ "${LIBPATHS[CNT]}" != "" ]; do
180185
if [ "${DEBUG}" == "0" ]; then
181186
SU_FILE="${TMPLOG}${OUTFILE}" \
182187
SU_SIGNO="${SIGNO}" \
188+
SU_HUMAN="${HUMAN}" \
183189
LD_PRELOAD="${LIBPATH}" \
184190
DYLD_INSERT_LIBRARIES="${LIBPATH}" \
185191
"${@:1}"
186192
else
187193
LLDBCMDPATH="${TMP}/lldb.cmd"
188194
echo "env SU_FILE=\"${TMPLOG}${OUTFILE}\"" > "${LLDBCMDPATH}"
189195
echo "env SU_SIGNO=\"${SIGNO}\"" >> "${LLDBCMDPATH}"
196+
echo "env SU_HUMAN=\"${HUMAN}\"" >> "${LLDBCMDPATH}"
190197
echo "env LD_PRELOAD=\"${LIBPATH}\"" >> "${LLDBCMDPATH}"
191198
echo "env DYLD_INSERT_LIBRARIES=\"${LIBPATH}\"" >> "${LLDBCMDPATH}"
192199
echo "run ${@:2}" >> "${LLDBCMDPATH}"

src/sumain.c

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
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;
128129
static int su_log_signo = 0;
129130
static int su_log_stderr = 0;
130131
static int su_log_syslog = 0;
132+
static int su_log_human = 0;
131133
static char *su_log_file = NULL;
132134
static struct su_threadinfo_s *threadinfo_head = NULL;
133135
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,
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+
529544
static 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

Comments
 (0)