Improve compilation speed

This commit is contained in:
Alexey Rybalchenko
2018-05-16 18:06:57 +02:00
committed by Dennis Klein
parent 436f79bee5
commit e4c349888d
24 changed files with 985 additions and 812 deletions

192
fairmq/tools/Network.cxx Normal file
View File

@@ -0,0 +1,192 @@
/********************************************************************************
* Copyright (C) 2017 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* *
* This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, *
* copied verbatim in the file "LICENSE" *
********************************************************************************/
#include <fairmq/tools/Network.h>
#ifndef _GNU_SOURCE
#define _GNU_SOURCE // To get defns of NI_MAXSERV and NI_MAXHOST
#endif
#include "FairMQLogger.h"
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <boost/algorithm/string.hpp> // trim
#include <boost/asio.hpp>
#include <map>
#include <string>
#include <iostream>
#include <array>
#include <exception>
#include <algorithm>
using namespace std;
namespace fair
{
namespace mq
{
namespace tools
{
// returns a map with network interface names as keys and their IP addresses as values
int getHostIPs(map<string, string>& addressMap)
{
struct ifaddrs *ifaddr, *ifa;
int s;
char host[NI_MAXHOST];
if (getifaddrs(&ifaddr) == -1)
{
perror("getifaddrs");
return -1;
}
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
{
if (ifa->ifa_addr == NULL)
{
continue;
}
if (ifa->ifa_addr->sa_family == AF_INET)
{
s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
if (s != 0)
{
cout << "getnameinfo() failed: " << gai_strerror(s) << endl;
return -1;
}
addressMap.insert(pair<string, string>(ifa->ifa_name, host));
}
}
freeifaddrs(ifaddr);
return 0;
}
// get IP address of a given interface name
string getInterfaceIP(const string& interface)
{
map<string, string> IPs;
getHostIPs(IPs);
if (IPs.count(interface))
{
return IPs[interface];
}
else
{
LOG(error) << "Could not find provided network interface: \"" << interface << "\"!, exiting.";
return "";
}
}
// get name of the default route interface
string getDefaultRouteNetworkInterface()
{
array<char, 128> buffer;
string interfaceName;
#ifdef __APPLE__ // MacOS
unique_ptr<FILE, decltype(pclose) *> file(popen("route -n get default | grep interface | cut -d \":\" -f 2", "r"), pclose);
#else // Linux
unique_ptr<FILE, decltype(pclose) *> file(popen("ip route | grep default | cut -d \" \" -f 5 | head -n 1", "r"), pclose);
#endif
if (!file)
{
LOG(error) << "Could not detect default route network interface name - popen() failed!";
return "";
}
while (!feof(file.get()))
{
if (fgets(buffer.data(), 128, file.get()) != NULL)
{
interfaceName += buffer.data();
}
}
boost::algorithm::trim(interfaceName);
if (interfaceName == "")
{
LOG(error) << "Could not detect default route network interface name";
}
else
{
LOG(debug) << "Detected network interface name for the default route: " << interfaceName;
}
return interfaceName;
}
string getIpFromHostname(const string& hostname)
{
try {
namespace bai = boost::asio::ip;
boost::asio::io_service ios;
bai::tcp::resolver resolver(ios);
bai::tcp::resolver::query query(hostname, "");
bai::tcp::resolver::iterator end;
auto it = find_if(static_cast<bai::basic_resolver_iterator<bai::tcp>>(resolver.resolve(query)), end, [](const bai::tcp::endpoint& ep) {
return ep.address().is_v4();
});
if (it != end) {
stringstream ss;
ss << static_cast<bai::tcp::endpoint>(*it).address();
return ss.str();
}
LOG(warn) << "could not find ipv4 address for hostname '" << hostname << "'";
return "";
} catch (exception& e) {
LOG(error) << "could not resolve hostname '" << hostname << "', reason: " << e.what();
return "";
}
}
string getIpFromHostname(const string& hostname, boost::asio::io_service& ios)
{
try {
namespace bai = boost::asio::ip;
bai::tcp::resolver resolver(ios);
bai::tcp::resolver::query query(hostname, "");
bai::tcp::resolver::iterator end;
auto it = find_if(static_cast<bai::basic_resolver_iterator<bai::tcp>>(resolver.resolve(query)), end, [](const bai::tcp::endpoint& ep) {
return ep.address().is_v4();
});
if (it != end) {
stringstream ss;
ss << static_cast<bai::tcp::endpoint>(*it).address();
return ss.str();
}
LOG(warn) << "could not find ipv4 address for hostname '" << hostname << "'";
return "";
} catch (exception& e) {
LOG(error) << "could not resolve hostname '" << hostname << "', reason: " << e.what();
return "";
}
}
} /* namespace tools */
} /* namespace mq */
} /* namespace fair */

View File

