git/t/t0211/scrub_perf.perl
Emily Shaffer 2f732bf15e tr2: log parent process name
It can be useful to tell who invoked Git - was it invoked manually by a
user via CLI or script? By an IDE?  In some cases - like 'repo' tool -
we can influence the source code and set the GIT_TRACE2_PARENT_SID
environment variable from the caller process. In 'repo''s case, that
parent SID is manipulated to include the string "repo", which means we
can positively identify when Git was invoked by 'repo' tool. However,
identifying parents that way requires both that we know which tools
invoke Git and that we have the ability to modify the source code of
those tools. It cannot scale to keep up with the various IDEs and
wrappers which use Git, most of which we don't know about. Learning
which tools and wrappers invoke Git, and how, would give us insight to
decide where to improve Git's usability and performance.

Unfortunately, there's no cross-platform reliable way to gather the name
of the parent process. If procfs is present, we can use that; otherwise
we will need to discover the name another way. However, the process ID
should be sufficient to look up the process name on most platforms, so
that code may be shareable.

Git for Windows gathers similar information and logs it as a "data_json"
event. However, since "data_json" has a variable format, it is difficult
to parse effectively in some languages; instead, let's pursue a
dedicated "cmd_ancestry" event to record information about the ancestry
of the current process and a consistent, parseable way.

Git for Windows also gathers information about more than one generation
of parent. In Linux further ancestry info can be gathered with procfs,
but it's unwieldy to do so. In the interest of later moving Git for
Windows ancestry logging to the 'cmd_ancestry' event, and in the
interest of later adding more ancestry to the Linux implementation - or
of adding this functionality to other platforms which have an easier
time walking the process tree - let's make 'cmd_ancestry' accept an
array of parentage.

Signed-off-by: Emily Shaffer <emilyshaffer@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2021-07-22 13:35:20 -07:00

81 lines
2.1 KiB
Perl

#!/usr/bin/perl
#
# Scrub the variable fields from the perf trace2 output to
# make testing easier.
use strict;
use warnings;
my $qpath = '\'[^\']*\'|[^ ]*';
my $col_depth=0;
my $col_thread=1;
my $col_event=2;
my $col_repo=3;
my $col_t_abs=4;
my $col_t_rel=5;
my $col_category=6;
my $col_rest=7;
# This code assumes that the trace2 data was written with bare
# turned on (which omits the "<clock> <file>:<line> | <parents>"
# prefix.
while (<>) {
my @tokens = split /\|/;
foreach my $col (@tokens) { $col =~ s/^\s+|\s+$//g; }
if ($tokens[$col_event] =~ m/^start/) {
# The 'start' message lists the contents of argv in $col_rest.
# On some platforms (Windows), argv[0] is *sometimes* a canonical
# absolute path to the EXE rather than the value passed in the
# shell script. Replace it with a placeholder to simplify our
# HEREDOC in the test script.
my $argv0;
my $argvRest;
$tokens[$col_rest] =~ s/^($qpath)\W*(.*)/_EXE_ $2/;
}
elsif ($tokens[$col_event] =~ m/cmd_path/) {
# Likewise, the 'cmd_path' message breaks out argv[0].
#
# This line is only emitted when RUNTIME_PREFIX is defined,
# so just omit it for testing purposes.
# $tokens[$col_rest] = "_EXE_";
goto SKIP_LINE;
}
elsif ($tokens[$col_event] =~ m/cmd_ancestry/) {
# 'cmd_ancestry' is platform-specific and not implemented everywhere,
# so skip it.
goto SKIP_LINE;
}
elsif ($tokens[$col_event] =~ m/child_exit/) {
$tokens[$col_rest] =~ s/ pid:\d* / pid:_PID_ /;
}
elsif ($tokens[$col_event] =~ m/data/) {
if ($tokens[$col_category] =~ m/process/) {
# 'data' and 'data_json' events containing 'process'
# category data are assumed to be platform-specific
# and highly variable. Just omit them.
goto SKIP_LINE;
}
}
# t_abs and t_rel are either blank or a float. Replace the float
# with a constant for matching the HEREDOC in the test script.
if ($tokens[$col_t_abs] =~ m/\d/) {
$tokens[$col_t_abs] = "_T_ABS_";
}
if ($tokens[$col_t_rel] =~ m/\d/) {
$tokens[$col_t_rel] = "_T_REL_";
}
my $out;
$out = join('|', @tokens);
print "$out\n";
SKIP_LINE:
}