|
| 1 | +#include "configuration.hh" |
| 2 | + |
| 3 | +#include <boost/program_options.hpp> |
| 4 | +#include <iomanip> |
| 5 | +#include <iostream> |
| 6 | + |
| 7 | +namespace configuration { |
| 8 | + |
| 9 | +Configuration::Configuration() { create_rdma_options(); } |
| 10 | + |
| 11 | +Configuration::Configuration(int argc, char** argv) : Configuration() { |
| 12 | + process_program_options(argc, argv); |
| 13 | + operator<<(std::cerr, *this); |
| 14 | +} |
| 15 | + |
| 16 | +void Configuration::create_rdma_options() { |
| 17 | + desc.add_options()("help,h", "Show help message")( |
| 18 | + "is-server,s", |
| 19 | + po::bool_switch(&is_server)->default_value(is_server), |
| 20 | + "Program acts as server if set")( |
| 21 | + "servers", |
| 22 | + po::value<vec<str>>(&server_nodes)->multitoken(), |
| 23 | + "A list of server nodes to which a client connects, e.g., \"cluster3\"")( |
| 24 | + "clients", |
| 25 | + po::value<vec<str>>(&client_nodes)->multitoken(), |
| 26 | + "A list of client nodes to which the initiator connects, e.g., " |
| 27 | + "\"cluster4 cluster5\"")( |
| 28 | + "initiator,i", |
| 29 | + po::bool_switch(&is_initiator)->default_value(is_initiator), |
| 30 | + "Program acts as initiating client if set")( |
| 31 | + "num-clients,c", |
| 32 | + po::value<u32>(&num_clients)->default_value(num_clients), |
| 33 | + "Number of clients that connect to each server (relevant only for " |
| 34 | + "server nodes)"); |
| 35 | + |
| 36 | + // configuration options |
| 37 | + desc.add_options()( |
| 38 | + "port", po::value<u32>(&port)->default_value(port), "TCP port")( |
| 39 | + "ib-port", |
| 40 | + po::value<u32>(&device_port)->default_value(device_port), |
| 41 | + "Port of infiniband device")( |
| 42 | + "max-poll-cqes", |
| 43 | + po::value<i32>(&max_poll_cqes)->default_value(max_poll_cqes), |
| 44 | + "Number of outstanding RDMA operations allowed (hardware-specific)")( |
| 45 | + "max-send-wrs", |
| 46 | + po::value<i32>(&max_send_queue_wr)->default_value(max_send_queue_wr), |
| 47 | + "Maximum number of outstanding send work requests")( |
| 48 | + "max-receive-wrs", |
| 49 | + po::value<i32>(&max_recv_queue_wr)->default_value(max_recv_queue_wr), |
| 50 | + "Maximum number of outstanding receive work requests"); |
| 51 | +} |
| 52 | + |
| 53 | +void Configuration::exit_with_help_message(char** argv) { |
| 54 | + std::cerr << "Try " << argv[0] << " --help" << std::endl; |
| 55 | + std::exit(EXIT_FAILURE); |
| 56 | +} |
| 57 | + |
| 58 | +void Configuration::process_program_options(int argc, char** argv) { |
| 59 | + try { |
| 60 | + po::variables_map vm; |
| 61 | + po::store(po::parse_command_line(argc, argv, desc), vm); |
| 62 | + |
| 63 | + if (vm.count("help")) { |
| 64 | + std::cerr << desc << std::endl; |
| 65 | + std::exit(EXIT_FAILURE); |
| 66 | + } |
| 67 | + |
| 68 | + po::notify(vm); |
| 69 | + |
| 70 | + if (!is_server && server_nodes.empty()) { |
| 71 | + std::cerr << "[ERROR]: --servers <arg-list> must be given if " |
| 72 | + "--server is not set" |
| 73 | + << std::endl; |
| 74 | + exit_with_help_message(argv); |
| 75 | + } |
| 76 | + |
| 77 | + if (is_server && is_initiator) { |
| 78 | + std::cerr << "[ERROR]: a server cannot be the initiator" << std::endl; |
| 79 | + exit_with_help_message(argv); |
| 80 | + } |
| 81 | + |
| 82 | + if (!is_initiator && !client_nodes.empty()) { |
| 83 | + std::cerr << "[ERROR]: --clients <arg-list> is only required by the " |
| 84 | + "initiating client" |
| 85 | + << std::endl; |
| 86 | + exit_with_help_message(argv); |
| 87 | + } |
| 88 | + |
| 89 | + } catch (const std::exception& e) { |
| 90 | + std::cerr << "[ERROR]: " << e.what() << std::endl; |
| 91 | + exit_with_help_message(argv); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +std::ostream& operator<<(std::ostream& os, const Configuration& config) { |
| 96 | + const int32_t width = 30; |
| 97 | + const int32_t max_width = width * 2; |
| 98 | + const char filler = '='; |
| 99 | + |
| 100 | + os << std::setfill(filler) << std::setw(max_width) << "" << std::endl |
| 101 | + << std::setfill(' ') << std::setw(width) |
| 102 | + << (config.is_server ? "SERVER" : "CLIENT") << std::endl |
| 103 | + << std::setfill(filler) << std::setw(max_width) << "" << std::endl; |
| 104 | + |
| 105 | + os << std::left << std::setfill(' '); |
| 106 | + if (!config.is_server) { |
| 107 | + os << std::setw(width) << "connect to: " |
| 108 | + << "["; |
| 109 | + for (const str& node : config.server_nodes) { |
| 110 | + os << node << ", "; |
| 111 | + } |
| 112 | + os << "\b\b]" << std::endl; |
| 113 | + os << std::boolalpha << std::setw(width) |
| 114 | + << "is initiator: " << config.is_initiator << std::endl; |
| 115 | + if (config.is_initiator) { |
| 116 | + if (!config.client_nodes.empty()) { |
| 117 | + os << std::setw(width) << "client nodes: " |
| 118 | + << "["; |
| 119 | + for (const str& node : config.client_nodes) { |
| 120 | + os << node << ", "; |
| 121 | + } |
| 122 | + os << "\b\b]" << std::endl; |
| 123 | + } |
| 124 | + } |
| 125 | + } else { |
| 126 | + os << std::setw(width) << "num clients: " << config.num_clients |
| 127 | + << std::endl; |
| 128 | + } |
| 129 | + os << std::setw(width) << "TCP port: " << config.port << std::endl |
| 130 | + << std::setw(width) << "IB port: " << config.device_port << std::endl |
| 131 | + << std::setw(width) << "max outstanding CQEs: " << config.max_poll_cqes |
| 132 | + << std::endl |
| 133 | + << std::setw(width) |
| 134 | + << "max send work requests: " << config.max_send_queue_wr << std::endl |
| 135 | + << std::setw(width) |
| 136 | + << "max receive work requests: " << config.max_recv_queue_wr << std::endl; |
| 137 | + |
| 138 | + os << std::setfill(filler) << std::setw(max_width) << "" << std::endl; |
| 139 | + |
| 140 | + return os; |
| 141 | +} |
| 142 | + |
| 143 | +} // namespace configuration |
0 commit comments