Program Listing for File config.cpp

↰ Return to documentation for file (config/config.cpp)

#include "config.hpp"
#include "felixtag.h"

#include "felix/FelixClientUtility.hpp"
#include "felix/felix_fid.h"
#include "felix/felix_ports.h"
#include <cstdint>
#include <format>

#include <format>


std::string Config::options() {
    return
R"(
    General Options:
        --bus-dir=DIRECTORY          Write felix-bus information to this directory. [default: ./bus]
        --bus-groupname=NAME         Use this groupname for the bus. [default: FELIX]
        --cid=<cid>...               CID (Connector Ids) to set in FID (Felix ID). Can be used once per device as a comma
                                     separated connector list e.g. 0x10,0x11. Deprecated, use CONNECTOR_ID registers.
        --did=<did>                  DID (Detector Id) to set in FID (Felix ID). Deprecated, use DETECTOR_ID register.
    -d, --device=<device>...         Use FLX device DEVICE. [default: 0]
        --error-out=<fifo>           Write error information to a UNIX FIFO
        --free-cmem                  Free previously booked cmem segment by name-<device>-<dma>
        --iface=<iface>              Send data to the interface. [calculated: use --ip value]
    -i, --ip=<ip>                    Publish data on the ip address IP. [default: libfabric:127.0.0.1]
    -b, --netio-pagesize=<size>      NetIO page size in Byte. [default: 65536]
    -B, --netio-pages=<size>         Number of NetIO pages. [default: 256]
        --is-monitoring              Sends monitoring data to "is"
        --prometheus-port=<port>     Sets Prometheus port and sends monitoring data to Prometheus
        --stats-out=<fifo>           Write periodic statistics data to a UNIX FIFO
        --stats-period=<ms>          Period in milliseconds for statistics dumps. [default: 1000]
    -?, --help                       Give this help list
    -v, --verbose                    Produce verbose output
        --verbose-bus                Produce verbose output for bus
    -V, --version                    Print program version
        --daq-network-mode=<mode>    Network mode to use for DAQ. [default: RDMA] {RDMA|TCP}
        --dcs-network-mode=<mode>    Network mode to use for DCS. [default: TCP] {RDMA|TCP}
        --vid=N                      VID (Version Id) to set in FID (Felix ID) [default: 1]
)";
}

Config::Config() {}

Config::~Config() {}

void Config::parse(int argc, char **argv) {
    appname = argv[0];

    std::map<std::string, docopt::value> args
            = docopt::docopt(usage() + options() + bugs(),
                            { argv + 1, argv + argc },
                            true,               // show help if requested
                            (std::string(argv[0]) + " " + FELIX_TAG).c_str());  // version string

    handle_cmd_line(args);
}

std::ostream& Config::format(std::ostream& os) const {
    os << "Config" << std::endl;
    os << "bus_dir: " << network.bus_dir << std::endl;
    os << "bus_groupname: " << network.bus_groupname << std::endl;
    os << "cid: ";
        if (resource.cid.empty()) { os << "read from registers"; }
        for (const auto& device_cids : resource.cid) {
            os << "[";
            std::copy(device_cids.begin(), device_cids.end(), std::ostream_iterator<int>(os << std::hex << std::showbase, " "));
            os << "] ";
        }
        os << std::dec << std::endl;
    os << "did: ";
        if (resource.did) { os << "0x" << std::hex << *resource.did << std::dec; }
        else { os << "read from register"; }
        os << std::endl;
    os << "device: ";
        std::copy(resource.device.begin(), resource.device.end(), std::ostream_iterator<int>(os, " "));
        os << std::endl;
    os << "ip: " << network.ip << std::endl;
    os << "netio_pagesize: " << network.netio_pagesize << std::endl;
    os << "netio_pages: " << network.netio_pages << std::endl;
    os << "statistics_fifo: " << stats.monitoring_fifo << std::endl;
    os << "statistics_period: " << stats.monitoring_period_ms << std::endl;
    os << "statistics_port: " << stats.prometheus_port << std::endl;
    os << "monitoring_type: ";
    if (not stats.local_monitoring_types.empty()) {
        for (const auto & t : stats.local_monitoring_types) {
            os << MonitoringConfig::printType(t) << " ";
        }
    } else {
        os << "N/A";
    }
    os << std::endl;
    os << "verbose: " << verbose << std::endl;
    os << "verbose_bus: " << network.verbose_bus << std::endl;
    os << "daq_network_mode: " << network_mode_to_str(network.daq_network_mode) << std::endl;
    os << "dcs_network_mode: " << network_mode_to_str(network.dcs_network_mode) << std::endl;
    os << "vid: 0x" << std::hex << static_cast<uint32_t>(resource.vid) << std::dec << std::endl;
    os << std::endl;
    return os;
}

std::ostream& operator<<(std::ostream& os, const Config& c) {
    return c.format(os);
}

