Introduce --timelog=FILE flag to GNU Make

This commit is contained in:
Justine Tunney 2024-05-25 14:49:29 -07:00
parent edb03b89d8
commit c2db3b703a
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
7 changed files with 214 additions and 0 deletions

View file

@ -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

View file

@ -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)

View file

@ -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);

View file

@ -15,6 +15,7 @@ You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>. */
#include "output.h"
#include "timelog.h"
/* Structure describing a running or dead child process. */
@ -31,6 +32,7 @@ this program. If not, see <https://www.gnu.org/licenses/>. */
#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. */

View file

@ -27,6 +27,7 @@ this program. If not, see <https://www.gnu.org/licenses/>. */
#include "getopt.h"
#include "libc/runtime/runtime.h"
#include "shuffle.h"
#include "timelog.h"
#include <assert.h>
#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);

186
third_party/make/timelog.c vendored Normal file
View file

@ -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 <time.h>
#include <fcntl.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/uio.h>
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);
}

13
third_party/make/timelog.h vendored Normal file
View file

@ -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_ */