dumping ground for random patches and texts
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: spew@80x24.org
Subject: [PATCH 4/4] tracecompress
Date: Fri,  5 Apr 2024 21:05:04 +0000	[thread overview]
Message-ID: <20240405210504.3110367-4-e@80x24.org> (raw)
In-Reply-To: <20240405210504.3110367-1-e@80x24.org>

---
 lib/Devel/Mwrap/TraceReplay.pm |  2 +-
 mwrap_core.h                   | 33 ++++++++++++++++++---
 script/mwrap-trace-replay      | 53 ++++++++++++++++++++++++++--------
 t/httpd.t                      |  4 +--
 4 files changed, 73 insertions(+), 19 deletions(-)

diff --git a/lib/Devel/Mwrap/TraceReplay.pm b/lib/Devel/Mwrap/TraceReplay.pm
index fa3af7c..bb2551b 100644
--- a/lib/Devel/Mwrap/TraceReplay.pm
+++ b/lib/Devel/Mwrap/TraceReplay.pm
@@ -74,7 +74,7 @@ sub check_build () {
 
 sub run (@) {
 	check_build();
-	system $bin, @_;
+	exec $bin, @_;
 }
 
 1;
diff --git a/mwrap_core.h b/mwrap_core.h
index 2910a5a..d236a3a 100644
--- a/mwrap_core.h
+++ b/mwrap_core.h
@@ -1082,6 +1082,9 @@ static int trace_on(const char *env)
 {
 	char trace_path[PATH_MAX];
 	size_t len = 0;
+	const char *cmpr = NULL;
+	const char *sfx = ".gz";
+	char cmpr_cmd[32];
 
 	if (env) {
 		const char *td = strstr(env, "trace_dir:");
@@ -1094,6 +1097,17 @@ static int trace_on(const char *env)
 				return ENAMETOOLONG;
 			memcpy(trace_path, td, len);
 		}
+		cmpr = strstr(env, "trace_compress:");
+		if (cmpr) {
+			cmpr += sizeof("trace_compress");
+			const char *end = strchrnul(cmpr, ',');
+
+			len = end - td;
+			if (len >= sizeof(cmpr_cmd))
+				return ENAMETOOLONG;
+			strcpy(cmpr_cmd, cmpr);
+			cmpr = cmpr_cmd;
+		}
 	}
 	if (!len) {
 		env = getenv("TMPDIR");
@@ -1108,8 +1122,17 @@ static int trace_on(const char *env)
 	}
 	if (trace_path[len - 1] != '/')
 		trace_path[len++] = '/';
+	if (cmpr) {
+		if (strstr(cmpr, "zstd")) {
+			sfx = ".zst";
+		} else if (strstr(cmpr, "bzip2")) {
+			sfx = ".bz2";
+		}
+	} else {
+		cmpr = "gzip";
+	}
 	int rc = snprintf(trace_path + len, 32,
-			"mwrap.%d.trace.gz", (int)getpid());
+			"mwrap.%d.trace%s", (int)getpid(), sfx);
 	if (rc < 0 || rc >= 32)
 		return ENAMETOOLONG;
 	int fd = open(trace_path, O_CLOEXEC|O_CREAT|O_APPEND|O_WRONLY, 0666);
@@ -1134,9 +1157,11 @@ static int trace_on(const char *env)
 			close(pfds[0]);
 			if (dup2(fd, 1) < 1) err(1, "dup2");
 			close(fd);
-
-			execlp("gzip", "gzip", "-c", NULL);
-			err(1, "execlp");
+			if (strchr(cmpr, ' ') || strchr(cmpr, '\t'))
+				execl("/bin/sh", "sh", "-c", cmpr, NULL);
+			else
+				execlp(cmpr, cmpr, "-c", NULL);
+			err(1, "execl(p) %s", cmpr);
 		} else {
 			_exit(0);
 		}
diff --git a/script/mwrap-trace-replay b/script/mwrap-trace-replay
index 22cbc82..e1feb23 100644
--- a/script/mwrap-trace-replay
+++ b/script/mwrap-trace-replay
@@ -4,17 +4,46 @@
 use v5.12;
 use autodie;
 use Devel::Mwrap::TraceReplay;
-(-f STDIN || -p STDIN) or die "Usage: $0 </path/to/mwrap.\$PID.trace.gz\n";
-pipe(my $r, my $w);
-my $gzip = $ENV{GZIP} // 'gzip';
-my $pid = fork;
-if ($pid == 0) {
-	open STDOUT, '>&', $w;
+my (@files, @opt);
+for (@ARGV) {
+	if (-f $_) {
+		push @files, $_;
+	} else {
+		push @opt, $_;
+	}
+}
+
+warn "opt=@opt f=@files";
+if (@files) {
+	pipe(my $r, my $w);
+	my $tpid = fork;
+	if ($tpid == 0) {
+		open STDIN, '<&', $r;
+		close $_ for ($r, $w);
+		Devel::Mwrap::TraceReplay::run @opt;
+		die "exec trace-replay: $!";
+	}
+	for my $f (@files) {
+		my $dc = 'gzip';
+		if ($f =~ /\.zst\z/i) {
+			$dc = 'zstd';
+		} elsif ($f =~ /\.bz2\z/i) {
+			$dc = 'bzip2';
+		}
+		my $pid = fork;
+		if ($pid == 0) {
+			open STDOUT, '>&', $w;
+			open STDIN, '<', $f;
+			close $_ for ($r, $w);
+			exec $dc, '-dc';
+			die "exec: $dc: $!";
+		}
+		waitpid($pid, 0);
+	}
 	close $_ for ($r, $w);
-	exec $gzip, '-dc';
-	die "exec: $!";
+	waitpid($tpid, 0);
+} else {
+	(-f STDIN || -p STDIN) or
+		die "Usage: $0 </path/to/mwrap.\$PID.trace\n";
+	Devel::Mwrap::TraceReplay::run @opt;
 }
-open STDIN, '<&', $r;
-close $_ for ($r, $w);
-Devel::Mwrap::TraceReplay::run @ARGV;
-waitpid $pid, 0;
diff --git a/t/httpd.t b/t/httpd.t
index d7006d5..4b8ed82 100644
--- a/t/httpd.t
+++ b/t/httpd.t
@@ -194,15 +194,15 @@ SKIP: {
 	is $rc, 0, 'trace disabled';
 	like(slurp($cout), qr/trace off/, 'tracing disabled');
 	ok -s $trace_file, 'trace file data';
+	ok -f $trace_file, 'trace file data';
 
 	my @replay = ($^X, '-w', './blib/script/mwrap-trace-replay');
 	my $trace_out = "$mwrap_tmp/tr.out";
 	my $tr_pid = fork;
 	if ($tr_pid == 0) {
-		open STDIN, '<', $trace_file;
 		open STDOUT, '+>>', $trace_out;
 		open STDERR, '+>>', $trace_out;
-		exec @replay;
+		exec @replay, $trace_file;
 		die "exec: $!";
 	}
 	waitpid($tr_pid, 0);

      parent reply	other threads:[~2024-04-05 21:05 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-05 21:05 [PATCH 1/4] support malloc tracing Eric Wong
2024-04-05 21:05 ` [PATCH 2/4] realloc Eric Wong
2024-04-05 21:05 ` [PATCH 3/4] gzip Eric Wong
2024-04-05 21:05 ` Eric Wong [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240405210504.3110367-4-e@80x24.org \
    --to=e@80x24.org \
    --cc=spew@80x24.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).