void Config::handle_cmd_line(std::map<std::string, docopt::value> args) {

    auto devices = args["--device"].asStringList();
    for(const auto& i : devices){
        resource.device.push_back(stoi(i));
    }

    resource.vid = args["--vid"].asLong();
    parse_detector_connector_ids(args);

    if (args["--free-cmem"].asBool()) {
        ers::warning(felix_log::config_issue("--free-cmem option is deprecated, cmem buffers are freed by default and the option has no effect"));
    }

    network.bus_dir = args["--bus-dir"].asString();
    network.bus_groupname = args["--bus-groupname"].asString();
    network.netio_pagesize = args["--netio-pagesize"].asLong();
    network.netio_pages = args["--netio-pages"].asLong();
    network.verbose_bus = args["--verbose-bus"].asBool();

    network.daq_network_mode = network_mode_from_str(args["--daq-network-mode"].asString());
    network.dcs_network_mode = network_mode_from_str(args["--dcs-network-mode"].asString());

    stats.monitoring_fifo = args["--stats-out"] ? args["--stats-out"].asString() : "";
    stats.prometheus_port = args["--prometheus-port"] ? args["--prometheus-port"].asLong() : -1;

    if(args["--stats-out"]) { stats.local_monitoring_types.push_back(MonitoringType::FIFO); }
    if(args["--prometheus-port"]) { stats.local_monitoring_types.push_back(MonitoringType::Prometheus); }
    if(args["--is-monitoring"].asBool()) { stats.local_monitoring_types.push_back(MonitoringType::WebIS); }

    stats.monitoring_period_ms = args["--stats-period"].asLong();

    verbose = args["--verbose"].asBool();

    // derive ip
    if (args["--iface"]) {
        network.ip = utility::get_value_from_getifaddrs(args["--iface"].asString());
    } else {
        network.ip = args["--ip"].asString();
    }
}


void Config::parse_detector_connector_ids(std::map<std::string, docopt::value> args) {
    // TODO: Remove these deprecated options in the future, only registers.
    // If unset, the device then reads them from the card registers.

    if (args["--did"]) {
        resource.did = static_cast<uint16_t>(args["--did"].asLong());
        ers::warning(felix_log::config_issue(
            "--did is a deprecated overwrite and will be removed in a future release. "
            "Instead, set the DETECTOR_ID_VALUE register to the desired DID and the "
            "DETECTOR_ID_VALID register to 0x1."));
    }

    auto cids = args["--cid"].asStringList();
    if (cids.empty()) {
        return;
    }

    ers::warning(felix_log::config_issue(
        "--cid is a deprecated overwrite and will be removed in a future release. "
        "Instead, set the CONNECTOR_ID_*_VALUE registers to the desired CIDs and the "
        "CONNECTOR_ID_*_VALID registers to 0x1."));

    if (cids.size() == 1 and cids.at(0) == "device") {
        // Keep old default behavior for now
        for(const auto dev : resource.device) {
            resource.cid.push_back({dev});
        }
    }
    else if (cids.size() == 1 and cids.at(0).find(',') == std::string::npos) {
        // One connector id supplied, count from here upwards per device
        int base_cid = stoi(cids.at(0), 0, 16);
        for(unsigned int i = 0; i < resource.device.size(); ++i) {
            resource.cid.push_back({static_cast<uint16_t>(base_cid + i)});
        }
    }
    else if (cids.size() == resource.device.size()){
        // One comma separated list of connector ids per device
        for(const auto& c : cids) {
            std::vector<uint16_t> connector_ids;
            std::stringstream stream(c);
            std::string item;
            while (std::getline(stream, item, ',')) {
                connector_ids.push_back(static_cast<uint16_t>(stoi(item, 0, 16)));
            }
            resource.cid.push_back(connector_ids);
        }
    }
    else {
        ers::error(felix_log::config_issue("The number of provided CIDs is not one nor equal to the number of devices."));
        throw std::runtime_error("Invalid number of CIDs provided, aborting.");
    }
}


std::string Config::bugs() {
    return
R"(
Report bugs to <https://its.cern.ch/jira/projects/FLXUSERS>.
)";
}


int Config::get_number_devices()
{
    return resource.device.size();
};


std::vector<uint16_t> Config::get_device_cids(unsigned int dev_no)
{
    if (resource.cid.empty()) {
        return {};   // not given, the device reads them from the card
    }
    auto it = std::find(resource.device.begin(), resource.device.end(), dev_no);
    if ( it == resource.device.end() ){
        throw std::invalid_argument("Cannot find cid for the requested device.");
    } else {
        int idx = it - resource.device.begin();
        return resource.cid.at(idx);
    }
}


int Config::get_unique_dmaid(int dmaid, uint16_t device)
{
    return (dmaid + 10*device);
}


int Config::udmaid_to_dmaid(int udmaid)
{
    return (udmaid % 10);
}


int Config::udmaid_to_deviceid(int udmaid)
{
    return ( (udmaid - udmaid_to_dmaid(udmaid)) / 10 );
}

std::string Config::network_mode_to_str(NetworkMode type)
{
    switch (type) {
        case NetworkMode::rdma:
            return "RDMA";
        case NetworkMode::tcp:
            return "TCP";
        default:
            throw std::logic_error("Invalid NetworkMode");
    }
}

NetworkMode Config::network_mode_from_str(std::string_view type)
{
    if (type == "RDMA") {
        return NetworkMode::rdma;
    } else if (type == "TCP") {
        return NetworkMode::tcp;
    } else {
        throw std::logic_error("Invalid NetworkMode");
    }
}