Skip to content

Commit 410dcdd

Browse files
Print argv at the top for all benchmarking runs (#463)
* Print argv at the top for all benchmarking runs * Apply suggestion from @oleksandr-pavlyk Co-authored-by: Oleksandr Pavlyk <[email protected]> * fixup! Print argv at the top for all benchmarking runs * fixup! Print argv at the top for all benchmarking runs * fixup! Apply suggestion from @oleksandr-pavlyk --------- Co-authored-by: Oleksandr Pavlyk <[email protected]>
1 parent 65d36b3 commit 410dcdd

14 files changed

Lines changed: 433 additions & 14 deletions

‎README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ Examples are built by default into `build/bin` and are prefixed with `nvbench.ex
9696
<summary>Example output from `nvbench.example.throughput`</summary>
9797
9898
```
99+
# Command Line
100+
101+
./bin/nvbench.example.throughput
102+
99103
# Devices
100104

101105
## [0] `Quadro GV100`

‎nvbench/json_printer.cu‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,10 @@ void json_printer::do_process_bulk_data_float64(state &state,
381381
} // end hint == sample_freqs
382382
}
383383

384-
static void add_devices_section(nlohmann::ordered_json &root)
384+
namespace
385+
{
386+
387+
void add_devices_section(nlohmann::ordered_json &root)
385388
{
386389
auto &devices = root["devices"];
387390
for (const auto &dev_info : nvbench::device_manager::get().get_devices())
@@ -409,20 +412,28 @@ static void add_devices_section(nlohmann::ordered_json &root)
409412
}
410413
}
411414

