Skip to content

Commit

Permalink
i#7180: Add drmemtrace support for stdin via "-infile -"
Browse files Browse the repository at this point in the history
Adds support for reading a single drmemtrace file from stdin when
"-infile -" is specified.  The file can be uncompressed or in gzip
format.

Adds a UNIX-only test which was easiest to create directly using bash.

Fixes #7180
  • Loading branch information
derekbruening committed Jan 10, 2025
1 parent e034f37 commit 6e3a93f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 4 deletions.
2 changes: 2 additions & 0 deletions api/docs/release.dox
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ Further non-compatibility-affecting changes include:
a pure-static application via a new configure_DynamoRIO_static_client() CMake
function and new "_drstatic" static library CMake targets for the provided
extension libraries.
- Added support for reading a single drmemtrace trace file from stdin
via "-infile -".

**************************************************
<hr>
Expand Down
6 changes: 4 additions & 2 deletions clients/drcachesim/common/options.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* **********************************************************
* Copyright (c) 2015-2024 Google, Inc. All rights reserved.
* Copyright (c) 2015-2025 Google, Inc. All rights reserved.
* **********************************************************/

/*
Expand Down Expand Up @@ -97,7 +97,9 @@ droption_t<std::string> op_indir(
droption_t<std::string> op_infile(
DROPTION_SCOPE_ALL, "infile", "", "Offline legacy file for input to the simulator",
"Directs the simulator to use a single all-threads-interleaved-into-one trace file. "
"This is a legacy file format that is no longer produced.");
"This is a legacy file format that is no longer produced as it does not support "
"mixing multiple inputs nor using auxiliary metadata files. Passing '-' will read "
"from stdin as plain or gzip-compressed data.");

droption_t<int> op_jobs(
DROPTION_SCOPE_ALL, "jobs", -1, "Number of parallel jobs",
Expand Down
10 changes: 8 additions & 2 deletions clients/drcachesim/reader/compressed_file_reader.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* **********************************************************
* Copyright (c) 2017-2023 Google, Inc. All rights reserved.
* Copyright (c) 2017-2025 Google, Inc. All rights reserved.
* **********************************************************/

/*
Expand Down Expand Up @@ -52,7 +52,13 @@ namespace drmemtrace {
bool
open_single_file_common(const std::string &path, gzFile &out)
{
out = gzopen(path.c_str(), "rb");
if (path == "-") {
// We assume stdin is 0.
// We do not use "stdin->_fileno" as that is not portable.
constexpr int STDIN_FD = 0;
out = gzdopen(STDIN_FD, "rb");
} else
out = gzopen(path.c_str(), "rb");
return out != nullptr;
}

Expand Down
16 changes: 16 additions & 0 deletions clients/drcachesim/tests/offline-stdin.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Output format:
<--record#-> <--instr#->: <---tid---> <record details>
------------------------------------------------------------
1 0: 1257596 <marker: version 4>
2 0: 1257596 <marker: filetype 0x10e40>
3 0: 1257596 <marker: cache line size 64>
4 0: 1257596 <marker: chunk instruction count 10000000>
5 0: 1257596 <marker: page size 4096>
6 0: 1257596 <marker: timestamp 13331862027607247>
7 0: 1257596 <marker: tid 1257596 on core 8>
8 1: 1257596 ifetch 4 byte\(s\) @ 0x00007f3e429fce35 48 8b 45 f8 mov -0x08\(%rbp\), %rax
9 1: 1257596 read 8 byte\(s\) @ 0x00007ffefb03e128 by PC 0x00007f3e429fce35
10 2: 1257596 ifetch 8 byte\(s\) @ 0x00007f3e429fce39 48 8d 14 c5 00 00 00 lea 0x00\(,%rax,8\), %rdx
10 2: 1257596 00
View tool results:
2 : total instructions
22 changes: 22 additions & 0 deletions suite/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4006,6 +4006,28 @@ if (BUILD_CLIENTS)
# Avoid the default core-sharded from re-scheduling the trace.
"-indir ${core_sharded_dir} -tool schedule_stats -only_shards 2,3 -no_core_sharded" "")
set(tool.core_on_disk_rawtemp ON) # no preprocessor

# Test reading a file over stdin.
# We rely on there being just one subfile in this zip file: gunzip only
# extracts the first one (bsdtar can extract all but is not likely installed).
# This is UNIX only so we rely on cat and gunzip being available.
find_program(CAT_EXE cat DOC "path to cat")
find_program(GUNZIP_EXE gunzip DOC "path to gunzip")
find_program(BASH_EXE bash DOC "path to bash")
if (CAT_EXE AND GUNZIP_EXE AND BASH_EXE)
# Easiest to make our pipeline in bash with a direct add_test()
# (although this bypasses the torun() features used by all other tests).
set(infile "${core_sharded_dir}/drmemtrace.core.0.trace.zip")
add_test(tool.drcacheoff.stdin
${BASH_EXE} -c
"${CAT_EXE} ${infile} | ${GUNZIP_EXE} | ${drrun_path} -t drmemtrace -tool view -sim_refs 10 -infile -")
file(READ "${PROJECT_SOURCE_DIR}/clients/drcachesim/tests/offline-stdin.expect"
stdin_expect)
set_tests_properties(tool.drcacheoff.stdin PROPERTIES
PASS_REGULAR_EXPRESSION "${stdin_expect}")
else ()
message("Failed to find cat, gunzip, and bash: not running stdin test")
endif ()
endif ()
endif ()

Expand Down

0 comments on commit 6e3a93f

Please sign in to comment.