Skip to content

Commit 6fa33c7

Browse files
author
olevole
committed
ability to view function execution time (for debug)
1 parent de8aed6 commit 6fa33c7

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

bin/cbsdsh/eval.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ __FBSDID("$FreeBSD: head/bin/sh/eval.c 340284 2018-11-09 14:58:24Z jilles $");
7272
#include "myhistedit.h"
7373
#endif
7474

75+
// CBSD
76+
#include <sys/time.h>
77+
extern int cbsd_function_time;
78+
int cbsd_function_time = 0;
79+
7580
int evalskip; /* set if we are skipping commands */
7681
int skipcount; /* number of levels to skip */
7782
static int loopnest; /* current loop nesting level */
@@ -821,6 +826,9 @@ evalcommand(union node *cmd, int flags, struct backcmd *backcmd)
821826
const char *path = pathval();
822827
int i;
823828

829+
//CBSD
830+
struct timeval start, end;
831+
824832
/* First expand the arguments. */
825833
TRACE(("evalcommand(%p, %d) called\n", (void *)cmd, flags));
826834
emptyarglist(&arglist);
@@ -1004,6 +1012,9 @@ evalcommand(union node *cmd, int flags, struct backcmd *backcmd)
10041012
trputs("Shell function: ");
10051013
trargs(argv);
10061014
#endif
1015+
if (cbsd_function_time == 1) {
1016+
gettimeofday(&start, NULL);
1017+
}
10071018
saveparam = shellparam;
10081019
shellparam.malloc = 0;
10091020
shellparam.reset = 1;
@@ -1052,6 +1063,14 @@ evalcommand(union node *cmd, int flags, struct backcmd *backcmd)
10521063
}
10531064
if (jp)
10541065
exitshell(exitstatus);
1066+
1067+
if (cbsd_function_time==1) {
1068+
gettimeofday(&end, NULL);
1069+
long seconds = end.tv_sec - start.tv_sec;
1070+
long useconds = end.tv_usec - start.tv_usec;
1071+
double elapsed = seconds + useconds / 1e6;
1072+
out2fmt_flush("cbsd_function_time{function=\"%s\"} %.6f\n", argv[0],elapsed);
1073+
}
10551074
} else if (cmdentry.cmdtype == CMDBUILTIN) {
10561075
#ifdef DEBUG
10571076
trputs("builtin command: ");

bin/cbsdsh/main.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ int localeisutf8, initial_localeisutf8;
9494
char *cbsd_history_file = NULL;
9595
int cbsd_enable_history = 0;
9696
const char cbsd_distdir[] = "/usr/local/cbsd";
97+
//int cbsd_function_time = 0;
9798
_REDIS(cbsdredis_t *redis;)
9899
_INFLUX(cbsdinflux_t *influx;)
99100
_DBI(cbsddbi_t *databases;)
@@ -134,6 +135,7 @@ main(int argc, char *argv[])
134135
load_config();
135136
#endif
136137

138+
char *cbsd_function_time_env = NULL;
137139
char *cbsdpath = NULL;
138140
char *workdir = NULL;
139141
char *cbsd_disable_history = NULL; // getenv
@@ -222,6 +224,12 @@ main(int argc, char *argv[])
222224
putenv("inter=0");
223225
}
224226

227+
cbsd_function_time_env=lookupvar("CBSD_FUNCTION_TIME");
228+
if (cbsd_function_time_env != NULL)
229+
cbsd_function_time=atoi(cbsd_function_time_env);
230+
else
231+
cbsd_function_time=0;
232+
225233
if (cbsd_enable_history == 1) {
226234
cbsd_history_file = calloc(MAXPATHLEN, sizeof(char *));
227235
sprintf(cbsd_history_file, "%s/%s", workdir, CBSD_HISTORYFILE);

bin/cbsdsh/main.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,5 @@ extern char
4747
*cbsd_history_file; /* full path to history for "cbsd history" command */
4848
extern int cbsd_enable_history; /* true if we must register command in history
4949
journal */
50+
extern int cbsd_function_time;
5051
#endif

etc/defaults/global.conf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,8 @@ configure_default_cbsd_vs_cidr4="auto"
4141
# What IPv6 address should be used for default network switch (CIDR subnet notation, “auto” or “none”)
4242
# can be: 'none', 'auto' or 'xxxx/yy'
4343
configure_default_cbsd_vs_cidr6="auto"
44+
45+
# function TRACE/TIME stats
46+
# e.g.:
47+
# env CBSD_FUNCTION_TIME=1 cbsd jls 2>&1 | grep ^cbsd_function | sort -u -k 2
48+
#CBSD_FUNCTION_TIME=0

0 commit comments

Comments
 (0)