Compute Library
 21.08
CommonOptions.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2020 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #include "CommonOptions.h"
25 
26 #include "../Framework.h"
27 #include "../printers/Printers.h"
28 #include <unistd.h>
29 
30 using namespace arm_compute::utils;
31 
32 namespace arm_compute
33 {
34 namespace test
35 {
36 namespace framework
37 {
38 CommonOptions::CommonOptions(CommandLineParser &parser)
39  : help(parser.add_option<ToggleOption>("help")),
40  instruments(),
41  iterations(parser.add_option<SimpleOption<int>>("iterations", 1)),
42  log_format(),
43  log_file(parser.add_option<SimpleOption<std::string>>("log-file")),
44  log_level(),
45  throw_errors(parser.add_option<ToggleOption>("throw-errors")),
46  color_output(parser.add_option<ToggleOption>("color-output", isatty(STDOUT_FILENO))), // Only enable colors by default if we're running in a terminal
47  pretty_console(parser.add_option<ToggleOption>("pretty-console", false)),
48  json_file(parser.add_option<SimpleOption<std::string>>("json-file")),
49  pretty_file(parser.add_option<SimpleOption<std::string>>("pretty-file")),
50  log_streams()
51 {
52  Framework &framework = Framework::get();
53  std::set<InstrumentsDescription> allowed_instruments
54  {
55  std::pair<InstrumentType, ScaleFactor>(InstrumentType::ALL, ScaleFactor::NONE),
56  std::pair<InstrumentType, ScaleFactor>(InstrumentType::NONE, ScaleFactor::NONE),
57  };
58 
59  for(const auto &type : framework.available_instruments())
60  {
61  allowed_instruments.insert(type);
62  }
63 
64  std::set<LogFormat> supported_log_formats
65  {
69  };
70 
71  std::set<LogLevel> supported_log_levels
72  {
80  };
81 
82  instruments = parser.add_option<EnumListOption<InstrumentsDescription>>("instruments", allowed_instruments, std::initializer_list<InstrumentsDescription> { std::pair<InstrumentType, ScaleFactor>(InstrumentType::WALL_CLOCK_TIMER, ScaleFactor::NONE) });
83  log_format = parser.add_option<EnumOption<LogFormat>>("log-format", supported_log_formats, LogFormat::PRETTY);
84  log_level = parser.add_option<EnumOption<LogLevel>>("log-level", supported_log_levels, LogLevel::ALL);
85 
86  help->set_help("Show this help message");
87  instruments->set_help("Set the profiling instruments to use");
88  iterations->set_help("Number of iterations per test case");
89  log_format->set_help("Output format for measurements and failures (affects only log-file)");
90  log_file->set_help("Write output to file instead of to the console (affected by log-format)");
91  log_level->set_help("Verbosity of the output");
92  throw_errors->set_help("Don't catch fatal errors (useful for debugging)");
93  color_output->set_help("Produce colored output on the console");
94  pretty_console->set_help("Produce pretty output on the console");
95  json_file->set_help("Write output to a json file.");
96  pretty_file->set_help("Write output to a text file");
97 }
98 std::vector<std::unique_ptr<Printer>> CommonOptions::create_printers()
99 {
100  std::vector<std::unique_ptr<Printer>> printers;
101 
103  {
104  auto pretty_printer = std::make_unique<PrettyPrinter>();
105  pretty_printer->set_color_output(color_output->value());
106  printers.push_back(std::move(pretty_printer));
107  }
108 
109  std::unique_ptr<Printer> printer;
110  switch(log_format->value())
111  {
112  case LogFormat::JSON:
113  printer = std::make_unique<JSONPrinter>();
114  break;
115  case LogFormat::NONE:
116  break;
117  case LogFormat::PRETTY:
118  default:
119  auto pretty_printer = std::make_unique<PrettyPrinter>();
120  // Don't use colours if we print to a file:
121  pretty_printer->set_color_output((!log_file->is_set()) && color_output->value());
122  printer = std::move(pretty_printer);
123  break;
124  }
125 
126  if(log_file->is_set())
127  {
128  log_streams.push_back(std::make_shared<std::ofstream>(log_file->value()));
129  if(printer != nullptr)
130  {
131  printer->set_stream(*log_streams.back().get());
132  }
133  }
134 
135  if(printer != nullptr)
136  {
137  printers.push_back(std::move(printer));
138  }
139 
140  if(json_file->is_set())
141  {
142  printers.push_back(std::make_unique<JSONPrinter>());
143  log_streams.push_back(std::make_shared<std::ofstream>(json_file->value()));
144  printers.back()->set_stream(*log_streams.back().get());
145  }
146 
147  if(pretty_file->is_set())
148  {
149  printers.push_back(std::make_unique<PrettyPrinter>());
150  log_streams.push_back(std::make_shared<std::ofstream>(pretty_file->value()));
151  printers.back()->set_stream(*log_streams.back().get());
152  }
153 
154  return printers;
155 }
156 } // namespace framework
157 } // namespace test
158 } // namespace arm_compute
arm_compute::utils::ToggleOption * help
Show help option.
Definition: CommonOptions.h:74
arm_compute::utils::EnumOption< LogFormat > * log_format
Log format option.
Definition: CommonOptions.h:77
Implementation of an option that can be either true or false.
Definition: ToggleOption.h:36
arm_compute::utils::ToggleOption * color_output
Color output option.
Definition: CommonOptions.h:81
Class to parse command line arguments.
decltype(strategy::transforms) typedef type
Copyright (c) 2017-2021 Arm Limited.
static Framework & get()
Access to the singleton.
Definition: Framework.cpp:124
T * add_option(const std::string &name, As &&... args)
Function to add a new option to the parser.
std::vector< std::unique_ptr< Printer > > create_printers()
Create the printers based on parsed command line options.
std::vector< std::shared_ptr< std::ofstream > > log_streams
Log streams.
Definition: CommonOptions.h:85
std::set< InstrumentsDescription > available_instruments() const
Supported instrument types for benchmarking.
Definition: Framework.cpp:100
arm_compute::utils::SimpleOption< std::string > * pretty_file
Pretty output file option.
Definition: CommonOptions.h:84
const T & value() const
Get the option value.
Definition: SimpleOption.h:112
arm_compute::utils::SimpleOption< int > * iterations
Number of iterations option.
Definition: CommonOptions.h:76
arm_compute::utils::EnumListOption< InstrumentsDescription > * instruments
Instruments option.
Definition: CommonOptions.h:75
arm_compute::utils::EnumOption< LogLevel > * log_level
Logging level option.
Definition: CommonOptions.h:79
bool is_set() const
Has a value been assigned to the option?
Definition: Option.h:135
arm_compute::utils::SimpleOption< std::string > * log_file
Log file option.
Definition: CommonOptions.h:78
const T & value() const
Get the selected value.
arm_compute::utils::ToggleOption * pretty_console
Pretty console option.
Definition: CommonOptions.h:82
arm_compute::utils::SimpleOption< std::string > * json_file
JSON output file option.
Definition: CommonOptions.h:83
Implementation of an option that accepts a single value.
Definition: SimpleOption.h:39
void set_help(std::string help)
Set the help message for the option.
Definition: Option.h:125
arm_compute::utils::ToggleOption * throw_errors
Throw errors option.
Definition: CommonOptions.h:80