@@ -9,27 +9,20 @@
#ifndef FAIR_MQ_TOOLS_NETWORK_H
#define FAIR_MQ_TOOLS_NETWORK_H
#ifndef _GNU_SOURCE
#define _GNU_SOURCE // To get defns of NI_MAXSERV and NI_MAXHOST
#endif
#include "FairMQLogger.h"
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <boost/algorithm/string.hpp> // trim
#include <boost/asio.hpp>
#include <map>
#include <string>
#include <iostream>
#include <array>
#include <exception>
#include <algorithm>
// forward declarations
namespace boost
{
namespace asio
{
class io_context;
typedef class io_context io_service;
} // namespace asio
} // namespace boost
namespace fair
{
@@ -39,152 +32,17 @@ namespace tools
{
// returns a map with network interface names as keys and their IP addresses as values
inline int getHostIPs(std::map<std::string, std::string>& addressMap)
{
struct ifaddrs *ifaddr, *ifa;
int s;
char host[NI_MAXHOST];
if (getifaddrs(&ifaddr) == -1)
{
perror("getifaddrs");
return -1;
}
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
{
if (ifa->ifa_addr == NULL)
{
continue;
}
if (ifa->ifa_addr->sa_family == AF_INET)
{
s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
if (s != 0)
{
std::cout << "getnameinfo() failed: " << gai_strerror(s) << std::endl;
return -1;
}
addressMap.insert(std::pair<std::string, std::string>(ifa->ifa_name, host));
}
}
freeifaddrs(ifaddr);
return 0;
}
int getHostIPs(std::map<std::string, std::string>& addressMap);
// get IP address of a given interface name
inline std::string getInterfaceIP(std::string interface)
{
std::map<std::string, std::string> IPs;
getHostIPs(IPs);
if (IPs.count(interface))
{
return IPs[interface];
}
else
{
LOG(error) << "Could not find provided network interface: \"" << interface << "\"!, exiting.";
return "";
}
}
std::string getInterfaceIP(const std::string& interface);
// get name of the default route interface
inline std::string getDefaultRouteNetworkInterface()
{
std::array<char, 128> buffer;
std::string interfaceName;
std::string getDefaultRouteNetworkInterface();
#ifdef __APPLE__ // MacOS
std::unique_ptr<FILE, decltype(pclose) *> file(popen("route -n get default | grep interface | cut -d \":\" -f 2", "r"), pclose);
#else // Linux
std::unique_ptr<FILE, decltype(pclose) *> file(popen("ip route | grep default | cut -d \" \" -f 5 | head -n 1", "r"), pclose);
#endif
std::string getIpFromHostname(const std::string& hostname);
if (!file)
{
LOG(error) << "Could not detect default route network interface name - popen() failed!";
return "";
}
while (!feof(file.get()))
{
if (fgets(buffer.data(), 128, file.get()) != NULL)
{
interfaceName += buffer.data();
}
}
boost::algorithm::trim(interfaceName);
if (interfaceName == "")
{
LOG(error) << "Could not detect default route network interface name";
}
else
{
LOG(debug) << "Detected network interface name for the default route: " << interfaceName;
}
return interfaceName;
}
inline std::string getIpFromHostname(const std::string& hostname)
{
try {
namespace bai = boost::asio::ip;
boost::asio::io_service ios;
bai::tcp::resolver resolver(ios);
bai::tcp::resolver::query query(hostname, "");
bai::tcp::resolver::iterator end;
auto it = std::find_if(static_cast<bai::basic_resolver_iterator<bai::tcp>>(resolver.resolve(query)), end, [](const bai::tcp::endpoint& ep) {
return ep.address().is_v4();
});
if (it != end) {
std::stringstream ss;
ss << static_cast<bai::tcp::endpoint>(*it).address();
return ss.str();
}
LOG(warn) << "could not find ipv4 address for hostname '" << hostname << "'";
return "";
} catch (std::exception& e) {
LOG(error) << "could not resolve hostname '" << hostname << "', reason: " << e.what();
return "";
}
}
inline std::string getIpFromHostname(const std::string& hostname, boost::asio::io_service& ios)
{
try {
namespace bai = boost::asio::ip;
bai::tcp::resolver resolver(ios);
bai::tcp::resolver::query query(hostname, "");
bai::tcp::resolver::iterator end;
auto it = std::find_if(static_cast<bai::basic_resolver_iterator<bai::tcp>>(resolver.resolve(query)), end, [](const bai::tcp::endpoint& ep) {
return ep.address().is_v4();
});
if (it != end) {
std::stringstream ss;
ss << static_cast<bai::tcp::endpoint>(*it).address();
return ss.str();
}
LOG(warn) << "could not find ipv4 address for hostname '" << hostname << "'";
return "";
} catch (std::exception& e) {
LOG(error) << "could not resolve hostname '" << hostname << "', reason: " << e.what();
return "";
}
}
std::string getIpFromHostname(const std::string& hostname, boost::asio::io_service& ios);
} /* namespace tools */
} /* namespace mq */