415+
void add_argv_array(nlohmann::ordered_json &metadata,
416+
const char *key,
417+
const std::vector<std::string> &args)
418+
{
419+
auto &argv = metadata[key];
420+
for (const auto &arg : args)
421+
{
422+
argv.push_back(arg);
423+
}
424+
}
425+
426+
} // namespace
427+
412428
void json_printer::do_print_benchmark_results(const benchmark_vector &benches)
413429
{
414430
nlohmann::ordered_json root;
415431

416432
{
417433
auto &metadata = root["meta"];
418434

419-
{
420-
auto &argv = metadata["argv"];
421-
for (const auto &arg : m_argv)
422-
{
423-
argv.push_back(arg);
424-
}
425-
} // "argv"
435+
add_argv_array(metadata, "argv", this->get_argv());
436+
add_argv_array(metadata, "raw_argv", this->get_raw_argv());
426437

427438
{
428439
auto &version = metadata["version"];

‎nvbench/json_printer.cuh‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ struct json_printer : nvbench::printer_base
7676

7777
protected:
7878
// Virtual API from printer_base:
79-
void do_log_argv(const std::vector<std::string> &argv) override { m_argv = argv; }
8079
void do_process_bulk_data_float64(nvbench::state &state,
8180
const std::string &tag,
8281
const std::string &hint,
@@ -87,8 +86,6 @@ protected:
8786
bool m_enable_binary_output{false};
8887
std::size_t m_num_jsonbin_files{};
8988
std::size_t m_num_jsonbin_freq_files{};
90-
91-
std::vector<std::string> m_argv;
9289
};
9390

9491
} // namespace nvbench

‎nvbench/main.cuh‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include <exception>
4040
#include <iostream>
4141
#include <string>
42+
#include <utility>
4243
#include <vector>
4344

4445
// Advanced users can rebuild NVBench's `main` function using the macros in this file, or replace
@@ -57,12 +58,16 @@
5758
// Customization point, called before NVBench parsing. Update argc/argv if needed.
5859
// argc/argv are the usual command line arguments types. The ARGS version of this
5960
// macro is a bit more convenient.
61+
// NVBench captures raw argv before this handler runs. Changes made here do not
62+
// alter the raw argv that printers may report separately from the parsed args.
6063
#ifndef NVBENCH_MAIN_CUSTOM_ARGC_ARGV_HANDLER
6164
#define NVBENCH_MAIN_CUSTOM_ARGC_ARGV_HANDLER(argc, argv) []() {}()
6265
#endif
6366

6467
// Customization point, called before NVBench parsing. Update args if needed.
6568
// Args is a vector of strings, each element is an argument.
69+
// NVBench captures raw argv before this handler runs. Changes made here do not
70+
// alter the raw argv that printers may report separately from the parsed args.
6671
#ifndef NVBENCH_MAIN_CUSTOM_ARGS_HANDLER
6772
#define NVBENCH_MAIN_CUSTOM_ARGS_HANDLER(args) []() {}()
6873
#endif
@@ -132,10 +137,12 @@
132137

133138
#ifndef NVBENCH_MAIN_PARSE
134139
#define NVBENCH_MAIN_PARSE(argc, argv) \
140+
std::vector<std::string> raw_args = nvbench::detail::main_convert_args(argc, argv); \
135141
NVBENCH_MAIN_CUSTOM_ARGC_ARGV_HANDLER(argc, argv); \
136142
std::vector<std::string> args = nvbench::detail::main_convert_args(argc, argv); \
137143
NVBENCH_MAIN_CUSTOM_ARGS_HANDLER(args); \
138144
nvbench::option_parser parser; \
145+
parser.set_raw_args(std::move(raw_args)); \
139146
NVBENCH_MAIN_PARSE_CUSTOM_PRE(parser, args); \
140147
parser.parse(args); \
141148
NVBENCH_MAIN_PARSE_CUSTOM_POST(parser)
@@ -209,6 +216,7 @@ inline void main_print_preamble(option_parser &parser)
209216
{
210217
auto &printer = parser.get_printer();
211218

219+
printer.print_argv();
212220
printer.print_device_info();
213221
printer.print_log_preamble();
214222
}

‎nvbench/markdown_printer.cu‎

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,146 @@
4141
namespace nvbench
4242
{
4343

44+
namespace
45+
{
46+
47+
#ifdef _WIN32
48+
49+
// Quote an argument with the backslash/double-quote rules used by
50+
// CommandLineToArgvW. This is not a full cmd.exe escaping layer; cmd.exe
51+
// expansion rules may still apply when pasted into a shell.
52+
std::string shell_quote(const std::string &arg)
53+
{
54+
if (!arg.empty() && arg.find_first_of(" \t\n\v\"^&|<>()%!") == std::string::npos)
55+
{
56+
return arg;
57+
}
58+
59+
// Follow the rules of CommandLineToArgvW: a run of backslashes is only special
60+
// when a double quote comes after it.
61+
std::string result;
62+
63+
result.reserve((4 * arg.size()) + 2);
64+
result += '"';
65+
for (auto iter = arg.begin(); iter != arg.end(); ++iter)
66+
{
67+
std::size_t num_backslashes = 0;
68+
while (iter != arg.end() && *iter == '\\')
69+
{
70+
++num_backslashes;
71+
++iter;
72+
}
73+
74+
if (iter == arg.end())
75+
{ // Double the backslashes that come before the closing quote.
76+
result.append(num_backslashes * 2, '\\');
77+
break;
78+
}
79+
80+
if (*iter == '"')
81+
{ // Double the backslashes that come before a quote, then escape the quote.
82+
result.append(num_backslashes * 2, '\\');
83+
result += "\\\"";
84+
}
85+
else
86+
{
87+
result.append(num_backslashes, '\\');
88+
result += *iter;
89+
}
90+
}
91+
result += '"';
92+
return result;
93+
}
94+
95+
#else
96+
97+
// Quote an argument for POSIX shells (sh, bash, zsh), so that the printed
98+
// command line can be copied and pasted.
99+
std::string shell_quote(const std::string &arg)
100+
{
101+
if (!arg.empty() && arg.find_first_of(" \t\n\"'\\$`|&;<>()*?[]{}#~!") == std::string::npos)
102+
{
103+
return arg;
104+
}
105+
106+
std::string result;
107+
108+
result.reserve((4 * arg.size()) + 2);
109+
result += '\'';
110+
for (const char c : arg)
111+
{
112+
if (c == '\'')
113+
{ // A single quote cannot appear inside single quotes; close, escape, reopen.
114+
result += "'\\''";
115+
}
116+
else
117+
{
118+
result += c;
119+
}
120+
}
121+
result += '\'';
122+
return result;
123+
}
124+
125+
#endif // _WIN32
126+
127+
std::size_t max_backtick_run(const std::string &str)
128+
{
129+
std::size_t max_run{};
130+
std::size_t current_run{};
131+
for (const char c : str)
132+
{
133+
if (c == '`')
134+
{
135+
++current_run;
136+
max_run = current_run > max_run ? current_run : max_run;
137+
}
138+
else
139+
{
140+
current_run = 0;
141+
}
142+
}
143+
144+
return max_run;
145+
}
146+
147+
std::string markdown_code_fence(const std::string &contents)
148+
{
149+
const auto fence_size = max_backtick_run(contents) + 1;
150+
return std::string(fence_size < 3 ? 3 : fence_size, '`');
151+
}
152+
153+
} // namespace
154+
155+
void markdown_printer::do_print_argv()
156+
{
157+
const auto &argv = this->get_raw_argv();
158+
if (argv.empty())
159+
{
160+
return;
161+
}
162+
163+
std::string command_line;
164+
for (std::size_t i = 0; i < argv.size(); ++i)
165+
{
166+
if (i != 0)
167+
{
168+
command_line += ' ';
169+
}
170+
command_line += shell_quote(argv[i]);
171+
}
172+
173+
const auto fence = markdown_code_fence(command_line);
174+
175+
fmt::memory_buffer buffer;
176+
fmt::format_to(fmt::appender(buffer),
177+
"# Command Line\n\n{}\n{}\n{}\n\n",
178+
fence,
179+
command_line,
180+
fence);
181+
m_ostream << fmt::to_string(buffer);
182+
}
183+
44184
void markdown_printer::do_print_device_info()
45185
{
46186
fmt::memory_buffer buffer;

‎nvbench/markdown_printer.cuh‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <nvbench/printer_base.cuh>
3232

3333
#include <string>
34+
#include <vector>
3435

3536
namespace nvbench
3637
{
@@ -62,6 +63,7 @@ struct markdown_printer : nvbench::printer_base
6263

6364
protected:
6465
// Virtual API from printer_base:
66+
void do_print_argv() override;
6567
void do_print_device_info() override;
6668
void do_print_log_preamble() override;
6769
void do_print_log_epilogue() override;

‎nvbench/option_parser.cu‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ void option_parser::parse_impl()
496496
this->update_used_device_state();
497497

498498
m_printer.log_argv(m_args);
499+
m_printer.log_raw_argv(this->get_raw_args());
499500
}
500501

501502
void option_parser::parse_range(option_parser::arg_iterator_t first,

‎nvbench/option_parser.cuh‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <optional>
3838
#include <string>
3939
#include <string_view>
40+
#include <utility>
4041
#include <vector>
4142

4243
namespace nvbench
@@ -62,11 +63,30 @@ struct option_parser
6263
void parse(int argc, char const *const argv[]);
6364
void parse(std::vector<std::string> args);
6465

66+
/*!
67+
* Set the command line that invoked the executable, before any modification.
68+
*
69+
* Call this before `parse`. `parse` sends these args to the printers as raw
70+
* args alongside the args that NVBench parsed.
71+
*/
72+
void set_raw_args(std::vector<std::string> raw_args) { m_raw_args = std::move(raw_args); }
73+
6574
[[nodiscard]] benchmark_vector &get_benchmarks() { return m_benchmarks; };
6675
[[nodiscard]] const benchmark_vector &get_benchmarks() const { return m_benchmarks; };
6776

77+
/*!
78+
* The args given to `parse` after customization handlers have modified them.
79+
*/
6880
[[nodiscard]] const std::vector<std::string> &get_args() const { return m_args; }
6981

82+
/*!
83+
* The args given to `set_raw_args`, or `get_args` if it was not called.
84+
*/
85+
[[nodiscard]] const std::vector<std::string> &get_raw_args() const
86+
{
87+
return m_raw_args ? *m_raw_args : m_args;
88+
}
89+
7090
/*!
7191
* Returns the output format requested by the parse options.
7292
*
@@ -141,6 +161,9 @@ private:
141161
// Command line args
142162
std::vector<std::string> m_args;
143163

164+
// The unmodified command line, if the caller supplied one.
165+
std::optional<std::vector<std::string>> m_raw_args;
166+
144167
// Store benchmark modifiers passed in before any benchmarks are requested as
145168
// "global args". Replay them after every benchmark.
146169
std::vector<std::string> m_global_benchmark_args;

0 commit comments

Comments
 (0)