From c2db3b703a3b1d4f92d84c4c7f4b4f90e01868b5 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Sat, 25 May 2024 14:49:29 -0700 Subject: [PATCH] Introduce --timelog=FILE flag to GNU Make --- third_party/make/README.cosmo | 1 + third_party/make/function.c | 1 + third_party/make/job.c | 3 + third_party/make/job.h | 2 + third_party/make/main.c | 8 ++ third_party/make/timelog.c | 186 ++++++++++++++++++++++++++++++++++ third_party/make/timelog.h | 13 +++ 7 files changed, 214 insertions(+) create mode 100644 third_party/make/timelog.c create mode 100644 third_party/make/timelog.h diff --git a/third_party/make/README.cosmo b/third_party/make/README.cosmo index 17e3a479a..2f9ca417e 100644 --- a/third_party/make/README.cosmo +++ b/third_party/make/README.cosmo @@ -15,6 +15,7 @@ LICENSE LOCAL CHANGES + - Introduce -T FILE, --time-log=FILE flag - Introduce $(uniq token...) native function - Remove code that forces slow path if not using /bin/sh diff --git a/third_party/make/function.c b/third_party/make/function.c index 8608b4604..64bd61035 100644 --- a/third_party/make/function.c +++ b/third_party/make/function.c @@ -1949,6 +1949,7 @@ func_shell_base (char *o, char **argv, int trim_newlines) child.output.out = pipedes[1]; child.output.err = errfd; + child.timelog = timelog_begin (command_argv); pid = child_execute_job (&child, 1, command_argv); if (pid < 0) diff --git a/third_party/make/job.c b/third_party/make/job.c index 9b54489dc..9b0399d98 100644 --- a/third_party/make/job.c +++ b/third_party/make/job.c @@ -1104,6 +1104,8 @@ reap_children (int block, int err) void free_childbase (struct childbase *child) { + timelog_end (child->timelog); + if (child->environment != 0) { char **ep = child->environment; @@ -1463,6 +1465,7 @@ start_job_command (struct child *child) jobserver_pre_child (ANY_SET (flags, COMMANDS_RECURSE)); + child->timelog = timelog_begin (argv); child->pid = child_execute_job ((struct childbase *)child, child->good_stdin, argv); diff --git a/third_party/make/job.h b/third_party/make/job.h index 00fac9984..daf414565 100644 --- a/third_party/make/job.h +++ b/third_party/make/job.h @@ -15,6 +15,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "output.h" +#include "timelog.h" /* Structure describing a running or dead child process. */ @@ -31,6 +32,7 @@ this program. If not, see . */ #define CHILDBASE \ char *cmd_name; /* Allocated copy of command run. */ \ char **environment; /* Environment for commands. */ \ + struct timelog *timelog; /* [jart] the lorde of all time. */ \ VMSCHILD \ struct output output /* Output for this child. */ diff --git a/third_party/make/main.c b/third_party/make/main.c index 8a367f766..0bd269481 100644 --- a/third_party/make/main.c +++ b/third_party/make/main.c @@ -27,6 +27,7 @@ this program. If not, see . */ #include "getopt.h" #include "libc/runtime/runtime.h" #include "shuffle.h" +#include "timelog.h" #include #ifdef HAVE_FCNTL_H @@ -374,6 +375,8 @@ static const char *const usage[] = N_("\ --trace Print tracing information.\n"), N_("\ + -T FILE, --time-log=FILE Log command invocation microseconds to FILE.\n"), + N_("\ -v, --version Print the version number of make and exit.\n"), N_("\ -w, --print-directory Print the current directory.\n"), @@ -471,6 +474,7 @@ static struct command_switch switches[] = { 'o', filename, &old_files, 0, 0, 0, 0, 0, 0, "old-file", 0 }, { 'O', string, &output_sync_option, 1, 1, 0, 0, "target", 0, "output-sync", 0 }, { 'W', filename, &new_files, 0, 0, 0, 0, 0, 0, "what-if", 0 }, + { 'T', string, &timelog_path, 0, 0, 0, 0, 0, 0, "timelog", 0 }, // [jart] /* These are long-style options. */ { CHAR_MAX+1, strlist, &db_flags, 1, 1, 0, 0, "basic", 0, "debug", 0 }, @@ -2258,6 +2262,10 @@ main (int argc, char **argv, char **envp) } #endif + /* [jart] Setup command latency log. */ + + timelog_init (); + /* Set up MAKEFLAGS and MFLAGS again, so they will be right. */ define_makeflags (0); diff --git a/third_party/make/timelog.c b/third_party/make/timelog.c new file mode 100644 index 000000000..b6be15c8c --- /dev/null +++ b/third_party/make/timelog.c @@ -0,0 +1,186 @@ +/* Copyright 2024 Justine Alexandra Roberts Tunney + + Permission to use, copy, modify, and/or distribute this software for + any purpose with or without fee is hereby granted, provided that the + above copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL + WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE + AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR + PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER + TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + PERFORMANCE OF THIS SOFTWARE. */ + +#include "timelog.h" + +#include +#include +#include +#include +#include +#include +#include + +struct timelog + { + struct timespec started; + size_t command_length; + char command[4000]; + }; + +char *timelog_path; +int timelog_fd = -1; + +void timelog_init (void) +{ + if (!timelog_path) + return; + if ((timelog_fd = open (timelog_path, O_APPEND | O_WRONLY | O_CREAT, 0644)) == -1) + { + perror (timelog_path); + exit (1); + } +} + +static int is_shell_safe (int c) +{ + if (c > 127) + return true; + if (isalnum(c)) + return true; + switch (c) + { + case '+': + case '-': + case '.': + case '/': + case '_': + case '=': + case ':': + return true; + default: + return false; + } +} + +static int needs_quotes (const char *s) +{ + int c; + if (!*s) + return true; + while ((c = *s++ & 255)) + if (!is_shell_safe (c)) + return true; + return false; +} + +static long long get_microseconds (struct timespec beg, struct timespec end) +{ + end.tv_sec -= beg.tv_sec; + if (end.tv_nsec < beg.tv_nsec) + { + end.tv_nsec += 1000000000; + end.tv_sec--; + } + end.tv_nsec -= beg.tv_nsec; + return end.tv_sec * 1000000ll + end.tv_nsec / 1000ll; +} + +struct timelog *timelog_begin (char **argv) +{ + int i, j, q; + char *p, *pe; + + /* don't bother if disabled */ + if (timelog_fd == -1) + return NULL; + + /* allocate object */ + struct timelog *tl; + if (!(tl = malloc (sizeof (struct timelog)))) + return NULL; + + /* stringify command */ + p = tl->command; + pe = tl->command + sizeof (tl->command) - 1; + for (i = 0; argv[i]; ++i) + { + if (i && p < pe) + *p++ = ' '; + q = needs_quotes (argv[i]); + if (q && p < pe) + *p++ = '\''; + for (j = 0; argv[i][j]; ++j) + { + if (isspace(argv[i][j]) || + iscntrl(argv[i][j])) + { + if (p < pe) + *p++ = ' '; + } + else if (argv[i][j] == '\'') + { + if (p < pe) + *p++ = '\''; + if (p < pe) + *p++ = '"'; + if (p < pe) + *p++ = '\''; + if (p < pe) + *p++ = '"'; + if (p < pe) + *p++ = '\''; + } + else + { + if (p < pe) + *p++ = argv[i][j]; + } + } + if (q && p < pe) + *p++ = '\''; + } + if (p == pe) + { + p[-3] = '.'; + p[-2] = '.'; + p[-1] = '.'; + } + *p++ = '\n'; + tl->command_length = p - tl->command; + + /* record starting timestamp */ + clock_gettime (CLOCK_REALTIME, &tl->started); + + /* return object */ + return tl; +} + +void timelog_end (struct timelog *tl) +{ + long long us; + char ibuf[22]; + struct iovec iov[2]; + struct timespec ended; + + /* don't bother if disabled */ + if (tl == NULL) + return; + + /* get elapsed microseconds string */ + clock_gettime (CLOCK_REALTIME, &ended); + us = get_microseconds (tl->started, ended); + snprintf (ibuf, sizeof(ibuf), "% 20lld ", us); + + // write to log + iov[0].iov_base = ibuf; + iov[0].iov_len = 21; + iov[1].iov_base = tl->command; + iov[1].iov_len = tl->command_length; + writev (timelog_fd, iov, 2); + + // free object + free (tl); +} diff --git a/third_party/make/timelog.h b/third_party/make/timelog.h new file mode 100644 index 000000000..72d2af31b --- /dev/null +++ b/third_party/make/timelog.h @@ -0,0 +1,13 @@ +#ifndef MAKE_TIMELOG_H_ +#define MAKE_TIMELOG_H_ + +struct timelog; + +extern int timelog_fd; +extern char *timelog_path; + +void timelog_init (void); +struct timelog *timelog_begin (char **); +void timelog_end (struct timelog *); + +#endif /* MAKE_TIMELOG_H_ */