72
fairmq/tools/Process.cxx Normal file
View File

@@ -0,0 +1,72 @@
/********************************************************************************
* Copyright (C) 2017 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* *
* This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, *
* copied verbatim in the file "LICENSE" *
********************************************************************************/
#include <fairmq/tools/Process.h>
#include <boost/process.hpp>
#include <iostream>
using namespace std;
namespace fair
{
namespace mq
{
namespace tools
{
/**
* Execute given command in forked process and capture stdout output
* and exit code.
*
* @param[in] cmd Command to execute
* @param[in] log_prefix How to prefix each captured output line with
* @return Captured stdout output and exit code
*/
execute_result execute(string cmd, string prefix)
{
execute_result result;
stringstream out;
// print full line thread-safe
stringstream printCmd;
printCmd << prefix << cmd << "\n";
cout << printCmd.str() << flush;
out << prefix << cmd << endl;
// Execute command and capture stdout, add prefix line by line
boost::process::ipstream stdout;
boost::process::child c(cmd, boost::process::std_out > stdout);
string line;
while (getline(stdout, line))
{
// print full line thread-safe
stringstream printLine;
printLine << prefix << line << "\n";
cout << printLine.str() << flush;
out << prefix << line << "\n";
}
c.wait();
// Capture exit code
result.exit_code = c.exit_code();
out << prefix << " Exit code: " << result.exit_code << endl;
result.console_out = out.str();
// Return result
return result;
}
} /* namespace tools */
} /* namespace mq */
} /* namespace fair */

View File

@@ -9,8 +9,6 @@
#ifndef FAIR_MQ_TOOLS_PROCESS_H
#define FAIR_MQ_TOOLS_PROCESS_H
#include <boost/process.hpp>
#include <string>
namespace fair
@@ -37,43 +35,7 @@ struct execute_result
* @param[in] log_prefix How to prefix each captured output line with
* @return Captured stdout output and exit code
*/
inline execute_result execute(std::string cmd, std::string prefix = "")
{
execute_result result;
std::stringstream out;
// print full line thread-safe
std::stringstream printCmd;
printCmd << prefix << cmd << "\n";
std::cout << printCmd.str() << std::flush;
out << prefix << cmd << std::endl;
// Execute command and capture stdout, add prefix line by line
boost::process::ipstream stdout;
boost::process::child c(cmd, boost::process::std_out > stdout);
std::string line;
while (getline(stdout, line))
{
// print full line thread-safe
std::stringstream printLine;
printLine << prefix << line << "\n";
std::cout << printLine.str() << std::flush;
out << prefix << line << "\n";
}
c.wait();
// Capture exit code
result.exit_code = c.exit_code();
out << prefix << " Exit code: " << result.exit_code << std::endl;
result.console_out = out.str();
// Return result
return result;
}
execute_result execute(std::string cmd, std::string prefix = "");
} /* namespace tools */
} /* namespace mq */

44
fairmq/tools/Unique.cxx Normal file
View File

@@ -0,0 +1,44 @@
/********************************************************************************
* Copyright (C) 2017 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* *
* This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, *
* copied verbatim in the file "LICENSE" *
********************************************************************************/
#include <fairmq/tools/Unique.h>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/functional/hash.hpp>
using namespace std;
namespace fair
{
namespace mq
{
namespace tools
{
// generates UUID string
string Uuid()
{
boost::uuids::random_generator gen;
boost::uuids::uuid u = gen();
return boost::uuids::to_string(u);
}
// generates UUID and returns its hash
size_t UuidHash()
{
boost::uuids::random_generator gen;
boost::hash<boost::uuids::uuid> uuid_hasher;
boost::uuids::uuid u = gen();
return uuid_hasher(u);
}
} /* namespace tools */
} /* namespace mq */
} /* namespace fair */

View File

@@ -9,11 +9,6 @@
#ifndef FAIR_MQ_TOOLS_UNIQUE_H
#define FAIR_MQ_TOOLS_UNIQUE_H
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/functional/hash.hpp>
#include <string>
namespace fair
@@ -24,21 +19,10 @@ namespace tools
{
// generates UUID string
inline std::string Uuid()
{
boost::uuids::random_generator gen;
boost::uuids::uuid u = gen();
return boost::uuids::to_string(u);
}
std::string Uuid();
// generates UUID and returns its hash
inline std::size_t UuidHash()
{
boost::uuids::random_generator gen;
boost::hash<boost::uuids::uuid> uuid_hasher;
boost::uuids::uuid u = gen();
return uuid_hasher(u);
}
std::size_t UuidHash();
} /* namespace tools */
} /* namespace mq */