14 #include <boost/regex.hpp> 15 #include <boost/algorithm/string/trim.hpp> 16 #include <boost/filesystem.hpp> 18 #include <yaml-cpp/yaml.h> 20 #include <fmt/format.h> 24 template<
typename... Args>
25 std::runtime_error
error(
const char* fmt,
const Args& ... args)
27 return std::runtime_error(fmt::format(fmt, args...));
36 : m_config(
std::move(config))
37 , m_fdWatcher(
std::move(watcher))
40 for(
auto& launchNode :
m_config->nodes())
42 auto node = std::make_shared<NodeMonitor>(launchNode,
m_fdWatcher,
m_nh);
46 if(launchNode->required())
48 node->exitedSignal.connect(
69 std::vector<std::string> paramNames;
73 if(node->clearParams())
75 std::string paramNamespace = node->namespaceString() +
"/" + node->name() +
"/";
77 log(
"Deleting parameters in namespace {}", paramNamespace.c_str());
79 if(paramNames.empty())
82 throw error(
"Could not get list of parameters for clear_param");
85 for(
auto&
param : paramNames)
87 if(
param.substr(0, paramNamespace.length()) == paramNamespace)
116 log(
"Killing the following nodes, which are refusing to exit:\n");
121 log(
" - {}\n", node->name());
141 double timeout = 0.0;
144 if(node->stopTimeout() > timeout)
145 timeout = node->stopTimeout();
152 log(
"Required node '{}' exited, shutting down...", name);
156 template<
typename... Args>
161 fmt::format(fmt, args...)
167 namespace fs = boost::filesystem;
169 fs::directory_iterator it(
"/proc");
170 fs::directory_iterator end;
172 std::map<int, NodeMonitor::Ptr> nodeMap;
175 if(node->pid() != -1)
176 nodeMap[node->pid()] = node;
178 node->beginStatUpdate();
182 procInfo.second.active =
false;
184 for(; it != end; ++it)
186 fs::path statPath = (*it) /
"stat";
194 auto it = nodeMap.find(stat.
pgrp);
195 if(it == nodeMap.end())
198 auto& node = it->second;
202 auto infoIt = m_processInfos.find(stat.
pid);
203 if(infoIt == m_processInfos.end())
208 m_processInfos[stat.
pid] = info;
212 const auto& oldStat = infoIt->second.
stat;
214 node->addCPUTime(stat.
utime - oldStat.utime, stat.
stime - oldStat.stime);
217 infoIt->second.active =
true;
218 infoIt->second.stat = stat;
221 for(
auto& node : m_nodes)
225 for(
auto it = m_processInfos.begin(); it != m_processInfos.end();)
227 if(!it->second.active)
228 it = m_processInfos.erase(it);
ros::WallTimer m_statTimer
ROSCPP_DECL bool getParamNames(std::vector< std::string > &keys)
bool param(const std::string ¶m_name, T ¶m_val, const T &default_val)
jiffies_t stime
Total time spent in kernel space.
std::runtime_error error(const char *fmt, const Args &...args)
std::map< int, ProcessInfo > m_processInfos
process_info::ProcessStat stat
boost::signals2::signal< void(std::string, std::string)> logMessageSignal
bool readStatFile(const char *filename, ProcessStat *stat)
SteadyTimer createSteadyTimer(WallDuration period, void(T::*callback)(const SteadyTimerEvent &), T *obj, bool oneshot=false, bool autostart=true) const
std::vector< NodeMonitor::Ptr > m_nodes
Monitor(launch::LaunchConfig::ConstPtr config, FDWatcher::Ptr watcher)
WallTimer createWallTimer(WallDuration period, void(T::*callback)(const WallTimerEvent &), T *obj, bool oneshot=false, bool autostart=true) const
FDWatcher::Ptr m_fdWatcher
std::shared_ptr< const LaunchConfig > ConstPtr
ROSCPP_DECL bool del(const std::string &key)
jiffies_t utime
Total time spent in userspace.
jiffies_t kernel_hz()
Number of kernel jiffies per second.
std::size_t mem_rss
Resident memory size in bytes.
void handleRequiredNodeExit(const std::string &name)
launch::LaunchConfig::ConstPtr m_config
unsigned long pgrp
Process group ID.
void setParam(const std::string &key, const XmlRpc::XmlRpcValue &v) const
void log(const char *fmt, const Args &...args)